MaterialBoxService.php 4.3 KB

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