| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace App\Services;
- use App\Events\BroadcastToStation;
- use App\Station;
- use App\StationTask;
- use App\StationTaskMaterialBox;
- use App\StationType;
- use Exception;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Cache;
- use App\Traits\ServiceAppAop;
- use Illuminate\Support\Facades\DB;
- class StationService
- {
- use ServiceAppAop;
- protected $modelClass=Station::class;
- /** @var $stationTaskService StationTaskService */
- protected $stationTaskService;
- /**
- * @param string $typeName
- * @return Station
- * @throws Exception
- */
- function getStation_byType(string $typeName):Station{
- $station= Cache::remember('Station_typeName_'.$typeName,
- config('cache.expirations.rarelyChange'),
- function ()use($typeName) {
- $stationType= StationType::query()->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;
- }
- }
|