RealtimePendingOrdersService.php 2.0 KB

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