OrderService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. $data['todayCashSales'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ', [$todayTimeUnix])
  43. ->whereNotIn('pay_prepayid',['WXPAY','ALIPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
  44. ->where('join_pay_order_id', '<>', '')
  45. ->where('pay_category','<>','REFUND')
  46. ->where('pay_status', 'SUCCESS')
  47. ->sum('pay_amount');
  48. $data['todayCashSales'] = round($data['todayCashSales'], 2);
  49. // 柱状图
  50. // 产品订单
  51. $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(order_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order group by month");
  52. // $newAddition = Db::select("
  53. // SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  54. // left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  55. // where g.goods_classify = 'GOODS'
  56. // group by month
  57. // ");
  58. $category = [];
  59. $newAdditionData = [];
  60. foreach ($newAddition as $item) {
  61. if (!in_array($item->month, $category)) {
  62. $category[] = $item->month;
  63. }
  64. // $newAdditionData['category'][] = $item->month;
  65. // $newAdditionData['data'][] = $item->num;
  66. }
  67. // 服务订单
  68. $newAdditionService = Db::select("
  69. SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  70. left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  71. where g.goods_classify = 'SERVICE'
  72. group by month
  73. ");
  74. $newAdditionServiceData = [];
  75. foreach ($newAdditionService as $item) {
  76. if (!in_array($item->month, $category)) {
  77. $category[] = $item->month;
  78. }
  79. // $newAdditionServiceData['category'][] = $item->month;
  80. // $newAdditionServiceData['data'][] = $item->num;
  81. }
  82. sort($category);
  83. foreach ($category as $item2) {
  84. $isHaveService = false;
  85. $isHaveGoods = false;
  86. foreach ($newAdditionService as $item) {
  87. if ($item2 == $item->month) {
  88. $isHaveService = true;
  89. $newAdditionServiceData['category'][] = $item->month;
  90. $newAdditionServiceData['data'][] = $item->num;
  91. }
  92. }
  93. foreach ($newAddition as $item3) {
  94. if ($item2 == $item3->month) {
  95. $isHaveGoods = true;
  96. $newAdditionData['category'][] = $item3->month;
  97. $newAdditionData['data'][] = $item3->num;
  98. }
  99. }
  100. if (!$isHaveService) {
  101. $newAdditionServiceData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  102. $newAdditionServiceData['data'][] = 0;
  103. }
  104. if (!$isHaveGoods) {
  105. $newAdditionData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  106. $newAdditionData['data'][] = 0;
  107. }
  108. }
  109. $data['addition']['goods'] = $newAdditionData;
  110. $data['addition']['service'] = $newAdditionServiceData;
  111. return $data;
  112. }
  113. }