return userService.getListByDeptId(deptId); | return userService.getListByDeptId(deptId); | ||||
} | } | ||||
/** | |||||
* 初始化系统超级管理员用户 | |||||
* | |||||
* @param username | |||||
* @return | |||||
*/ | |||||
@PostMapping("/init/{username}") | |||||
public JsonResult init(@PathVariable("username") String username) { | |||||
return userService.init(username); | |||||
} | |||||
} | } |
package com.tuoheng.admin.service.dept.add; | package com.tuoheng.admin.service.dept.add; | ||||
import cn.hutool.core.util.ObjectUtil; | |||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||||
import com.tuoheng.admin.entity.*; | import com.tuoheng.admin.entity.*; | ||||
import com.tuoheng.admin.enums.code.dept.AddDeptCodeEnum; | import com.tuoheng.admin.enums.code.dept.AddDeptCodeEnum; | ||||
import com.tuoheng.admin.mapper.DeptMapper; | import com.tuoheng.admin.mapper.DeptMapper; | ||||
import com.tuoheng.admin.mapper.RoadDeptMapper; | import com.tuoheng.admin.mapper.RoadDeptMapper; | ||||
import com.tuoheng.admin.mapper.SectionDeptMapper; | import com.tuoheng.admin.mapper.SectionDeptMapper; | ||||
import com.tuoheng.admin.mapper.UserMapper; | |||||
import com.tuoheng.admin.request.dept.AddDeptRequest; | import com.tuoheng.admin.request.dept.AddDeptRequest; | ||||
import com.tuoheng.admin.dto.RoadSectionDto; | import com.tuoheng.admin.dto.RoadSectionDto; | ||||
import com.tuoheng.admin.utils.CurrentUserUtil; | import com.tuoheng.admin.utils.CurrentUserUtil; | ||||
@Autowired | @Autowired | ||||
private DeptMapper deptMapper; | private DeptMapper deptMapper; | ||||
@Autowired | |||||
private UserMapper userMapper; | |||||
@Autowired | @Autowired | ||||
private RoadDeptMapper roadDeptMapper; | private RoadDeptMapper roadDeptMapper; | ||||
dept.setTenantId(tenantId); | dept.setTenantId(tenantId); | ||||
dept.setName(addDeptRequest.getName()); | dept.setName(addDeptRequest.getName()); | ||||
dept.setPid(addDeptRequest.getPid()); | dept.setPid(addDeptRequest.getPid()); | ||||
// dept.setCreateUser(ShiroUtils.getUserId()); | |||||
dept.setCreateUser(CurrentUserUtil.getUserId()); | |||||
dept.setCreateTime(DateUtils.now()); | dept.setCreateTime(DateUtils.now()); | ||||
Integer rowId = deptMapper.insert(dept); | Integer rowId = deptMapper.insert(dept); | ||||
log.info("新增部门, 返回结果: deptId={}", dept.getId()); | log.info("新增部门, 返回结果: deptId={}", dept.getId()); | ||||
return JsonResult.error(AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getCode(), AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getMsg()); | return JsonResult.error(AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getCode(), AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getMsg()); | ||||
} | } | ||||
this.updateSuperAdminUser(dept); | |||||
// 新增公路/路段与部门数据 | // 新增公路/路段与部门数据 | ||||
addRoadAndSectionToDept(tenantId, dept.getId(), addDeptRequest.getRoadSectionDtoList()); | addRoadAndSectionToDept(tenantId, dept.getId(), addDeptRequest.getRoadSectionDtoList()); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* 如果创建的是顶级部门,将该部门与超级管理员关联 | |||||
*/ | |||||
private void updateSuperAdminUser(Dept dept) { | |||||
if (!"0".equals(dept.getPid())) { | |||||
return; | |||||
} | |||||
User user = userMapper.selectOne(new LambdaQueryWrapper<User>() | |||||
.eq(User::getRoleId, 1) | |||||
.eq(User::getMark, 1)); | |||||
if (ObjectUtil.isNull(user)) { | |||||
return; | |||||
} | |||||
user.setDeptId(dept.getId()); | |||||
userMapper.updateById(user); | |||||
} | |||||
} | } |
dept.setTenantId(tenantId); | dept.setTenantId(tenantId); | ||||
dept.setName(newEditDeptRequest.getName()); | dept.setName(newEditDeptRequest.getName()); | ||||
dept.setFullname(newEditDeptRequest.getName()); | dept.setFullname(newEditDeptRequest.getName()); | ||||
// dept.setUpdateUser(ShiroUtils.getUserId()); | |||||
dept.setUpdateUser(CurrentUserUtil.getUserId()); | |||||
dept.setUpdateTime(DateUtils.now()); | dept.setUpdateTime(DateUtils.now()); | ||||
Integer rowId = deptMapper.update(dept); | Integer rowId = deptMapper.update(dept); |
*/ | */ | ||||
JsonResult getListByDeptId(String deptId); | JsonResult getListByDeptId(String deptId); | ||||
/** | |||||
* 初始化系统超级管理员用户 | |||||
*/ | |||||
JsonResult init(String username); | |||||
} | } |
import com.tuoheng.admin.request.user.UserResetPwdRequest; | import com.tuoheng.admin.request.user.UserResetPwdRequest; | ||||
import com.tuoheng.admin.service.user.add.AddUserService; | import com.tuoheng.admin.service.user.add.AddUserService; | ||||
import com.tuoheng.admin.service.user.delete.DeleteUserService; | import com.tuoheng.admin.service.user.delete.DeleteUserService; | ||||
import com.tuoheng.admin.service.user.init.SystemUserInitService; | |||||
import com.tuoheng.admin.service.user.query.QueryUserInfoByIdService; | import com.tuoheng.admin.service.user.query.QueryUserInfoByIdService; | ||||
import com.tuoheng.admin.service.user.query.QueryUserListByDeptIdService; | import com.tuoheng.admin.service.user.query.QueryUserListByDeptIdService; | ||||
import com.tuoheng.admin.service.user.query.QueryUserPageListService; | import com.tuoheng.admin.service.user.query.QueryUserPageListService; | ||||
@Autowired | @Autowired | ||||
private QueryUserInfoByIdService queryUserInfoByIdService; | private QueryUserInfoByIdService queryUserInfoByIdService; | ||||
@Autowired | |||||
private SystemUserInitService systemUserInitService; | |||||
/** | /** | ||||
* 获取用户分页列表 | * 获取用户分页列表 | ||||
* | * | ||||
return queryUserListByDeptIdService.getListByDeptId(deptId); | return queryUserListByDeptIdService.getListByDeptId(deptId); | ||||
} | } | ||||
/** | |||||
* 初始化系统超级管理员用户 | |||||
*/ | |||||
@Override | |||||
public JsonResult init(String username) { | |||||
return systemUserInitService.init(username); | |||||
} | |||||
} | } |
package com.tuoheng.admin.service.user.init; | |||||
import cn.hutool.core.util.ObjectUtil; | |||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |||||
import com.tuoheng.admin.constant.SystemConstant; | |||||
import com.tuoheng.admin.entity.Dept; | |||||
import com.tuoheng.admin.entity.Role; | |||||
import com.tuoheng.admin.entity.Tenant; | |||||
import com.tuoheng.admin.entity.User; | |||||
import com.tuoheng.admin.enums.code.user.AddUserCodeEnum; | |||||
import com.tuoheng.admin.mapper.DeptMapper; | |||||
import com.tuoheng.admin.mapper.RoleMapper; | |||||
import com.tuoheng.admin.mapper.TenantMapper; | |||||
import com.tuoheng.admin.mapper.UserMapper; | |||||
import com.tuoheng.admin.service.third.oidc.OidcService; | |||||
import com.tuoheng.admin.utils.CurrentUserUtil; | |||||
import com.tuoheng.common.core.config.common.CommonConfig; | |||||
import com.tuoheng.common.core.utils.CommonUtils; | |||||
import com.tuoheng.common.core.utils.DateUtils; | |||||
import com.tuoheng.common.core.utils.JsonResult; | |||||
import com.tuoheng.common.core.utils.StringUtils; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.stereotype.Service; | |||||
/** | |||||
* 初始化系统用户业务层处理 | |||||
* | |||||
* @author wanjing | |||||
* @team tuoheng | |||||
* @date 2022-12-29 | |||||
*/ | |||||
@Slf4j | |||||
@Service | |||||
public class SystemUserInitService { | |||||
@Autowired | |||||
private UserMapper userMapper; | |||||
@Autowired | |||||
private RoleMapper roleMapper; | |||||
@Autowired | |||||
private TenantMapper tenantMapper; | |||||
@Autowired | |||||
private OidcService oidcService; | |||||
/** | |||||
* 添加用户数据 | |||||
* | |||||
* @return | |||||
*/ | |||||
public JsonResult init(String username) { | |||||
log.info("进入添加用户业务接口"); | |||||
Tenant tenant = tenantMapper.selectOne(new LambdaQueryWrapper<Tenant>() | |||||
.eq(Tenant::getMark, 1)); | |||||
String tenantId = ""; | |||||
if (ObjectUtil.isNotNull(tenant)) { | |||||
tenantId = tenant.getId(); | |||||
} | |||||
User user = this.buildUser(username, tenantId); | |||||
JsonResult result = this.check(user, tenantId); | |||||
if (0 != result.getCode()) { | |||||
log.info("初始化系统用户业务接口:校验失败:{}", result.getMsg()); | |||||
return result; | |||||
} | |||||
// 添加oidc | |||||
result = oidcService.addUser(user, tenant); | |||||
if (0 != result.getCode()) { | |||||
log.info("初始化系统用户业务接口:添加oidc数据失败:{}", result.getMsg()); | |||||
return result; | |||||
} | |||||
Integer rowCount = userMapper.insert(user); | |||||
if (rowCount <= 0) { | |||||
return JsonResult.error(400, "初始化系统用户失败"); | |||||
} | |||||
return JsonResult.success(); | |||||
} | |||||
/** | |||||
* | |||||
* 构建实体字段数据 | |||||
* | |||||
*/ | |||||
private User buildUser(String username, String tenantId) { | |||||
if (StringUtils.isEmpty(username)) { | |||||
username = "gs"; | |||||
} | |||||
User user = new User(); | |||||
user.setCode("gs"); | |||||
user.setUsername(username); | |||||
user.setRealname("超级管理员"); | |||||
user.setNickname("超级管理员"); | |||||
user.setCityName(""); | |||||
user.setPassword(CommonUtils.password(SystemConstant.INIT_PASSWORD)); | |||||
user.setAvatar("imagedir/n1o1j9ndau_1670488066287.png"); | |||||
user.setClientId("1,2"); | |||||
user.setRoleId(1); | |||||
user.setDeptId(""); | |||||
user.setStatus(1); | |||||
user.setTenantId(tenantId); | |||||
user.setCreateUser(CurrentUserUtil.getUserId()); | |||||
user.setCreateTime(DateUtils.now()); | |||||
return user; | |||||
} | |||||
/** | |||||
* 检查参数 | |||||
* | |||||
* @param user | |||||
* @return | |||||
*/ | |||||
private JsonResult check(User user, String tenantId) { | |||||
Integer count = userMapper.selectCount(new LambdaQueryWrapper<User>() | |||||
.eq(User::getTenantId, tenantId) | |||||
.eq(User::getRoleId, 1) | |||||
.eq(User::getMark, 1)); | |||||
if (count > 0) { | |||||
return JsonResult.error(400, "系统中已经存在超级管理员"); | |||||
} | |||||
count = userMapper.selectCount(new LambdaQueryWrapper<User>() | |||||
.eq(User::getTenantId, tenantId) | |||||
.eq(User::getUsername, user.getUsername()) | |||||
.eq(User::getMark, 1)); | |||||
if (count > 0) { | |||||
return JsonResult.error(400, "系统中已经存在该用户名"); | |||||
} | |||||
count = roleMapper.selectCount(new LambdaQueryWrapper<Role>() | |||||
.eq(Role::getTenantId, tenantId) | |||||
.eq(Role::getId, 1) | |||||
.eq(Role::getMark, 1)); | |||||
if (count <= 0) { | |||||
return JsonResult.error(400, "该角色不存在"); | |||||
} | |||||
return JsonResult.success(); | |||||
} | |||||
} |