OrderService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\admin\service\statistics;
  3. use app\model\Order;
  4. use app\model\OrderReturn;
  5. use support\Db;
  6. class OrderService
  7. {
  8. public static function getOrder()
  9. {
  10. // 今日订单
  11. $todayTimeUnix = strtotime(date('Y-m-d'));
  12. // 今日订单
  13. $data['todayOrder'] = Order::where('order_addtimes', '>', $todayTimeUnix)->where('order_category', '<>', 'RECHARGE')->count();
  14. // 今日收入
  15. $data['todayRevenue'] = Order::where('order_status_payment', 'SUCCESS')
  16. ->where('order_addtimes', '>', $todayTimeUnix)
  17. ->where('order_category', '<>', 'RETURN')
  18. ->where('order_category', '<>', 'RECHARGE')
  19. ->sum('order_amount_pay');
  20. // 今日充值额
  21. $data['todayRecharge'] = Order::where('order_status_payment', 'SUCCESS')
  22. ->where('order_addtimes', '>', $todayTimeUnix)
  23. ->where('order_category', 'RECHARGE')
  24. ->where('order_status_payment','SUCCESS')
  25. ->sum('order_amount_pay');
  26. // 今日退单
  27. $data['todayRefund'] = OrderReturn::where('order_return_addtimes', '>', $todayTimeUnix)
  28. ->count();
  29. // 柱状图
  30. // 产品订单
  31. $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(order_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order group by month");
  32. // $newAddition = Db::select("
  33. // SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  34. // left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  35. // where g.goods_classify = 'GOODS'
  36. // group by month
  37. // ");
  38. $category = [];
  39. $newAdditionData = [];
  40. foreach ($newAddition as $item) {
  41. if (!in_array($item->month, $category)) {
  42. $category[] = $item->month;
  43. }
  44. // $newAdditionData['category'][] = $item->month;
  45. // $newAdditionData['data'][] = $item->num;
  46. }
  47. // 服务订单
  48. $newAdditionService = Db::select("
  49. SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  50. left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  51. where g.goods_classify = 'SERVICE'
  52. group by month
  53. ");
  54. $newAdditionServiceData = [];
  55. foreach ($newAdditionService as $item) {
  56. if (!in_array($item->month, $category)) {
  57. $category[] = $item->month;
  58. }
  59. // $newAdditionServiceData['category'][] = $item->month;
  60. // $newAdditionServiceData['data'][] = $item->num;
  61. }
  62. sort($category);
  63. foreach ($category as $item2){
  64. $isHaveService = false;
  65. $isHaveGoods = false;
  66. foreach ($newAdditionService as $item){
  67. if ($item2 == $item->month){
  68. $isHaveService = true;
  69. $newAdditionServiceData['category'][] = $item->month;
  70. $newAdditionServiceData['data'][] = $item->num;
  71. }
  72. }
  73. foreach ($newAddition as $item3){
  74. if ($item2 == $item3->month){
  75. $isHaveGoods = true;
  76. $newAdditionData['category'][] = $item3->month;
  77. $newAdditionData['data'][] = $item3->num;
  78. }
  79. }
  80. if (!$isHaveService){
  81. $newAdditionServiceData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  82. $newAdditionServiceData['data'][] = 0;
  83. }
  84. if (!$isHaveGoods){
  85. $newAdditionData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  86. $newAdditionData['data'][] = 0;
  87. }
  88. }
  89. $data['addition']['goods'] = $newAdditionData;
  90. $data['addition']['service'] = $newAdditionServiceData;
  91. return $data;
  92. }
  93. }