OwnerController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Authority;
  4. use App\Log;
  5. use App\Logistic;
  6. use App\Owner;
  7. use Exception;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Gate;
  12. use Illuminate\Support\Facades\Validator;
  13. class OwnerController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Response
  19. */
  20. public function index()
  21. {
  22. if(!Gate::allows('货主-查询')){ return redirect(url('/')); }
  23. $owners=Owner::orderBy('id','desc')->paginate(25);
  24. return view('maintenance.owner.index',['owners'=>$owners]);
  25. }
  26. public function create()
  27. {
  28. if(!Gate::allows('货主-录入')){ return redirect(url('/')); }
  29. return view('maintenance.owner.create');
  30. }
  31. /**
  32. * Store a newly created resource in storage.
  33. *
  34. * @param Request $request
  35. * @return string
  36. */
  37. public function store(Request $request)
  38. {
  39. if(!Gate::allows('货主-录入')){ return redirect(url('/')); }
  40. $this->validatorCreate($request->all())->validate();
  41. $owner=new Owner($request->all());
  42. $owner->save();
  43. $authority=new Authority([
  44. 'name'=>"_{$owner['id']}",
  45. 'alias_name'=>"(货主:{$owner['name']})",
  46. 'remark'=>"(key: _{$owner['id']})",
  47. ]);
  48. $authority->save();
  49. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  50. return redirect('maintenance/owner/create')->with('successTip',$request->input('name')??'');
  51. }
  52. protected function validatorCreate(array $data)
  53. {
  54. return Validator::make($data, [
  55. 'code' => ['required', 'string', 'max:50', 'unique:owners'],
  56. 'name' => ['required', 'string', 'max:50'],
  57. ]);
  58. }
  59. protected function validatorUpdate(array $data)
  60. {
  61. return Validator::make($data, [
  62. 'name' => ['required', 'string', 'max:50'],
  63. ]);
  64. }
  65. /**
  66. * Display the specified resource.
  67. *
  68. * @param Owner $owner
  69. * @return Response
  70. */
  71. public function show(Owner $owner)
  72. {
  73. //
  74. }
  75. /**
  76. * Show the form for editing the specified resource.
  77. *
  78. * @param Owner $owner
  79. * @return Response
  80. */
  81. public function edit(Owner $owner)
  82. {
  83. if(!Gate::allows('货主-编辑')){ return redirect(url('/')); }
  84. return view('maintenance.owner.edit',['owner'=>$owner]);
  85. }
  86. /**
  87. * Update the specified resource in storage.
  88. *
  89. * @param Request $request
  90. * @param Owner $owner
  91. * @return Response
  92. */
  93. public function update(Request $request, Owner $owner)
  94. {
  95. if(!Gate::allows('货主-编辑')){ return redirect(url('/')); }
  96. $this->validatorUpdate($request->all())->validate();
  97. $owner->fill($request->all());
  98. $owner->update();
  99. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  100. return redirect('maintenance/owner/')->with('successTip',"成功修改货主{$owner['name']}!");
  101. }
  102. /**
  103. * Remove the specified resource from storage.
  104. *
  105. * @param Owner $owner
  106. * @return array|Response
  107. * @throws Exception
  108. */
  109. public function destroy(Owner $owner)
  110. {
  111. if(!Gate::allows('货主-删除')){ return redirect(url('/')); }
  112. $authority=Authority::where('name',"_{$owner['id']}")->first();
  113. if($authority)$authority->delete();
  114. $this->log(__METHOD__,__FUNCTION__,$owner->toJson(),Auth::user()['id']);
  115. $re=$owner->delete();
  116. return ['success'=>$re];
  117. }
  118. }