StationController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\Station;
  5. use App\StationTypeBinMonitor;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. class StationController extends Controller
  10. {
  11. use AsyncResponse;
  12. public function monitorIndex()
  13. {
  14. $stations = Station::query()->with('stationType:name','parent:name')->whereHas('stationType',function($query){
  15. /** @var Builder $query */
  16. $query->where('name','料箱监视器');
  17. })->paginate(100);
  18. return view('station.monitor.index',compact('stations'));
  19. }
  20. public function monitorShow(Station $station)
  21. {
  22. $station->loadMissing([
  23. "stationTasks_today.stationTaskCommodities.commodity.barcodes",
  24. "stationTasks_today.stationTaskCommodities.stationTaskMaterialBox",
  25. "stationTasks_today.stationTaskBatches.batch.owner",
  26. "stationTasks_today.stationTaskMaterialBoxes.materialBox",
  27. "stationTypeBinMonitor",
  28. ]);
  29. if (!$station['stationTypeBinMonitor']){
  30. StationTypeBinMonitor::query()->create([
  31. 'station_id' => $station['id'],
  32. 'bin_row_length' => 4,
  33. 'bin_column_length' => 5,
  34. 'bin_wall_amount' => 2,
  35. ]);
  36. $station->load("stationTypeBinMonitor");
  37. }
  38. return view('station.monitor.show',compact('station'));
  39. }
  40. public function getVisual()
  41. {
  42. $arr = \request("arr");
  43. $cacheKey = "loadBoxMonitor_".$arr[0][0];
  44. if (Cache::tags("loadBoxMonitor")->has($cacheKey))$this->success(Cache::tags("loadBoxMonitor")->get($cacheKey));
  45. $res = [];
  46. foreach ($arr as $item){
  47. $query = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_code");
  48. foreach ($item as $value)$query->orWhere("ks_bin_space_code",'like',$value);
  49. $codes = [];
  50. $query->get()->each(function ($bin)use(&$codes){
  51. $codes[] = $bin->ks_bin_code;
  52. });
  53. if (!$codes){$res[] = 100;continue;}
  54. $count = DB::connection("oracle")->table("INV_LOT_LOC_ID")->selectRaw("1")
  55. ->where("traceid","*")->whereRaw("SUM(qty) = 0")
  56. ->whereIn("locationid",$codes)->groupBy("locationid")->count();
  57. $res[] =number_format($count/count($codes),1)*100;
  58. }
  59. Cache::tags("loadBoxMonitor")->put($cacheKey,$res,1800);
  60. $this->success($res);
  61. }
  62. public function getDetail()
  63. {
  64. $arr = \request("arr");
  65. $cacheKey = "loadBoxMonitorDetail_".$arr[0];
  66. if (Cache::tags("loadBoxMonitor")->has($cacheKey))$this->success(Cache::tags("loadBoxMonitor")->get($cacheKey));
  67. $ks = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_code","ks_bin_space_code")
  68. ->whereIn("ks_bin_space_code",$arr)->get();
  69. $res = [];
  70. $codes = [];
  71. foreach ($ks as $k){
  72. $res[$k->ks_bin_code] = $k->ks_bin_space_code;
  73. $codes[] = $k->ks_bin_code;
  74. }
  75. $locations = [];
  76. DB::connection("oracle")->table("INV_LOT_LOC_ID")->selectRaw("locationid,SUM(qty) qty")
  77. ->where("traceid","*")
  78. ->whereIn("locationid",$codes)->groupBy("locationid")->get()->each(function ($inv)use(&$locations,&$res){
  79. $locations[] = [
  80. "location" => $res[$inv->locationid],
  81. "status" => $inv->qty>0,
  82. ];
  83. unset($res[$inv->locationid]);
  84. });
  85. if ($res)foreach ($res as $item)$locations[] = [
  86. "location" => $item,
  87. "status" => false,
  88. ];
  89. Cache::tags("loadBoxMonitor")->put($cacheKey,$locations,1800);
  90. $this->success($locations);
  91. }
  92. }