MaterialBoxService.php 2.1 KB

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