MaterialBoxService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Services;
  3. use App\MaterialBox;
  4. use App\MaterialBoxModel;
  5. use App\StationTaskMaterialBox;
  6. use App\Traits\ServiceAppAop;
  7. use Illuminate\Support\Facades\DB;
  8. class MaterialBoxService
  9. {
  10. use ServiceAppAop;
  11. protected $modelClass=MaterialBox::class;
  12. /**
  13. * 获取一个空料箱
  14. *
  15. * @param MaterialBoxModel|\stdClass $model
  16. * @param array $blacklist
  17. *
  18. * @return MaterialBox|null
  19. */
  20. public function getAnEmptyBox(MaterialBoxModel $model,array $blacklist = []):?MaterialBox
  21. {
  22. $id = 0;
  23. while (true){
  24. //检测WAS
  25. $boxes = MaterialBox::query()->select('id',"code")
  26. ->whereNotIn("id",$blacklist)
  27. ->where("id",">",$id)->where("code","like","IDE%")
  28. ->where("status",4)->limit(50)->orderBy("id")
  29. ->whereNotIn("id",StationTaskMaterialBox::query()->select("material_box_id")
  30. ->whereNotIn("status",["完成","取消"])
  31. ->where("material_box_model_id",$model->id)->groupBy("material_box_id"));
  32. $boxes = $boxes->get();
  33. if ($boxes->count()==0)break;
  34. $id = $boxes[count($boxes)-1]->id;
  35. $boxCodes = array_column($boxes->toArray(),"code");
  36. //检测海柔的有效料箱
  37. $haiBoxes = DB::connection("mysql_haiRobotics")->table("ks_bin")
  38. ->where("status",1)->whereIn("ks_bin_code",$boxCodes)->get();
  39. if ($haiBoxes->count()==0)continue;
  40. $haiBoxes->each(function ($haiBox)use(&$codes){$codes[$haiBox->ks_bin_code] = true;});
  41. //剔除有任务待处理的料箱
  42. $haiBoxes = DB::connection("mysql_haiRobotics_ess")->table("ks_ess_task_detail")
  43. ->whereIn("bin_code",$boxCodes)
  44. ->whereNotIn("status",[0,4])->get();
  45. $haiBoxes->each(function ($haiBox)use(&$notCodes){$notCodes[$haiBox->bin_code] = true;});
  46. //剔除FLUX已有库存并且库位内种类超出的料箱
  47. $ides = [];
  48. $str = "(";
  49. foreach ($boxes as $box){
  50. if (!isset($codes[$box->code]) || isset($notCodes[$box->code]))continue;
  51. $str .= "'".$box->code."',";
  52. $ides[$box->code] = $box;
  53. }
  54. if (!$ides)continue;
  55. $str = rtrim($str,",").")";
  56. $sql = <<<sql
  57. SELECT LOCATIONID FROM (SELECT LOCATIONID FROM (SELECT LOCATIONID,SUM(QTY+QTYPA) qty FROM INV_LOT_LOC_ID WHERE LOCATIONID IN {$str} GROUP BY LOCATIONID,LOTNUM,CUSTOMERID,SKU) where qty>0) GROUP BY
  58. LOCATIONID HAVING (COUNT(*)>={$model->maximum_kind})
  59. sql;
  60. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $item)unset($ides[$item->locationid]);
  61. if ($ides)return current($ides);
  62. }
  63. return null;
  64. }
  65. /**
  66. * 获取所有者排序的空盒子
  67. *
  68. * @param integer|null $ownerId
  69. * @param array $blacklist
  70. *
  71. * @return MaterialBox|null
  72. */
  73. public function getAnEmptyBoxSortedByOwner(?int $ownerId=null, array $blacklist=[]):?MaterialBox
  74. {
  75. $models = app("MaterialBoxModelService")->getModelSortedByOwner($ownerId);
  76. foreach ($models as $model){
  77. $box = $this->getAnEmptyBox($model,$blacklist);
  78. if ($box)return $box;
  79. }
  80. return 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. if (StationTaskMaterialBox::query()->select(DB::raw(1))->whereNotIn("status",['完成','取消'])->where("material_box_id",$boxId)->first())return false;
  93. return true;
  94. }
  95. }