| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Http\Controllers\Api\thirdPart\weight;
- use App\Events\WeightEvent;
- use App\Http\Controllers\Controller;
- use App\Jobs\WeightQueue;
- use App\MeasuringMachine;
- use App\Owner;
- use App\Package;
- use App\PaperBox;
- use function foo\func;
- use Illuminate\Http\Request;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Validator;
- class PackageController extends Controller
- {
- public function new_(Request $request){
- $reqDate=Carbon::now();
- $errors=$this->validatorWeight($request)->errors();
- if (count($errors)>0){
- $response=["msg"=>$errors,"code"=>500,"data"=>null];
- return json_encode($response);
- }
- $measuringMachine=MeasuringMachine::where('code',$request->id)->first();
- if (!$measuringMachine){
- $this->log(__METHOD__,'weightApi(no measuring machine)'.__FUNCTION__,json_encode($request->all()),null);
- $response=["msg"=>"设备未录入在系统中","code"=>500,"data"=>null];
- return json_encode($response,JSON_UNESCAPED_UNICODE);
- }
- $package=Package::where('logistic_number',$request->barcode)->first();
- $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;
- }
- if ($package){
- $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->status="未上传";
- $packagePaperBox=new Package();
- $paperBox_id=$packagePaperBox->checkPaperBox($max,$centre,$min,$package->owner_id);
- if (!$paperBox_id){
- $this->log(__METHOD__,'weightApi(no paper box)'.__FUNCTION__,json_encode($request->all()),null);
- $response=["msg"=>"找不到相近纸箱!","code"=>500,"data"=>null];
- return json_encode($response,JSON_UNESCAPED_UNICODE);
- }
- $package->paper_box_id=$paperBox_id;
- $accomplishToWMS=new \App\Http\Controllers\Api\thirdPart\flux\PackageController();
- $result=$accomplishToWMS->accomplishToWMS($package);
- if ($result['result']=='success'){
- $package->status="已上传";
- }else{
- $package->status="上传异常";
- }
- if ($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->all().$response),null);
- return json_encode($response,JSON_UNESCAPED_UNICODE);
- }
- $response=["msg"=>"保存时发生错误(未上传)!","code"=>500,"data"=>null];
- return json_encode($response,JSON_UNESCAPED_UNICODE);
- /* $paper_box_id=DB::table('owner_paper_box')->select('paper_box_id')->where('owner_id',1);
- $paper_box=PaperBox::select('id')->whereIn('id',$paper_box_id)->orderBy("asb(($max*$centre*$min)-(length*width*height))");*/
- }
- if (!$package){
- $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,
- 'status'=>"未下发",
- ]);
- if ($createPackage->save()){
- 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->all().$response),null);
- return json_encode($response,JSON_UNESCAPED_UNICODE);
- }
- $this->log(__METHOD__,'weightApi(ERROR)'.__FUNCTION__,json_encode($request->all()),null);
- $response=["msg"=>"保存时发生错误(未下发)!","code"=>500,"data"=>null];
- return json_encode($response,JSON_UNESCAPED_UNICODE);
- }
- }
- public function validatorWeight(Request $request){
- $validator=Validator::make($request->input(),[
- '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'=>['required','numeric','min:0'],
- 'width'=>['required','numeric','min:0'],
- 'height'=>['required','numeric','min:0'],
- ],[
- 'required'=>':attribute 为必填项',
- 'max'=>':attribute 字符过多或数值过大',
- 'min'=>':attribute 不得为负',
- 'numeric'=>':attribute 应为数字',
- ],[]);
- return $validator;
- }
- }
|