StatisticsController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\model\PayDetail;
  4. use support\Db;
  5. use support\Request;
  6. class StatisticsController
  7. {
  8. public function order(Request $request)
  9. {
  10. $orderCategory = $request->get('order_category');
  11. if (!$orderCategory) {
  12. return json_fail('参数异常');
  13. }
  14. if ($orderCategory == 'SERVICE') {
  15. $orderCategory = ['SERVICE', 'CHNMED', 'CHNNCD'];
  16. } else {
  17. $orderCategory = [$orderCategory];
  18. }
  19. $orderCategoryStr = "'" . implode("','", $orderCategory) . "'";
  20. $todayTimeUnix = strtotime(date('Y-m-d'));
  21. $yesterdayStart = strtotime(date('Y-m-d', strtotime("-1 days")));
  22. $yesterdayEnd = strtotime(date('Y-m-d 23:59:59', strtotime("-1 days")));
  23. $monthStart = strtotime(date('Y-m-01'));
  24. $monthEnd = strtotime(date('Y-m-t 23:59:59'));
  25. // 今日收入
  26. $statistics['todayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? ', [$todayTimeUnix])
  27. ->where('pay_status', 'SUCCESS')
  28. ->whereIn('pay_category', $orderCategory)
  29. ->sum('pay_amount');
  30. $todayRevenueOrderNbrModel = Db::select("select count(1) as total from (select join_pay_order_id from app_pay_detail where
  31. CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= {$todayTimeUnix}
  32. and pay_status = 'SUCCESS'
  33. and pay_category in ({$orderCategoryStr})
  34. group by join_pay_order_id) count");
  35. $statistics['todayRevenueOrderNbr'] = $todayRevenueOrderNbrModel[0]->total;
  36. // 昨日收入
  37. $statistics['yesterdayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= ?', [$yesterdayStart, $yesterdayEnd])
  38. ->where('pay_status', 'SUCCESS')
  39. ->whereIn('pay_category', $orderCategory)
  40. ->sum('pay_amount');
  41. $yesterdayRevenueOrderNbrModel = Db::select("select count(1) as total from (select join_pay_order_id from app_pay_detail where
  42. CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= {$yesterdayStart} and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= {$yesterdayEnd}
  43. and pay_status = 'SUCCESS'
  44. and pay_category in ({$orderCategoryStr})
  45. group by join_pay_order_id) count");
  46. $statistics['yesterdayRevenueOrderNbr'] = $yesterdayRevenueOrderNbrModel[0]->total;
  47. // 本月收入
  48. $statistics['monthRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= ?', [$monthStart, $monthEnd])
  49. ->where('pay_status', 'SUCCESS')
  50. ->whereIn('pay_category', $orderCategory)
  51. ->sum('pay_amount');
  52. $monthRevenueOrderNbrModel = Db::select("select count(1) as total from (select join_pay_order_id from app_pay_detail where
  53. CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= {$monthStart} and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= {$monthEnd}
  54. and pay_status = 'SUCCESS'
  55. and pay_category in ({$orderCategoryStr})
  56. group by join_pay_order_id) count");
  57. $statistics['monthRevenueOrderNbr'] = $monthRevenueOrderNbrModel[0]->total;
  58. // 累计收入
  59. // $statistics['totalRevenue'] = PayDetail::where('pay_status', 'SUCCESS')
  60. // ->whereIn('pay_category', $orderCategory)
  61. // ->sum('pay_amount');
  62. // 今日退款
  63. $todayRefundItem = Db::select("select SUM(d2.pay_amount) as amount,count(1) as total from app_pay_detail d1
  64. inner join app_pay_detail d2 ON d1.join_pay_order_id=d2.join_pay_order_id and d2.pay_category='REFUND'
  65. where CAST(UNIX_TIMESTAMP(d2.pay_paytimes) as SIGNED) >= ? AND d1.pay_category in ({$orderCategoryStr})
  66. ", [$todayTimeUnix]);
  67. $statistics['todayRefund'] = $todayRefundItem[0]->amount;
  68. $statistics['todayRefundNbr'] = $todayRefundItem[0]->total;
  69. // 累计退款
  70. $totalRefundItem = Db::select("select SUM(d2.pay_amount) as amount,count(1) as total, d1.pay_category as d1_pay_category,d2.pay_category as d2_pay_category from app_pay_detail d1
  71. inner join app_pay_detail d2 ON d1.join_pay_order_id=d2.join_pay_order_id and d2.pay_category='REFUND'
  72. where d1.pay_category in ('" . implode("','", $orderCategory) . "')
  73. ");
  74. $statistics['totalRefund'] = $totalRefundItem[0]->amount;
  75. $statistics['totalRefundNbr'] = $totalRefundItem[0]->total;
  76. if (in_array('MEALS',$orderCategory)){
  77. // 挂账
  78. $awaitingModel = Db::select("select SUM(order_amount_pay) as amount, count(1) as total from app_order where order_status_payment = 'AWAITING'");
  79. dump($awaitingModel);
  80. }
  81. return json_success('success', $statistics);
  82. }
  83. }