| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Services;
- use App\MaterialBox;
- use App\Station;
- use App\StationTaskMaterialBox;
- 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;
- /**
- * 获取一个空料箱
- *
- * @return MaterialBox|null
- */
- public function getAnEmptyBox()
- {
- $id = 0;
- while (true){
- $boxes = MaterialBox::query()->select('id',"code")->where("id",">",$id)->where("code","like","IDE%")
- ->where("status",4)->limit(10)->orderBy("id")
- ->whereNotIn("id",StationTaskMaterialBox::query()->select("material_box_id")
- ->where("status","!=","已完成")->groupBy("material_box_id"))
- ->get();
- if ($boxes->count()==0)break;
- $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)
- {
- $bin = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_space_code")
- ->where("ks_bin_code",$ide)->where("status",1)->first();
- return $bin->ks_bin_space_code ?? null;
- }
- }
|