OrderService.php 4.7 KB

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