MaterialBoxService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Collection;
  8. use Illuminate\Support\Facades\DB;
  9. class MaterialBoxService
  10. {
  11. use ServiceAppAop;
  12. protected $modelClass=MaterialBox::class;
  13. /**
  14. * 获取一个空料箱
  15. *
  16. * @param MaterialBoxModel|\stdClass $model
  17. * @param array $blacklist
  18. *
  19. * @return MaterialBox|null
  20. */
  21. public function getAnEmptyBox(MaterialBoxModel $model,array $blacklist = []):?MaterialBox
  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. ->whereNotIn("status",["完成","取消"])
  32. ->where("material_box_model_id",$model->id)->groupBy("material_box_id"));
  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 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
  59. LOCATIONID HAVING (COUNT(*)>={$model->maximum_kind})
  60. sql;
  61. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $item)unset($ides[$item->locationid]);
  62. if ($ides)return current($ides);
  63. }
  64. return null;
  65. }
  66. /**
  67. * 获取所有者排序的空盒子
  68. *
  69. * @param integer|null $ownerId
  70. * @param array $blacklist
  71. *
  72. * @return MaterialBox|null
  73. */
  74. public function getAnEmptyBoxSortedByOwner(?int $ownerId=null, array $blacklist=[]):?MaterialBox
  75. {
  76. $models = app("MaterialBoxModelService")->getModelSortedByOwner($ownerId);
  77. foreach ($models as $model){
  78. $box = $this->getAnEmptyBox($model,$blacklist);
  79. if ($box)return $box;
  80. }
  81. return null;
  82. }
  83. /**
  84. * 检查可用料箱
  85. *
  86. * @param integer $boxId
  87. *
  88. * @return bool
  89. */
  90. public function checkUsableBox(int $boxId):bool
  91. {
  92. /** @var MaterialBox|\stdClass $box */
  93. if (StationTaskMaterialBox::query()->select(DB::raw(1))->whereNotIn("status",['完成','取消'])->where("material_box_id",$boxId)->first())return false;
  94. return true;
  95. }
  96. /**
  97. * 获取某个型号下的可用料箱
  98. *
  99. * @param integer $modelId
  100. *
  101. * @return Collection|string|null
  102. */
  103. public function getModelAvailableBox(int $modelId, bool $sqlQuery = false, array $blacklist = [])
  104. {
  105. $query = MaterialBox::query()->where("material_box_model_id",$modelId)->where("status",4);
  106. $boxes = $query->whereNotIn("id",$query->whereHas("performTask"))
  107. ->whereNotIn("id",$blacklist)->get();
  108. if (!$sqlQuery)return $boxes;
  109. if ($boxes->count()==0)return null;
  110. $boxCodes = "";
  111. foreach ($boxes as $box){
  112. $boxCodes .= "'".$box->code."',";
  113. }
  114. return rtrim($boxCodes,",");
  115. }
  116. }