StationTaskMaterialBoxService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 markHasPut(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. LogService::log('海柔请求','markHasTaken1','');
  101. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  102. LogService::log('海柔请求','markHasTaken2',
  103. json_encode($taskType));
  104. switch ($taskType){
  105. case '分波次':
  106. $this->markProcessing($stationTaskMaterialBox);
  107. $this->stationTaskBatchService->markProcessing_byIds($stationTaskMaterialBox['station_task_batch_id']);
  108. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  109. $this->stationTaskService->markProcessing_byIds(data_get($stationTaskMaterialBox['stationTaskCommodities'],'*.station_task_id'));
  110. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  111. break;
  112. case '入立库':
  113. $this->set($stationTaskMaterialBox,[
  114. 'id' => $stationTaskMaterialBox['station_id'],
  115. 'status' => '完成',
  116. ]);
  117. break;
  118. case '入缓存架':break;
  119. default:;
  120. }
  121. }catch (\Exception $e){
  122. throw new ErrorException('放置料箱出错');
  123. }
  124. }
  125. function markHasTaken($stationTaskMaterialBox){
  126. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  127. }
  128. function processNextQueued(StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  129. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  130. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  131. ->where('station_id',$station_id)
  132. ->where('status','处理队列')
  133. ->orderBy('updated_at')
  134. ->first();
  135. if($stationTaskMaterialBox_next){
  136. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  137. }
  138. return $stationTaskMaterialBox_next;
  139. }
  140. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  141. $stationTaskMaterialBox['status'] = '完成';
  142. $stationTaskMaterialBox->update();
  143. }
  144. /**
  145. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  146. * 如某波次已经有“处理中“,则他部置入队列
  147. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  148. */
  149. function markProcessing($stationTaskMaterialBox_orBoxes)
  150. {
  151. $this->instant($this->stationTaskService,'StationTaskService');
  152. $stationTaskMaterialBoxes =
  153. (function()use($stationTaskMaterialBox_orBoxes){
  154. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  155. return collect([$stationTaskMaterialBox_orBoxes]);
  156. }
  157. return collect($stationTaskMaterialBox_orBoxes);
  158. })();
  159. $stationTaskMaterialBoxes_grouped=
  160. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  161. return $stationTaskMaterialBoxes
  162. ->sortBy('id')
  163. ->groupBy('station_task_batch_id');
  164. })();
  165. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  166. ($将所以要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  167. $groupByBatch->each(function (&$stationTaskMaterialBox){
  168. $stationTaskMaterialBox['status']='处理队列';
  169. });
  170. })();
  171. ($如果之前没有处理中则标记第一个为处理目标,并持久化=function()use(&$groupByBatch){
  172. $taskBatchId=$groupByBatch[0]['station_task_batch_id'];
  173. $processing=$this->getProcessing_byTaskBatch($taskBatchId);
  174. if(!$processing){
  175. $groupByBatch[0]['status']='处理中';
  176. $groupByBatch[0]->update();
  177. }else{
  178. foreach ($groupByBatch as &$stationTaskMaterialBox){
  179. if($stationTaskMaterialBox['id']==$processing['id']){
  180. $stationTaskMaterialBox['status']='处理中';
  181. }
  182. }
  183. }
  184. })();
  185. });
  186. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  187. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  188. $toArray=$toArray->where('status','处理队列');
  189. $ids_toUpdate = data_get($toArray, '*.id');
  190. if(count($ids_toUpdate))
  191. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  192. })();
  193. // StationTaskMaterialBox::query()
  194. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  195. // ->update(['status'=>'处理中']);
  196. $this->stationTaskService
  197. ->markProcessing_byIds(
  198. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  199. );
  200. }
  201. function getProcessing_byTaskBatch($stationTaskBatch_id)
  202. {
  203. //这里不能用缓存,因为更新会非常快
  204. return StationTaskMaterialBox::query()
  205. ->where('station_task_batch_id',$stationTaskBatch_id)
  206. ->where('status','处理中')
  207. ->first();
  208. }
  209. function excepted($stationTaskMaterialBoxes_orBox){
  210. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  211. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  212. }
  213. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  214. ->update(['status'=>'异常']);
  215. switch (get_class($stationTaskMaterialBoxes_orBox)){
  216. case MaterialBox::class:
  217. case StationTaskMaterialBox::class:
  218. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  219. }
  220. }
  221. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  222. {
  223. $stationTaskMaterialBox->load('station.stationType');
  224. if($isBatching=(
  225. $stationTaskMaterialBox['station_task_batch_id'] &&
  226. $stationTaskMaterialBox['station']['stationType']['name'] == '料箱监视器')
  227. ){
  228. return '分波次';
  229. }
  230. if($isPuttingBack=(
  231. $stationTaskMaterialBox['station']['stationType']['name'] == '立库')
  232. ){
  233. return '入立库';
  234. }
  235. // if($isStoring=false){
  236. // return '入库';
  237. // }
  238. throw new ErrorException('当前类型找不到');
  239. }
  240. }