PackageController.php 8.7 KB

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