MaterialBoxService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Services;
  3. use App\MaterialBox;
  4. use App\Station;
  5. use App\StationTaskMaterialBox;
  6. use App\Storage;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use App\Traits\ServiceAppAop;
  11. use Illuminate\Support\Facades\DB;
  12. class MaterialBoxService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=MaterialBox::class;
  16. /**
  17. * 获取一个空料箱
  18. *
  19. * @param array $blacklist
  20. * @param integer|null $modelId
  21. *
  22. * @return MaterialBox|null
  23. */
  24. public function getAnEmptyBox(array $blacklist = [], $modelId = null)
  25. {
  26. $id = 0;
  27. while (true){
  28. //检测WAS
  29. $boxes = MaterialBox::query()->select('id',"code")
  30. ->whereNotIn("id",$blacklist)
  31. ->where("id",">",$id)->where("code","like","IDE%")
  32. ->where("status",4)->limit(50)->orderBy("id")
  33. ->whereNotIn("id",StationTaskMaterialBox::query()->select("material_box_id")
  34. ->where("status","!=","完成")->groupBy("material_box_id"));
  35. if ($modelId)$boxes->where("material_box_model_id",$modelId);
  36. $boxes = $boxes->get();
  37. if ($boxes->count()==0)break;
  38. //检测海柔的有效料箱
  39. $haiBoxes = DB::connection("mysql_haiRobotics")->table("ks_bin")
  40. ->where("status",1)->whereIn("ks_bin_code",array_column($boxes->toArray(),"code"))->get();
  41. if ($haiBoxes->count()==0)continue;
  42. $haiBoxes->each(function ($haiBox)use(&$codes){$codes[$haiBox->ks_bin_code] = true;});
  43. foreach ($boxes as $index=>$box)if (!isset($codes[$box->code]))$boxes->splice($index,1);
  44. //剔除有任务待处理的料箱
  45. $haiBoxes = DB::connection("mysql_haiRobotics_ess")->table("ks_ess_task_detail")
  46. ->whereIn("bin_code",array_column($boxes->toArray(),"code"))
  47. ->whereNotIn("status",[0,4])->get();
  48. $haiBoxes->each(function ($haiBox)use(&$notCodes){$notCodes[$haiBox->bin_code] = true;});
  49. foreach ($boxes as $index=>$box)if (isset($notCodes[$box->code]))$boxes->splice($index,1);
  50. //检测FLUX
  51. $ides = [];
  52. $str = "(";
  53. for ($i=0;$i<count($boxes)-1;$i++){
  54. $str .= "'".$boxes[$i]->code."',";
  55. $ides[$boxes[$i]->code] = $boxes[$i];
  56. }
  57. $box = $boxes[count($boxes)-1];
  58. $ides[$box->code] = $box;
  59. $str .= "'".$box->code."')";
  60. $id = $box->id;
  61. $sql = <<<sql
  62. SELECT LOCATIONID,SUM(QTY+QTYPA) qty FROM INV_LOT_LOC_ID WHERE LOCATIONID IN {$str} GROUP BY LOCATIONID
  63. sql;
  64. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $item){
  65. if ((int)$item->qty==0)return $ides[$item->locationid];
  66. unset($ides[$item->locationid]);
  67. }
  68. if ($ides)return current($ides);
  69. }
  70. return null;
  71. }
  72. /**
  73. * 获取料箱所在库区
  74. *
  75. * @param string $ide
  76. *
  77. * @return string|null
  78. */
  79. public function getBoxLocation(string $ide):?string
  80. {
  81. $storage = Storage::query()->with("station")->whereHas("materialBox",function (Builder $box)use($ide){
  82. $box->where("code",$ide);
  83. })->whereNotNull("station_id")->first();
  84. return $storage->station->code ?? null;
  85. }
  86. /**
  87. * 检查可用料箱
  88. *
  89. * @param integer $boxId
  90. *
  91. * @return bool
  92. */
  93. public function checkUsableBox(int $boxId):bool
  94. {
  95. /** @var MaterialBox|\stdClass $box */
  96. //$box = MaterialBox::query()->find($boxId);
  97. //if (!$this->getBoxLocation($box->code))return false;
  98. if (StationTaskMaterialBox::query()->select(DB::raw(1))->whereNotIn("status",['完成','取消'])->where("material_box_id",$boxId)->first())return false;
  99. return true;
  100. }
  101. }