PaperBoxController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Imports\PaperBoxesImport;
  4. use App\Owner;
  5. use App\PaperBox;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Gate;
  10. use Illuminate\Support\Facades\Validator;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. class PaperBoxController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. * @param \Illuminate\Http\Request
  17. * @return \Illuminate\Http\Response
  18. */
  19. public function indexModel()
  20. {
  21. if(!Gate::allows('纸箱-查询')){ return redirect(url('/')); }
  22. $paperBoxes=PaperBox::with('owners')->orderBy('id','DESC')->paginate(50);
  23. return view('maintenance.paperBox.index',['paperBoxes'=>$paperBoxes]);
  24. }
  25. public function indexOwner()
  26. {
  27. if(!Gate::allows('纸箱-查询')){ return redirect(url('/')); }
  28. $paperBoxes_owner=Owner::with('paperBoxes')->orderBy('id','DESC')->paginate(50);
  29. return view('maintenance.paperBox.indexOwner',['paperBoxes_owner'=>$paperBoxes_owner]);
  30. }
  31. /**
  32. * Show the form for creating a new resource.
  33. *
  34. * @return
  35. */
  36. public function create()
  37. {
  38. if(!Gate::allows('纸箱-录入')){ return redirect(url('/')); }
  39. $owners=Owner::get();
  40. return view('maintenance.paperBox.create',['owners'=>$owners]);
  41. }
  42. /**
  43. * Store a newly created resource in storage.
  44. *
  45. * @param \Illuminate\Http\Request $request
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function store(Request $request)
  49. {
  50. if(!Gate::allows('纸箱-录入')){ return redirect(url('/')); }
  51. $id=false;
  52. $this->validator($request,$id)->validate();
  53. $length=$request->input('length');
  54. $width=$request->input('width');
  55. $height=$request->input('height');
  56. $max=($length>=($width>=$height?$width:$height)?$length:($width>=$height?$width:$height));
  57. if ($max==$length){
  58. $centre=$width>=$height?$width:$height;
  59. $min=$width<$height?$width:$height;
  60. }elseif ($max==$width){
  61. $centre=$length>=$height?$length:$height;
  62. $min=$length<$height?$length:$height;
  63. }else{
  64. $centre=$width>=$length?$width:$length;
  65. $min=$width<$length?$width:$length;
  66. }
  67. $paperBox=new PaperBox([
  68. 'model'=>$request->input('model'),
  69. 'length'=>$max,
  70. 'width'=>$centre,
  71. 'height'=>$min,
  72. ]);
  73. $paperBox->save();
  74. $owner_id=$request->input('owner_id');
  75. if ($owner_id){
  76. $paperBox->owners()->sync($owner_id);
  77. }
  78. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  79. return redirect('maintenance/paperBox/index/model')->with('successTip','新纸箱'.$request->input('model').'录入成功');
  80. }
  81. /**
  82. * Show the form for editing the specified resource.
  83. *
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function edit($id)
  87. {
  88. if(!Gate::allows('纸箱-编辑')){ return redirect(url('/')); }
  89. $paperBox=PaperBox::with('owners')->find($id);
  90. $owners=Owner::get();
  91. return view('maintenance/paperBox/edit',['paperBox'=>$paperBox,'owners'=>$owners]);
  92. }
  93. /**
  94. * Update the specified resource in storage.
  95. *
  96. * @param \Illuminate\Http\Request $request
  97. * @return \Illuminate\Http\Response
  98. */
  99. public function update(Request $request,$id)
  100. {
  101. if(!Gate::allows('纸箱-编辑')){ return redirect(url('/')); }
  102. $this->validator($request,$id)->validate();
  103. $length=$request->input('length');
  104. $width=$request->input('width');
  105. $height=$request->input('height');
  106. $max=($length>=($width>=$height?$width:$height)?$length:($width>=$height?$width:$height));
  107. if ($max==$length){
  108. $centre=$width>=$height?$width:$height;
  109. $min=$width<$height?$width:$height;
  110. }elseif ($max==$width){
  111. $centre=$length>=$height?$length:$height;
  112. $min=$length<$height?$length:$height;
  113. }else{
  114. $centre=$width>=$length?$width:$length;
  115. $min=$width<$length?$width:$length;
  116. }
  117. $paperBox=PaperBox::find($id);
  118. $paperBox->model=$request->input('model');
  119. $paperBox->length=$max;
  120. $paperBox->width=$centre;
  121. $paperBox->height=$min;
  122. $paperBox->save();
  123. $owner_id=$request->input('owner_id');
  124. if ($owner_id){
  125. $paperBox->owners()->sync($owner_id);
  126. }
  127. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  128. return redirect('maintenance/paperBox/index/model')->with('successTip','纸箱'.$request->input('model').'信息更新成功');
  129. }
  130. /**
  131. * Remove the specified resource from storage.
  132. *
  133. */
  134. public function destroy($id)
  135. {
  136. if(!Gate::allows('纸箱-删除')){ return redirect(url('/')); }
  137. $paperBox=PaperBox::find($id);
  138. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($paperBox),Auth::user()['id']);
  139. if ($paperBox->delete()){
  140. $paperBox->owners()->detach();
  141. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($paperBox),Auth::user()['id']);
  142. return ['success'=>true];
  143. }else{
  144. return ['success'=>false];
  145. }
  146. }
  147. public function import(Request $request){
  148. if(!Gate::allows('纸箱-录入')){ return redirect(url('/')); }
  149. $fileSuffix=$request->file('file')->getClientOriginalExtension();
  150. if ($fileSuffix=='xlsx'||$fileSuffix=='xlsm'||$fileSuffix=='xltx'||$fileSuffix=='xltm'||$fileSuffix=='xls'||$fileSuffix=='xlt'||$fileSuffix=='ods'||$fileSuffix=='ots'||$fileSuffix=='slk'
  151. ||$fileSuffix=='xml'||$fileSuffix=='gnumeric'||$fileSuffix=='htm'||$fileSuffix=='html'||$fileSuffix=='csv'||$fileSuffix=='tsv'){
  152. $isOverride = $request->input('isOverride');
  153. ini_set('max_execution_time',2100);
  154. ini_set('memory_limit','512M');
  155. $extension=$request->file()['file']->getClientOriginalExtension();
  156. $extension[0] = strtoupper($extension[0]);
  157. Excel::import(new PaperBoxesImport($isOverride),$request->file()['file']->path(),null,$extension);
  158. if (Cache::has('error')){
  159. return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">'.Cache::pull('error').'</p></h1>';
  160. }else{
  161. $exception=Cache::get('exception');
  162. $a='';
  163. for ($i=0;$i<count($exception);$i++){$a.=implode(',',$exception[$i]).'&#10'; };
  164. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  165. return '<h1 class="text-danger">导入Excel成功<br><textarea style="width: 50%;height: 50%">'.$a.'</textarea></h1>';
  166. }
  167. }else{
  168. return '<h1 class="text-danger">失败<br><p style="color: red">不支持该文件类型</p></h1>';
  169. }
  170. }
  171. public function validator(Request $request,$id){
  172. if ($id){$model=$id;}
  173. $validator=Validator::make($request->input(),[
  174. 'model'=>['max:50','required',isset($model)?"unique:paper_boxes,model,$model":'unique:paper_boxes,model'],
  175. 'length'=>'required|min:0|numeric|max:999999',
  176. 'width'=>'required|min:0|numeric|max:999999',
  177. 'height'=>'required|min:0|numeric|max:999999',
  178. ],[
  179. 'required'=>':attribute 不应为空',
  180. 'min'=>':attribute 不得为0或为负',
  181. 'numeric'=>':attribute 必须为数字',
  182. 'unique'=>':attribute 已存在',
  183. 'max'=>':attribute 输入值过大',
  184. ],[
  185. 'model'=>'型号',
  186. 'length'=>'长(cm)',
  187. 'width'=>'宽(cm)',
  188. 'height'=>'高(cm)',
  189. ]);
  190. return $validator;
  191. }
  192. }