CarTypesController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\CarType;
  4. use App\Components\AsyncResponse;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Gate;
  9. use Illuminate\Support\Facades\Validator;
  10. class CarTypesController extends Controller
  11. {
  12. use AsyncResponse;
  13. public function index()
  14. {
  15. if(!Gate::allows('车型-查询')){ return redirect(url('/')); }
  16. $carTypes=CarType::orderBy('id', 'DESC')->paginate(50);
  17. return view('maintenance.carType.index',['carTypes'=>$carTypes]);
  18. }
  19. public function create()
  20. {
  21. if(!Gate::allows('车型-录入')){ return redirect(url('/')); }
  22. return view('maintenance.carType.create');
  23. }
  24. public function store(Request $request)
  25. {
  26. if(!Gate::allows('车型-录入')){ return redirect(url('/')); }
  27. $id=false;
  28. $this->validatorCarType($request,$id)->validate();
  29. $carTypes=$request->input('CarType');
  30. if(CarType::create($carTypes)){
  31. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  32. return redirect('maintenance/carType')->with('successTip','新车辆“'.$request->input('CarType.name').'”添加成功');
  33. };
  34. }
  35. public function edit($id)
  36. {
  37. if(!Gate::allows('车型-编辑')){ return redirect(url('/')); }
  38. $carType=CarType::find($id);
  39. return view('maintenance.carType.edit',['carType'=>$carType]);
  40. }
  41. public function update(Request $request, $id)
  42. {
  43. if(!Gate::allows('车型-编辑')){ return redirect(url('/')); }
  44. $this->validatorCarType($request,$id)->validate();
  45. $data=$request->input('CarType');
  46. $carType=CarType::find($id);
  47. $carType->fill($data);
  48. if ($carType->save()){
  49. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  50. return redirect('maintenance/carType')->with('successTip','车型“'.$request->input('CarType.name').'”修改成功');
  51. }
  52. }
  53. public function destroy($id)
  54. {
  55. if(!Gate::allows('车型-删除')){ return redirect(url('/')); }
  56. $carType=CarType::find($id);
  57. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($carType),Auth::user()['id']);
  58. $result=$carType->delete();
  59. return ['success'=>$result];
  60. }
  61. public function get()
  62. {
  63. return CarType::query()->select("id","name","model")->get();
  64. }
  65. protected function validatorCarType(Request $request,$id){
  66. if ($id){$name=$id;}
  67. $validator=Validator::make($request->input(),[
  68. 'CarType.name'=>['required','max:50',isset($name)?"unique:car_types,name,$name":'unique:car_types,name'],
  69. 'CarType.model'=>'nullable|max:50',
  70. 'CarType.length'=>'nullable|min:0',
  71. 'CarType.load'=>'nullable|min:0',
  72. ],[
  73. 'required'=>':attribute 为必填项',
  74. 'max'=>':attribute 字符过多或数值过大',
  75. 'min'=>':attribute 不得为负',
  76. 'unique'=>':attribute 已存在',
  77. ],[
  78. 'CarType.name'=>'车辆名称',
  79. 'CarType.model'=>'车辆型号',
  80. 'CarType.length'=>'车辆长度',
  81. 'CarType.load'=>'车辆载重',
  82. ]);
  83. return $validator;
  84. }
  85. public function batchInsert()
  86. {
  87. $arr = request("arr");
  88. if (!$arr)$this->error("非法参数");
  89. DB::transaction(function()use($arr){
  90. $cars = CarType::query()->whereIn("name",$arr)->lockForUpdate()->get()->toArray();
  91. $result = array_diff($arr,array_column($cars,"name"));
  92. if ($result){
  93. foreach ($result as &$r)$r = ["name"=>$r];
  94. CarType::query()->insert($result);
  95. }
  96. });
  97. $this->success(CarType::query()->select("id","name")->whereIn("name",$arr)->get());
  98. }
  99. }