WaybillPriceModelsController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\WaybillPriceModel;
  4. use App\City;
  5. use App\Events\WaybillPriceModelEvent;
  6. use App\Imports\WaybillPriceModelsImport;
  7. use App\Province;
  8. use App\Unit;
  9. use function GuzzleHttp\Psr7\str;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Gate;
  14. use Illuminate\Support\Facades\Validator;
  15. use Maatwebsite\Excel\Facades\Excel;
  16. class WaybillPriceModelsController extends Controller
  17. {
  18. public function index(Request $request)
  19. {
  20. if(!Gate::allows('计费模型-查询')){ return redirect(url('/')); }
  21. $logistics=app('LogisticService')->getSelection(["id","name"],"物流");
  22. $provinces=Province::query()->get();
  23. $data=$request->input();
  24. $waybillPriceModels= WaybillPriceModel::query()->orderBy('id', 'DESC');
  25. if ($data){
  26. if ($request->input('logistic_id')){
  27. $waybillPriceModels=$waybillPriceModels->where('logistic_id',$request->input('logistic_id'));
  28. }
  29. if ($request->input('province_id')){
  30. $waybillPriceModels=$waybillPriceModels->where('province_id',$request->input('province_id'));
  31. }
  32. $waybillPriceModels=$waybillPriceModels->paginate($request->input('paginate')?$request->input('paginate'):50);
  33. return view('maintenance.priceModel.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels,'logistics'=>$logistics,'provinces'=>$provinces,'filterData'=>$data]);
  34. }else{
  35. $waybillPriceModels= $waybillPriceModels->paginate(50);
  36. return view('maintenance.priceModel.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels,'logistics'=>$logistics,'provinces'=>$provinces,'filterData'=>$data]);
  37. }
  38. }
  39. public function create()
  40. {
  41. if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
  42. $logistics=app('LogisticService')->getSelection(["id","name"],"物流");
  43. $provinces=Province::get();
  44. $units=Unit::get();
  45. return view('maintenance.priceModel.waybillPriceModel.create',['logistics'=>$logistics,'provinces'=>$provinces,'units'=>$units]);
  46. }
  47. public function getCities($province_id){
  48. $cities=City::where('province_id',$province_id)->get();
  49. return ['cities'=>$cities];
  50. }
  51. public function store(Request $request)
  52. {
  53. if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
  54. $this->validateWaybillPriceModel($request)->validate();
  55. $waybillPriceModel=$request->input('WaybillPriceModel');
  56. $waybillPriceModelIs=WaybillPriceModel::where('logistic_id',$waybillPriceModel['logistic_id'])->where('province_id',$waybillPriceModel['province_id'])->where('unit_id',$waybillPriceModel['unit_id']);
  57. if (isset($waybillPriceModel['city_id'])){
  58. $waybillPriceModelIs=$waybillPriceModelIs->where('city_id',$waybillPriceModel['city_id']);
  59. }
  60. if (isset($waybillPriceModel['range_min'])){
  61. $waybillPriceModelIs=$waybillPriceModelIs->where('range_min',$waybillPriceModel['range_min']);
  62. }
  63. if (isset($waybillPriceModel['range_max'])){
  64. $waybillPriceModelIs=$waybillPriceModelIs->where('range_max',$waybillPriceModel['range_max']);
  65. }
  66. $waybillPriceModelIs=$waybillPriceModelIs->first();
  67. if (!$waybillPriceModelIs){
  68. if (isset($waybillPriceModel['city_id'])){
  69. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NULL',[$waybillPriceModel['logistic_id'],$waybillPriceModel['province_id']])->first();
  70. if ($waybillPriceModelProvince){
  71. return redirect()->back()->with('successTip','已存在省份模型,无需录入城市模型');
  72. }
  73. }else{
  74. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NOT NULL',[$waybillPriceModel['logistic_id'],$waybillPriceModel['province_id']])->first();
  75. if ($waybillPriceModelProvince){
  76. return redirect()->back()->with('successTip','已存在城市模型,无法录入省份模型');
  77. }
  78. }
  79. if ($waybillPriceModel['base_fee']==null){ unset($waybillPriceModel['base_fee']);}
  80. if ($waybillPriceModel['initial_weight']==null){unset($waybillPriceModel['initial_weight']);}
  81. $waybillPriceModel=WaybillPriceModel::create($waybillPriceModel);
  82. event(new WaybillPriceModelEvent($waybillPriceModel));
  83. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  84. return redirect('maintenance/priceModel/waybillPriceModel')->with('successTip','新计费模型录入成功');
  85. }else{
  86. return redirect()->back()->with('successTip','该计费模型已存在');
  87. }
  88. }
  89. public function edit($id)
  90. {
  91. if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); }
  92. $waybillPriceModel=WaybillPriceModel::find($id);
  93. $logistics=app('LogisticService')->getSelection(["id","name"],"物流");
  94. $provinces=Province::get();
  95. $cities=City::where('province_id',$waybillPriceModel->province_id)->get();
  96. $units=Unit::get();
  97. return view('maintenance.priceModel.waybillPriceModel.edit',['waybillPriceModel'=>$waybillPriceModel,'logistics'=>$logistics,'provinces'=>$provinces,'units'=>$units,'cities'=>$cities]);
  98. }
  99. public function update(Request $request, $id)
  100. {
  101. if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); }
  102. $this->validateWaybillPriceModel($request)->validate();
  103. $waybillPriceModel=WaybillPriceModel::find($id);
  104. $data=$request->input('WaybillPriceModel');
  105. if ($data['base_fee']==null){ unset($data['base_fee']);}
  106. if ($data['initial_weight']==null){unset($data['initial_weight']);}
  107. $waybillPriceModel->fill($data);
  108. if ($waybillPriceModel->save()){
  109. event(new WaybillPriceModelEvent($waybillPriceModel));
  110. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  111. return redirect('maintenance/priceModel/waybillPriceModel')->with('successTip','新计费模型修改成功');
  112. }
  113. }
  114. public function destroy($id)
  115. {
  116. if(!Gate::allows('计费模型-删除')){ return redirect(url('/')); }
  117. $waybillPriceModel=WaybillPriceModel::query()->find($id);
  118. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybillPriceModel),Auth::user()['id']);
  119. $result=$waybillPriceModel->delete();
  120. return ['success'=>$result];
  121. }
  122. public function import(Request $request){
  123. if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
  124. $fileSuffix=$request->file('file')->getClientOriginalExtension();
  125. if ($fileSuffix=='xlsx'||$fileSuffix=='xlsm'||$fileSuffix=='xltx'||$fileSuffix=='xltm'||$fileSuffix=='xls'||$fileSuffix=='xlt'||$fileSuffix=='ods'||$fileSuffix=='ots'||$fileSuffix=='slk'
  126. ||$fileSuffix=='xml'||$fileSuffix=='gnumeric'||$fileSuffix=='htm'||$fileSuffix=='html'||$fileSuffix=='csv'||$fileSuffix=='tsv'){
  127. $isOverride = $request->input('isOverride');
  128. ini_set('max_execution_time',2500);
  129. ini_set('memory_limit','1526M');
  130. $extension=$request->file()['file']->getClientOriginalExtension();
  131. $extension[0] = strtoupper($extension[0]);
  132. Excel::import(new WaybillPriceModelsImport($isOverride),$request->file()['file']->path(),null,$extension);
  133. if (Cache::has('error')){
  134. return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">'.Cache::pull('error').'</p></h1>';
  135. }else{
  136. $exception=Cache::get('exception');
  137. $a='';
  138. for ($i=0;$i<count($exception);$i++){$a.=implode(',',$exception[$i]).'&#10'; };
  139. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  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.logistic_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.logistic_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. }