MaterialBoxService.php 3.8 KB

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