OwnerController.php 3.9 KB

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