MaterialBoxService.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. *
  20. * @return MaterialBox|null
  21. */
  22. public function getAnEmptyBox(array $blacklist = [])
  23. {
  24. $id = 0;
  25. while (true){
  26. $boxes = MaterialBox::query()->select('id',"code")
  27. ->whereNotIn("id",$blacklist)
  28. ->where("id",">",$id)->where("code","like","IDE%")
  29. ->where("status",4)->limit(10)->orderBy("id")
  30. ->whereNotIn("id",StationTaskMaterialBox::query()->select("material_box_id")
  31. ->where("status","!=","已完成")->groupBy("material_box_id"))
  32. ->get();
  33. if ($boxes->count()==0)break;
  34. $haiBoxes = DB::connection("mysql_haiRobotics")->table("ks_bin")
  35. ->where("status",1)->whereIn("ks_bin_code",array_column($boxes->toArray(),"code"))->get();
  36. if ($haiBoxes->count()==0)continue;
  37. $haiBoxes->each(function ($haiBox)use(&$codes){$codes[$haiBox->ks_bin_code] = true;});
  38. foreach ($boxes as $index=>$box)if (!isset($codes[$box->code]))$boxes->splice($index,1);
  39. $ides = [];
  40. $str = "(";
  41. for ($i=0;$i<count($boxes)-1;$i++){
  42. $str .= "'".$boxes[$i]->code."',";
  43. $ides[$boxes[$i]->code] = $boxes[$i];
  44. }
  45. $box = $boxes[count($boxes)-1];
  46. $ides[$box->code] = $box;
  47. $str .= "'".$box->code."')";
  48. $id = $box->id;
  49. $sql = <<<sql
  50. SELECT LOCATIONID,SUM(QTY+QTYPA) qty FROM INV_LOT_LOC_ID WHERE LOCATIONID IN {$str} GROUP BY LOCATIONID
  51. sql;
  52. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $item){
  53. if ((int)$item->qty==0)return $ides[$item->locationid];
  54. unset($ides[$item->locationid]);
  55. }
  56. if ($ides)return current($ides);
  57. }
  58. return null;
  59. }
  60. /**
  61. * 获取料箱所在库区
  62. *
  63. * @param string $ide
  64. *
  65. * @return string|null
  66. */
  67. public function getBoxLocation(string $ide)
  68. {
  69. $bin = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_space_code")
  70. ->where("ks_bin_code",$ide)->where("status",1)->first();
  71. return $bin->ks_bin_space_code ?? null;
  72. }
  73. }