OwnerController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Authority;
  4. use App\Components\AsyncResponse;
  5. use App\Events\CustomerStored;
  6. use App\Owner;
  7. use App\OwnerGroup;
  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. use AsyncResponse;
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @param Request $request
  21. *
  22. * @return Response
  23. */
  24. public function index(Request $request)
  25. {
  26. if (!Gate::allows('货主-查询')) {
  27. return redirect(url('/'));
  28. }
  29. $query = Owner::query()
  30. ->orderBy('id', 'desc')
  31. ->whereNull('deleted_at');
  32. if ($request->has('name')) {
  33. $name=$request->input('name');
  34. if (strpos($name, ',') || strpos($name, ',') || strpos($name, ' ')) {
  35. $arr = array_filter(preg_split('/[,, ]+/is', $name));
  36. $query->whereIn('name', $arr);
  37. unset($name);
  38. } else {
  39. $query->where('name','like',$name.'%');
  40. }
  41. }
  42. if ($request->has('code')) {
  43. $code=$request->input('code');
  44. if (strpos($code, ',') || strpos($code, ',') || strpos($code, ' ')) {
  45. $arr = array_filter(preg_split('/[,, ]+/is', $code));
  46. $query->whereIn('code', $arr);
  47. unset($code);
  48. } else {
  49. $query->where('code','like',$code.'%');
  50. }
  51. }
  52. $owners = $query->paginate($request->paginate);
  53. return view('maintenance.owner.index', ['owners' => $owners]);
  54. }
  55. public function create()
  56. {
  57. if (!Gate::allows('货主-录入')) {
  58. return redirect(url('/'));
  59. }
  60. return view('maintenance.owner.create');
  61. }
  62. public function store(Request $request)
  63. {
  64. if (!Gate::allows('货主-录入')) {
  65. return redirect(url('/'));
  66. }
  67. $this->validatorCreate($request->all())->validate();
  68. $owner = new Owner($request->all());
  69. $owner->save();
  70. $authority = new Authority([
  71. 'name' => "_{$owner['id']}",
  72. 'alias_name' => "(货主:{$owner['name']})",
  73. 'remark' => "(key: _{$owner['id']})",
  74. ]);
  75. $authority->save();
  76. app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  77. return redirect('maintenance/owner/create')->with('successTip', $request->input('name') ?? '');
  78. }
  79. public function apiStore()
  80. {
  81. $this->gate("货主-录入");
  82. if (request("id"))$errors = $this->validatorUpdate(request()->input(),request("id"))->errors();
  83. else $errors = $this->validatorCreate(request()->input())->errors();
  84. if (count($errors)>0)$this->success(["errors"=>$errors]);
  85. if (request("id")){
  86. $owner = app("OwnerService")->find(request("id"));
  87. if (!$owner)$this->error("项目已被删除");
  88. if ($owner->name == request("name") && $owner->code == request("code"))
  89. $this->success($owner);
  90. $owner = app("OwnerService")->update($owner,[
  91. "name" => request("name"),
  92. "code" => request("code"),
  93. ]);
  94. }else $owner = app("OwnerService")->create([
  95. "name" => request("name"),
  96. "code" => request("code"),
  97. ]);
  98. $this->success($owner);
  99. }
  100. protected function validatorCreate(array $data)
  101. {
  102. return Validator::make($data, [
  103. 'code' => ['required', 'string', 'max:50', "unique:owners,code"],
  104. 'name' => ['required', 'string', 'max:50'],
  105. ],[],[
  106. "code" => "货主代码",
  107. "name" => "货主名称",
  108. ]);
  109. }
  110. protected function validatorUpdate(array $data, $id)
  111. {
  112. return Validator::make($data, [
  113. 'name' => ['required', 'string', 'max:50'],
  114. 'code' => ['required', 'string', 'max:50', "unique:owners,code,$id"]
  115. ],[],[
  116. "code" => "货主代码",
  117. "name" => "货主名称",
  118. ]);
  119. }
  120. public function getOwners()
  121. {
  122. $owners = app('OwnerService')->getIntersectPermitting(['id', 'code', 'name']);
  123. return $owners;
  124. }
  125. /**
  126. * Show the form for editing the specified resource.
  127. *
  128. * @param Owner $owner
  129. * @return Response
  130. */
  131. public function edit(Owner $owner)
  132. {
  133. if (!Gate::allows('货主-编辑')) {
  134. return redirect(url('/'));
  135. }
  136. return view('maintenance.owner.edit', ['owner' => $owner]);
  137. }
  138. /**
  139. * Update the specified resource in storage.
  140. *
  141. * @param Request $request
  142. * @param Owner $owner
  143. *
  144. * @return Response
  145. *
  146. * @throws Exception
  147. */
  148. public function update(Request $request, Owner $owner)
  149. {
  150. if (!Gate::allows('货主-编辑')) {
  151. return redirect(url('/'));
  152. }
  153. $this->validatorUpdate($request->all(), $owner->id)->validate();
  154. $owner->fill($request->all());
  155. $owner->update();
  156. event(new CustomerStored($owner));
  157. app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  158. return redirect('maintenance/owner/')->with('successTip', "成功修改货主{$owner['name']}!");
  159. }
  160. /**
  161. * Remove the specified resource from storage.
  162. *
  163. * @param Owner $owner
  164. * @return array|Response
  165. * @throws Exception
  166. */
  167. public function destroy(Owner $owner)
  168. {
  169. if (!Gate::allows('货主-删除')) {
  170. return redirect(url('/'));
  171. }
  172. $re = $owner->update([
  173. "deleted_at" => date('Y-m-d H:i:s'),
  174. ]);
  175. app("OwnerService")->syncUpdate($owner);//停用更新flux
  176. app("OwnerService")->deleteAuthority($owner);
  177. app('LogService')->log(__METHOD__, __FUNCTION__, $owner->toJson(), Auth::user()['id']);
  178. return ['success' => $re];
  179. }
  180. public function recycle()
  181. {
  182. if (!Gate::allows('货主-删除')) {
  183. return redirect(url('/'));
  184. }
  185. $owners = Owner::query()->orderBy('id', 'desc')->whereNotNull('deleted_at')->paginate(25);
  186. return view('maintenance.owner.recycle', ['owners' => $owners]);
  187. }
  188. public function restoreSelected(Request $request)
  189. {
  190. if (!Gate::allows('货主-删除')) {
  191. return ['success' => 'false', 'fail_info' => "没有权限"];
  192. }
  193. $id = $request->input('id');
  194. $owner = Owner::query()->whereNotNull('deleted_at')->where('id', $id)->first();
  195. $owner->update(["deleted_at" => null]);
  196. //app("OwnerService")->createAuthority($owner);
  197. app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  198. return ['success' => 'true', 'owner' => $owner];
  199. }
  200. public function get(Request $request)
  201. {
  202. $params = [];
  203. if ($request->has("customer_id"))$params["customer_id"] = $request->input("customer_id");
  204. return ["success"=>true,"data"=>app("OwnerService")->get($params,null,false)];
  205. }
  206. /**
  207. * @param Request $request
  208. * @return array
  209. * 设置货主是否自动回传订单
  210. */
  211. public function changeManualBackType(Request $request): array
  212. {
  213. if (!Gate::allows('货主-编辑')) return ["success"=>false,"data"=>"无权操作!"];
  214. $id=$request->input('id');
  215. $is_manual_back=$request->input('isManualBack');
  216. $owner=app("OwnerService")->changeManualBackStatus($id,$is_manual_back);
  217. return ["success"=>true,"data"=>$owner];
  218. }
  219. /**
  220. * @param Request $request
  221. * @return array
  222. * 设置货主自动回传时间间隔
  223. */
  224. public function intervalTime(Request $request): array
  225. {
  226. if (!Gate::allows('货主-编辑')) return ["success"=>false,"data"=>"无权操作!"];
  227. $id=$request->input('id');
  228. $intervalTime=$request->input('intervalTime');
  229. $owner=app("OwnerService")->changeIntervalTime($id,$intervalTime);
  230. return ["success"=>true,"data"=>$owner];
  231. }
  232. }