| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace App\Services;
- use App\MaterialBox;
- use App\Station;
- use App\StationTaskMaterialBox;
- use App\Storage;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use App\Traits\ServiceAppAop;
- use Illuminate\Support\Facades\DB;
- class MaterialBoxService
- {
- use ServiceAppAop;
- protected $modelClass=MaterialBox::class;
- /**
- * 获取一个空料箱
- *
- * @param array $blacklist
- * @param integer|null $modelId
- *
- * @return MaterialBox|null
- */
- public function getAnEmptyBox(array $blacklist = [], $modelId = null)
- {
- $id = 0;
- while (true){
- //检测WAS
- $boxes = MaterialBox::query()->select('id',"code")
- ->whereNotIn("id",$blacklist)
- ->where("id",">",$id)->where("code","like","IDE%")
- ->where("status",4)->limit(50)->orderBy("id")
- ->whereNotIn("id",StationTaskMaterialBox::query()->select("material_box_id")
- ->where("status","!=","完成")->groupBy("material_box_id"));
- if ($modelId)$boxes->where("material_box_model_id",$modelId);
- $boxes = $boxes->get();
- if ($boxes->count()==0)break;
- //检测海柔的有效料箱
- $haiBoxes = DB::connection("mysql_haiRobotics")->table("ks_bin")
- ->where("status",1)->whereIn("ks_bin_code",array_column($boxes->toArray(),"code"))->get();
- if ($haiBoxes->count()==0)continue;
- $haiBoxes->each(function ($haiBox)use(&$codes){$codes[$haiBox->ks_bin_code] = true;});
- foreach ($boxes as $index=>$box)if (!isset($codes[$box->code]))$boxes->splice($index,1);
- //剔除有任务待处理的料箱
- $haiBoxes = DB::connection("mysql_haiRobotics_ess")->table("ks_ess_task_detail")
- ->whereIn("bin_code",array_column($boxes->toArray(),"code"))
- ->whereNotIn("status",[0,4])->get();
- $haiBoxes->each(function ($haiBox)use(&$notCodes){$notCodes[$haiBox->bin_code] = true;});
- foreach ($boxes as $index=>$box)if (isset($notCodes[$box->code]))$boxes->splice($index,1);
- //检测FLUX
- $ides = [];
- $str = "(";
- for ($i=0;$i<count($boxes)-1;$i++){
- $str .= "'".$boxes[$i]->code."',";
- $ides[$boxes[$i]->code] = $boxes[$i];
- }
- $box = $boxes[count($boxes)-1];
- $ides[$box->code] = $box;
- $str .= "'".$box->code."')";
- $id = $box->id;
- $sql = <<<sql
- SELECT LOCATIONID,SUM(QTY+QTYPA) qty FROM INV_LOT_LOC_ID WHERE LOCATIONID IN {$str} GROUP BY LOCATIONID
- sql;
- foreach (DB::connection("oracle")->select(DB::raw($sql)) as $item){
- if ((int)$item->qty==0)return $ides[$item->locationid];
- unset($ides[$item->locationid]);
- }
- if ($ides)return current($ides);
- }
- return null;
- }
- /**
- * 获取料箱所在库区
- *
- * @param string $ide
- *
- * @return string|null
- */
- public function getBoxLocation(string $ide):?string
- {
- $storage = Storage::query()->with("station")->whereHas("materialBox",function (Builder $box)use($ide){
- $box->where("code",$ide);
- })->whereNotNull("station_id")->first();
- return $storage->station->code ?? null;
- }
- /**
- * 检查可用料箱
- *
- * @param integer $boxId
- *
- * @return bool
- */
- public function checkUsableBox(int $boxId):bool
- {
- /** @var MaterialBox|\stdClass $box */
- //$box = MaterialBox::query()->find($boxId);
- //if (!$this->getBoxLocation($box->code))return false;
- if (StationTaskMaterialBox::query()->select(DB::raw(1))->whereNotIn("status",['完成','取消'])->where("material_box_id",$boxId)->first())return false;
- return true;
- }
- }
|