OrderService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. ->sum('pay_amount');
  21. $refundAmount = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
  22. ->where('pay_status', 'SUCCESS')
  23. ->whereIn('pay_category', ['REFUND', 'ROLE_REFUND'])
  24. ->sum('pay_amount');
  25. $data['todayRevenue'] = round($data['todayRevenue'], 2);
  26. // 今日充值额
  27. $data['todayRecharge'] = Order::where('order_status_payment', 'SUCCESS')
  28. ->where('order_addtimes', '>', $todayTimeUnix)
  29. ->whereIn('order_category', ['RECHARGE','COMBINE','PARTNER','REFERRER','VIP'])
  30. ->where('order_status_payment', 'SUCCESS')
  31. ->sum('order_amount_pay');
  32. // 今日退单
  33. $data['todayRefund'] = $refundAmount;
  34. // 今日销售额
  35. $data['todaySales'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
  36. ->whereIn('pay_prepayid', ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
  37. ->where('join_pay_order_id', '<>', '')
  38. ->where('pay_status', 'SUCCESS')
  39. ->sum('pay_amount');
  40. $data['todaySales'] = round($data['todaySales'], 2);
  41. // 柱状图
  42. // 产品订单
  43. $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(order_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order group by month");
  44. // $newAddition = Db::select("
  45. // SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  46. // left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  47. // where g.goods_classify = 'GOODS'
  48. // group by month
  49. // ");
  50. $category = [];
  51. $newAdditionData = [];
  52. foreach ($newAddition as $item) {
  53. if (!in_array($item->month, $category)) {
  54. $category[] = $item->month;
  55. }
  56. // $newAdditionData['category'][] = $item->month;
  57. // $newAdditionData['data'][] = $item->num;
  58. }
  59. // 服务订单
  60. $newAdditionService = Db::select("
  61. SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  62. left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  63. where g.goods_classify = 'SERVICE'
  64. group by month
  65. ");
  66. $newAdditionServiceData = [];
  67. foreach ($newAdditionService as $item) {
  68. if (!in_array($item->month, $category)) {
  69. $category[] = $item->month;
  70. }
  71. // $newAdditionServiceData['category'][] = $item->month;
  72. // $newAdditionServiceData['data'][] = $item->num;
  73. }
  74. sort($category);
  75. foreach ($category as $item2) {
  76. $isHaveService = false;
  77. $isHaveGoods = false;
  78. foreach ($newAdditionService as $item) {
  79. if ($item2 == $item->month) {
  80. $isHaveService = true;
  81. $newAdditionServiceData['category'][] = $item->month;
  82. $newAdditionServiceData['data'][] = $item->num;
  83. }
  84. }
  85. foreach ($newAddition as $item3) {
  86. if ($item2 == $item3->month) {
  87. $isHaveGoods = true;
  88. $newAdditionData['category'][] = $item3->month;
  89. $newAdditionData['data'][] = $item3->num;
  90. }
  91. }
  92. if (!$isHaveService) {
  93. $newAdditionServiceData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  94. $newAdditionServiceData['data'][] = 0;
  95. }
  96. if (!$isHaveGoods) {
  97. $newAdditionData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  98. $newAdditionData['data'][] = 0;
  99. }
  100. }
  101. $data['addition']['goods'] = $newAdditionData;
  102. $data['addition']['service'] = $newAdditionServiceData;
  103. return $data;
  104. }
  105. }