TeamService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. use Tinywan\Jwt\JwtToken;
  10. class TeamService
  11. {
  12. const DEPT_CATEGORY = '获客团队';
  13. /**
  14. * @Desc 营销团队详情
  15. * @Author Gorden
  16. * @Date 2024/2/21 10:35
  17. *
  18. * @param $id
  19. * @return \support\Response
  20. */
  21. public static function deptInfo($id)
  22. {
  23. $dept = SysDept::find($id);
  24. if (!$dept) {
  25. return json_fail('营销团队不存在');
  26. }
  27. $dept = $dept->toArray();
  28. return json_success('', $dept);
  29. }
  30. /**
  31. * @Desc 创建营销团队
  32. * @Author Gorden
  33. * @Date 2024/2/21 9:22
  34. *
  35. * @param $params
  36. * @return \support\Response
  37. */
  38. public static function insertDept($params)
  39. {
  40. Db::beginTransaction();
  41. try {
  42. $data = [
  43. 'dept_category' => self::DEPT_CATEGORY,
  44. 'dept_name' => $params['dept_name'],
  45. 'dept_super_id' => $params['dept_super_id'] ?? 0,
  46. 'dept_status' => $params['dept_status'] ?? 'ACTIVED',
  47. 'dept_sort' => $params['dept_sort'] ?? 1,
  48. 'dept_addtimes' => time()
  49. ];
  50. $where = [
  51. ['dept_category', '=', $data['dept_category']],
  52. ['dept_name', '=', $data['dept_name']],
  53. ['dept_super_id', '=', $data['dept_super_id']],
  54. ];
  55. $isExist = SysDept::where($where)->first();
  56. if ($isExist) {
  57. throw new BusinessException('团队已存在');
  58. }
  59. $deptId = SysDept::insertGetId($data);
  60. if (!$deptId) {
  61. throw new BusinessException('创建团队失败');
  62. }
  63. // 获取上级部门path
  64. $deptSuperPath = '/0/';
  65. if ($params['dept_super_id'] != 0) {
  66. $deptSuperPath = SysDept::where('dept_id', $params['dept_super_id'])->value('dept_super_path');
  67. }
  68. // 更新部门path
  69. $path = $deptSuperPath . $deptId . '/';
  70. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  71. throw new \Exception('创建团队失败');
  72. }
  73. Db::commit();
  74. } catch (BusinessException|\Exception $e) {
  75. Db::rollBack();
  76. return json_fail($e->getMessage());
  77. }
  78. return json_success('团队创建成功');
  79. }
  80. /**
  81. * @Desc 修改营销团队
  82. * @Author Gorden
  83. * @Date 2024/2/21 10:15
  84. *
  85. * @param $id
  86. * @param $params
  87. * @return \support\Response
  88. */
  89. public static function updateDept($params)
  90. {
  91. $id = $params['id'];
  92. $dept = SysDept::where('dept_id', $id)->first();
  93. if (!$dept) {
  94. return json_fail('团队不存在');
  95. }
  96. try {
  97. $data = [
  98. 'dept_name' => $params['dept_name'],
  99. 'dept_sort' => $params['dept_sort'] ?? 0,
  100. 'dept_status' => $params['dept_status'] ?? 'ACTIVED',
  101. ];
  102. $where = [
  103. ['dept_category', '=', self::DEPT_CATEGORY],
  104. ['dept_name', '=', $data['dept_name']],
  105. ['dept_super_id', '=', $dept['dept_super_id']],
  106. ['dept_id', '<>', $id],
  107. ];
  108. $isExist = SysDept::where($where)->first();
  109. if ($isExist) {
  110. throw new BusinessException('团队已存在');
  111. }
  112. // 修改失败,异常
  113. SysDept::where('dept_id', $id)->update($data);
  114. } catch (BusinessException|\Exception $e) {
  115. return json_fail($e->getMessage());
  116. }
  117. return json_success('修改团队成功');
  118. }
  119. /**
  120. * @Desc 修改营销团队状态
  121. * @Author Gorden
  122. * @Date 2024/2/21 10:41
  123. *
  124. * @param $id
  125. * @param $status
  126. * @return \support\Response
  127. */
  128. public static function updateStatus($id, $status)
  129. {
  130. try {
  131. if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
  132. throw new \Exception('营销团队状态修改失败');
  133. }
  134. } catch (\Exception $e) {
  135. return json_fail('营销团队状态修改失败');
  136. }
  137. return json_success('营销团队状态修改成功');
  138. }
  139. /**
  140. * @Desc 删除营销团队
  141. * @Author Gorden
  142. * @Date 2024/2/21 10:48
  143. *
  144. * @param $id
  145. * @return \support\Response
  146. */
  147. public static function delDept(Request $request)
  148. {
  149. $ids = $request->post('dept_id');
  150. if (!$ids) {
  151. return json_fail("数据错误");
  152. }
  153. if (!is_array($ids)) {
  154. $ids = [$ids];
  155. }
  156. //查询团队下是否存在员工
  157. $nums = Consultant::whereIn('dept_id', $ids)->count();
  158. if ($nums > 0) {
  159. return json_fail('请清除团队下员工后删除团队');
  160. }
  161. //查询团队下是否存在管理账号
  162. $nums = SysUser::whereIn('join_user_dept_id', $ids)->count();
  163. if ($nums > 0) {
  164. return json_fail('请清除团队下管理账号后删除团队');
  165. }
  166. try {
  167. if (is_array($ids)) {
  168. SysDept::whereIn('dept_id', $ids)->delete();
  169. } else {
  170. SysDept::where('dept_id', $ids)->delete();
  171. }
  172. } catch (\Exception $e) {
  173. return json_fail('删除团队失败');
  174. }
  175. return json_success('删除团队成功');
  176. }
  177. /**
  178. * @Desc 检查营销团队是否存在
  179. * @Author Gorden
  180. * @Date 2024/2/21 13:37
  181. *
  182. * @param $deptId
  183. * @return bool
  184. */
  185. public static function checkDeptExist($deptId)
  186. {
  187. return SysDept::where('dept_id', $deptId)->exists();
  188. }
  189. /**
  190. * Notes: 上级团队列表
  191. * User: yb
  192. * Date: 2024/8/1
  193. * Time: 17:17
  194. */
  195. public static function parentList()
  196. {
  197. $deptIds = TeamService::getIdsByUser(1);
  198. $where = [
  199. ['dept_category', '=', self::DEPT_CATEGORY],
  200. ['dept_super_id', '=', 0]
  201. ];
  202. if (false === $deptIds) {
  203. //无权限
  204. $where[] = ['dept_id', '=', 0];
  205. } else if (is_array($deptIds)) {
  206. //指定团队下的权限
  207. $where[] = [function($query) use ($deptIds) {
  208. $query->whereIn('dept_id', $deptIds);
  209. }];
  210. }
  211. $list = SysDept::where($where)->select(['dept_id','dept_name'])->get();
  212. $result = $list->toArray();
  213. $result[] = [
  214. 'dept_id' => 0,
  215. 'dept_name' => '顶级团队',
  216. ];
  217. return json_success('请求成功', $result);
  218. }
  219. /**
  220. * Notes: 获取团队信息 只有名字
  221. * User: yb
  222. * Date: 2024/8/2
  223. * Time: 10:00
  224. * @return array
  225. */
  226. public static function getKeys()
  227. {
  228. $data = self::getTeams();
  229. $keys = [];
  230. if (!empty($data)) {
  231. $array = $data->toArray();
  232. foreach ($array as $item) {
  233. $keys[$item['dept_id']] = $item['dept_name'];
  234. }
  235. }
  236. return $keys;
  237. }
  238. /**
  239. * Notes: 获取团队信息包含整个item
  240. * User: yb
  241. * Date: 2024/8/9
  242. * Time: 20:31
  243. */
  244. public static function getItemKeys()
  245. {
  246. $data = self::getTeams();
  247. $keys = [];
  248. if (!empty($data)) {
  249. foreach ($data->toArray() as $item) {
  250. $keys[$item['dept_id']] = $item;
  251. }
  252. }
  253. return $keys;
  254. }
  255. /**
  256. * Notes: 根据id获取所有的子集
  257. * User: yb
  258. * Date: 2024/8/12
  259. * Time: 11:34
  260. * @param $id
  261. */
  262. public static function getIds($id, $self = 0)
  263. {
  264. $where = [
  265. ['dept_category', '=', self::DEPT_CATEGORY]
  266. ];
  267. $deptInfo = SysDept::where('dept_id', $id)->where('dept_category', self::DEPT_CATEGORY)->first();
  268. $deptSuperPath = $deptInfo->dept_super_path;
  269. $where[] = ['dept_super_path', 'like', "{$deptSuperPath}%"];
  270. if ($self == 1) {
  271. $where[] = ['dept_id', '<>', $id];
  272. }
  273. return SysDept::where($where)->pluck('dept_id')->toArray();
  274. }
  275. /**
  276. * Notes: 通过用户获取所有子集
  277. * User: yb
  278. * Date: 2024/8/16
  279. * Time: 16:48
  280. */
  281. public static function getIdsByUser($self = 0)
  282. {
  283. $userId = JwtToken::getCurrentId();
  284. $userInfo = SysUser::firstWhere(['user_id' => $userId]);
  285. $joinUserDeptId = $userInfo->join_user_dept_id;
  286. //获取部门信息
  287. $deptInfo = SysDept::firstWhere(['dept_id' => $joinUserDeptId]);
  288. if (empty($deptInfo)) {
  289. //无部门信息
  290. return false;
  291. }
  292. if ($deptInfo->dept_category != self::DEPT_CATEGORY) {
  293. //超管权限
  294. return true;
  295. }
  296. return self::getIds($joinUserDeptId, $self);
  297. }
  298. /**
  299. * Notes: 获取团队完整名称包含所有父集
  300. * User: yb
  301. * Date: 2024/8/9
  302. * Time: 20:43
  303. * @param $keys
  304. * @param $id
  305. * @return string
  306. */
  307. public static function getTeamName($keys, $id)
  308. {
  309. if (isset($keys[$id])) {
  310. $path = $keys[$id]['dept_super_path'];
  311. $array = explode('/', $path);
  312. array_shift($array);
  313. array_pop($array);
  314. $namesArr = [];
  315. foreach ($array as $item) {
  316. if (!empty($item)) {
  317. if (isset($keys[$item]['dept_name'])) {
  318. $namesArr[] = $keys[$item]['dept_name'];
  319. }
  320. }
  321. }
  322. $name = implode(' - ', $namesArr);
  323. return $name;
  324. }
  325. return '';
  326. }
  327. public static function getTeams()
  328. {
  329. $where = [
  330. ['dept_category', '=', self::DEPT_CATEGORY]
  331. ];
  332. $data = SysDept::where($where)->select(['dept_id', 'dept_super_id', 'dept_super_path', 'dept_name'])->get();
  333. return $data;
  334. }
  335. }