PackageController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $packageBatch->weighed_at=$reqDate;
  81. if (isset($paperBox_id))$packageBatch->paper_box_id=$paperBox_id;
  82. $packageBatch->status="未上传";
  83. $packageBatch->save();
  84. $result=$accomplishToWMS->accomplishToWMS($packageBatch);
  85. if ($result['result']=='success'){
  86. if ($package->status=="记录异常")$package->status="已上传异常";
  87. else $package->status="已上传";
  88. }else{
  89. $packageBatch->status="上传异常";
  90. }
  91. $packageBatch->save();
  92. }
  93. }else{
  94. $package->measuring_machine_id=$measuringMachine->id;
  95. $package->weight=$request['weight'];
  96. $package->length=$max;
  97. $package->width=$centre;
  98. $package->height=$min;
  99. $package->bulk=$max*$centre*$min;
  100. $package->weighed_at=$reqDate;
  101. if (isset($paperBox_id))$package->paper_box_id=$paperBox_id;
  102. $package->status="未上传";
  103. $this->log(__METHOD__,'Batch_'.__FUNCTION__,json_encode($package),null);
  104. $package->save();
  105. $result=$accomplishToWMS->accomplishToWMS($package);
  106. if ($result['result']=='success'){
  107. if ($package->status=="记录异常")$package->status="已上传异常";
  108. else $package->status="已上传";
  109. }else{
  110. $package->status="上传异常";
  111. }
  112. $package->save();
  113. }
  114. event(new WeightEvent($package));
  115. $response=["msg"=>"保存成功",
  116. "code"=>200,
  117. "data"=>true,
  118. "serverMsg"=>null,
  119. "requestor"=>[
  120. "requestor"=>"1",
  121. "eventCode"=>"0",
  122. "reqDate"=>$reqDate,
  123. "resDate"=>Carbon::now()]
  124. ];
  125. $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request).'|'.json_encode($response),null);
  126. return json_encode($response,JSON_UNESCAPED_UNICODE);
  127. }
  128. if (!$package){
  129. $logisticNumberFeature=new LogisticNumberFeatureController();
  130. $logistic=$logisticNumberFeature->getLogisticByFeatures($request['barcode']);
  131. $createPackage=new Package([
  132. 'logistic_number'=>$request['barcode'],
  133. 'measuring_machine_id'=>$measuringMachine->id,
  134. 'weight'=>$request['weight'],
  135. 'length'=>$max,
  136. 'width'=>$centre,
  137. 'height'=>$min,
  138. 'bulk'=>$max*$centre*$min,
  139. 'weighed_at'=>$reqDate,
  140. 'status'=>"未下发",
  141. ]);
  142. if ($logistic)$createPackage->logistic_id=$logistic->id;
  143. if ($createPackage->save()){
  144. $measuringMachine->touch();
  145. MeasuringMachineQueue::dispatch($measuringMachine)->delay(\Carbon\Carbon::now()->addMinutes(30));
  146. WeightQueue::dispatch($createPackage)->delay(Carbon::now()->addMinutes(1440));
  147. $response=["msg"=>"保存成功",
  148. "code"=>200,
  149. "data"=>true,
  150. "serverMsg"=>null,
  151. "requestor"=>[
  152. "requestor"=>"1",
  153. "eventCode"=>"0",
  154. "reqDate"=>$reqDate,
  155. "resDate"=>Carbon::now()]
  156. ];
  157. $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request).'||'.json_encode($response),null);
  158. return json_encode($response,JSON_UNESCAPED_UNICODE);
  159. }
  160. $response=["msg"=>"保存时发生错误(未下发)!","code"=>500,"data"=>null];
  161. $this->log(__METHOD__,'weightApi(ERROR)'.__FUNCTION__,json_encode($request).'||'.json_encode($response),null);
  162. return json_encode($response,JSON_UNESCAPED_UNICODE);
  163. }
  164. }
  165. public function validatorWeight(array $request){
  166. $validator=Validator::make($request,[
  167. 'id'=>['required','max:30',/*function ($attribute, $value, $fail) {
  168. $measuringMachine=MeasuringMachine::where('code',$value)->first();
  169. if (!$measuringMachine) {
  170. $fail($attribute.' 设备未录入在系统中!');
  171. }}*/],
  172. 'barcode'=>['required','max:191'],
  173. 'weight'=>['required','numeric','min:0'],
  174. 'length'=>['nullable','numeric','min:0'],
  175. 'width'=>['nullable','numeric','min:0'],
  176. 'height'=>['nullable','numeric','min:0'],
  177. ],[
  178. 'required'=>':attribute 为必填项',
  179. 'max'=>':attribute 字符过多或数值过大',
  180. 'min'=>':attribute 不得为负',
  181. 'numeric'=>':attribute 应为数字',
  182. ],[]);
  183. return $validator;
  184. }
  185. }