| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace App\Services;
- use App\Events\BroadcastToStation;
- use App\Exceptions\ErrorException;
- use App\MaterialBox;
- use App\Station;
- use App\StationTask;
- use App\StationTaskChild;
- use App\StationTaskChildren;
- use App\StationTaskMaterialBox;
- use App\StationType;
- use App\Storage;
- use App\Traits\ServiceAppAop;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Collection;
- class CacheShelfService
- {
- use ServiceAppAop;
- protected $modelClass = Station::class;
- /** @var StationService $stationService */
- private $stationService;
- /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
- private $stationTaskMaterialBoxService;
- /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
- private $foreignHaiRoboticsService;
- /** @var StationTaskService $stationTaskService */
- private $stationTaskService;
- /** @var StationTaskChildService $stationTaskChildService */
- private $stationTaskChildService;
- /**
- * 获取缓存架上子货架当前任务
- * @param $id
- * @return Builder[]|Collection
- */
- public function getChildStation($id)
- {
- return Station::query()->where('parent_id', $id)->with('storage.materialBox')->get();
- }
- /**
- * 拍灯触发任务
- * @param $locCode
- * @param $PTLAction
- * @return array|bool[]
- * @throws \Exception
- */
- public function lightOffTask($locCode, $PTLAction): array
- {
- $station = Station::query()->with(['pendingStationTask.stationTaskMaterialBoxes.materialBox','storage'=>function($query){
- $query->whereNotNull("material_box_id")->orderByDesc("updated_at");
- }])->where('code', $locCode)->first();
- //站存在 站为缓存架2 站为蓝灯状态
- if ($station && $station->parent_id==7){
- if (!Storage::query()->where("station_id",$station->id)->whereNotNull("material_box_id")->first())return ['success' => false, 'errMsg' => "任务执行中,不允许灭灯"];
- if (!app("StorageService")->checkStorage($station)){
- $this->stationLightUp($station->code,null,'0','1','上架任务失败');
- return ['success' => false,'errMsg' => '上架任务失败'];
- };
- }
- try {
- $bool = $this->putBinToStore($station); // 推送任务
- if ($bool) {
- LogService::log(__CLASS__, 'lightOffTask', 'code' . ' true' . $locCode . json_encode($station));
- return ['success' => true];
- } else {
- return ['success' => false, 'errMsg' => '机器人推送失败'];
- }
- } catch (ErrorException $e) {
- LogService::log(__FUNCTION__, '缓存架推送任务失败', json_encode($e->getMessage()));
- return ['success' => false, 'errMsg' => $e->getMessage()];
- }
- }
- /**
- * 推任务至海柔机器人
- * @param $station
- * @return array
- * @throws ErrorException
- * @throws \Exception
- */
- public function putBinToStore($station): array
- {
- $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
- $this->instant($this->stationService, 'StationService');
- /** @var MaterialBox $materialbox */
- $materialBox = $station->storage->materialbox;
- $formStation = $this->stationService->getStation_byType('立库'); // 立库
- $stationTask = StationTask::query()->create(['station_id' => $formStation['id'], 'status' => '待处理']); // 生成任务
- /** @var StationTaskMaterialBox $stationTaskMaterialBox */
- $stationTaskMaterialBox = StationTaskMaterialBox::query()->create([
- 'station_id' => $formStation['id'],
- 'material_box_id' => $materialBox['id'],
- 'status' => '待处理',
- 'station_task_id' => $stationTask['id'],
- 'type' => '放',
- ]);
- StationTaskChildren::query()->create([
- 'station_task_id' => $stationTask['id'],
- 'station_taskable_type' => StationTaskMaterialBox::class,
- 'station_taskable_id' => $stationTaskMaterialBox['id']
- ]);
- $bool = $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station['code']);
- return $bool ? ['success' => true] : ['success' => false];
- }
- /**
- * 缓存架和料箱的绑定
- * @param $stationCode
- * @param $materialBoxCode
- * @return array
- */
- public function bindMaterialBox($stationCode, $materialBoxCode): array
- {
- $station = Station::query()->with('storage')->where('code', $stationCode)->first();
- if (!$station) {
- $arr = [];
- preg_match('/^HAI([\w]+)/', $stationCode, $arr);
- $parentCode = $arr[1] ?? '';
- $stationType = StationType::query()->where('name', '缓存架')->first();
- $parentStation = Station::query()->firstOrCreate(['code' => $parentCode], ['station_type_id' => $stationType['id']]);
- $station = Station::query()->firstOrCreate(['code' => $stationCode, 'parent_id' => $parentStation['id']], ['name' => $stationCode, 'station_type_id' => $stationType['id']]);
- }
- $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
- $storage = $station->storage ?? Storage::query()->firstOrCreate(['station_id' => $station['id']]);
- $result = $this->_stationCacheLightOn($station['code'],$materialBoxCode);
- if($result['code'] == 200){
- $storage->update(['material_box_id' => $materialBox['id'],'status' => 1]);
- return ['success' => true];
- }
- return ['success' => false,'message' => $result['errMsg']];
- }
- /**
- * 控制格口亮灯
- * @param $locCode
- * @param null $materialCode
- * @param string $title
- * @param string $color
- * @return mixed
- */
- public function _stationCacheLightOn($locCode, $materialCode = null, $title = 'title', string $color = '1')
- {
- $params = [
- "areaCode" => "1004",
- 'locCode' => $locCode,
- 'PTLAction' => 1,
- 'PTLSettings' => ['color' => $color, 'frequency' => 1],
- "displayInfo" => [
- "detail01" => $materialCode,
- "detail02" => "detail02",
- "detail03" => "detail03",
- "qrCode" => "qrCode",
- "qty00" => "11",
- "qty01" => 1,
- "qty02" => 2,
- "title" => $title,
- "uomDesc01" => "uo",
- "uomDesc02" => "uo"
- ],
- ];
- $response = Http::post(config('api.haiq.storage.light'), $params);
- return json_decode($response->body());
- }
- /**
- * 站亮灯
- *
- * @param string $stationCode
- * @param string|null $materialCode
- * @param string $color explain: 0-red 1-green 2-blue 3-yellow
- * @param string $frequency explain: 0-常亮 1-一次 2-两次 3-三次 4-四次 5-五次 (均为/秒)
- * @param string $title
- * @return mixed
- */
- public function stationLightUp(string $stationCode,?string $materialCode = null, string $color = '1', string $frequency = '0', $title = '')
- {
- $params = [
- "areaCode" => "1004",
- 'locCode' => $stationCode,
- 'PTLAction' => 1,
- 'PTLSettings' => ['color' => $color, 'frequency' => $frequency],
- "displayInfo" => [
- "detail01" => $materialCode,
- "detail02" => "detail02",
- "detail03" => "detail03",
- "qrCode" => "qrCode",
- "qty00" => "11",
- "qty01" => 1,
- "qty02" => 2,
- "title" => $title,
- "uomDesc01" => "uo",
- "uomDesc02" => "uo"
- ],
- ];
- $response = Http::post(config('api.haiq.storage.light'), $params);
- return json_decode($response->body());
- }
- /**
- * 站亮灯
- *
- * @param string $stationCode
- * @param string $color explain: 0-red 1-green 2-blue 3-yellow
- * @param string $frequency explain: 0-常亮 1-一次 2-两次 3-三次 4-四次 5-五次 (均为/秒)
- * @param array $info
- * @return bool
- */
- public function lightUp(string $stationCode, string $color = '1', string $frequency = '0',array $info = []):bool
- {
- $default = [
- "detail01" => '',
- "detail02" => "",
- "detail03" => "",
- "qrCode" => "",
- "qty00" => "",
- "qty01" => 0,
- "qty02" => 0,
- "title" => '',
- "uomDesc01" => "",
- "uomDesc02" => ""
- ];
- foreach ($info as $key=>$item)$default[$key] = $item;
- $params = [
- "areaCode" => "1004",
- 'locCode' => $stationCode,
- 'PTLAction' => 1,
- 'PTLSettings' => ['color' => $color, 'frequency' => $frequency],
- "displayInfo" => $default,
- ];
- $response = Http::post(config('api.haiq.storage.light'), $params);
- return $response->status()==200;
- }
- /**
- * 控制格口灭灯
- * @param $locCode
- * @return mixed
- */
- public function _stationCacheLightOff($locCode)
- {
- if (!$locCode) return null;
- $params = [
- "areaCode" => "1004",
- 'locCode' => $locCode,
- 'PTLAction' => 0,
- ];
- $response = Http::post(config('api.haiq.storage.light'), $params);
- return json_decode($response->body());
- }
- /**
- * 广播 type success成功 error 异常
- * @param $locCode
- * @param $PTLAction
- * @param string $type
- */
- public function _stationCacheBroadCast($locCode, $PTLAction, string $type = 'success')
- {
- if (!$locCode) return;
- if ($PTLAction == 0) {
- $station = Station::query()->with('parent')->where('code', $locCode)->first();
- $json = json_encode([
- 'station_id' => $station['parent']['id'],
- 'code' => $station['parent']['code'],
- 'gird_id' => $station['id'],
- 'grid_code' => $station['code'],
- 'type' => $type,
- 'status' => 0
- ]);
- broadcast(new BroadcastToStation($station['parent_id'], $json));
- }
- }
- /**
- * 根据立库任务完成对storage进行修改
- * @param StationTaskMaterialBox $stationTaskMaterial
- */
- public function putStationTaskMaterialBoxProcess(StationTaskMaterialBox $stationTaskMaterial)
- {
- $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
- $storage = Storage::query()->where('material_box_id', $stationTaskMaterial['material_box_id'])->where('status',1)->first();
- if($storage)$storage->update(['status' => 0, 'material_box_id' => null]);
- $this->_stationCacheLightOff($stationTaskMaterial->station->code ?? null); //海柔格口灭灯
- $this->_stationCacheBroadCast($stationTaskMaterial->station->code ?? null, 0); //通知缓存架任务完成
- }
- /**
- * 取消任务
- * @param $stationCode
- * @return array
- * @throws \Exception
- */
- public function clearTask($stationCode): array
- {
- $station = Station::query()->with('storage')->where('code', $stationCode)->first();
- if (!$station) return ['success' => false, 'message' => '传入参数异常,找不到对应的缓存架记录'];
- $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$station['storage']['material_box_id'])->first();
- if($stationTaskMaterialBox ){
- if($stationTaskMaterialBox->status == '处理中')
- return ['success' => false, 'message' => '当前缓存架任务正在处理中'];
- else{
- $stationTaskMaterialBox->delete();
- }
- }
- $station->storage->update(['status' => 0,'material_box_id' => null]);
- return ['success' => true];
- }
- }
|