PackageController.php 8.3 KB

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