StationTaskMaterialBoxService.php 7.3 KB

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