CacheShelfService.php 10 KB

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