WaybillPriceModelsController.php 9.6 KB

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