PaperBoxController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Owner;
  4. use App\PaperBox;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Gate;
  8. use Illuminate\Support\Facades\Validator;
  9. class PaperBoxController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. * @param \Illuminate\Http\Request
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function indexModel()
  17. {
  18. if(!Gate::allows('纸箱-查询')){ return redirect(url('/')); }
  19. $paperBoxes=PaperBox::with('owners')->orderBy('id','DESC')->paginate(50);
  20. return view('maintenance.paperBox.index',['paperBoxes'=>$paperBoxes]);
  21. }
  22. public function indexOwner()
  23. {
  24. if(!Gate::allows('纸箱-查询')){ return redirect(url('/')); }
  25. $paperBoxes_owner=Owner::with('paperBoxes')->orderBy('id','DESC')->paginate(50);
  26. return view('maintenance.paperBox.indexOwner',['paperBoxes_owner'=>$paperBoxes_owner]);
  27. }
  28. /**
  29. * Show the form for creating a new resource.
  30. *
  31. * @return
  32. */
  33. public function create()
  34. {
  35. if(!Gate::allows('纸箱-录入')){ return redirect(url('/')); }
  36. $owners=Owner::get();
  37. return view('maintenance.paperBox.create',['owners'=>$owners]);
  38. }
  39. /**
  40. * Store a newly created resource in storage.
  41. *
  42. * @param \Illuminate\Http\Request $request
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function store(Request $request)
  46. {
  47. if(!Gate::allows('纸箱-录入')){ return redirect(url('/')); }
  48. $id=false;
  49. $this->validator($request,$id)->validate();
  50. $length=$request->input('length');
  51. $width=$request->input('width');
  52. $height=$request->input('height');
  53. $max=($length>=($width>=$height?$width:$height)?$length:($width>=$height?$width:$height));
  54. if ($max==$length){
  55. $centre=$width>=$height?$width:$height;
  56. $min=$width<$height?$width:$height;
  57. }elseif ($max==$width){
  58. $centre=$length>=$height?$length:$height;
  59. $min=$length<$height?$length:$height;
  60. }else{
  61. $centre=$width>=$length?$width:$length;
  62. $min=$width<$length?$width:$length;
  63. }
  64. $paperBox=new PaperBox([
  65. 'model'=>$request->input('model'),
  66. 'length'=>$max,
  67. 'width'=>$centre,
  68. 'height'=>$min,
  69. ]);
  70. $paperBox->save();
  71. $owner_id=$request->input('owner_id');
  72. if ($owner_id){
  73. $paperBox->owners()->sync($owner_id);
  74. }
  75. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  76. return redirect('maintenance/paperBox/index/model')->with('successTip','新纸箱'.$request->input('model').'录入成功');
  77. }
  78. /**
  79. * Show the form for editing the specified resource.
  80. *
  81. * @return \Illuminate\Http\Response
  82. */
  83. public function edit($id)
  84. {
  85. if(!Gate::allows('纸箱-编辑')){ return redirect(url('/')); }
  86. $paperBox=PaperBox::with('owners')->find($id);
  87. $owners=Owner::get();
  88. return view('maintenance/paperBox/edit',['paperBox'=>$paperBox,'owners'=>$owners]);
  89. }
  90. /**
  91. * Update the specified resource in storage.
  92. *
  93. * @param \Illuminate\Http\Request $request
  94. * @return \Illuminate\Http\Response
  95. */
  96. public function update(Request $request,$id)
  97. {
  98. if(!Gate::allows('纸箱-编辑')){ return redirect(url('/')); }
  99. $this->validator($request,$id)->validate();
  100. $length=$request->input('length');
  101. $width=$request->input('width');
  102. $height=$request->input('height');
  103. $max=($length>=($width>=$height?$width:$height)?$length:($width>=$height?$width:$height));
  104. if ($max==$length){
  105. $centre=$width>=$height?$width:$height;
  106. $min=$width<$height?$width:$height;
  107. }elseif ($max==$width){
  108. $centre=$length>=$height?$length:$height;
  109. $min=$length<$height?$length:$height;
  110. }else{
  111. $centre=$width>=$length?$width:$length;
  112. $min=$width<$length?$width:$length;
  113. }
  114. $paperBox=PaperBox::find($id);
  115. $paperBox->model=$request->input('model');
  116. $paperBox->length=$max;
  117. $paperBox->width=$centre;
  118. $paperBox->height=$min;
  119. $paperBox->save();
  120. $owner_id=$request->input('owner_id');
  121. if ($owner_id){
  122. $paperBox->owners()->sync($owner_id);
  123. }
  124. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  125. return redirect('maintenance/paperBox/index/model')->with('successTip','纸箱'.$request->input('model').'信息更新成功');
  126. }
  127. /**
  128. * Remove the specified resource from storage.
  129. *
  130. */
  131. public function destroy($id)
  132. {
  133. if(!Gate::allows('纸箱-删除')){ return redirect(url('/')); }
  134. $paperBox=PaperBox::find($id);
  135. $this->log(__METHOD__,__FUNCTION__,json_encode($paperBox),Auth::user()['id']);
  136. if ($paperBox->delete()){
  137. $paperBox->owners()->detach();
  138. $this->log(__METHOD__,__FUNCTION__,json_encode($paperBox),Auth::user()['id']);
  139. return ['success'=>true];
  140. }else{
  141. return ['success'=>false];
  142. }
  143. }
  144. public function validator(Request $request,$id){
  145. if ($id){$model=$id;}
  146. $validator=Validator::make($request->input(),[
  147. 'model'=>['max:50','required',isset($model)?"unique:paper_boxes,model,$model":'unique:paper_boxes,model'],
  148. 'length'=>'required|min:0|numeric|max:999999',
  149. 'width'=>'required|min:0|numeric|max:999999',
  150. 'height'=>'required|min:0|numeric|max:999999',
  151. ],[
  152. 'required'=>':attribute 不应为空',
  153. 'min'=>':attribute 不得为0或为负',
  154. 'numeric'=>':attribute 必须为数字',
  155. 'unique'=>':attribute 已存在',
  156. 'max'=>':attribute 输入值过大',
  157. ],[
  158. 'model'=>'型号',
  159. 'length'=>'长(cm)',
  160. 'width'=>'宽(cm)',
  161. 'height'=>'高(cm)',
  162. ]);
  163. return $validator;
  164. }
  165. }