RealtimePendingOrdersService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. class RealtimePendingOrdersService
  10. {
  11. /**
  12. * 获取实时待处理订单
  13. * @param null $start
  14. * @param null $end
  15. * @return Builder[]|Collection|mixed
  16. */
  17. public function warehousesOrders($start = null, $end = null)
  18. {
  19. try {
  20. return Cache::remember('RealTimePendingOrders' . Auth::id(), 60, function () use ($start, $end) {
  21. return $this->getOrderStatistics($start, $end);
  22. });
  23. } catch (\Exception $e) {
  24. return $this->getOrderStatistics($start, $end);
  25. }
  26. }
  27. /**
  28. * @param $start
  29. * @param $end
  30. * @return Builder[]|Collection
  31. */
  32. public function getOrderStatistics($start, $end)
  33. {
  34. $start = $start ?? date('Y-m-d 00:00:00');
  35. $end = $end ?? date('Y-m-d 23:59:59');
  36. $ownerIds = app('OwnerService')->getSelectionId();
  37. $builders = Order::query()->selectRaw("warehouses.name," .
  38. "count(case wms_status when '创建订单' then 1 end) as createOrder, " .
  39. "count(case wms_status when '分配完成' then 1 end) as assignedComplete, " .
  40. "count(case wms_status when '部分分配' then 1 end) as partialAllocation, " .
  41. "count(case wms_status when '部分装箱' then 1 end) as partPacking, " .
  42. "count(case wms_status when '播种完成' then 1 end) as sowComplete, " .
  43. "count(1) as total"
  44. )->whereBetween('orders.created_at', [$start, $end,])
  45. ->whereIn('wms_status', ['创建订单', '分配完成', '部分分配', '部分装箱', '播种完成'])
  46. ->leftJoin('warehouses', 'orders.warehouse_id', 'warehouses.id')
  47. ->groupBy('warehouse_id');
  48. if ($ownerIds) $builders->whereIn('owner_id', $ownerIds);
  49. return $builders->get();
  50. }
  51. }