PackageController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Package;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Carbon;
  10. use Illuminate\Support\Facades\Validator;
  11. class PackageController extends Controller
  12. {
  13. public function new_(Request $requestInitial){
  14. $request=[];
  15. foreach ($requestInitial->all() as $k=>$v){
  16. $request[strtolower($k)]=$v;
  17. }
  18. $reqDate=isset($request['time'])?$request['time']:Carbon::now();
  19. $errors=$this->validatorWeight($request)->errors();
  20. if (count($errors)>0){
  21. $this->log(__METHOD__,'error'.__FUNCTION__,json_encode($request).'||'.json_encode($errors),null);
  22. $response=["msg"=>$errors,"code"=>500,"data"=>null];
  23. return json_encode($response);
  24. }
  25. $measuringMachine=MeasuringMachine::where('code',$request['id'])->first();
  26. if (!$measuringMachine){
  27. $measuringMachine=new MeasuringMachine([
  28. 'name'=>$request['id'],
  29. 'code'=>$request['id']
  30. ]);
  31. $measuringMachine->save();
  32. $this->log(__METHOD__,'weightApi(new measuring machine)'.__FUNCTION__,json_encode($request),null);
  33. }
  34. $package=Package::where('logistic_number',$request['barcode'])->first();
  35. if (isset($request['length'])&&isset($request['width'])&&isset($request['height'])){
  36. $length=$request['length'];
  37. $width=$request['width'];
  38. $height=$request['height'];
  39. $max=($length>=($width>=$height?$width:$height)?$length:($width>=$height?$width:$height));
  40. if ($max==$length){
  41. $centre=$width>=$height?$width:$height;
  42. $min=$width<$height?$width:$height;
  43. }elseif ($max==$width){
  44. $centre=$length>=$height?$length:$height;
  45. $min=$length<$height?$length:$height;
  46. }else{
  47. $centre=$width>=$length?$width:$length;
  48. $min=$width<$length?$width:$length;
  49. }
  50. }else{
  51. $max=0;$centre=0;$min=0;
  52. }
  53. if ($package){
  54. $packagePaperBox=new Package();
  55. if ($package->owner_id){
  56. $paperBox_id=$packagePaperBox->checkPaperBox($max,$centre,$min,$package->owner_id);
  57. if (!$paperBox_id)$this->log(__METHOD__,'weightApi(no paper box)'.__FUNCTION__,json_encode($request),null);
  58. }else{
  59. $this->log(__METHOD__,'weightApi(no owner)'.__FUNCTION__,json_encode($request),null);
  60. }
  61. $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
  62. //处理活动波次
  63. if ($package->batch_rule&&strstr($package->batch_rule,'活动')){
  64. $packagesBatch=Package::where('batch_number',$package->batch_number)->get();
  65. foreach ($packagesBatch as $packageBatch){
  66. $packageBatch->measuring_machine_id=$measuringMachine->id;
  67. $packageBatch->weight=$request['weight'];
  68. $packageBatch->length=$max;
  69. $packageBatch->width=$centre;
  70. $packageBatch->height=$min;
  71. $packageBatch->bulk=$max*$centre*$min;
  72. if (isset($paperBox_id))$packageBatch->paper_box_id=$paperBox_id;
  73. $packageBatch->status="未上传";
  74. $this->log(__METHOD__,'Batch_'.__FUNCTION__,json_encode($packageBatch),null);
  75. $packageBatch->save();
  76. $result=$accomplishToWMS->accomplishToWMS($packageBatch);
  77. if ($result['result']=='success'){
  78. $packageBatch->status="已上传";
  79. }else{
  80. $packageBatch->status="上传异常";
  81. }
  82. $packageBatch->save();
  83. }
  84. }else{
  85. $package->measuring_machine_id=$measuringMachine->id;
  86. $package->weight=$request['weight'];
  87. $package->length=$max;
  88. $package->width=$centre;
  89. $package->height=$min;
  90. $package->bulk=$max*$centre*$min;
  91. if (isset($paperBox_id))$package->paper_box_id=$paperBox_id;
  92. $package->status="未上传";
  93. $this->log(__METHOD__,'Batch_'.__FUNCTION__,json_encode($package),null);
  94. $package->save();
  95. $result=$accomplishToWMS->accomplishToWMS($package);
  96. if ($result['result']=='success'){
  97. $package->status="已上传";
  98. }else{
  99. $package->status="上传异常";
  100. }
  101. $package->save();
  102. }
  103. event(new WeightEvent($package));
  104. $response=["msg"=>"保存成功",
  105. "code"=>200,
  106. "data"=>true,
  107. "serverMsg"=>null,
  108. "requestor"=>[
  109. "requestor"=>"1",
  110. "eventCode"=>"0",
  111. "reqDate"=>$reqDate,
  112. "resDate"=>Carbon::now()]
  113. ];
  114. $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request).'|'.json_encode($response),null);
  115. return json_encode($response,JSON_UNESCAPED_UNICODE);
  116. }
  117. if (!$package){
  118. $createPackage=new Package([
  119. 'logistic_number'=>$request['barcode'],
  120. 'measuring_machine_id'=>$measuringMachine->id,
  121. 'weight'=>$request['weight'],
  122. 'length'=>$max,
  123. 'width'=>$centre,
  124. 'height'=>$min,
  125. 'bulk'=>$max*$centre*$min,
  126. 'status'=>"未下发",
  127. ]);
  128. if ($createPackage->save()){
  129. WeightQueue::dispatch($createPackage)->delay(Carbon::now()->addMinutes(1440));
  130. $response=["msg"=>"保存成功",
  131. "code"=>200,
  132. "data"=>true,
  133. "serverMsg"=>null,
  134. "requestor"=>[
  135. "requestor"=>"1",
  136. "eventCode"=>"0",
  137. "reqDate"=>$reqDate,
  138. "resDate"=>Carbon::now()]
  139. ];
  140. $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request).'||'.json_encode($response),null);
  141. return json_encode($response,JSON_UNESCAPED_UNICODE);
  142. }
  143. $response=["msg"=>"保存时发生错误(未下发)!","code"=>500,"data"=>null];
  144. $this->log(__METHOD__,'weightApi(ERROR)'.__FUNCTION__,json_encode($request).'||'.json_encode($response),null);
  145. return json_encode($response,JSON_UNESCAPED_UNICODE);
  146. }
  147. }
  148. public function validatorWeight(array $request){
  149. $validator=Validator::make($request,[
  150. 'id'=>['required','max:30',/*function ($attribute, $value, $fail) {
  151. $measuringMachine=MeasuringMachine::where('code',$value)->first();
  152. if (!$measuringMachine) {
  153. $fail($attribute.' 设备未录入在系统中!');
  154. }}*/],
  155. 'barcode'=>['required','max:191'],
  156. 'weight'=>['required','numeric','min:0'],
  157. 'length'=>['nullable','numeric','min:0'],
  158. 'width'=>['nullable','numeric','min:0'],
  159. 'height'=>['nullable','numeric','min:0'],
  160. ],[
  161. 'required'=>':attribute 为必填项',
  162. 'max'=>':attribute 字符过多或数值过大',
  163. 'min'=>':attribute 不得为负',
  164. 'numeric'=>':attribute 应为数字',
  165. ],[]);
  166. return $validator;
  167. }
  168. }