DeptService.php 7.2 KB

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