MaterialBoxService.php 3.6 KB

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