PackageController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\Api\thirdPart\weight;
  3. use App\Events\WeightEvent;
  4. use App\Http\Controllers\Controller;
  5. use App\Jobs\WeightQueue;
  6. use App\MeasuringMachine;
  7. use App\Owner;
  8. use App\Package;
  9. use App\PaperBox;
  10. use function foo\func;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Validator;
  15. class PackageController extends Controller
  16. {
  17. public function new_(Request $request){
  18. $reqDate=Carbon::now();
  19. $errors=$this->validatorWeight($request)->errors();
  20. if (count($errors)>0){
  21. $response=["msg"=>$errors,"code"=>500,"data"=>null];
  22. return json_encode($response);
  23. }
  24. $measuringMachine=MeasuringMachine::where('code',$request->id)->first();
  25. if (!$measuringMachine){
  26. $this->log(__METHOD__,'weightApi(no measuring machine)'.__FUNCTION__,json_encode($request->all()),null);
  27. $response=["msg"=>"设备未录入在系统中","code"=>500,"data"=>null];
  28. return json_encode($response,JSON_UNESCAPED_UNICODE);
  29. }
  30. $package=Package::where('logistic_number',$request->barcode)->first();
  31. $length=$request->length;
  32. $width=$request->width;
  33. $height=$request->height;
  34. $max=($length>=($width>=$height?$width:$height)?$length:($width>=$height?$width:$height));
  35. if ($max==$length){
  36. $centre=$width>=$height?$width:$height;
  37. $min=$width<$height?$width:$height;
  38. }elseif ($max==$width){
  39. $centre=$length>=$height?$length:$height;
  40. $min=$length<$height?$length:$height;
  41. }else{
  42. $centre=$width>=$length?$width:$length;
  43. $min=$width<$length?$width:$length;
  44. }
  45. if ($package){
  46. $package->measuring_machine_id=$measuringMachine->id;
  47. $package->weight=$request->weight;
  48. $package->length=$max;
  49. $package->width=$centre;
  50. $package->height=$min;
  51. $package->bulk=$max*$centre*$min;
  52. $package->status="未上传";
  53. $packagePaperBox=new Package();
  54. $paperBox_id=$packagePaperBox->checkPaperBox($max,$centre,$min,$package->owner_id);
  55. if (!$paperBox_id){
  56. $this->log(__METHOD__,'weightApi(no paper box)'.__FUNCTION__,json_encode($request->all()),null);
  57. $response=["msg"=>"找不到相近纸箱!","code"=>500,"data"=>null];
  58. return json_encode($response,JSON_UNESCAPED_UNICODE);
  59. }
  60. $package->paper_box_id=$paperBox_id;
  61. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  62. $result=$accomplishToWMS->accomplishToWMS($package);
  63. if ($result['result']=='success'){
  64. $package->status="已上传";
  65. }else{
  66. $package->status="上传异常";
  67. }
  68. if ($package->save()){
  69. event(new WeightEvent($package));
  70. $response=["msg"=>"保存成功",
  71. "code"=>200,
  72. "data"=>true,
  73. "serverMsg"=>null,
  74. "requestor"=>[
  75. "requestor"=>"1",
  76. "eventCode"=>"0",
  77. "reqDate"=>$reqDate,
  78. "resDate"=>Carbon::now()]
  79. ];
  80. $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request->all().$response),null);
  81. return json_encode($response,JSON_UNESCAPED_UNICODE);
  82. }
  83. $response=["msg"=>"保存时发生错误(未上传)!","code"=>500,"data"=>null];
  84. return json_encode($response,JSON_UNESCAPED_UNICODE);
  85. /* $paper_box_id=DB::table('owner_paper_box')->select('paper_box_id')->where('owner_id',1);
  86. $paper_box=PaperBox::select('id')->whereIn('id',$paper_box_id)->orderBy("asb(($max*$centre*$min)-(length*width*height))");*/
  87. }
  88. if (!$package){
  89. $createPackage=new Package([
  90. 'logistic_number'=>$request->barcode,
  91. 'measuring_machine_id'=>$measuringMachine->id,
  92. 'weight'=>$request->weight,
  93. 'length'=>$max,
  94. 'width'=>$centre,
  95. 'height'=>$min,
  96. 'bulk'=>$max*$centre*$min,
  97. 'status'=>"未下发",
  98. ]);
  99. if ($createPackage->save()){
  100. WeightQueue::dispatch($createPackage)->delay(Carbon::now()->addMinutes(1440));
  101. $response=["msg"=>"保存成功",
  102. "code"=>200,
  103. "data"=>true,
  104. "serverMsg"=>null,
  105. "requestor"=>[
  106. "requestor"=>"1",
  107. "eventCode"=>"0",
  108. "reqDate"=>$reqDate,
  109. "resDate"=>Carbon::now()]
  110. ];
  111. $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request->all().$response),null);
  112. return json_encode($response,JSON_UNESCAPED_UNICODE);
  113. }
  114. $this->log(__METHOD__,'weightApi(ERROR)'.__FUNCTION__,json_encode($request->all()),null);
  115. $response=["msg"=>"保存时发生错误(未下发)!","code"=>500,"data"=>null];
  116. return json_encode($response,JSON_UNESCAPED_UNICODE);
  117. }
  118. }
  119. public function validatorWeight(Request $request){
  120. $validator=Validator::make($request->input(),[
  121. 'id'=>['required','max:30',/*function ($attribute, $value, $fail) {
  122. $measuringMachine=MeasuringMachine::where('code',$value)->first();
  123. if (!$measuringMachine) {
  124. $fail($attribute.' 设备未录入在系统中!');
  125. }}*/],
  126. 'barcode'=>['required','max:191'],
  127. 'weight'=>['required','numeric','min:0'],
  128. 'length'=>['required','numeric','min:0'],
  129. 'width'=>['required','numeric','min:0'],
  130. 'height'=>['required','numeric','min:0'],
  131. ],[
  132. 'required'=>':attribute 为必填项',
  133. 'max'=>':attribute 字符过多或数值过大',
  134. 'min'=>':attribute 不得为负',
  135. 'numeric'=>':attribute 应为数字',
  136. ],[]);
  137. return $validator;
  138. }
  139. }