RealtimePendingOrdersService.php 2.0 KB

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