where('name',$typeName)->orderBy('id')->get('id')->first(); if(!$stationType) throw new Exception('指定站类型获取不到'); return Station::query()->where('station_type_id',$stationType['id'])->first(); }); if(!$station)throw new Exception('默认站获取不到'); return $station; } function getULineEntrance(Station $station):?Station{ $station->loadMissing(['stationType','child']); if (($station['stationType']['name']??'')=='料箱出货口'){ return $station; } if (($station['child']['stationType']['name']??'')=='料箱出货口'){ return $station['child']; } return null; } function getULineExit(Station $station):?Station{ $station->loadMissing(['stationType','child']); if ($station['stationType']['name']??''=='料箱入货口'){ return $station; } if ($station['child']['stationType']['name']??''=='料箱入货口'){ return $station['child']; } return null; } function broadcast($station_id, ?StationTask $stationTask){ LogService::log('海柔请求in2','broadcast', $station_id.'|'.json_encode($stationTask)); if($stationTask) $json = $stationTask->toJson(); else $json =[]; LogService::log('海柔请求done2','broadcast', $station_id.'|'.$json); broadcast(new BroadcastToStation($station_id, $json)); } function broadcastBinMonitor($station_id, ?StationTask $stationTask){ LogService::log('海柔请求in1','broadcastBinMonitor', $station_id.'|'.json_encode($stationTask)); if(!$stationTask)return; $this->instant($this->stationTaskService,'StationTaskService'); if($stationTask['status']=='完成') $stationTask=$this->stationTaskService->getCurrent_shouldProcess_ByStationId($stationTask['station_id']); if($stationTask) $stationTask->loadMissing([ "stationTaskCommodities.commodity.barcodes", "stationTaskCommodities.stationTaskMaterialBox", "stationTaskBatches.batch", "stationTaskMaterialBoxes.materialBox", ]); LogService::log('海柔请求done1','broadcastBinMonitor', $station_id.'|'.json_encode($stationTask)); $this->broadcast($station_id, $stationTask); } /** * 获取镜像映射库位 * * @param string $mirrorLocation * * @return Model|null */ public function getMirrorMappingLocation(string $mirrorLocation):?Station { if (!$mirrorLocation)return null; return Station::query()->where("code",substr($mirrorLocation,2))->first(); } /** * 是否为半箱缓存架位置 * * @param Station|null|\stdClass $station * @return bool */ public function isHalfBoxLocation(?Station $station):bool { if (!$station)return false; return $station->station_type_id==5 && $station->parent_id; } /** * 是否为缓存架位置 * * @param Station|\stdClass $station * * @return bool */ public function isCacheShelfLocation(Station $station):bool { return $station->station_type_id == 5; } /** * 获取缓存架 * * @param bool $onlyAvailable * * @return Collection */ public function getCacheShelf(bool $onlyAvailable = false):Collection { $stations = Station::query()->where("station_type_id",5) ->whereNotNull("parent_id")->orderByRaw("LEFT(code,5),RIGHT(code,1)"); if ($onlyAvailable)$stations->where("status",0) ->whereNotIn("id",Station::query()->select("id")->whereHas("task",function ($query){ $query->whereNotIn("status",["完成","取消"]); }))->lockForUpdate(); return $stations->get(); } /** * @param array $codes * * @return array */ public function getStationMapping(array $codes):array { $mapping = []; foreach (Station::query()->whereIn("code",$codes)->get() as $station){ $mapping[$station->code] = $station->id; } return $mapping; } /** * 库位占用 * 为未执行的上架任务预定库位,禁止二次下发 * * @param string $location * @param int|null $boxId * * @return int */ public function locationOccupy(string $location, ?int $boxId=null):int { $update = ["status"=>1]; if ($boxId)$update["material_box_id"]=$boxId; return Station::query()->where("code",$location)->update($update); } /** * 多库位占用 * @param array $location * @return bool */ public function locationOccupyMulti(array $location):bool { return !!Station::query()->whereIn("code",$location)->update(["status"=>1]); } /** * 库位释放 * * @param ?string $location * @param int|null $boxId * * @return int */ public function locationFreed(?string $location, ?int $boxId=null):int { if (!$location)return 0; $update = ["status"=>0,"material_box_id"=>$boxId]; return Station::query()->where("code",$location)->update($update); } /** * 检查库位是否可用 * * @param mixed $station * * @return bool */ public function isAvailable($station):bool { if (!is_a($station,Model::class)) $station = Station::query()->where("code",$station)->first(); $s = StationTaskMaterialBox::query()->selectRaw("1")->where("station_id",$station->id)->whereNotIn("status",["完成","取消"])->first(); return $station && $station->status==0 && !$s; } }