CacheShelfService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace App\Services;
  3. use App\Events\BroadcastToStation;
  4. use App\Exceptions\ErrorException;
  5. use App\MaterialBox;
  6. use App\Station;
  7. use App\StationTask;
  8. use App\StationTaskChildren;
  9. use App\StationTaskMaterialBox;
  10. use App\StationType;
  11. use App\Traits\ServiceAppAop;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Http;
  14. use Illuminate\Database\Eloquent\Builder;
  15. use Illuminate\Database\Eloquent\Collection;
  16. class CacheShelfService
  17. {
  18. use ServiceAppAop;
  19. protected $modelClass = Station::class;
  20. /** @var StationService $stationService */
  21. private $stationService;
  22. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  23. private $stationTaskMaterialBoxService;
  24. /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
  25. private $foreignHaiRoboticsService;
  26. /** @var StationTaskService $stationTaskService */
  27. private $stationTaskService;
  28. /** @var StationTaskChildService $stationTaskChildService */
  29. private $stationTaskChildService;
  30. /**
  31. * 获取缓存架上子货架当前任务
  32. * @param $id
  33. * @return Builder[]|Collection
  34. */
  35. public function getChildStation($id)
  36. {
  37. return Station::query()->where('parent_id',$id)->with('parent','pendingStationTask.stationTaskMaterialBoxes.materialBox')->get();
  38. }
  39. /**
  40. * 拍灯触发任务
  41. * @param $locCode
  42. * @param $PTLAction
  43. * @return array|bool[]
  44. */
  45. public function lightOffTask($locCode, $PTLAction): array
  46. {
  47. $station = Station::query()->with('pendingStationTask.stationTaskMaterialBoxes.materialBox')->where('code', $locCode)->first();
  48. try {
  49. $bool = $this->putBinToStore($station); // 推送任务
  50. if($bool){
  51. LogService::log(__CLASS__,'lightOffTask','code' .' true'. $locCode.json_encode($station));
  52. return ['success' => true];
  53. }else{
  54. return ['success' => false,'errMsg' => '机器人推送失败'];
  55. }
  56. } catch (ErrorException $e) {
  57. LogService::log(__FUNCTION__,'缓存架推送任务失败',json_encode($e->getMessage()));
  58. return ['success' => false,'errMsg' => $e->getMessage()];
  59. }
  60. }
  61. /**
  62. * 推任务至海柔机器人
  63. * @param $station
  64. * @return bool
  65. * @throws ErrorException
  66. */
  67. public function putBinToStore($station): bool
  68. {
  69. $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
  70. $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
  71. $this->instant($this->stationService, 'StationService');
  72. $this->instant($this->stationTaskService, 'StationTaskService');
  73. $this->instant($this->stationTaskChildService, 'StationTaskChildService');
  74. /** @var StationTaskMaterialBox $takeStationTaskMaterialBox */
  75. $takeStationTaskMaterialBox = $station['pendingStationTask']['stationTaskMaterialBoxes']->first();
  76. $formStation = $this->stationService->getStation_byType('立库');
  77. // 查询是否有待处理的入库任务
  78. $putStationTaskMaterialBox = StationTaskMaterialBox::query()->where([
  79. 'station_id' => $formStation['id'],
  80. 'material_box_id' => $takeStationTaskMaterialBox['material_box_id'],
  81. 'status' => '待处理',
  82. ])->first();
  83. // 创建入立库任务
  84. if(!$putStationTaskMaterialBox){
  85. $stationTask = $this->stationTaskService->create(1); // 生成站任务
  86. $this->stationTaskService->registerStations($stationTask,[$formStation['id']]); // 注册站任务站
  87. /** @var StationTaskMaterialBox $putStationTaskMaterialBox */
  88. $putStationTaskMaterialBox = $this->stationTaskMaterialBoxService->create([
  89. 'station_id' => $formStation['id'],
  90. 'material_box_id' => $takeStationTaskMaterialBox['material_box_id'],
  91. 'status' => '待处理',
  92. ]);
  93. $putStationTaskMaterialBox['station_task_id'] = $stationTask->first()['id'];
  94. $putStationTaskMaterialBox['type'] = '放';
  95. $putStationTaskMaterialBox->update();
  96. $params = [[
  97. 'station_task_id'=>$stationTask->first()['id'],
  98. 'station_taskable_type'=>StationTaskMaterialBox::class,
  99. 'station_taskable_id'=>$putStationTaskMaterialBox['id']
  100. ]];
  101. $this->stationTaskChildService->insert($params); // 任务任务注册
  102. }
  103. // 推立库任务
  104. $isSuccess = $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($putStationTaskMaterialBox,$station['code']);
  105. if($isSuccess) $this->stationTaskMaterialBoxService->set($takeStationTaskMaterialBox,['status' => '处理中']); // 任务推送成功 标记站任务为处理中
  106. else {
  107. $materialBoxCode = $station['pendingStationTask']['stationTaskMaterialBoxes']->first()->code ?? '';
  108. $this->_stationCacheBroadCast($station->code,0,'error');
  109. $this->_stationCacheLightOn($station->code,$materialBoxCode,'拍灯重试任务');
  110. }
  111. return $isSuccess;
  112. }
  113. /**
  114. * 创建站任务和料箱任务
  115. * @param $stationCode
  116. * @param $materialBoxCode
  117. * @return array
  118. */
  119. public function createStationTask($stationCode,$materialBoxCode): array
  120. {
  121. $this->instant($this->stationTaskService, 'StationTaskService');
  122. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  123. $this->instant($this->stationTaskChildService,'StationTaskChildService');
  124. $station = Station::query()->where('code' , $stationCode)->first();
  125. if(!$station){
  126. $arr = [];
  127. preg_match('/^HAI([\w]+)/',$stationCode,$arr);
  128. $parentCode = $arr[1] ?? '';
  129. $stationType = StationType::query()->where('name','缓存架')->first();
  130. $parentStation = Station::query()->firstOrCreate(['code'=>$parentCode],['station_type_id'=>$stationType['id']]);
  131. $station = Station::query()->firstOrCreate(['code' => $stationCode,'parent_id'=>$parentStation['id']],['name'=>$stationCode,'station_type_id' => $stationType['id']]);
  132. }
  133. $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
  134. $station->load('pendingStationTask.stationTaskMaterialBoxes.materialBox');
  135. if($station->pendingStationTask){
  136. if($station['pendingStationTask']['stationTaskMaterialBoxes']->first()->materialBox->code == $materialBoxCode){
  137. $response = $this->_stationCacheLightOn($stationCode,$materialBoxCode,'任务重试');
  138. if($response->code) return ['success'=>true,'message' => '任务重试'];
  139. return ['success'=>true,'message' => '任务重试失败'];
  140. }
  141. return ['success' => false,'message' => '当前已有进行重的任务'];
  142. }
  143. $stationTask = $this->stationTaskService->create(1); // 生成站任务
  144. $stationTaskMaterialBox = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($station,$materialBox); // 创建料箱任务
  145. $this->stationTaskService->registerStations($stationTask,[$station['id']]); // 注册站任务站
  146. $stationTaskMaterialBox['station_task_id'] = $stationTask->first()['id'];
  147. $stationTaskMaterialBox->update();
  148. $params = [[
  149. 'station_task_id'=>$stationTask->first()['id'],
  150. 'station_taskable_type'=>StationTaskMaterialBox::class,
  151. 'station_taskable_id'=>$stationTaskMaterialBox['id']
  152. ]];
  153. $this->stationTaskChildService->insert($params); // 任务任务注册
  154. $body = $this->_stationCacheLightOn($stationCode,$materialBoxCode);
  155. if($body->code == 200)return ['success'=>true];
  156. return ['success' => false,'message' => '机器人亮灯异常'];
  157. }
  158. /**
  159. * 控制格口亮灯
  160. * @param $locCode
  161. * @param null $materialCode
  162. * @param string $title
  163. * @param string $color
  164. * @return mixed
  165. */
  166. public function _stationCacheLightOn($locCode,$materialCode = null,$title = 'title' ,string $color = '1')
  167. {
  168. $params = [
  169. "areaCode" => "1004",
  170. 'locCode' => $locCode,
  171. 'PTLAction' => 1,
  172. 'PTLSettings' => ['color'=> $color, 'frequency' =>1],
  173. "displayInfo" => [
  174. "detail01" => $materialCode,
  175. "detail02" => "detail02",
  176. "detail03" => "detail03",
  177. "qrCode" => "qrCode",
  178. "qty00" => "11",
  179. "qty01" => 1,
  180. "qty02" => 2,
  181. "title" => $title,
  182. "uomDesc01" => "uo",
  183. "uomDesc02" => "uo"
  184. ],
  185. ];
  186. $response = Http::post(config('api.haiq.storage.light'), $params);
  187. return json_decode($response->body());
  188. }
  189. /**
  190. * 控制格口灭灯
  191. * @param $locCode
  192. * @return mixed
  193. */
  194. public function _stationCacheLightOff($locCode){
  195. if(!$locCode)return null;
  196. $params = [
  197. "areaCode" => "1004",
  198. 'locCode' => $locCode,
  199. 'PTLAction' => 0,
  200. ];
  201. $response = Http::post(config('api.haiq.storage.light'), $params);
  202. return json_decode($response->body());
  203. }
  204. /**
  205. * 广播 type success成功 error 异常
  206. * @param $locCode
  207. * @param $PTLAction
  208. * @param string $type
  209. */
  210. public function _stationCacheBroadCast($locCode, $PTLAction,string $type = 'success')
  211. {
  212. if($PTLAction == 0){
  213. $station = Station::query()->with('parent')->where('code',$locCode)->first();
  214. $json = json_encode( [
  215. 'station_id' => $station['parent']['id'],
  216. 'code' => $station['parent']['code'],
  217. 'gird_id' => $station['id'],
  218. 'grid_code' => $station['code'],
  219. 'type' => $type
  220. ]);
  221. broadcast(new BroadcastToStation($station['parent_id'],$json));
  222. }
  223. }
  224. /**
  225. * 根据立库任务完成 缓存架任务 和 入立架任务
  226. * @param StationTaskMaterialBox $putStationTaskMaterial
  227. */
  228. public function putStationTaskMaterialBoxProcess(StationTaskMaterialBox $putStationTaskMaterial)
  229. {
  230. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  231. $takeStationTaskMaterialBox = StationTaskMaterialBox::query()->with('station','stationTask')->where('material_box_id',$putStationTaskMaterial['material_box_id'])->whereIn('station_id',function($query){
  232. $query->from('stations')->selectRaw('id')->whereIn('station_type_id',function($query){
  233. $query->from('station_types')->selectRaw('id')->where('name','缓存架');
  234. });
  235. })->where('status','处理中')->first();
  236. if(!$takeStationTaskMaterialBox)return ;
  237. $this->stationTaskMaterialBoxService->set($takeStationTaskMaterialBox, [
  238. 'status' => '完成'
  239. ]);
  240. // 缓存架任务
  241. if($takeStationTaskMaterialBox->stationTask)$takeStationTaskMaterialBox->stationTask->update(['status' => '完成']);
  242. // 入立架任务
  243. if($putStationTaskMaterial->stationTask)$putStationTaskMaterial->stationTask->update(['status' => '完成']);
  244. $this->_stationCacheLightOff($takeStationTaskMaterialBox->station->code ?? null); //海柔格口灭灯
  245. $this->_stationCacheBroadCast($takeStationTaskMaterialBox->station->code, 0); //通知缓存架任务完成
  246. }
  247. /**
  248. * 取消任务
  249. * @param $stationCode
  250. * @return array
  251. */
  252. public function clearTask($stationCode): array
  253. {
  254. $station = Station::query()->with(['currentStationTask.stationTaskMaterialBoxes.materialBox',
  255. 'pendingStationTask.stationTaskMaterialBoxes.materialBox'])
  256. ->where('code',$stationCode)->first();
  257. if($station->currentStationTask)return ['success' => false,'message' => '当前任务正在执行','data'=>$stationCode];
  258. if($station->pendingStationTask->stationTaskMaterialBoxes->count() == 0){
  259. $station->pendingStationTask->delete();
  260. return ['success' => true];
  261. }
  262. $taskStationTaskMaterialBox = $station->pendingStationTask->stationTaskMaterialBoxes->first() ?? null;
  263. $stationTaskMaterialBoxes = StationTaskMaterialBox::query()->with('stationTask')
  264. ->where('material_box_id',$taskStationTaskMaterialBox['material_box_id'])
  265. ->where('station_id','!=',$station->pendingStationTask['id'])
  266. ->get();
  267. foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
  268. if($stationTaskMaterialBox->stationTask){
  269. StationTaskChildren::query()->where([
  270. 'station_task_id' => $stationTaskMaterialBox->stationTask['id'],
  271. 'station_taskable_type'=>StationTaskMaterialBox::class,
  272. 'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
  273. $stationTaskMaterialBox->stationTask->delete();
  274. }
  275. $stationTaskMaterialBox->delete();
  276. }
  277. return ['success' => true];
  278. }
  279. }