StationTaskMaterialBoxService.php 6.5 KB

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