OrderService.php 5.4 KB

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