WaybillPriceModelController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Region;
  4. use App\WaybillPriceModel;
  5. use App\Events\WaybillPriceModelEvent;
  6. use App\Imports\WaybillPriceModelsImport;
  7. use App\Unit;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Support\Facades\Gate;
  12. use Illuminate\Support\Facades\Validator;
  13. use Maatwebsite\Excel\Facades\Excel;
  14. class WaybillPriceModelController extends Controller
  15. {
  16. public function index(Request $request)
  17. {
  18. if(!Gate::allows('计费模型-查询')){ return redirect(url('/')); }
  19. $logistics=app('LogisticService')->getSelection(["id","name"],"物流");
  20. $provinces=Region::query()->where("type",1)->get();
  21. $data=$request->input();
  22. $waybillPriceModels= WaybillPriceModel::query()->with(["logistic","province","city","unit"])->orderBy('id', 'DESC');
  23. if ($data){
  24. if ($request->input('logistic_id')){
  25. $waybillPriceModels=$waybillPriceModels->where('logistic_id',$request->input('logistic_id'));
  26. }
  27. if ($request->input('province_id')){
  28. $waybillPriceModels=$waybillPriceModels->where('province_id',$request->input('province_id'));
  29. }
  30. $waybillPriceModels=$waybillPriceModels->paginate($request->input('paginate')?$request->input('paginate'):50);
  31. return view('maintenance.priceModel.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels,'logistics'=>$logistics,'provinces'=>$provinces,'filterData'=>$data]);
  32. }else{
  33. $waybillPriceModels= $waybillPriceModels->paginate(50);
  34. return view('maintenance.priceModel.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels,'logistics'=>$logistics,'provinces'=>$provinces,'filterData'=>$data]);
  35. }
  36. }
  37. public function create()
  38. {
  39. if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
  40. $logistics=app('LogisticService')->getSelection(["id","name"],"物流");
  41. $regions=Region::query()->whereIn("type",[1,2])->get();
  42. $units=Unit::query()->get();
  43. return view('maintenance.priceModel.waybillPriceModel.create',['logistics'=>$logistics,'regions'=>$regions,'units'=>$units]);
  44. }
  45. public function store(Request $request)
  46. {
  47. if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
  48. $this->validateWaybillPriceModel($request)->validate();
  49. $waybillPriceModel=$request->input('WaybillPriceModel');
  50. $waybillPriceModelIs=WaybillPriceModel::query()->where('logistic_id',$waybillPriceModel['logistic_id'])->where('province_id',$waybillPriceModel['province_id'])->where('unit_id',$waybillPriceModel['unit_id']);
  51. if (isset($waybillPriceModel['city_id'])){
  52. $waybillPriceModelIs=$waybillPriceModelIs->where('city_id',$waybillPriceModel['city_id']);
  53. }
  54. if (isset($waybillPriceModel['range_min'])){
  55. $waybillPriceModelIs=$waybillPriceModelIs->where('range_min',$waybillPriceModel['range_min']);
  56. }
  57. if (isset($waybillPriceModel['range_max'])){
  58. $waybillPriceModelIs=$waybillPriceModelIs->where('range_max',$waybillPriceModel['range_max']);
  59. }
  60. $waybillPriceModelIs=$waybillPriceModelIs->first();
  61. if (!$waybillPriceModelIs){
  62. if (isset($waybillPriceModel['city_id'])){
  63. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NULL',[$waybillPriceModel['logistic_id'],$waybillPriceModel['province_id']])->first();
  64. if ($waybillPriceModelProvince){
  65. return redirect()->back()->with('successTip','已存在省份模型,无需录入城市模型');
  66. }
  67. }else{
  68. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NOT NULL',[$waybillPriceModel['logistic_id'],$waybillPriceModel['province_id']])->first();
  69. if ($waybillPriceModelProvince){
  70. return redirect()->back()->with('successTip','已存在城市模型,无法录入省份模型');
  71. }
  72. }
  73. if ($waybillPriceModel['base_fee']==null){ unset($waybillPriceModel['base_fee']);}
  74. if ($waybillPriceModel['initial_weight']==null){unset($waybillPriceModel['initial_weight']);}
  75. $waybillPriceModel=WaybillPriceModel::query()->create($waybillPriceModel);
  76. event(new WaybillPriceModelEvent($waybillPriceModel));
  77. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  78. return redirect('maintenance/priceModel/waybillPriceModel')->with('successTip','新计费模型录入成功');
  79. }else{
  80. return redirect()->back()->with('successTip','该计费模型已存在');
  81. }
  82. }
  83. public function edit($id)
  84. {
  85. if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); }
  86. $waybillPriceModel=WaybillPriceModel::query()->find($id);
  87. $logistics=app('LogisticService')->getSelection(["id","name"],"物流");
  88. $regions=Region::query()->whereIn("type",[1,2])->get();
  89. $units=Unit::query()->get();
  90. return view('maintenance.priceModel.waybillPriceModel.edit',['waybillPriceModel'=>$waybillPriceModel,'logistics'=>$logistics,'regions'=>$regions,'units'=>$units]);
  91. }
  92. public function update(Request $request, $id)
  93. {
  94. if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); }
  95. $this->validateWaybillPriceModel($request)->validate();
  96. $waybillPriceModel=WaybillPriceModel::query()->find($id);
  97. $data=$request->input('WaybillPriceModel');
  98. if ($data['base_fee']==null){ unset($data['base_fee']);}
  99. if ($data['initial_weight']==null){unset($data['initial_weight']);}
  100. $waybillPriceModel->fill($data);
  101. if ($waybillPriceModel->save()){
  102. event(new WaybillPriceModelEvent($waybillPriceModel));
  103. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  104. return redirect('maintenance/priceModel/waybillPriceModel')->with('successTip','新计费模型修改成功');
  105. }
  106. }
  107. public function destroy($id)
  108. {
  109. if(!Gate::allows('计费模型-删除')){ return redirect(url('/')); }
  110. $waybillPriceModel=WaybillPriceModel::query()->find($id);
  111. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybillPriceModel),Auth::user()['id']);
  112. $result=$waybillPriceModel->delete();
  113. return ['success'=>$result];
  114. }
  115. public function import(Request $request){
  116. if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
  117. $fileSuffix=$request->file('file')->getClientOriginalExtension();
  118. if ($fileSuffix=='xlsx'||$fileSuffix=='xlsm'||$fileSuffix=='xltx'||$fileSuffix=='xltm'||$fileSuffix=='xls'||$fileSuffix=='xlt'||$fileSuffix=='ods'||$fileSuffix=='ots'||$fileSuffix=='slk'
  119. ||$fileSuffix=='xml'||$fileSuffix=='gnumeric'||$fileSuffix=='htm'||$fileSuffix=='html'||$fileSuffix=='csv'||$fileSuffix=='tsv'){
  120. $isOverride = $request->input('isOverride');
  121. ini_set('max_execution_time',2500);
  122. ini_set('memory_limit','1526M');
  123. $extension=$request->file()['file']->getClientOriginalExtension();
  124. $extension[0] = strtoupper($extension[0]);
  125. Excel::import(new WaybillPriceModelsImport($isOverride),$request->file()['file']->path(),null,$extension);
  126. if (Cache::has('error')){
  127. return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">'.Cache::pull('error').'</p></h1>';
  128. }else{
  129. $exception=Cache::get('exception');
  130. $a='';
  131. for ($i=0;$i<count($exception);$i++){$a.=implode(',',$exception[$i]).'&#10'; };
  132. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  133. return '<h1 class="text-danger">导入Excel成功<br><textarea style="width: 50%;height: 50%">'.$a.'</textarea></h1>';
  134. }
  135. }else{
  136. return '<h1 class="text-danger">失败<br><p style="color: red">不支持该文件类型</p></h1>';
  137. }
  138. }
  139. protected function validateWaybillPriceModel(Request $request){
  140. $min = $request->input('WaybillPriceModel.range_min');
  141. $validator= Validator::make($request->input(),[
  142. 'WaybillPriceModel.province_id'=>'required|integer',
  143. 'WaybillPriceModel.logistic_id'=>'required|integer',
  144. 'WaybillPriceModel.unit_id'=>'required|integer',
  145. 'WaybillPriceModel.city_id'=>'nullable|integer',
  146. 'WaybillPriceModel.range_min'=> 'nullable|min:0|numeric|max:999999',
  147. 'WaybillPriceModel.range_max'=> "nullable|min:{$min}|numeric|max:999999",
  148. 'WaybillPriceModel.unit_price'=>'required|min:0|numeric|max:999999',
  149. 'WaybillPriceModel.base_fee'=>'nullable|min:0|numeric|max:999999',
  150. 'WaybillPriceModel.initial_weight'=>'nullable|min:0|numeric|max:999999',
  151. ],[
  152. 'required'=>':attribute 为必填项',
  153. 'min' =>':attribute 数值过小',
  154. 'max' => ':attribute 数值过大',
  155. 'numeric' =>':attribute 应为数字',
  156. 'integer'=> ':attribute 选择错误',
  157. ],[
  158. 'WaybillPriceModel.province_id'=>'省份',
  159. 'WaybillPriceModel.logistic_id'=>'承运商',
  160. 'WaybillPriceModel.unit_id'=>'计重单位',
  161. 'WaybillPriceModel.city_id'=>'城市',
  162. 'WaybillPriceModel.range_min'=>'价格区间最小值',
  163. 'WaybillPriceModel.range_max'=>'价格区间最大值',
  164. 'WaybillPriceModel.unit_price'=>'单价',
  165. 'WaybillPriceModel.base_fee'=>'起步费',
  166. 'WaybillPriceModel.initial_weight'=>'始重',
  167. ]);
  168. return $validator;
  169. }
  170. }