MaterialBoxService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. $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. ->where("status","!=","完成")->groupBy("material_box_id"));
  33. if ($modelId)$boxes->where("material_box_model_id",$modelId);
  34. $boxes = $boxes->get();
  35. if ($boxes->count()==0)break;
  36. $haiBoxes = DB::connection("mysql_haiRobotics")->table("ks_bin")
  37. ->where("status",1)->whereIn("ks_bin_code",array_column($boxes->toArray(),"code"))->get();
  38. if ($haiBoxes->count()==0)continue;
  39. $haiBoxes->each(function ($haiBox)use(&$codes){$codes[$haiBox->ks_bin_code] = true;});
  40. foreach ($boxes as $index=>$box)if (!isset($codes[$box->code]))$boxes->splice($index,1);
  41. $ides = [];
  42. $str = "(";
  43. for ($i=0;$i<count($boxes)-1;$i++){
  44. $str .= "'".$boxes[$i]->code."',";
  45. $ides[$boxes[$i]->code] = $boxes[$i];
  46. }
  47. $box = $boxes[count($boxes)-1];
  48. $ides[$box->code] = $box;
  49. $str .= "'".$box->code."')";
  50. $id = $box->id;
  51. $sql = <<<sql
  52. SELECT LOCATIONID,SUM(QTY+QTYPA) qty FROM INV_LOT_LOC_ID WHERE LOCATIONID IN {$str} GROUP BY LOCATIONID
  53. sql;
  54. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $item){
  55. if ((int)$item->qty==0)return $ides[$item->locationid];
  56. unset($ides[$item->locationid]);
  57. }
  58. if ($ides)return current($ides);
  59. }
  60. return null;
  61. }
  62. /**
  63. * 获取料箱所在库区
  64. *
  65. * @param string $ide
  66. *
  67. * @return string|null
  68. */
  69. public function getBoxLocation(string $ide)
  70. {
  71. $bin = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_space_code")
  72. ->where("ks_bin_code",$ide)->where("status",1)->first();
  73. return $bin->ks_bin_space_code ?? null;
  74. }
  75. /**
  76. * 检查可用料箱
  77. *
  78. * @param integer $boxId
  79. *
  80. * @return bool
  81. */
  82. public function checkUsableBox($boxId):bool
  83. {
  84. /** @var MaterialBox|\stdClass $box */
  85. $box = MaterialBox::query()->find($boxId);
  86. if (!$this->getBoxLocation($box->code))return false;
  87. if (StationTaskMaterialBox::query()->select(DB::raw(1))->where("status","!=",'完成')->where("material_box_id",$boxId)->first())return false;
  88. return true;
  89. }
  90. }