CacheShelfService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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('storage.materialBox')->where('code', $locCode)->first();
  51. try {
  52. $bool = $this->putBinToStore($station); // 推送任务
  53. if ($bool) {
  54. LogService::log(__CLASS__, 'lightOffTask', 'code' . ' true' . $locCode . json_encode($station));
  55. return ['success' => true];
  56. } else {
  57. return ['success' => false, 'errMsg' => '机器人推送失败'];
  58. }
  59. } catch (ErrorException $e) {
  60. LogService::log(__FUNCTION__, '缓存架推送任务失败', json_encode($e->getMessage()));
  61. return ['success' => false, 'errMsg' => $e->getMessage()];
  62. }
  63. }
  64. /**
  65. * 推任务至海柔机器人
  66. * @param $station
  67. * @return array
  68. * @throws ErrorException
  69. * @throws \Exception
  70. */
  71. public function putBinToStore($station): array
  72. {
  73. $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
  74. $this->instant($this->stationService, 'StationService');
  75. /** @var MaterialBox $materialbox */
  76. $materialBox = $station->storage->materialbox;
  77. $formStation = $this->stationService->getStation_byType('立库'); // 立库
  78. $stationTask = StationTask::query()->create(['station_id' => $formStation['id'], 'status' => '待处理']); // 生成任务
  79. /** @var StationTaskMaterialBox $stationTaskMaterialBox */
  80. $stationTaskMaterialBox = StationTaskMaterialBox::query()->create([
  81. 'station_id' => $formStation['id'],
  82. 'material_box_id' => $materialBox['id'],
  83. 'status' => '待处理',
  84. 'station_task_id' => $stationTask['id'],
  85. 'type' => '放',
  86. ]);
  87. StationTaskChildren::query()->create([
  88. 'station_task_id' => $stationTask['id'],
  89. 'station_taskable_type' => StationTaskMaterialBox::class,
  90. 'station_taskable_id' => $stationTaskMaterialBox['id']
  91. ]);
  92. $bool = $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station['code']);
  93. return $bool ? ['success' => true] : ['success' => false];
  94. }
  95. /**
  96. * 缓存架和料箱的绑定
  97. * @param $stationCode
  98. * @param $materialBoxCode
  99. * @return array
  100. */
  101. public function bindMaterialBox($stationCode, $materialBoxCode): array
  102. {
  103. $station = Station::query()->with('storage')->where('code', $stationCode)->first();
  104. if (!$station) {
  105. $arr = [];
  106. preg_match('/^HAI([\w]+)/', $stationCode, $arr);
  107. $parentCode = $arr[1] ?? '';
  108. $stationType = StationType::query()->where('name', '缓存架')->first();
  109. $parentStation = Station::query()->firstOrCreate(['code' => $parentCode], ['station_type_id' => $stationType['id']]);
  110. $station = Station::query()->firstOrCreate(['code' => $stationCode, 'parent_id' => $parentStation['id']], ['name' => $stationCode, 'station_type_id' => $stationType['id']]);
  111. }
  112. $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
  113. $storage = $station->storage ?? Storage::query()->firstOrCreate(['station_id' => $station['id']]);
  114. $result = $this->_stationCacheLightOn($station['code'],$materialBoxCode);
  115. if($result['code'] == 200){
  116. $storage->update(['material_box_id' => $materialBox['id'],'status' => 1]);
  117. return ['success' => true];
  118. }
  119. return ['success' => false,'message' => $result['errMsg']];
  120. }
  121. /**
  122. * 控制格口亮灯
  123. * @param $locCode
  124. * @param null $materialCode
  125. * @param string $title
  126. * @param string $color
  127. * @return mixed
  128. */
  129. public function _stationCacheLightOn($locCode, $materialCode = null, $title = 'title', string $color = '1')
  130. {
  131. $params = [
  132. "areaCode" => "1004",
  133. 'locCode' => $locCode,
  134. 'PTLAction' => 1,
  135. 'PTLSettings' => ['color' => $color, 'frequency' => 1],
  136. "displayInfo" => [
  137. "detail01" => $materialCode,
  138. "detail02" => "detail02",
  139. "detail03" => "detail03",
  140. "qrCode" => "qrCode",
  141. "qty00" => "11",
  142. "qty01" => 1,
  143. "qty02" => 2,
  144. "title" => $title,
  145. "uomDesc01" => "uo",
  146. "uomDesc02" => "uo"
  147. ],
  148. ];
  149. $response = Http::post(config('api.haiq.storage.light'), $params);
  150. return json_decode($response->body());
  151. }
  152. /**
  153. * 控制格口灭灯
  154. * @param $locCode
  155. * @return mixed
  156. */
  157. public function _stationCacheLightOff($locCode)
  158. {
  159. if (!$locCode) return null;
  160. $params = [
  161. "areaCode" => "1004",
  162. 'locCode' => $locCode,
  163. 'PTLAction' => 0,
  164. ];
  165. $response = Http::post(config('api.haiq.storage.light'), $params);
  166. return json_decode($response->body());
  167. }
  168. /**
  169. * 广播 type success成功 error 异常
  170. * @param $locCode
  171. * @param $PTLAction
  172. * @param string $type
  173. */
  174. public function _stationCacheBroadCast($locCode, $PTLAction, string $type = 'success')
  175. {
  176. if (!$locCode) return;
  177. if ($PTLAction == 0) {
  178. $station = Station::query()->with('parent')->where('code', $locCode)->first();
  179. $json = json_encode([
  180. 'station_id' => $station['parent']['id'],
  181. 'code' => $station['parent']['code'],
  182. 'gird_id' => $station['id'],
  183. 'grid_code' => $station['code'],
  184. 'type' => $type,
  185. 'status' => 0
  186. ]);
  187. broadcast(new BroadcastToStation($station['parent_id'], $json));
  188. }
  189. }
  190. /**
  191. * 根据立库任务完成对storage进行修改
  192. * @param StationTaskMaterialBox $stationTaskMaterial
  193. */
  194. public function putStationTaskMaterialBoxProcess(StationTaskMaterialBox $stationTaskMaterial)
  195. {
  196. $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
  197. $storage = Storage::query()->where('material_box_id', $stationTaskMaterial['material_box_id'])->where('status',1)->first();
  198. if($storage)$storage->update(['status' => 0, 'material_box_id' => null]);
  199. $this->_stationCacheLightOff($stationTaskMaterial->station->code ?? null); //海柔格口灭灯
  200. $this->_stationCacheBroadCast($stationTaskMaterial->station->code ?? null, 0); //通知缓存架任务完成
  201. }
  202. /**
  203. * 取消任务
  204. * @param $stationCode
  205. * @return array
  206. * @throws \Exception
  207. */
  208. public function clearTask($stationCode): array
  209. {
  210. $station = Station::query()->with('storage')->where('code', $stationCode)->first();
  211. if (!$station) return ['success' => false, 'message' => '传入参数异常,找不到对应的缓存架记录'];
  212. $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$station['storage']['material_box_id'])->first();
  213. if($stationTaskMaterialBox ){
  214. if($stationTaskMaterialBox->status == '处理中')
  215. return ['success' => false, 'message' => '当前缓存架任务正在处理中'];
  216. else{
  217. $stationTaskMaterialBox->delete();
  218. }
  219. }
  220. $station->storage->update(['status' => 0,'material_box_id' => null]);
  221. return ['success' => true];
  222. }
  223. }