TeamService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\admin\service\consultant;
  3. use app\model\Consultant;
  4. use app\model\SysDept;
  5. use app\model\SysUser;
  6. use support\Db;
  7. use support\exception\BusinessException;
  8. use support\Request;
  9. class TeamService
  10. {
  11. const DEPT_CATEGORY = '获客团队';
  12. /**
  13. * @Desc 营销团队详情
  14. * @Author Gorden
  15. * @Date 2024/2/21 10:35
  16. *
  17. * @param $id
  18. * @return \support\Response
  19. */
  20. public static function deptInfo($id)
  21. {
  22. $dept = SysDept::find($id);
  23. if (!$dept) {
  24. return json_fail('营销团队不存在');
  25. }
  26. $dept = $dept->toArray();
  27. return json_success('', $dept);
  28. }
  29. /**
  30. * @Desc 创建营销团队
  31. * @Author Gorden
  32. * @Date 2024/2/21 9:22
  33. *
  34. * @param $params
  35. * @return \support\Response
  36. */
  37. public static function insertDept($params)
  38. {
  39. Db::beginTransaction();
  40. try {
  41. $data = [
  42. 'dept_category' => self::DEPT_CATEGORY,
  43. 'dept_name' => $params['dept_name'],
  44. 'dept_super_id' => $params['dept_super_id'] ?? 0,
  45. 'dept_status' => $params['dept_status'] ?? 'ACTIVED',
  46. 'dept_sort' => $params['dept_sort'] ?? 1,
  47. 'dept_addtimes' => time()
  48. ];
  49. $where = [
  50. ['dept_category', '=', $data['dept_category']],
  51. ['dept_name', '=', $data['dept_name']],
  52. ['dept_super_id', '=', $data['dept_super_id']],
  53. ];
  54. $isExist = SysDept::where($where)->first();
  55. if ($isExist) {
  56. throw new BusinessException('团队已存在');
  57. }
  58. $deptId = SysDept::insertGetId($data);
  59. if (!$deptId) {
  60. throw new BusinessException('创建团队失败');
  61. }
  62. // 获取上级部门path
  63. $deptSuperPath = '/0/';
  64. if ($params['dept_super_id'] != 0) {
  65. $deptSuperPath = SysDept::where('dept_id', $params['dept_super_id'])->value('dept_super_path');
  66. }
  67. // 更新部门path
  68. $path = $deptSuperPath . $deptId . '/';
  69. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  70. throw new \Exception('创建团队失败');
  71. }
  72. Db::commit();
  73. } catch (BusinessException|\Exception $e) {
  74. Db::rollBack();
  75. return json_fail($e->getMessage());
  76. }
  77. return json_success('团队创建成功');
  78. }
  79. /**
  80. * @Desc 修改营销团队
  81. * @Author Gorden
  82. * @Date 2024/2/21 10:15
  83. *
  84. * @param $id
  85. * @param $params
  86. * @return \support\Response
  87. */
  88. public static function updateDept($params)
  89. {
  90. $id = $params['id'];
  91. $dept = SysDept::where('dept_id', $id)->first();
  92. if (!$dept) {
  93. return json_fail('团队不存在');
  94. }
  95. try {
  96. $data = [
  97. 'dept_name' => $params['dept_name'],
  98. 'dept_sort' => $params['dept_sort'] ?? 0,
  99. 'dept_status' => $params['dept_status'] ?? 'ACTIVED',
  100. ];
  101. $where = [
  102. ['dept_category', '=', self::DEPT_CATEGORY],
  103. ['dept_name', '=', $data['dept_name']],
  104. ['dept_super_id', '=', $dept['dept_super_id']],
  105. ['dept_id', '<>', $id],
  106. ];
  107. $isExist = SysDept::where($where)->first();
  108. if ($isExist) {
  109. throw new BusinessException('团队已存在');
  110. }
  111. // 修改失败,异常
  112. SysDept::where('dept_id', $id)->update($data);
  113. } catch (BusinessException|\Exception $e) {
  114. return json_fail($e->getMessage());
  115. }
  116. return json_success('修改团队成功');
  117. }
  118. /**
  119. * @Desc 修改营销团队状态
  120. * @Author Gorden
  121. * @Date 2024/2/21 10:41
  122. *
  123. * @param $id
  124. * @param $status
  125. * @return \support\Response
  126. */
  127. public static function updateStatus($id, $status)
  128. {
  129. try {
  130. if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
  131. throw new \Exception('营销团队状态修改失败');
  132. }
  133. } catch (\Exception $e) {
  134. return json_fail('营销团队状态修改失败');
  135. }
  136. return json_success('营销团队状态修改成功');
  137. }
  138. /**
  139. * @Desc 删除营销团队
  140. * @Author Gorden
  141. * @Date 2024/2/21 10:48
  142. *
  143. * @param $id
  144. * @return \support\Response
  145. */
  146. public static function delDept(Request $request)
  147. {
  148. $ids = $request->post('dept_id');
  149. if (!$ids) {
  150. return json_fail("数据错误");
  151. }
  152. if (!is_array($ids)) {
  153. $ids = [$ids];
  154. }
  155. //查询团队下是否存在员工
  156. $nums = Consultant::whereIn('dept_id', $ids)->count();
  157. if ($nums > 0) {
  158. return json_fail('请清除团队下员工后删除团队');
  159. }
  160. //查询团队下是否存在管理账号
  161. $nums = SysUser::whereIn('join_user_dept_id', $ids)->count();
  162. if ($nums > 0) {
  163. return json_fail('请清除团队下管理账号后删除团队');
  164. }
  165. try {
  166. if (is_array($ids)) {
  167. SysDept::whereIn('dept_id', $ids)->delete();
  168. } else {
  169. SysDept::where('dept_id', $ids)->delete();
  170. }
  171. } catch (\Exception $e) {
  172. return json_fail('删除团队失败');
  173. }
  174. return json_success('删除团队成功');
  175. }
  176. /**
  177. * @Desc 检查营销团队是否存在
  178. * @Author Gorden
  179. * @Date 2024/2/21 13:37
  180. *
  181. * @param $deptId
  182. * @return bool
  183. */
  184. public static function checkDeptExist($deptId)
  185. {
  186. return SysDept::where('dept_id', $deptId)->exists();
  187. }
  188. /**
  189. * Notes: 上级团队列表
  190. * User: yb
  191. * Date: 2024/8/1
  192. * Time: 17:17
  193. */
  194. public static function parentList()
  195. {
  196. $list = SysDept::where(['dept_category' => self::DEPT_CATEGORY, 'dept_super_id' => 0])->select(['dept_id','dept_name'])->get();
  197. $result = $list->toArray();
  198. $result[] = [
  199. 'dept_id' => 0,
  200. 'dept_name' => '顶级团队',
  201. ];
  202. return json_success('请求成功', $result);
  203. }
  204. /**
  205. * Notes: 获取团队信息
  206. * User: yb
  207. * Date: 2024/8/2
  208. * Time: 10:00
  209. * @return array
  210. */
  211. public static function getKeys()
  212. {
  213. $where = [
  214. ['dept_category', '=', self::DEPT_CATEGORY]
  215. ];
  216. $data = SysDept::where($where)->select(['dept_id', 'dept_super_id', 'dept_super_path', 'dept_name'])->get();
  217. $keys = [];
  218. if (!empty($data)) {
  219. $array = $data->toArray();
  220. foreach ($array as $item) {
  221. $keys[$item['dept_id']] = $item['dept_name'];
  222. }
  223. }
  224. return $keys;
  225. }
  226. }