Department.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. $all_data['company_id'] = $company_id;
  144. } else {
  145. $map['company_id'] = $admin_company_id;
  146. $all_data['company_id'] = $admin_company_id;
  147. }
  148. $data = $DepartmentModel->where($map)->first();
  149. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  150. // 写入数据表
  151. $result = $DepartmentModel->addDepartment($all_data);
  152. // 如果操作失败
  153. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  154. // 记录行为
  155. $admin_id = request('access_token.uid', 0); //用户ID
  156. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  157. $table_name = $DepartmentModel->getTable();
  158. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  159. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了部门' . $all_data['name'] . '信息');
  160. // 告知结果
  161. return json_send(['code' => 'success', 'msg' => '新增成功']);
  162. }
  163. /**
  164. * 修改
  165. * @author 唐远望
  166. * @version 1.0
  167. * @date 2025-12-04
  168. *
  169. */
  170. public function edit(Request $request, DepartmentModel $DepartmentModel)
  171. {
  172. $request->scene('edit')->validate();
  173. $admin_company_id = request('admin_company_id', '0');
  174. $company_id = request('access_token.company_id', '0');
  175. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  176. // 接收参数
  177. $id = request('id', 0);
  178. // 接收数据
  179. $all_data = request()->all();
  180. $store_scope = request('store_scope', '');
  181. $all_data['store_scope'] = $store_scope;
  182. $where = ['id' => $id];
  183. // 权限判断
  184. if ($is_admin != 1 && $company_id != 0) {
  185. $where['company_id'] = $company_id;
  186. } else {
  187. $where['company_id'] = $admin_company_id;
  188. }
  189. $Department = $DepartmentModel->where($where)->first();
  190. if (!$Department) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  191. $oldData = $Department->toArray();
  192. //查询是否存在
  193. $map = ['name' => $all_data['name']];
  194. // 权限判断
  195. if ($is_admin != 1 && $company_id != 0) {
  196. $map['company_id'] = $company_id;
  197. } else {
  198. $map['company_id'] = $admin_company_id;
  199. }
  200. $data = $DepartmentModel->where($map)->where('id', '!=', $id)->first();
  201. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  202. // 更新数据表
  203. $result = $DepartmentModel->editDepartment_content($Department, $all_data);
  204. // 如果操作失败
  205. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  206. // 记录行为
  207. $admin_id = request('access_token.uid', 0); //用户ID
  208. $table_name = $DepartmentModel->getTable();
  209. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  210. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了部门' . $oldData['name'] . '信息');
  211. // 告知结果
  212. return json_send(['code' => 'success', 'msg' => '修改成功']);
  213. }
  214. /**
  215. * 修改状态
  216. * @author 唐远望
  217. * @version 1.0
  218. * @date 2025-12-04
  219. *
  220. */
  221. public function set_status(Request $request, DepartmentModel $DepartmentModel, EmployeeDepartmentModel $EmployeeDepartmentModel)
  222. {
  223. // 验证参数
  224. $request->scene('set_status')->validate();
  225. $admin_company_id = request('admin_company_id', '0');
  226. $company_id = request('access_token.company_id', '0');
  227. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  228. // 接收数据
  229. $id = request('id', 0);
  230. $status = request('status', 0);
  231. // 查询用户
  232. $where = ['id' => $id];
  233. // 权限判断
  234. if ($is_admin != 1 && $company_id != 0) {
  235. $where['company_id'] = $company_id;
  236. } else {
  237. $where['company_id'] = $admin_company_id;
  238. }
  239. // 执行修改
  240. $Department = $DepartmentModel->where($where)->first();
  241. if (!$Department) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  242. if ($status == 1) {
  243. // 查询部门下是否有员工
  244. $map = ['department_id' => $id];
  245. // 权限判断
  246. if ($is_admin != 1 && $company_id != 0) {
  247. $map['company_id'] = $company_id;
  248. } else {
  249. $map['company_id'] = $admin_company_id;
  250. }
  251. $data = $EmployeeDepartmentModel->where($map)->first();
  252. if ($data) return json_send(['code' => 'error', 'msg' => '该部门下存在员工,不能禁用']);
  253. }
  254. $Department->status = $status;
  255. $Department->update_time = time();
  256. $result = $Department->save();
  257. // 提示新增失败
  258. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  259. // 记录行为
  260. $admin_id = request('access_token.uid', 0); //用户ID
  261. $table_name = $DepartmentModel->getTable();
  262. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  263. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了部门' . $Department->name . '状态');
  264. // 告知结果
  265. return json_send(['code' => 'success', 'msg' => '设置成功']);
  266. }
  267. /**
  268. * 删除
  269. * @author 唐远望
  270. * @version 1.0
  271. * @date 2025-12-04
  272. *
  273. */
  274. public function delete(Request $request, DepartmentModel $DepartmentModel, EmployeeDepartmentModel $EmployeeDepartmentModel)
  275. {
  276. // 验证参数
  277. $request->scene('delete')->validate();
  278. $admin_company_id = request('admin_company_id', '0');
  279. $company_id = request('access_token.company_id', '0');
  280. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  281. // 接收数据
  282. $id = request('id', 0);
  283. // 查询用户
  284. $where = ['id' => $id];
  285. if ($is_admin != 1 && $company_id != 0) {
  286. $where['company_id'] = $company_id;
  287. } else {
  288. $where['company_id'] = $admin_company_id;
  289. }
  290. $Department = $DepartmentModel->where($where)->first();
  291. if (!$Department) {
  292. return json_send(['code' => 'error', 'msg' => '记录不存在']);
  293. }
  294. // 查询部门下是否有员工
  295. $map = ['department_id' => $id];
  296. // 权限判断
  297. if ($is_admin != 1 && $company_id != 0) {
  298. $map['company_id'] = $company_id;
  299. } else {
  300. $map['company_id'] = $admin_company_id;
  301. }
  302. $data = $EmployeeDepartmentModel->where($map)->first();
  303. if ($data) return json_send(['code' => 'error', 'msg' => '该部门下存在员工,不能删除']);
  304. $result = $Department->delete();
  305. // 提示删除失败
  306. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  307. // 记录行为
  308. $admin_id = request('access_token.uid', 0); //用户ID
  309. $table_name = $DepartmentModel->getTable();
  310. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  311. $this->addAdminHistory('人员信息管理-部门管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Department->toArray(), [], '删除了部门' . $Department->name . '信息');
  312. // 告知结果
  313. return json_send(['code' => 'success', 'msg' => '删除成功']);
  314. }
  315. }