all() as $k=>$v){ $request[strtolower($k)]=$v; } $reqDate=isset($request['time'])?$request['time']:Carbon::now(); $errors=$this->validatorWeight($request)->errors(); if (count($errors)>0){ $this->log(__METHOD__,'error'.__FUNCTION__,json_encode($request).'||'.json_encode($errors),null); $response=["msg"=>$errors,"code"=>500,"data"=>null]; return json_encode($response); } $measuringMachine=MeasuringMachine::where('code',$request['id'])->first(); if (!$measuringMachine){ $measuringMachine=new MeasuringMachine([ 'name'=>$request['id'], 'code'=>$request['id'], 'status'=>'在线' ]); $measuringMachine->save(); $this->log(__METHOD__,'weightApi(new measuring machine)'.__FUNCTION__,json_encode($request),null); }else{ $measuringMachineStatus=new MeasuringMachine(); $measuringMachineStatus->changeStatus($measuringMachine); } $measuringMachine->touch(); MeasuringMachineQueue::dispatch($measuringMachine)->delay(\Carbon\Carbon::now()->addMinutes(30)); $package=Package::where('logistic_number',$request['barcode'])->first(); if (isset($request['length'])&&isset($request['width'])&&isset($request['height'])){ $length=$request['length']; $width=$request['width']; $height=$request['height']; $max=($length>=($width>=$height?$width:$height)?$length:($width>=$height?$width:$height)); if ($max==$length){ $centre=$width>=$height?$width:$height; $min=$width<$height?$width:$height; }elseif ($max==$width){ $centre=$length>=$height?$length:$height; $min=$length<$height?$length:$height; }else{ $centre=$width>=$length?$width:$length; $min=$width<$length?$width:$length; } }else{ $max=0;$centre=0;$min=0; } if ($package){ $packagePaperBox=new Package(); if ($package->owner_id){ $paperBox_id=$packagePaperBox->checkPaperBox($max,$centre,$min,$package->owner_id); if (!$paperBox_id)$this->log(__METHOD__,'weightApi(no paper box)'.__FUNCTION__,json_encode($request),null); }else{ $this->log(__METHOD__,'weightApi(no owner)'.__FUNCTION__,json_encode($request),null); } $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController(); //处理活动波次 if ($package->batch_rule&&strstr($package->batch_rule,'组合')){ $packagesBatch=Package::where('batch_number',$package->batch_number)->get(); foreach ($packagesBatch as $packageBatch){ $packageBatch->measuring_machine_id=$measuringMachine->id; $packageBatch->weight=$request['weight']; $packageBatch->length=$max; $packageBatch->width=$centre; $packageBatch->height=$min; $packageBatch->bulk=$max*$centre*$min; $packageBatch->weighed_at=$reqDate; if (isset($paperBox_id))$packageBatch->paper_box_id=$paperBox_id; $packageBatch->status="未上传"; $packageBatch->save(); $result=$accomplishToWMS->accomplishToWMS($packageBatch); if ($result['result']=='success'){ if ($package->status=="记录异常")$package->status="已上传异常"; else $package->status="已上传"; }else{ $packageBatch->status="上传异常"; } $packageBatch->save(); } }else{ $package->measuring_machine_id=$measuringMachine->id; $package->weight=$request['weight']; $package->length=$max; $package->width=$centre; $package->height=$min; $package->bulk=$max*$centre*$min; $package->weighed_at=$reqDate; if (isset($paperBox_id))$package->paper_box_id=$paperBox_id; $package->status="未上传"; $this->log(__METHOD__,'Batch_'.__FUNCTION__,json_encode($package),null); $package->save(); $result=$accomplishToWMS->accomplishToWMS($package); if ($result['result']=='success'){ if ($package->status=="记录异常")$package->status="已上传异常"; else $package->status="已上传"; }else{ $package->status="上传异常"; } $package->save(); } event(new WeightEvent($package)); $response=["msg"=>"保存成功", "code"=>200, "data"=>true, "serverMsg"=>null, "requestor"=>[ "requestor"=>"1", "eventCode"=>"0", "reqDate"=>$reqDate, "resDate"=>Carbon::now()] ]; $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request).'|'.json_encode($response),null); return json_encode($response,JSON_UNESCAPED_UNICODE); } if (!$package){ $logisticNumberFeature=new LogisticNumberFeatureController(); $logistic=$logisticNumberFeature->getLogisticByFeatures($request['barcode']); $createPackage=new Package([ 'logistic_number'=>$request['barcode'], 'measuring_machine_id'=>$measuringMachine->id, 'weight'=>$request['weight'], 'length'=>$max, 'width'=>$centre, 'height'=>$min, 'bulk'=>$max*$centre*$min, 'weighed_at'=>$reqDate, 'status'=>"未下发", ]); if ($logistic)$createPackage->logistic_id=$logistic->id; if ($createPackage->save()){ $measuringMachine->touch(); MeasuringMachineQueue::dispatch($measuringMachine)->delay(\Carbon\Carbon::now()->addMinutes(30)); WeightQueue::dispatch($createPackage)->delay(Carbon::now()->addMinutes(1440)); $response=["msg"=>"保存成功", "code"=>200, "data"=>true, "serverMsg"=>null, "requestor"=>[ "requestor"=>"1", "eventCode"=>"0", "reqDate"=>$reqDate, "resDate"=>Carbon::now()] ]; $this->log(__METHOD__,'weightApi'.__FUNCTION__,json_encode($request).'||'.json_encode($response),null); return json_encode($response,JSON_UNESCAPED_UNICODE); } $response=["msg"=>"保存时发生错误(未下发)!","code"=>500,"data"=>null]; $this->log(__METHOD__,'weightApi(ERROR)'.__FUNCTION__,json_encode($request).'||'.json_encode($response),null); return json_encode($response,JSON_UNESCAPED_UNICODE); } } public function validatorWeight(array $request){ $validator=Validator::make($request,[ 'id'=>['required','max:30',/*function ($attribute, $value, $fail) { $measuringMachine=MeasuringMachine::where('code',$value)->first(); if (!$measuringMachine) { $fail($attribute.' 设备未录入在系统中!'); }}*/], 'barcode'=>['required','max:191'], 'weight'=>['required','numeric','min:0'], 'length'=>['nullable','numeric','min:0'], 'width'=>['nullable','numeric','min:0'], 'height'=>['nullable','numeric','min:0'], ],[ 'required'=>':attribute 为必填项', 'max'=>':attribute 字符过多或数值过大', 'min'=>':attribute 不得为负', 'numeric'=>':attribute 应为数字', ],[]); return $validator; } }