DeptService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. try {
  117. $data = [
  118. 'dept_status' => $params['dept_status'],
  119. 'dept_category' => $params['dept_category'],
  120. 'dept_code' => $params['dept_code'],
  121. 'dept_city' => $params['dept_city'],
  122. 'dept_name' => $params['dept_name'],
  123. 'dept_telephone' => $params['dept_telephone'],
  124. 'dept_position' => $params['dept_position'],
  125. 'dept_address' => $params['dept_address'],
  126. 'dept_remark' => $params['dept_remark'],
  127. 'dept_extend_json' => !empty($params['dept_extend_json']) ? $params['dept_extend_json'] : '{}',
  128. ];
  129. // 上级变动,更新
  130. if (isset($deptSuperPath)) {
  131. $data['dept_super_id'] = $params['dept_super_id'];
  132. $data['dept_super_path'] = $deptSuperPath . $id . '/';
  133. }
  134. // 修改失败,异常
  135. if (!SysDept::where('dept_id', $id)->update($data)) {
  136. throw new \Exception('修改部门失败');
  137. }
  138. } catch (\Exception $e) {
  139. return json_fail('修改部门失败');
  140. }
  141. return json_success('修改部门成功');
  142. }
  143. /**
  144. * @Desc 修改部门状态
  145. * @Author Gorden
  146. * @Date 2024/2/21 10:41
  147. *
  148. * @param $id
  149. * @param $status
  150. * @return \support\Response
  151. */
  152. public static function updateStatus($id, $status)
  153. {
  154. try {
  155. if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
  156. throw new \Exception('部门状态修改失败');
  157. }
  158. } catch (\Exception $e) {
  159. return json_fail('部门状态修改失败');
  160. }
  161. return json_success('部门状态修改成功');
  162. }
  163. /**
  164. * @Desc 删除部门
  165. * @Author Gorden
  166. * @Date 2024/2/21 10:48
  167. *
  168. * @param $id
  169. * @return \support\Response
  170. */
  171. public static function delDept($id)
  172. {
  173. try {
  174. if (!SysDept::where('dept_id', $id)->delete()) {
  175. throw new \Exception('删除部门失败');
  176. }
  177. } catch (\Exception $e) {
  178. return json_fail('删除部门失败');
  179. }
  180. return json_success('删除部门成功');
  181. }
  182. /**
  183. * @Desc 检查部门是否存在
  184. * @Author Gorden
  185. * @Date 2024/2/21 13:37
  186. *
  187. * @param $deptId
  188. * @return bool
  189. */
  190. public static function checkDeptExist($deptId)
  191. {
  192. return SysDept::where('dept_id', $deptId)->exists();
  193. }
  194. }