Department.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace App\Http\Controllers\Manager\Personnel;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\Personnel\Department as Request;
  5. use App\Models\Manager\Personnel\Department as DepartmentModel;
  6. use App\Models\Manager\Personnel\EmployeeDepartment as EmployeeDepartmentModel;
  7. /**
  8. * 人员信息管理-部门管理
  9. * @author 唐远望
  10. * @version 1.0
  11. * @date 2025-12-04
  12. */
  13. class Department extends Controller
  14. {
  15. /**
  16. * 列表
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-12-04
  20. *
  21. */
  22. public function list(Request $request, DepartmentModel $DepartmentModel)
  23. {
  24. $request->scene('list')->validate();
  25. $admin_company_id = request('admin_company_id', '0');
  26. $company_id = request('access_token.company_id', '0');
  27. $is_admin = request('access_token.is_admin', '0');
  28. // 查询条件
  29. $map = [];
  30. $limit = request('limit', config('page_num', 10));
  31. $status = request('status', '');
  32. $start_time = request('start_time', '');
  33. $end_time = request('end_time', '');
  34. $name = request('name', '');
  35. // 时间条件
  36. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  37. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  38. // 其他条件
  39. if (is_numeric($status)) $map[] = ['status', '=', $status];
  40. if ($name) $map[] = ['name', 'like', "%$name%"];
  41. // 权限判断
  42. if ($is_admin != 1 && $company_id != 0) {
  43. $map[] = ['company_id', '=', $company_id];
  44. } else {
  45. $map[] = ['company_id', '=', $admin_company_id];
  46. }
  47. // 查询数据
  48. $result = $DepartmentModel->query()
  49. ->where($map)
  50. ->orderByDesc('id')
  51. ->paginate($limit);
  52. // 分配数据
  53. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  54. // 加载模板
  55. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  56. }
  57. /**
  58. * 所有部门
  59. * @author 唐远望
  60. * @version 1.0
  61. * @date 2025-12-08
  62. *
  63. */
  64. public function all(DepartmentModel $DepartmentModel)
  65. {
  66. $map = [];
  67. $admin_company_id = request('admin_company_id', '0');
  68. $company_id = request('access_token.company_id', '0');
  69. $is_admin = request('access_token.is_admin', '0');
  70. $status = request('status', '0');
  71. $start_time = request('start_time', '');
  72. $end_time = request('end_time', '');
  73. $name = request('name', '');
  74. // 权限判断
  75. if ($is_admin != 1 && $company_id != 0) {
  76. $map[] = ['company_id', '=', $company_id];
  77. } else {
  78. $map[] = ['company_id', '=', $admin_company_id];
  79. }
  80. // 时间条件
  81. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  82. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  83. // 其他条件
  84. if ($name) $map[] = ['name', 'like', "%$name%"];
  85. if (is_numeric($status)) $map[] = ['status', '=', $status];
  86. $result = $DepartmentModel->query()
  87. ->where($map)
  88. ->orderByDesc('id')
  89. ->get();
  90. // 分配数据
  91. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  92. // 加载模板
  93. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  94. }
  95. /**
  96. * 详情
  97. * @author 唐远望
  98. * @version 1.0
  99. * @date 2025-12-04
  100. */
  101. public function detail(Request $request, DepartmentModel $DepartmentModel)
  102. {
  103. $request->scene('detail')->validate();
  104. $admin_company_id = request('admin_company_id', '0');
  105. $company_id = request('access_token.company_id', '0');
  106. $is_admin = request('access_token.is_admin', '0');
  107. // 接收参数
  108. $id = request('id', 0);
  109. $map = ['id' => $id];
  110. // 权限判断
  111. if ($is_admin != 1 && $company_id != 0) {
  112. $map['company_id'] = $company_id;
  113. } else {
  114. $map['company_id'] = $admin_company_id;
  115. }
  116. $data = $DepartmentModel->where($map)->first();
  117. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  118. // 加载模板
  119. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  120. }
  121. /**
  122. * 添加
  123. * @author 唐远望
  124. * @version 1.0
  125. * @date 2025-12-04
  126. *
  127. */
  128. public function add(Request $request, DepartmentModel $DepartmentModel)
  129. {
  130. $request->scene('add')->validate();
  131. $admin_company_id = request('admin_company_id', '0');
  132. $company_id = request('access_token.company_id', '0');
  133. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  134. // 接收数据
  135. $all_data = request()->all();
  136. $store_scope = request('store_scope', '');
  137. $all_data['store_scope'] = $store_scope;
  138. //查询是否存在
  139. $map = ['name' => $all_data['name']];
  140. // 权限判断
  141. if ($is_admin != 1 && $company_id != 0) {
  142. $map['company_id'] = $company_id;
  143. } else {
  144. $map['company_id'] = $admin_company_id;
  145. }
  146. $data = $DepartmentModel->where($map)->first();
  147. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  148. // 写入数据表
  149. $all_data['company_id'] = $company_id;
  150. $result = $DepartmentModel->addDepartment($all_data);
  151. // 如果操作失败
  152. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  153. // 记录行为
  154. $admin_id = request('access_token.uid', 0); //用户ID
  155. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  156. $table_name = $DepartmentModel->getTable();
  157. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  158. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了部门' . $all_data['name'] . '信息');
  159. // 告知结果
  160. return json_send(['code' => 'success', 'msg' => '新增成功']);
  161. }
  162. /**
  163. * 修改
  164. * @author 唐远望
  165. * @version 1.0
  166. * @date 2025-12-04
  167. *
  168. */
  169. public function edit(Request $request, DepartmentModel $DepartmentModel)
  170. {
  171. $request->scene('edit')->validate();
  172. $admin_company_id = request('admin_company_id', '0');
  173. $company_id = request('access_token.company_id', '0');
  174. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  175. // 接收参数
  176. $id = request('id', 0);
  177. // 接收数据
  178. $all_data = request()->all();
  179. $store_scope = request('store_scope', '');
  180. $all_data['store_scope'] = $store_scope;
  181. $where = ['id' => $id];
  182. // 权限判断
  183. if ($is_admin != 1 && $company_id != 0) {
  184. $where['company_id'] = $company_id;
  185. } else {
  186. $where['company_id'] = $admin_company_id;
  187. }
  188. $Department = $DepartmentModel->where($where)->first();
  189. if (!$Department) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  190. $oldData = $Department->toArray();
  191. //查询是否存在
  192. $map = ['name' => $all_data['name']];
  193. // 权限判断
  194. if ($is_admin != 1 && $company_id != 0) {
  195. $map['company_id'] = $company_id;
  196. } else {
  197. $map['company_id'] = $admin_company_id;
  198. }
  199. $data = $DepartmentModel->where($map)->where('id', '!=', $id)->first();
  200. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  201. // 更新数据表
  202. $result = $DepartmentModel->editDepartment_content($Department, $all_data);
  203. // 如果操作失败
  204. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  205. // 记录行为
  206. $admin_id = request('access_token.uid', 0); //用户ID
  207. $table_name = $DepartmentModel->getTable();
  208. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  209. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了部门' . $oldData['name'] . '信息');
  210. // 告知结果
  211. return json_send(['code' => 'success', 'msg' => '修改成功']);
  212. }
  213. /**
  214. * 修改状态
  215. * @author 唐远望
  216. * @version 1.0
  217. * @date 2025-12-04
  218. *
  219. */
  220. public function set_status(Request $request, DepartmentModel $DepartmentModel, EmployeeDepartmentModel $EmployeeDepartmentModel)
  221. {
  222. // 验证参数
  223. $request->scene('set_status')->validate();
  224. $admin_company_id = request('admin_company_id', '0');
  225. $company_id = request('access_token.company_id', '0');
  226. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  227. // 接收数据
  228. $id = request('id', 0);
  229. $status = request('status', 0);
  230. // 查询用户
  231. $where = ['id' => $id];
  232. // 权限判断
  233. if ($is_admin != 1 && $company_id != 0) {
  234. $where['company_id'] = $company_id;
  235. } else {
  236. $where['company_id'] = $admin_company_id;
  237. }
  238. // 执行修改
  239. $Department = $DepartmentModel->where($where)->first();
  240. if (!$Department) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  241. if ($status == 1) {
  242. // 查询部门下是否有员工
  243. $map = ['department_id' => $id];
  244. // 权限判断
  245. if ($is_admin != 1 && $company_id != 0) {
  246. $map['company_id'] = $company_id;
  247. } else {
  248. $map['company_id'] = $admin_company_id;
  249. }
  250. $data = $EmployeeDepartmentModel->where($map)->first();
  251. if ($data) return json_send(['code' => 'error', 'msg' => '该部门下存在员工,不能禁用']);
  252. }
  253. $Department->status = $status;
  254. $Department->update_time = time();
  255. $result = $Department->save();
  256. // 提示新增失败
  257. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  258. // 记录行为
  259. $admin_id = request('access_token.uid', 0); //用户ID
  260. $table_name = $DepartmentModel->getTable();
  261. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  262. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了部门' . $Department->name . '状态');
  263. // 告知结果
  264. return json_send(['code' => 'success', 'msg' => '设置成功']);
  265. }
  266. /**
  267. * 删除
  268. * @author 唐远望
  269. * @version 1.0
  270. * @date 2025-12-04
  271. *
  272. */
  273. public function delete(Request $request, DepartmentModel $DepartmentModel, EmployeeDepartmentModel $EmployeeDepartmentModel)
  274. {
  275. // 验证参数
  276. $request->scene('delete')->validate();
  277. $admin_company_id = request('admin_company_id', '0');
  278. $company_id = request('access_token.company_id', '0');
  279. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  280. // 接收数据
  281. $id = request('id', 0);
  282. // 查询用户
  283. $where = ['id' => $id];
  284. if ($is_admin != 1 && $company_id != 0) {
  285. $where['company_id'] = $company_id;
  286. } else {
  287. $where['company_id'] = $admin_company_id;
  288. }
  289. $Department = $DepartmentModel->where($where)->first();
  290. if (!$Department) {
  291. return json_send(['code' => 'error', 'msg' => '记录不存在']);
  292. }
  293. // 查询部门下是否有员工
  294. $map = ['department_id' => $id];
  295. // 权限判断
  296. if ($is_admin != 1 && $company_id != 0) {
  297. $map['company_id'] = $company_id;
  298. } else {
  299. $map['company_id'] = $admin_company_id;
  300. }
  301. $data = $EmployeeDepartmentModel->where($map)->first();
  302. if ($data) return json_send(['code' => 'error', 'msg' => '该部门下存在员工,不能删除']);
  303. $result = $Department->delete();
  304. // 提示删除失败
  305. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  306. // 记录行为
  307. $admin_id = request('access_token.uid', 0); //用户ID
  308. $table_name = $DepartmentModel->getTable();
  309. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  310. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Department->toArray(), [], '删除了部门' . $Department->name . '信息');
  311. // 告知结果
  312. return json_send(['code' => 'success', 'msg' => '删除成功']);
  313. }
  314. }