StatisticsController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. $todayTimeUnix = strtotime(date('Y-m-d'));
  20. $yesterdayStart = strtotime(date('Y-m-d', strtotime("-1 days")));
  21. $yesterdayEnd = strtotime(date('Y-m-d 23:59:59', strtotime("-1 days")));
  22. $monthStart = strtotime(date('Y-m-01'));
  23. $monthEnd = strtotime(date('Y-m-t 23:59:59'));
  24. // 今日收入
  25. $statistics['todayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? ', [$todayTimeUnix])
  26. ->where('pay_status', 'SUCCESS')
  27. ->whereIn('pay_category', $orderCategory)
  28. ->sum('pay_amount');
  29. // 昨日收入
  30. $statistics['yesterdayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= ?', [$yesterdayStart, $yesterdayEnd])
  31. ->where('pay_status', 'SUCCESS')
  32. ->whereIn('pay_category', $orderCategory)
  33. ->sum('pay_amount');
  34. // 本月收入
  35. $statistics['monthRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? and CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= ?', [$monthStart, $monthEnd])
  36. ->where('pay_status', 'SUCCESS')
  37. ->whereIn('pay_category', $orderCategory)
  38. ->sum('pay_amount');
  39. // 累计收入
  40. $statistics['totalRevenue'] = PayDetail::where('pay_status', 'SUCCESS')
  41. ->whereIn('pay_category', $orderCategory)
  42. ->sum('pay_amount');
  43. // 今日退款
  44. $todayRefundItem = Db::select("select SUM(d2.pay_amount) as amount from app_pay_detail d1
  45. inner join app_pay_detail d2 ON d1.join_pay_order_id=d2.join_pay_order_id and d2.pay_category='REFUND'
  46. where CAST(UNIX_TIMESTAMP(d2.pay_paytimes) as SIGNED) >= ? AND d1.pay_category in ('".implode("','",$orderCategory)."')
  47. ", [$todayTimeUnix]);
  48. $statistics['todayRefund'] = 0;
  49. if (!empty($todayRefundItem) && !empty($todayRefundItem[0])){
  50. $statistics['todayRefund'] = $todayRefundItem[0]->amount;
  51. }
  52. // 累计退款
  53. $totalRefundItem = Db::select("select SUM(d2.pay_amount) as amount, d1.pay_category as d1_pay_category,d2.pay_category as d2_pay_category from app_pay_detail d1
  54. inner join app_pay_detail d2 ON d1.join_pay_order_id=d2.join_pay_order_id and d2.pay_category='REFUND'
  55. where d1.pay_category in ('".implode("','",$orderCategory)."')
  56. ");
  57. dump($totalRefundItem);
  58. }
  59. }