CustomerBaseController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Customer;
  4. use App\CustomerTag;
  5. use App\Owner;
  6. use App\Services\LogService;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Facades\Gate;
  10. use Illuminate\Support\Facades\Validator;
  11. class CustomerBaseController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @param Request $request
  17. * @return Response
  18. */
  19. public function index(Request $request)
  20. {
  21. if(!Gate::allows('客户-查询')){ return redirect('denied'); }
  22. $customers = app('CustomerService')->paginate($request->input(),["owners.contracts.files","tags:id,name"]);
  23. $owners = app("OwnerService")->getIntersectPermitting(['id','name','customer_id']);
  24. $tags = CustomerTag::query()->get(["id","name"]);
  25. return response()->view('customer.customer.index',compact("customers","owners","tags"));
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return Response
  31. */
  32. public function create()
  33. {
  34. if(!Gate::allows('客户-录入')){ return redirect('denied'); }
  35. return response()->view('customer.customer.create');
  36. }
  37. /**
  38. * Store a newly created resource in storage.
  39. *
  40. * @param Request $request
  41. * @return Response
  42. * @throws
  43. */
  44. public function store(Request $request)
  45. {
  46. if(!Gate::allows('客户-录入')){ return redirect('denied'); }
  47. $this->validator($request->input())->validate();
  48. app('CustomerService')->create([
  49. "code"=>$request->input("code"),
  50. "name"=>$request->input("name"),
  51. "company_name"=>$request->input("company_name"),
  52. "invoice_address"=>$request->input("invoice_address"),
  53. "contact_man"=>$request->input("contact_man"),
  54. "phone"=>$request->input("phone"),
  55. "remark"=>$request->input("remark"),
  56. ]);
  57. LogService::log(__METHOD__,"录入客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  58. return response()->redirectTo("customer/customer")->with("successTip","成功创建客户“".$request->input("name")."”");
  59. }
  60. /**
  61. * Show the form for editing the specified resource.
  62. *
  63. * @param int $id
  64. * @return Response
  65. */
  66. public function edit($id)
  67. {
  68. if(!Gate::allows('客户-编辑')){ return redirect('denied'); }
  69. $customer = app('CustomerService')->find($id);
  70. return response()->view('customer.customer.create',compact("customer"));
  71. }
  72. /**
  73. * Update the specified resource in storage.
  74. *
  75. * @param Request $request
  76. * @param int $id
  77. * @return Response
  78. * @throws
  79. */
  80. public function update(Request $request, $id)
  81. {
  82. if(!Gate::allows('客户-编辑')){ return redirect('denied'); }
  83. $this->validator($request->input(),$id)->validate();
  84. $result = app('CustomerService')->update(["id"=>$id],[
  85. "code"=>$request->input("code"),
  86. "name"=>$request->input("name"),
  87. "company_name"=>$request->input("company_name"),
  88. "invoice_address"=>$request->input("invoice_address"),
  89. "contact_man"=>$request->input("contact_man"),
  90. "phone"=>$request->input("phone"),
  91. "remark"=>$request->input("remark"),
  92. ]);
  93. if ($result == 1){
  94. LogService::log(__METHOD__,"修改客户",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  95. return response()->redirectTo("customer/customer")->with("successTip","成功修改客户“".$request->input("name")."”的信息");
  96. }
  97. return response()->view("exception.default",["code"=>"509"]);
  98. }
  99. /**
  100. * Remove the specified resource from storage.
  101. *
  102. * @param int $id
  103. * @return array
  104. */
  105. public function destroy($id)
  106. {
  107. if(!Gate::allows('客户-删除')){ return ["success"=>false,"data"=>"无权操作!"]; }
  108. $result = app('CustomerService')->destroy($id);
  109. if ($result == 1){
  110. LogService::log(__METHOD__,"删除客户",$id);
  111. return ["success"=>true];
  112. }
  113. return ["success"=>false,"data"=>"删除了“".$result."”行"];
  114. }
  115. private function validator(array $params, $id = null)
  116. {
  117. return Validator::make($params,[
  118. 'code'=>['required',$id?"unique:customers,code,$id":'unique:customers,code','max:20'],
  119. 'name'=>['required','max:20'],
  120. ],[
  121. 'required'=>':attribute 为必填项',
  122. 'max'=>':attribute 字符过多或输入值过大',
  123. 'unique'=>':attribute 已存在',
  124. ],[
  125. 'code'=>'客户代码',
  126. 'name'=>'客户名称',
  127. ]);
  128. }
  129. public function relatedOwner(Request $request)
  130. {
  131. $ids = $request->input("ids") ?? [];
  132. $id = $request->input("customer_id");
  133. $row = Owner::query()->whereIn("id",$ids)->update(["customer_id"=>$id]);
  134. if ($row==count($ids))return ["success"=>true];
  135. return ["success"=>false,"data"=>"修改错误,影响了“".$row."”个项目"];
  136. }
  137. public function addTag(Request $request)
  138. {
  139. $tags = $request->input("tags");
  140. $id = $request->input("id");
  141. /** @var Customer $customer */
  142. $customer = app("CustomerService")->find($id);
  143. if (!$customer)return ["success"=>false,"data"=>"客户不存在"];
  144. $customer->tags()->sync($tags);
  145. $customer->load("tags");
  146. return ["success"=>true,"data"=>$customer->tags];
  147. }
  148. }