StationTaskMaterialBoxService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Exceptions\ErrorException;
  5. use App\MaterialBox;
  6. use App\OrderCommodity;
  7. use App\StationTask;
  8. use App\StationTaskMaterialBox;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Cache;
  11. use App\Traits\ServiceAppAop;
  12. class StationTaskMaterialBoxService
  13. {
  14. use ServiceAppAop;
  15. /** @var StationService $stationService */
  16. private $stationService;
  17. /** @var StationTypeService $stationTypeService */
  18. private $stationTypeService;
  19. /** @var StationTaskService $stationTaskService */
  20. private $stationTaskService;
  21. /** @var StationTaskBatchService $stationTaskBatchService */
  22. private $stationTaskBatchService;
  23. /** @var StationTaskCommodityService $stationTaskCommodityService */
  24. private $stationTaskCommodityService;
  25. /** @var MaterialBoxService $materialBoxService */
  26. private $materialBoxService;
  27. public function __construct(){
  28. $this->stationService=null;
  29. $this->stationTypeService=null;
  30. $this->stationTaskService=null;
  31. $this->materialBoxService=null;
  32. $this->stationTaskBatchService=null;
  33. $this->stationTaskCommodityService=null;
  34. }
  35. function createByBatches(Collection $batches,Collection $stationTasks_toAttach): Collection
  36. {
  37. $this->instant($this->stationTaskService,'StationTaskService');
  38. $stationTaskMaterialBoxes = (function () use ($batches) {
  39. $stationTaskMaterialBoxes_listByBatch = new Collection();
  40. foreach ($batches as $batch) {
  41. $stationTaskMaterialBoxes_listByBatch=
  42. $stationTaskMaterialBoxes_listByBatch->merge(
  43. $this->createByBatch($batch)
  44. );
  45. }
  46. return $stationTaskMaterialBoxes_listByBatch;
  47. })();
  48. $this->stationTaskService
  49. ->registerSubTasks(
  50. $stationTasks_toAttach,
  51. [$stationTaskMaterialBoxes]);
  52. return $stationTaskMaterialBoxes;
  53. }
  54. function createByBatch(Batch $batch): ?Collection
  55. {
  56. $this->instant($this->materialBoxService,'MaterialBoxService');
  57. $this->instant($this->stationTypeService,'StationTypeService');
  58. $this->instant($this->stationService,'StationService');
  59. $stationMaterialBoxes_toCreate=new Collection();
  60. $order_ids=data_get($batch['orders'],'*.id');
  61. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  62. if($orderCommodities->isEmpty())return $stationMaterialBoxes_toCreate;
  63. $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
  64. foreach ($orderCommodities as $orderCommodity){
  65. $station=$this->stationService->getStation_byType($stationType['name']);
  66. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  67. $stationMaterialBoxes_toCreate->push([
  68. 'station_id'=>$station['id'],
  69. 'material_box_id'=>$materialBox['id'],
  70. 'status'=>'待处理'
  71. ]);
  72. }
  73. return $this->insert($stationMaterialBoxes_toCreate->toArray());
  74. }
  75. function get(array $kvPairs){
  76. ksort($kvPairs);
  77. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs)), config('cache.expirations.fastChange'), function ()use($kvPairs) {
  78. $query = StationTaskMaterialBox::query();
  79. foreach ($kvPairs as $column => $value){
  80. if (is_array($value))$query->whereIn($column,$value);
  81. else $query->where($column,$value);
  82. }
  83. return $query->get();
  84. });
  85. }
  86. public function insert(array $stationTaskMaterialBoxes): Collection
  87. {
  88. $inserted=collect();
  89. foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox){
  90. $inserted->push(StationTaskMaterialBox::query()->create($stationTaskMaterialBox));
  91. }
  92. return $inserted;
  93. }
  94. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  95. try{
  96. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  97. switch ($taskType){
  98. case '分波次':
  99. $this->markProcessing($stationTaskMaterialBox);
  100. $this->stationTaskBatchService->markProcessing($stationTaskMaterialBox['stationTaskBatch']);
  101. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  102. break;
  103. case '入立库':
  104. break;
  105. case '入库':break;
  106. }
  107. }catch (\Exception $e){
  108. throw new ErrorException('放置料箱出错');
  109. }
  110. }
  111. function markTaken($stationTaskMaterialBox){
  112. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  113. }
  114. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  115. $stationTaskMaterialBox['status'] = '完成';
  116. $stationTaskMaterialBox->save();
  117. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  118. }
  119. function markProcessing($stationTaskMaterialBox)
  120. {
  121. if (get_class($stationTaskMaterialBox)==StationTaskMaterialBox::class){
  122. $stationTaskMaterialBox = collect($stationTaskMaterialBox);
  123. }
  124. StationTaskMaterialBox::query()
  125. ->whereIn('id', data_get($stationTaskMaterialBox, '*.id'))
  126. ->update(['status'=>'处理中']);
  127. $this->stationTaskService
  128. ->markProcessing_byId(
  129. data_get($stationTaskMaterialBox, '*.station_id')
  130. );
  131. }
  132. function excepted($stationTaskMaterialBox_orBox){
  133. switch (get_class($stationTaskMaterialBox_orBox)){
  134. case MaterialBox::class:
  135. case StationTaskMaterialBox::class:
  136. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBox_orBox->toJson()));
  137. }
  138. }
  139. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  140. {
  141. $stationTaskMaterialBox->loadMissing('station.stationType');
  142. if($isBatching=(
  143. $stationTaskMaterialBox['station_task_batch_id'] &&
  144. $stationTaskMaterialBox['station']['stationType'] == '料箱监视器')
  145. ){
  146. return '分波次';
  147. }
  148. if($isPuttingBack=(
  149. !$stationTaskMaterialBox &&
  150. $stationTaskMaterialBox['station']['stationType'] == '立库')
  151. ){
  152. return '入立库';
  153. }
  154. if($isStoring=false){
  155. return '入库';
  156. }
  157. throw new ErrorException('当前类型找不到');
  158. }
  159. }