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); } MeasuringMachineQueue::dispatch($measuringMachine)->delay(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(); $paperBox_id=null; 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(); $packageController=new \App\Http\Controllers\PackageController(); //处理活动波次 if ($package->batch_rule&&strstr($package->batch_rule,'组合')){ $packageController->syncBatch($package->batch_number,$request['weight'],$max,$centre,$min,$reqDate,$paperBox_id); }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 ($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']){ if ($package->status=="记录异常")$package->status="已上传异常"; else $package->status="已上传"; }else{ $package->status="上传异常"; } $package->save(); } if ($package->order_code){ $waybill=Waybill::where('wms_bill_number',$package->order_code)->where('status','!=','已完结') ->where('status','!=','无模型')->first(); if ($waybill){ $waybill->warehouse_weight_other=$package->weight; $waybill->warehouse_weight_unit_id_other=1; $waybill->update(); } } event(new WeighedEvent($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::now()->addMinutes(30)); MarkPackageExcepted::dispatch($createPackage)->delay(Carbon::now()->addMinutes(1440)); event(new WeighedEvent($createPackage)); $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; } }