OrderService.php 3.5 KB

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