StationTaskMaterialBoxService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c1:'.json_encode($batches));
  42. $stationTaskMaterialBoxes_byBatch = (function () use ($batches) {
  43. $stationTaskMaterialBoxes_listByBatch = new Collection();
  44. foreach ($batches as $batch) {
  45. $stationTaskMaterialBoxes_listByBatch->push(
  46. $this->createByBatch($batch)
  47. );
  48. }
  49. return $stationTaskMaterialBoxes_listByBatch;
  50. })();
  51. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c2:'.json_encode($batches));
  52. $this->stationTaskService
  53. ->registerSubTasks(
  54. $stationTasks_toAttach,
  55. $stationTaskMaterialBoxes_byBatch);
  56. return collect(data_get($stationTaskMaterialBoxes_byBatch,'*.*'));
  57. }
  58. function createByBatch(Batch $batch): ?Collection
  59. {
  60. $this->instant($this->materialBoxService,'MaterialBoxService');
  61. $this->instant($this->stationTypeService,'StationTypeService');
  62. $this->instant($this->stationService,'StationService');
  63. $stationMaterialBoxes_toCreate=new Collection();
  64. $order_ids=data_get($batch['orders'],'*.id');
  65. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  66. if($orderCommodities->isEmpty())return $stationMaterialBoxes_toCreate;
  67. $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
  68. foreach ($orderCommodities as $orderCommodity){
  69. $station=$this->stationService->getStation_byType($stationType['name']);
  70. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  71. $stationMaterialBoxes_toCreate->push([
  72. 'station_id'=>$station['id'],
  73. 'material_box_id'=>$materialBox['id'],
  74. 'station_task_batch_id'=>$batch['id'],
  75. 'status'=>'待处理'
  76. ]);
  77. }
  78. return $this->insert($stationMaterialBoxes_toCreate->toArray(),true);
  79. }
  80. function get(array $kvPairs, $with=null){
  81. ksort($kvPairs);
  82. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs).json_encode([$with])), config('cache.expirations.fastChange'), function ()use($kvPairs,$with) {
  83. $query = StationTaskMaterialBox::query();
  84. if($with){
  85. $query->with($with);
  86. }
  87. foreach ($kvPairs as $column => $value){
  88. if (is_array($value))$query->whereIn($column,$value);
  89. else $query->where($column,$value);
  90. }
  91. return $query->get();
  92. });
  93. }
  94. function markHasTaken(StationTaskMaterialBox $stationTaskMaterialBox){
  95. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  96. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  97. $this->instant($this->stationTaskService,'StationTaskService');
  98. $this->instant($this->stationService,'StationService');
  99. try{
  100. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  101. switch ($taskType){
  102. case '分波次':
  103. $this->markProcessing($stationTaskMaterialBox);
  104. $this->stationTaskBatchService->markProcessing_byIds($stationTaskMaterialBox['station_task_batch_id']);
  105. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  106. $this->stationTaskService->markProcessing_byIds(data_get($stationTaskMaterialBox['stationTaskCommodities'],'*.station_task_id'));
  107. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  108. break;
  109. case '入立库':
  110. break;
  111. case '入库':break;
  112. }
  113. }catch (\Exception $e){
  114. throw new ErrorException('放置料箱出错');
  115. }
  116. }
  117. function markPutStored($stationTaskMaterialBox){
  118. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  119. }
  120. function processNextQueued(StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  121. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  122. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  123. ->where('station_id',$station_id)
  124. ->where('status','处理队列')
  125. ->orderBy('updated_at')
  126. ->first();
  127. if($stationTaskMaterialBox_next){
  128. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  129. }
  130. return $stationTaskMaterialBox_next;
  131. }
  132. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  133. $stationTaskMaterialBox['status'] = '完成';
  134. $stationTaskMaterialBox->update();
  135. }
  136. /**
  137. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  138. * 如某波次已经有“处理中“,则他部置入队列
  139. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  140. */
  141. function markProcessing($stationTaskMaterialBox_orBoxes)
  142. {
  143. $this->instant($this->stationTaskService,'StationTaskService');
  144. $stationTaskMaterialBoxes =
  145. (function()use($stationTaskMaterialBox_orBoxes){
  146. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  147. return collect([$stationTaskMaterialBox_orBoxes]);
  148. }
  149. return collect($stationTaskMaterialBox_orBoxes);
  150. })();
  151. $stationTaskMaterialBoxes_grouped=
  152. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  153. return $stationTaskMaterialBoxes
  154. ->sortBy('id')
  155. ->groupBy('station_task_batch_id');
  156. })();
  157. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  158. ($将所以要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  159. $groupByBatch->each(function (&$stationTaskMaterialBox){
  160. $stationTaskMaterialBox['status']='处理队列';
  161. });
  162. })();
  163. ($如果之前没有处理中则标记第一个为处理目标,并持久化=function()use(&$groupByBatch){
  164. $taskBatchId=$groupByBatch[0]['station_task_batch_id'];
  165. $processing=$this->getProcessing_byTaskBatch($taskBatchId);
  166. if(!$processing){
  167. $groupByBatch[0]['status']='处理中';
  168. $groupByBatch[0]->update();
  169. }else{
  170. foreach ($groupByBatch as &$stationTaskMaterialBox){
  171. if($stationTaskMaterialBox['id']==$processing['id']){
  172. $stationTaskMaterialBox['status']='处理中';
  173. }
  174. }
  175. }
  176. })();
  177. });
  178. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  179. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  180. $toArray=$toArray->where('status','处理队列');
  181. $ids_toUpdate = data_get($toArray, '*.id');
  182. if(count($ids_toUpdate))
  183. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  184. })();
  185. // StationTaskMaterialBox::query()
  186. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  187. // ->update(['status'=>'处理中']);
  188. $this->stationTaskService
  189. ->markProcessing_byIds(
  190. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  191. );
  192. }
  193. function getProcessing_byTaskBatch($stationTaskBatch_id)
  194. {
  195. //这里不能用缓存,因为更新会非常快
  196. return StationTaskMaterialBox::query()
  197. ->where('station_task_batch_id',$stationTaskBatch_id)
  198. ->where('status','处理中')
  199. ->first();
  200. }
  201. function excepted($stationTaskMaterialBoxes_orBox){
  202. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  203. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  204. }
  205. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  206. ->update(['status'=>'异常']);
  207. switch (get_class($stationTaskMaterialBoxes_orBox)){
  208. case MaterialBox::class:
  209. case StationTaskMaterialBox::class:
  210. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  211. }
  212. }
  213. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  214. {
  215. $stationTaskMaterialBox->load('station.stationType');
  216. if($isBatching=(
  217. $stationTaskMaterialBox['station_task_batch_id'] &&
  218. $stationTaskMaterialBox['station']['stationType']['name'] == '料箱监视器')
  219. ){
  220. return '分波次';
  221. }
  222. if($isPuttingBack=(
  223. !$stationTaskMaterialBox &&
  224. $stationTaskMaterialBox['station']['stationType'] == '立库')
  225. ){
  226. return '入立库';
  227. }
  228. // if($isStoring=false){
  229. // return '入库';
  230. // }
  231. throw new ErrorException('当前类型找不到');
  232. }
  233. }