StatisticsController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\model\Order;
  4. use app\model\PayDetail;
  5. use support\Db;
  6. use support\Request;
  7. class StatisticsController
  8. {
  9. /**
  10. * @Desc 标准订单
  11. * @Author Gorden
  12. * @Date 2024/10/28 9:57
  13. *
  14. * @param Request $request
  15. * @return \support\Response
  16. */
  17. public function order(Request $request)
  18. {
  19. $orderCategory = $request->get('order_category');
  20. $searchTime = $request->get('order_addtimes');
  21. if (!empty($searchTime)) {
  22. $searchTime[0] = strtotime($searchTime[0]);
  23. $searchTime[1] = strtotime($searchTime[1]);
  24. }
  25. if (!$orderCategory) {
  26. return json_fail('参数异常');
  27. }
  28. if ($orderCategory == 'SERVICE') {
  29. $orderCategory = ['SERVICE', 'CHNMED', 'CHNNCD'];
  30. } else if ($orderCategory == 'MEALS') {
  31. $orderCategory = ['MEALS','DISHES'];
  32. } else if ($orderCategory == 'ALL') {
  33. $orderCategory = ['SERVICE', 'CHNMED', 'CHNNCD', 'GOODS', 'PACKAGE', 'MEALS','DISHES', 'RECHARGE', 'COMBINE', 'PARTNER', 'REFERRER','FRANCHISEE', 'VIP'];
  34. } else {
  35. $orderCategory = [$orderCategory];
  36. }
  37. $where = '';
  38. if (in_array('RECHARGE', $orderCategory)) {
  39. $where = " and join_pay_order_id not in ('COMBINE','PARTNER','REFERRER','FRANCHISEE')";
  40. }
  41. $orderCategoryStr = "'" . implode("','", $orderCategory) . "'";
  42. $todayTimeUnix = strtotime(date('Y-m-d'));
  43. $yesterdayStart = strtotime(date('Y-m-d', strtotime("-1 days")));
  44. $yesterdayEnd = strtotime(date('Y-m-d 23:59:59', strtotime("-1 days")));
  45. $monthStart = strtotime(date('Y-m-01'));
  46. $monthEnd = strtotime(date('Y-m-t 23:59:59'));
  47. // 今日收入
  48. $statistics['todayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? ', [$todayTimeUnix])
  49. ->where('pay_status', 'SUCCESS')
  50. ->where('pay_prepayid','<>', '')
  51. ->whereIn('pay_category', $orderCategory)
  52. ->when(in_array('RECHARGE', $orderCategory), function ($query) {
  53. $query->whereNotIn('join_pay_order_id', ['COMBINE', 'PARTNER', 'REFERRER','FRANCHISEE']);
  54. })
  55. ->sum('pay_amount');
  56. $todayRevenueOrderNbrModel = Db::select("select count(1) as total from (select JSON_EXTRACT(join_pay_object_json,'$.order_id') as order_id from app_pay_detail where
  57. CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= {$todayTimeUnix}
  58. and pay_status = 'SUCCESS'
  59. and pay_prepayid != ''
  60. and pay_category in ({$orderCategoryStr}) {$where}
  61. group by order_id) count");
  62. $statistics['todayRevenueOrderNbr'] = $todayRevenueOrderNbrModel[0]->total;
  63. // 昨日收入
  64. $statistics['yesterdayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= ?', [$yesterdayStart, $yesterdayEnd])
  65. ->where('pay_status', 'SUCCESS')
  66. ->where('pay_prepayid','<>', '')
  67. ->whereIn('pay_category', $orderCategory)
  68. ->when(in_array('RECHARGE', $orderCategory), function ($query) {
  69. $query->whereNotIn('join_pay_order_id', ['COMBINE', 'PARTNER', 'REFERRER','FRANCHISEE']);
  70. })
  71. ->sum('pay_amount');
  72. $yesterdayRevenueOrderNbrModel = Db::select("select count(1) as total from (select JSON_EXTRACT(join_pay_object_json,'$.order_id') as order_id from app_pay_detail where
  73. CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= {$yesterdayStart} and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= {$yesterdayEnd}
  74. and pay_status = 'SUCCESS'
  75. and pay_prepayid != ''
  76. and pay_category in ({$orderCategoryStr}) {$where}
  77. group by order_id) count");
  78. $statistics['yesterdayRevenueOrderNbr'] = $yesterdayRevenueOrderNbrModel[0]->total;
  79. // 本月收入
  80. $statistics['monthRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= ?', [$monthStart, $monthEnd])
  81. ->where('pay_status', 'SUCCESS')
  82. ->where('pay_prepayid','<>', '')
  83. ->whereIn('pay_category', $orderCategory)
  84. ->when(in_array('RECHARGE', $orderCategory), function ($query) {
  85. $query->whereNotIn('join_pay_order_id', ['COMBINE', 'PARTNER', 'REFERRER','FRANCHISEE']);
  86. })
  87. ->sum('pay_amount');
  88. $monthRevenueOrderNbrModel = Db::select("select count(1) as total from (select JSON_EXTRACT(join_pay_object_json,'$.order_id') as order_id from app_pay_detail where
  89. CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= {$monthStart} and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= {$monthEnd}
  90. and pay_status = 'SUCCESS'
  91. and pay_prepayid != ''
  92. and pay_category in ({$orderCategoryStr}) {$where}
  93. group by order_id) count");
  94. $statistics['monthRevenueOrderNbr'] = $monthRevenueOrderNbrModel[0]->total;
  95. // 累计收入
  96. $statistics['totalRevenue'] = PayDetail::where('pay_status', 'SUCCESS')
  97. ->where('pay_prepayid','<>', '')
  98. ->whereIn('pay_category', $orderCategory)
  99. ->sum('pay_amount');
  100. $totalRevenueOrderNbrModel = Db::select("select count(1) as total from (select JSON_EXTRACT(join_pay_object_json,'$.order_id') as order_id from app_pay_detail where
  101. pay_status = 'SUCCESS'
  102. and pay_prepayid != ''
  103. and pay_category in ({$orderCategoryStr}) {$where}
  104. group by order_id) count");
  105. $statistics['totalRevenueOrderNbr'] = $totalRevenueOrderNbrModel[0]->total;
  106. // 今日退款
  107. $todayRefundItem = Db::select("select SUM(d2.pay_amount) as amount,count(1) as total from app_pay_detail d1
  108. inner join app_pay_detail d2 ON d1.join_pay_order_id=d2.join_pay_order_id and d2.pay_category='REFUND'
  109. where CAST(UNIX_TIMESTAMP(d2.pay_paytimes) as SIGNED) >= ? AND d1.pay_category in ({$orderCategoryStr})
  110. ", [$todayTimeUnix]);
  111. $statistics['todayRefund'] = $todayRefundItem[0]->amount;
  112. $statistics['todayRefundNbr'] = $todayRefundItem[0]->total;
  113. // 累计退款
  114. // $totalRefundItem = Db::select("select SUM(d2.pay_amount) as amount,count(1) as total from app_pay_detail d1
  115. // inner join app_pay_detail d2 ON d1.join_pay_order_id=d2.join_pay_order_id and d2.pay_category='REFUND'
  116. // where d1.pay_category in ({$orderCategoryStr})
  117. // ");
  118. $refundOrderIds = Order::where('order_category','RETURN')
  119. ->whereIn('order_classify',$orderCategory)
  120. ->pluck('order_groupby')
  121. ->toArray();
  122. $statistics['totalRefund'] = PayDetail::whereIn('join_pay_order_id',$refundOrderIds)->where('pay_status','SUCCESS')->sum('pay_amount');
  123. $statistics['totalRefundNbr'] = count($refundOrderIds);
  124. if (in_array('MEALS', $orderCategory)) {
  125. // 挂账
  126. $sql = "select SUM(order_amount_pay) as amount, count(1) as total from app_order where order_status_payment = 'AWAITING'";
  127. if (!empty($searchTime)) {
  128. $sql .= " and order_addtimes > {$searchTime[0]} and order_addtimes < $searchTime[1]";
  129. }
  130. $awaitingModel = Db::select($sql);
  131. $statistics['awaitingRevenue'] = $awaitingModel[0]->amount;
  132. $statistics['awaitingRevenueOrderNbr'] = $awaitingModel[0]->total;
  133. }
  134. return json_success('success', $statistics);
  135. }
  136. }