OrderService.php 4.4 KB

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