StationTaskMaterialBoxService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\OrderCommodity;
  5. use App\StationTaskMaterialBox;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. class StationTaskMaterialBoxService
  9. {
  10. /** @var StationService $stationService */
  11. private $stationService;
  12. /** @var StationTypeService $stationTypeService */
  13. private $stationTypeService;
  14. /** @var StationTaskService $stationTaskService */
  15. private $stationTaskService;
  16. /** @var MaterialBoxService $materialBoxService */
  17. private $materialBoxService;
  18. public function __construct(){
  19. $this->stationService=null;
  20. $this->stationTypeService=null;
  21. $this->stationTaskService=null;
  22. $this->materialBoxService=null;
  23. }
  24. function createByBatches(array $batches,Collection $stationTasks_toAttach): Collection
  25. {
  26. $stationTaskMaterialBoxes_listByBatch=new Collection();
  27. foreach ($batches as $batch){
  28. $stationTaskMaterialBoxes_listByBatch->push(
  29. $this->createByBatch($batch)
  30. );
  31. }
  32. $this->stationTaskService->registerSubTasks($stationTasks_toAttach,$stationTaskMaterialBoxes_listByBatch);
  33. }
  34. function createByBatch(Batch $batch): Collection
  35. {
  36. $this->materialBoxService=app('MaterialBoxService');
  37. $this->stationTypeService=app('StationTypeService');
  38. $this->stationService=app('StationService');
  39. $stationMaterialBoxes_toCreate=new Collection();
  40. $order_ids=data_get($batch['orders'],'*.id');
  41. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  42. foreach ($orderCommodities as $orderCommodity){
  43. $stationType=$this->stationTypeService->getForMaterialBox();
  44. $station=$this->stationService->getStation_byType($stationType['name']);
  45. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  46. $stationMaterialBoxes_toCreate->push([
  47. 'station_id'=>$station['id'],
  48. 'material_box_id'=>$materialBox['id'],
  49. 'status'=>'待处理'
  50. ]);
  51. }
  52. $this->insert($stationMaterialBoxes_toCreate->toArray());
  53. }
  54. function get(array $kvPairs){
  55. ksort($kvPairs);
  56. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs)), config('cache.expirations.fastChange'), function ()use($kvPairs) {
  57. $query = StationTaskMaterialBox::query();
  58. foreach ($kvPairs as $column => $value){
  59. if (is_array($value))$query->whereIn($column,$value);
  60. else $query->where($column,$value);
  61. }
  62. return $query->get();
  63. });
  64. }
  65. public function insert(array $stationTaskMaterialBoxes): bool
  66. {
  67. $inserted = StationTaskMaterialBox::query()->insert($stationTaskMaterialBoxes);
  68. LogService::log(__METHOD__,__FUNCTION__,json_encode($stationTaskMaterialBoxes).
  69. '||'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3)));
  70. return $inserted;
  71. }
  72. }