MaterialBoxService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Services;
  3. use App\MaterialBox;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Facades\Cache;
  6. use App\Traits\ServiceAppAop;
  7. use Illuminate\Support\Facades\DB;
  8. class MaterialBoxService
  9. {
  10. use ServiceAppAop;
  11. protected $modelClass=MaterialBox::class;
  12. /**
  13. * 获取一个空料箱
  14. *
  15. * @return MaterialBox|null
  16. */
  17. public function getAnEmptyBox()
  18. {
  19. $id = 0;
  20. while (true){
  21. $boxes = MaterialBox::query()->select('id',"code")->where("id",">",$id)->where("code","like","IDE%")
  22. ->where("status",4)->limit(10)->orderBy("id")->get();
  23. if ($boxes->count()==0)break;
  24. $ides = [];
  25. $str = "(";
  26. for ($i=0;$i<count($boxes)-1;$i++){
  27. $str .= "'".$boxes[$i]->code."',";
  28. $ides[$boxes[$i]->code] = $boxes[$i];
  29. }
  30. $box = $boxes[count($boxes)-1];
  31. $ides[$box->code] = $box;
  32. $str .= "'".$box->code."')";
  33. $id = $box->id;
  34. $sql = <<<sql
  35. SELECT LOCATIONID,SUM(QTY+QTYPA) qty FROM INV_LOT_LOC_ID WHERE LOCATIONID IN {$str} GROUP BY LOCATIONID
  36. sql;
  37. foreach (DB::connection("oracle")->select(DB::raw($sql)) as $item){
  38. if ((int)$item->qty==0)return $ides[$item->locationid];
  39. unset($ides[$item->locationid]);
  40. }
  41. if ($ides)return current($ides);
  42. }
  43. return null;
  44. }
  45. }