OrderService.php 3.6 KB

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