DeptService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\service\sys_manage;
  3. use app\model\SysDept;
  4. use support\Db;
  5. class DeptService
  6. {
  7. /**
  8. * @Desc 部门列表
  9. * @Author Gorden
  10. * @Date 2024/2/21 10:28
  11. *
  12. * @param $page
  13. * @param $limit
  14. * @param $keywords
  15. * @return \support\Response
  16. */
  17. public static function deptList($page, $limit, $keywords)
  18. {
  19. $list = SysDept::select('*')
  20. ->when($keywords != '', function ($query) use ($keywords) {
  21. $query->where('dept_name', 'like', '%' . $keywords . '%');
  22. })
  23. ->orderBy('dept_addtimes', 'DESC')
  24. ->forPage($page, $limit)
  25. ->get()
  26. ->toArray();
  27. $count = SysDept::when($keywords != '', function ($query) use ($keywords) {
  28. $query->where('dept_name', 'like', '%' . $keywords . '%');
  29. })->count();
  30. return json_success('', compact('list', 'page', 'limit', 'count'));
  31. }
  32. /**
  33. * @Desc 部门详情
  34. * @Author Gorden
  35. * @Date 2024/2/21 10:35
  36. *
  37. * @param $id
  38. * @return \support\Response
  39. */
  40. public static function deptInfo($id)
  41. {
  42. $dept = SysDept::find($id);
  43. if (!$dept) {
  44. return json_fail('部门不存在');
  45. }
  46. $dept = $dept->toArray();
  47. return json_success('', $dept);
  48. }
  49. /**
  50. * @Desc 创建部门
  51. * @Author Gorden
  52. * @Date 2024/2/21 9:22
  53. *
  54. * @param $params
  55. * @return \support\Response
  56. */
  57. public static function insertDept($params)
  58. {
  59. Db::beginTransaction();
  60. try {
  61. $data = [
  62. 'dept_super_id' => $params['dept_super_id'],
  63. 'dept_status' => $params['dept_status'],
  64. 'dept_category' => $params['dept_category'],
  65. 'dept_code' => $params['dept_code'],
  66. 'dept_city' => $params['dept_city'],
  67. 'dept_name' => $params['dept_name'],
  68. 'dept_telephone' => $params['dept_telephone'],
  69. 'dept_position' => $params['dept_position'] ?? '',
  70. 'dept_address' => $params['dept_address'] ?? '',
  71. 'dept_remark' => $params['dept_remark'] ?? '',
  72. 'dept_extend_json' => !empty($params['dept_extend_json']) ? $params['dept_extend_json'] : '{}',
  73. 'dept_addtimes' => time()
  74. ];
  75. $deptId = SysDept::insertGetId($data);
  76. if (!$deptId) {
  77. throw new \Exception('创建部门失败');
  78. }
  79. // 获取上级部门path
  80. $deptSuperPath = '/0/';
  81. if ($params['dept_super_id'] != 0) {
  82. $deptSuperPath = SysDept::where('dept_id', $params['dept_super_id'])->value('dept_super_path');
  83. }
  84. // 更新部门path
  85. $path = $deptSuperPath . $deptId . '/';
  86. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  87. throw new \Exception('创建部门失败');
  88. }
  89. // 事务提交
  90. DB::commit();
  91. } catch (\Exception $e) {
  92. DB::rollBack();
  93. return json_fail('创建部门失败');
  94. }
  95. return json_success('部门创建成功');
  96. }
  97. /**
  98. * @Desc 修改部门
  99. * @Author Gorden
  100. * @Date 2024/2/21 10:15
  101. *
  102. * @param $id
  103. * @param $params
  104. * @return \support\Response
  105. */
  106. public static function updateDept($id, $params)
  107. {
  108. $dept = SysDept::where('dept_id', $id)->first();
  109. if (!$dept) {
  110. return json_fail('部门不存在');
  111. }
  112. // 上级部门是否变动
  113. if ($dept->dept_super_id != $params['dept_super_id']) {
  114. $deptSuperPath = SysDept::where('dept_id', $params['dept_super_id'])->value('dept_super_path');
  115. }
  116. Db::beginTransaction();
  117. try {
  118. $data = [
  119. 'dept_status' => $params['dept_status'],
  120. 'dept_category' => $params['dept_category'],
  121. 'dept_code' => $params['dept_code'],
  122. 'dept_city' => $params['dept_city'],
  123. 'dept_name' => $params['dept_name'],
  124. 'dept_telephone' => $params['dept_telephone'],
  125. 'dept_position' => $params['dept_position'] ?? '',
  126. 'dept_address' => $params['dept_address'] ?? '',
  127. 'dept_remark' => $params['dept_remark'] ?? '',
  128. 'dept_extend_json' => !empty($params['dept_extend_json']) ? $params['dept_extend_json'] : '{}',
  129. ];
  130. // 上级变动,更新
  131. if (isset($deptSuperPath)) {
  132. $data['dept_super_id'] = $params['dept_super_id'];
  133. $data['dept_super_path'] = $deptSuperPath . $id . '/';
  134. }
  135. // 修改失败,异常
  136. if (!SysDept::where('dept_id', $id)->update($data)) {
  137. throw new \Exception('修改部门失败');
  138. }
  139. if ($dept->dept_super_id != $params['dept_super_id']) {
  140. $subs = SysDept::getAllSubDept($dept->dept_super_path);
  141. if ($subs) {
  142. foreach ($subs as $sub) {
  143. SysDept::where('dept_id', $sub['dept_id'])->update(['dept_super_path' => str_replace($dept->dept_super_path, $data['dept_super_path'], $sub['dept_super_path'])]);
  144. }
  145. }
  146. }
  147. Db::commit();
  148. } catch (\Exception $e) {
  149. Db::rollBack();
  150. return json_fail('修改部门失败');
  151. }
  152. return json_success('修改部门成功');
  153. }
  154. /**
  155. * @Desc 修改部门状态
  156. * @Author Gorden
  157. * @Date 2024/2/21 10:41
  158. *
  159. * @param $id
  160. * @param $status
  161. * @return \support\Response
  162. */
  163. public static function updateStatus($id, $status)
  164. {
  165. try {
  166. if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
  167. throw new \Exception('部门状态修改失败');
  168. }
  169. } catch (\Exception $e) {
  170. return json_fail('部门状态修改失败');
  171. }
  172. return json_success('部门状态修改成功');
  173. }
  174. /**
  175. * @Desc 删除部门
  176. * @Author Gorden
  177. * @Date 2024/2/21 10:48
  178. *
  179. * @param $id
  180. * @return \support\Response
  181. */
  182. public static function delDept($id)
  183. {
  184. try {
  185. if (!SysDept::where('dept_id', $id)->delete()) {
  186. throw new \Exception('删除部门失败');
  187. }
  188. } catch (\Exception $e) {
  189. return json_fail('删除部门失败');
  190. }
  191. return json_success('删除部门成功');
  192. }
  193. /**
  194. * @Desc 检查部门是否存在
  195. * @Author Gorden
  196. * @Date 2024/2/21 13:37
  197. *
  198. * @param $deptId
  199. * @return bool
  200. */
  201. public static function checkDeptExist($deptId)
  202. {
  203. return SysDept::where('dept_id', $deptId)->exists();
  204. }
  205. }