CacheShelfService.php 12 KB

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