OrderService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\admin\service\statistics;
  3. use app\model\Order;
  4. use app\model\OrderReturn;
  5. use app\model\PayDetail;
  6. use support\Db;
  7. class OrderService
  8. {
  9. public static function getOrder()
  10. {
  11. // 今日订单
  12. $todayTimeUnix = strtotime(date('Y-m-d'));
  13. // 今日订单
  14. $data['todayOrder'] = Order::where('order_addtimes', '>', $todayTimeUnix)->where('order_category', '<>', 'RECHARGE')->count();
  15. // 今日营业额
  16. $data['todayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
  17. ->whereIn('pay_category', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE', 'MEALS'])
  18. ->where('join_pay_order_id', '<>', '')
  19. ->where('pay_status', 'SUCCESS')
  20. ->where('pay_category' ,'<>', 'REFUND')
  21. ->sum('pay_amount');
  22. $refundAmount = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
  23. ->where('pay_status', 'SUCCESS')
  24. ->whereIn('pay_category', ['REFUND', 'ROLE_REFUND'])
  25. ->sum('pay_amount');
  26. $data['todayRevenue'] = round($data['todayRevenue'], 2);
  27. // 今日充值额
  28. $data['todayRecharge'] = Order::where('order_status_payment', 'SUCCESS')
  29. ->where('order_addtimes', '>', $todayTimeUnix)
  30. ->whereIn('order_category', ['RECHARGE', 'COMBINE', 'PARTNER', 'REFERRER', 'VIP'])
  31. ->where('order_status_payment', 'SUCCESS')
  32. ->sum('order_amount_pay');
  33. // 今日退单
  34. $data['todayRefund'] = $refundAmount;
  35. // 今日销售额
  36. $data['todaySales'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
  37. ->whereIn('pay_prepayid', ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
  38. ->where('join_pay_order_id', '<>', '')
  39. ->where('pay_status', 'SUCCESS')
  40. ->where('pay_category' ,'<>', 'REFUND')
  41. ->sum('pay_amount');
  42. $data['todaySales'] = round($data['todaySales'], 2);
  43. // 今日余额收入
  44. $data['todayCashSales'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
  45. ->whereNotIn('pay_prepayid',['WXPAY','ALIPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
  46. ->where('join_pay_order_id', '<>', '')
  47. ->where('pay_category','<>','REFUND')
  48. ->where('pay_status', 'SUCCESS')
  49. ->sum('pay_amount');
  50. $data['todayCashSales'] = round($data['todayCashSales'], 2);
  51. // 柱状图
  52. // 产品订单
  53. $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(order_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order group by month");
  54. // $newAddition = Db::select("
  55. // SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  56. // left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  57. // where g.goods_classify = 'GOODS'
  58. // group by month
  59. // ");
  60. $category = [];
  61. $newAdditionData = [];
  62. foreach ($newAddition as $item) {
  63. if (!in_array($item->month, $category)) {
  64. $category[] = $item->month;
  65. }
  66. // $newAdditionData['category'][] = $item->month;
  67. // $newAdditionData['data'][] = $item->num;
  68. }
  69. // 服务订单
  70. $newAdditionService = Db::select("
  71. SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  72. left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  73. where g.goods_classify = 'SERVICE'
  74. group by month
  75. ");
  76. $newAdditionServiceData = [];
  77. foreach ($newAdditionService as $item) {
  78. if (!in_array($item->month, $category)) {
  79. $category[] = $item->month;
  80. }
  81. // $newAdditionServiceData['category'][] = $item->month;
  82. // $newAdditionServiceData['data'][] = $item->num;
  83. }
  84. sort($category);
  85. foreach ($category as $item2) {
  86. $isHaveService = false;
  87. $isHaveGoods = false;
  88. foreach ($newAdditionService as $item) {
  89. if ($item2 == $item->month) {
  90. $isHaveService = true;
  91. $newAdditionServiceData['category'][] = $item->month;
  92. $newAdditionServiceData['data'][] = $item->num;
  93. }
  94. }
  95. foreach ($newAddition as $item3) {
  96. if ($item2 == $item3->month) {
  97. $isHaveGoods = true;
  98. $newAdditionData['category'][] = $item3->month;
  99. $newAdditionData['data'][] = $item3->num;
  100. }
  101. }
  102. if (!$isHaveService) {
  103. $newAdditionServiceData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  104. $newAdditionServiceData['data'][] = 0;
  105. }
  106. if (!$isHaveGoods) {
  107. $newAdditionData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  108. $newAdditionData['data'][] = 0;
  109. }
  110. }
  111. $data['addition']['goods'] = $newAdditionData;
  112. $data['addition']['service'] = $newAdditionServiceData;
  113. return $data;
  114. }
  115. }