123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- namespace app\admin\service\consultant;
- use app\model\Consultant;
- use app\model\SysDept;
- use app\model\SysUser;
- use support\Db;
- use support\exception\BusinessException;
- use support\Request;
- use Tinywan\Jwt\JwtToken;
- class TeamService
- {
- const DEPT_CATEGORY = '获客团队';
-
- public static function deptInfo($id)
- {
- $dept = SysDept::find($id);
- if (!$dept) {
- return json_fail('营销团队不存在');
- }
- $dept = $dept->toArray();
- return json_success('', $dept);
- }
-
- public static function insertDept($params)
- {
- Db::beginTransaction();
- try {
- $data = [
- 'dept_category' => self::DEPT_CATEGORY,
- 'dept_name' => $params['dept_name'],
- 'dept_super_id' => $params['dept_super_id'] ?? 0,
- 'dept_status' => $params['dept_status'] ?? 'ACTIVED',
- 'dept_sort' => $params['dept_sort'] ?? 1,
- 'dept_addtimes' => time()
- ];
- $where = [
- ['dept_category', '=', $data['dept_category']],
- ['dept_name', '=', $data['dept_name']],
- ['dept_super_id', '=', $data['dept_super_id']],
- ];
- $isExist = SysDept::where($where)->first();
- if ($isExist) {
- throw new BusinessException('团队已存在');
- }
- $deptId = SysDept::insertGetId($data);
- if (!$deptId) {
- throw new BusinessException('创建团队失败');
- }
-
- $deptSuperPath = '/0/';
- if ($params['dept_super_id'] != 0) {
- $deptSuperPath = SysDept::where('dept_id', $params['dept_super_id'])->value('dept_super_path');
- }
-
- $path = $deptSuperPath . $deptId . '/';
- if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
- throw new \Exception('创建团队失败');
- }
- Db::commit();
- } catch (BusinessException|\Exception $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- }
- return json_success('团队创建成功');
- }
-
- public static function updateDept($params)
- {
- $id = $params['id'];
- $dept = SysDept::where('dept_id', $id)->first();
- if (!$dept) {
- return json_fail('团队不存在');
- }
- try {
- $data = [
- 'dept_name' => $params['dept_name'],
- 'dept_sort' => $params['dept_sort'] ?? 0,
- 'dept_status' => $params['dept_status'] ?? 'ACTIVED',
- ];
- $where = [
- ['dept_category', '=', self::DEPT_CATEGORY],
- ['dept_name', '=', $data['dept_name']],
- ['dept_super_id', '=', $dept['dept_super_id']],
- ['dept_id', '<>', $id],
- ];
- $isExist = SysDept::where($where)->first();
- if ($isExist) {
- throw new BusinessException('团队已存在');
- }
-
- SysDept::where('dept_id', $id)->update($data);
- } catch (BusinessException|\Exception $e) {
- return json_fail($e->getMessage());
- }
- return json_success('修改团队成功');
- }
-
- public static function updateStatus($id, $status)
- {
- try {
- if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
- throw new \Exception('营销团队状态修改失败');
- }
- } catch (\Exception $e) {
- return json_fail('营销团队状态修改失败');
- }
- return json_success('营销团队状态修改成功');
- }
-
- public static function delDept(Request $request)
- {
- $ids = $request->post('dept_id');
- if (!$ids) {
- return json_fail("数据错误");
- }
- if (!is_array($ids)) {
- $ids = [$ids];
- }
-
- $nums = Consultant::whereIn('dept_id', $ids)->count();
- if ($nums > 0) {
- return json_fail('请清除团队下员工后删除团队');
- }
-
- $nums = SysUser::whereIn('join_user_dept_id', $ids)->count();
- if ($nums > 0) {
- return json_fail('请清除团队下管理账号后删除团队');
- }
- try {
- if (is_array($ids)) {
- SysDept::whereIn('dept_id', $ids)->delete();
- } else {
- SysDept::where('dept_id', $ids)->delete();
- }
- } catch (\Exception $e) {
- return json_fail('删除团队失败');
- }
- return json_success('删除团队成功');
- }
-
- public static function checkDeptExist($deptId)
- {
- return SysDept::where('dept_id', $deptId)->exists();
- }
-
- public static function parentList()
- {
- $deptIds = TeamService::getIdsByUser(1);
- $where = [
- ['dept_category', '=', self::DEPT_CATEGORY],
- ['dept_super_id', '=', 0]
- ];
- if (false === $deptIds) {
-
- $where[] = ['dept_id', '=', 0];
- } else if (is_array($deptIds)) {
-
- $where[] = [function($query) use ($deptIds) {
- $query->whereIn('dept_id', $deptIds);
- }];
- }
- $list = SysDept::where($where)->select(['dept_id','dept_name'])->get();
- $result = $list->toArray();
- $result[] = [
- 'dept_id' => 0,
- 'dept_name' => '顶级团队',
- ];
- return json_success('请求成功', $result);
- }
-
- public static function getKeys()
- {
- $data = self::getTeams();
- $keys = [];
- if (!empty($data)) {
- $array = $data->toArray();
- foreach ($array as $item) {
- $keys[$item['dept_id']] = $item['dept_name'];
- }
- }
- return $keys;
- }
-
- public static function getItemKeys()
- {
- $data = self::getTeams();
- $keys = [];
- if (!empty($data)) {
- foreach ($data->toArray() as $item) {
- $keys[$item['dept_id']] = $item;
- }
- }
- return $keys;
- }
-
- public static function getIds($id, $self = 0)
- {
- $where = [
- ['dept_category', '=', self::DEPT_CATEGORY]
- ];
- $deptInfo = SysDept::where('dept_id', $id)->where('dept_category', self::DEPT_CATEGORY)->first();
- $deptSuperPath = $deptInfo->dept_super_path;
- $where[] = ['dept_super_path', 'like', "{$deptSuperPath}%"];
- if ($self == 1) {
- $where[] = ['dept_id', '<>', $id];
- }
- return SysDept::where($where)->pluck('dept_id')->toArray();
- }
-
- public static function getIdsByUser($self = 0)
- {
- $userId = JwtToken::getCurrentId();
- $userInfo = SysUser::firstWhere(['user_id' => $userId]);
- $joinUserDeptId = $userInfo->join_user_dept_id;
-
- $deptInfo = SysDept::firstWhere(['dept_id' => $joinUserDeptId]);
- if (empty($deptInfo)) {
-
- return false;
- }
- if ($deptInfo->dept_category != self::DEPT_CATEGORY) {
-
- return true;
- }
- return self::getIds($joinUserDeptId, $self);
- }
-
- public static function getTeamName($keys, $id)
- {
- if (isset($keys[$id])) {
- $path = $keys[$id]['dept_super_path'];
- $array = explode('/', $path);
- array_shift($array);
- array_pop($array);
- $namesArr = [];
- foreach ($array as $item) {
- if (!empty($item)) {
- if (isset($keys[$item]['dept_name'])) {
- $namesArr[] = $keys[$item]['dept_name'];
- }
- }
- }
- $name = implode(' - ', $namesArr);
- return $name;
- }
- return '';
- }
- public static function getTeams()
- {
- $where = [
- ['dept_category', '=', self::DEPT_CATEGORY]
- ];
- $data = SysDept::where($where)->select(['dept_id', 'dept_super_id', 'dept_super_path', 'dept_name'])->get();
- return $data;
- }
- }
|