input(); $waybillPriceModels= WaybillPriceModel::orderBy('id', 'DESC'); if ($data){ if ($request->input('carrier_id')){ $waybillPriceModels=$waybillPriceModels->where('carrier_id',$request->input('carrier_id')); } if ($request->input('province_id')){ $waybillPriceModels=$waybillPriceModels->where('province_id',$request->input('province_id')); } $waybillPriceModels=$waybillPriceModels->paginate($request->input('paginate')?$request->input('paginate'):50); return view('maintenance.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels,'carriers'=>$carriers,'provinces'=>$provinces,'filterData'=>$data]); }else{ $waybillPriceModels= $waybillPriceModels->paginate(50); return view('maintenance.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels,'carriers'=>$carriers,'provinces'=>$provinces,'filterData'=>$data]); } } public function create() { if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); } $carriers=Carrier::get(); $provinces=Province::get(); $units=Unit::get(); return view('maintenance.waybillPriceModel.create',['carriers'=>$carriers,'provinces'=>$provinces,'units'=>$units]); } public function getCities($province_id){ $cities=City::where('province_id',$province_id)->get(); return ['cities'=>$cities]; } public function store(Request $request) { if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); } $this->validateWaybillPriceModel($request)->validate(); $waybillPriceModel=$request->input('WaybillPriceModel'); $waybillPriceModelIs=WaybillPriceModel::where('carrier_id',$waybillPriceModel['carrier_id'])->where('province_id',$waybillPriceModel['province_id'])->where('unit_id',$waybillPriceModel['unit_id']); if (isset($waybillPriceModel['city_id'])){ $waybillPriceModelIs=$waybillPriceModelIs->where('city_id',$waybillPriceModel['city_id']); } if (isset($waybillPriceModel['range_min'])){ $waybillPriceModelIs=$waybillPriceModelIs->where('range_min',$waybillPriceModel['range_min']); } if (isset($waybillPriceModel['range_max'])){ $waybillPriceModelIs=$waybillPriceModelIs->where('range_max',$waybillPriceModel['range_max']); } $waybillPriceModelIs=$waybillPriceModelIs->first(); if (!$waybillPriceModelIs){ if (isset($waybillPriceModel['city_id'])){ $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND city_id IS NULL',[$waybillPriceModel['carrier_id'],$waybillPriceModel['province_id']])->first(); if ($waybillPriceModelProvince){ return redirect()->back()->with('successTip','已存在省份模型,无需录入城市模型'); } }else{ $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND city_id IS NOT NULL',[$waybillPriceModel['carrier_id'],$waybillPriceModel['province_id']])->first(); if ($waybillPriceModelProvince){ return redirect()->back()->with('successTip','已存在城市模型,无法录入省份模型'); } } if ($waybillPriceModel['base_fee']==null){ unset($waybillPriceModel['base_fee']);} if ($waybillPriceModel['initial_weight']==null){unset($waybillPriceModel['initial_weight']);} $waybillPriceModel=WaybillPriceModel::create($waybillPriceModel); event(new WaybillPriceModelEvent($waybillPriceModel)); $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return redirect('maintenance/waybillPriceModel')->with('successTip','新计费模型录入成功'); }else{ return redirect()->back()->with('successTip','该计费模型已存在'); } } public function edit($id) { if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); } $waybillPriceModel=WaybillPriceModel::find($id); $carriers=Carrier::get(); $provinces=Province::get(); $cities=City::where('province_id',$waybillPriceModel->province_id)->get(); $units=Unit::get(); return view('maintenance.waybillPriceModel.edit',['waybillPriceModel'=>$waybillPriceModel,'carriers'=>$carriers,'provinces'=>$provinces,'units'=>$units,'cities'=>$cities]); } public function update(Request $request, $id) { if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); } $this->validateWaybillPriceModel($request)->validate(); $waybillPriceModel=WaybillPriceModel::find($id); $data=$request->input('WaybillPriceModel'); if ($data['base_fee']==null){ unset($data['base_fee']);} if ($data['initial_weight']==null){unset($data['initial_weight']);} $waybillPriceModel->fill($data); if ($waybillPriceModel->save()){ event(new WaybillPriceModelEvent($waybillPriceModel)); $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return redirect('maintenance/waybillPriceModel')->with('successTip','新计费模型修改成功'); } } public function destroy($id) { if(!Gate::allows('计费模型-删除')){ return redirect(url('/')); } $waybillPriceModel=WaybillPriceModel::find($id); $this->log(__METHOD__,__FUNCTION__,json_encode($waybillPriceModel),Auth::user()['id']); $result=$waybillPriceModel->delete(); return ['success'=>$result]; } public function import(Request $request){ if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); } $fileSuffix=$request->file('file')->getClientOriginalExtension(); if ($fileSuffix=='xlsx'||$fileSuffix=='xlsm'||$fileSuffix=='xltx'||$fileSuffix=='xltm'||$fileSuffix=='xls'||$fileSuffix=='xlt'||$fileSuffix=='ods'||$fileSuffix=='ots'||$fileSuffix=='slk' ||$fileSuffix=='xml'||$fileSuffix=='gnumeric'||$fileSuffix=='htm'||$fileSuffix=='html'||$fileSuffix=='csv'||$fileSuffix=='tsv'){ $isOverride = $request->input('isOverride'); ini_set('max_execution_time',2500); ini_set('memory_limit','1526M'); $extension=$request->file()['file']->getClientOriginalExtension(); $extension[0] = strtoupper($extension[0]); Excel::import(new WaybillPriceModelsImport($isOverride),$request->file()['file']->path(),null,$extension); if (Cache::has('error')){ return '

导入Excel失败

'.Cache::pull('error').'

'; }else{ $exception=Cache::get('exception'); $a=''; for ($i=0;$ilog(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return '

导入Excel成功

'; } }else{ return '

失败

不支持该文件类型

'; } } protected function validateWaybillPriceModel(Request $request){ $min = $request->input('WaybillPriceModel.range_min'); $validator= Validator::make($request->input(),[ 'WaybillPriceModel.province_id'=>'required|integer', 'WaybillPriceModel.carrier_id'=>'required|integer', 'WaybillPriceModel.unit_id'=>'required|integer', 'WaybillPriceModel.city_id'=>'nullable|Integer', 'WaybillPriceModel.range_min'=> 'nullable|min:0|numeric|max:999999', 'WaybillPriceModel.range_max'=> "nullable|min:{$min}|numeric|max:999999", 'WaybillPriceModel.unit_price'=>'required|min:0|numeric|max:999999', 'WaybillPriceModel.base_fee'=>'nullable|min:0|numeric|max:999999', 'WaybillPriceModel.initial_weight'=>'nullable|min:0|numeric|max:999999', ],[ 'required'=>':attribute 为必填项', 'min' =>':attribute 数值过小', 'max' => ':attribute 数值过大', 'numeric' =>':attribute 应为数字', 'integer'=> ':attribute 选择错误', ],[ 'WaybillPriceModel.province_id'=>'省份', 'WaybillPriceModel.carrier_id'=>'承运商', 'WaybillPriceModel.unit_id'=>'计重单位', 'WaybillPriceModel.city_id'=>'城市', 'WaybillPriceModel.range_min'=>'价格区间最小值', 'WaybillPriceModel.range_max'=>'价格区间最大值', 'WaybillPriceModel.unit_price'=>'单价', 'WaybillPriceModel.base_fee'=>'起步费', 'WaybillPriceModel.initial_weight'=>'始重', ]); return $validator; } }