OrderService.php 4.3 KB

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