@@ -2,6 +2,7 @@ package com.tuoheng.admin.controller; | |||
import com.tuoheng.admin.entity.User; | |||
import com.tuoheng.admin.request.user.QueryUserPageListRequest; | |||
import com.tuoheng.admin.request.user.UserInitRequest; | |||
import com.tuoheng.admin.request.user.UserResetPwdRequest; | |||
import com.tuoheng.admin.service.user.IUserService; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
@@ -115,14 +116,14 @@ public class UserController { | |||
} | |||
/** | |||
* 初始化系统超级管理员用户 | |||
* 初始化系统用户 | |||
* | |||
* @param username | |||
* @param request | |||
* @return | |||
*/ | |||
@PostMapping("/init/{username}/{password}") | |||
public JsonResult init(@PathVariable("username") String username, @PathVariable("password") String password) { | |||
return userService.init(username, password); | |||
@PostMapping("/init") | |||
public JsonResult init(@RequestBody UserInitRequest request) { | |||
return userService.init(request); | |||
} | |||
} |
@@ -9,6 +9,7 @@ import com.tuoheng.admin.request.inspectionfile.QueryInspectionFilePageListReque | |||
import com.tuoheng.admin.request.user.QueryUserPageListRequest; | |||
import org.apache.ibatis.annotations.Param; | |||
import java.util.List; | |||
import java.util.Map; | |||
/** | |||
@@ -22,6 +23,8 @@ public interface UserMapper extends BaseMapper<User> { | |||
Integer updateByIdList(Map<String, Object> map); | |||
Integer updateDeptIdByIdList(Map<String, Object> map); | |||
/** | |||
* 查询任务问题分页列表 | |||
* |
@@ -0,0 +1,36 @@ | |||
package com.tuoheng.admin.request.user; | |||
import lombok.Data; | |||
/** | |||
* 重置密码 | |||
*/ | |||
@Data | |||
public class UserInitRequest { | |||
/** | |||
* 名称 | |||
*/ | |||
private String name; | |||
/** | |||
* 密码 | |||
*/ | |||
private String password; | |||
/** | |||
* 租户编号 | |||
*/ | |||
private String tenantCode; | |||
/** | |||
* 是否租户:0(否);1(是) | |||
*/ | |||
private Integer tenantFlag; | |||
/** | |||
* 机场平台url | |||
*/ | |||
private String airportUrl; | |||
} |
@@ -1,7 +1,8 @@ | |||
package com.tuoheng.admin.service.dept.add; | |||
import cn.hutool.core.util.ObjectUtil; | |||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |||
import com.tuoheng.admin.constant.SystemConstant; | |||
import com.tuoheng.admin.dto.RoadSectionDto; | |||
import com.tuoheng.admin.entity.*; | |||
import com.tuoheng.admin.enums.code.dept.AddDeptCodeEnum; | |||
import com.tuoheng.admin.mapper.DeptMapper; | |||
@@ -9,7 +10,6 @@ import com.tuoheng.admin.mapper.RoadDeptMapper; | |||
import com.tuoheng.admin.mapper.SectionDeptMapper; | |||
import com.tuoheng.admin.mapper.UserMapper; | |||
import com.tuoheng.admin.request.dept.AddDeptRequest; | |||
import com.tuoheng.admin.dto.RoadSectionDto; | |||
import com.tuoheng.admin.utils.CurrentUserUtil; | |||
import com.tuoheng.common.core.utils.DateUtils; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
@@ -55,6 +55,7 @@ public class AddDeptService { | |||
*/ | |||
public JsonResult add(AddDeptRequest addDeptRequest) { | |||
log.info("进入添加部门业务接口"); | |||
String userId = CurrentUserUtil.getUserId(); | |||
String tenantId = CurrentUserUtil.getTenantId(); | |||
JsonResult result = this.check(tenantId, addDeptRequest); | |||
if (0 != result.getCode()) { | |||
@@ -76,7 +77,7 @@ public class AddDeptService { | |||
return JsonResult.error(AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getCode(), AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getMsg()); | |||
} | |||
this.updateSuperAdminUser(dept); | |||
this.updateSuperAdminUser(userId, dept); | |||
// 新增公路/路段与部门数据 | |||
addRoadAndSectionToDept(tenantId, dept.getId(), addDeptRequest.getRoadSectionDtoList()); | |||
@@ -87,6 +88,7 @@ public class AddDeptService { | |||
/** | |||
* 检查参数 | |||
* | |||
* @param tenantId | |||
* @param addDeptRequest | |||
* @return | |||
@@ -152,8 +154,8 @@ public class AddDeptService { | |||
/** | |||
* 新增公路/路段与部门数据 | |||
* | |||
* @param tenantId 租户Id | |||
* @param deptId 部门Id | |||
* @param tenantId 租户Id | |||
* @param deptId 部门Id | |||
* @param roadSectionDtoList 公路/路段集合 | |||
* @return 结果 | |||
*/ | |||
@@ -233,7 +235,6 @@ public class AddDeptService { | |||
map.put("deptId", deptId); | |||
map.put("sectionIdList", sectionIdList); | |||
sectionDeptMapper.deleteBatchByMap(map); | |||
List<SectionDept> list = new ArrayList<>(); | |||
SectionDept sectionDept; | |||
for (Section section : sectionList) { | |||
@@ -252,27 +253,32 @@ public class AddDeptService { | |||
sectionDept.setSectionId(section.getId()); | |||
list.add(sectionDept); | |||
} | |||
if (!CollectionUtils.isEmpty(list)) { | |||
sectionDeptMapper.insertBatch(list); | |||
} | |||
} | |||
/** | |||
* 如果创建的是顶级部门,将该部门与超级管理员关联 | |||
* 如果创建的是顶级部门,将该部门与超级管理员关联 | |||
*/ | |||
private void updateSuperAdminUser(Dept dept) { | |||
if (!"0".equals(dept.getPid())) { | |||
private void updateSuperAdminUser(String userId, Dept dept) { | |||
if (!SystemConstant.ROOT_DEPT_PID.equals(dept.getPid())) { | |||
return; | |||
} | |||
User user = userMapper.selectOne(new LambdaQueryWrapper<User>() | |||
List<User> userList = userMapper.selectList(new LambdaQueryWrapper<User>() | |||
.eq(User::getRoleId, 1) | |||
.eq(User::getMark, 1)); | |||
if (ObjectUtil.isNull(user)) { | |||
if (CollectionUtils.isEmpty(userList)) { | |||
log.info("用户为空"); | |||
return; | |||
} | |||
user.setDeptId(dept.getId()); | |||
userMapper.updateById(user); | |||
List<String> userIdList = userList.stream().map(o -> o.getId()).collect(Collectors.toList()); | |||
Map<String, Object> map = new HashMap<>(); | |||
map.put("deptId", dept.getId()); | |||
map.put("idList", userIdList); | |||
map.put("updateUser", userId); | |||
map.put("updateTime", DateUtils.now()); | |||
userMapper.updateDeptIdByIdList(map); | |||
} | |||
} |
@@ -3,6 +3,7 @@ package com.tuoheng.admin.service.user; | |||
import com.baomidou.mybatisplus.extension.service.IService; | |||
import com.tuoheng.admin.entity.User; | |||
import com.tuoheng.admin.request.user.QueryUserPageListRequest; | |||
import com.tuoheng.admin.request.user.UserInitRequest; | |||
import com.tuoheng.admin.request.user.UserResetPwdRequest; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import org.springframework.web.bind.annotation.*; | |||
@@ -85,6 +86,6 @@ public interface IUserService { | |||
/** | |||
* 初始化系统超级管理员用户 | |||
*/ | |||
JsonResult init(String username, String password); | |||
JsonResult init(UserInitRequest request); | |||
} |
@@ -2,6 +2,7 @@ package com.tuoheng.admin.service.user; | |||
import com.tuoheng.admin.entity.User; | |||
import com.tuoheng.admin.request.user.QueryUserPageListRequest; | |||
import com.tuoheng.admin.request.user.UserInitRequest; | |||
import com.tuoheng.admin.request.user.UserResetPwdRequest; | |||
import com.tuoheng.admin.service.user.add.AddUserService; | |||
import com.tuoheng.admin.service.user.delete.DeleteUserService; | |||
@@ -153,8 +154,8 @@ public class UserServiceImpl implements IUserService { | |||
* 初始化系统超级管理员用户 | |||
*/ | |||
@Override | |||
public JsonResult init(String username, String password) { | |||
return systemUserInitService.init(username, password); | |||
public JsonResult init(UserInitRequest request) { | |||
return systemUserInitService.init(request); | |||
} | |||
} |
@@ -4,7 +4,6 @@ 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.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.RoleMapper; | |||
@@ -57,18 +56,13 @@ public class AddUserService { | |||
log.info("添加用户业务接口:校验失败:{}", result.getMsg()); | |||
return result; | |||
} | |||
Tenant tenant = tenantMapper.selectById(tenantId); | |||
// 添加oidc | |||
result = oidcService.addUser(user, SystemConstant.INIT_PASSWORD, tenant); | |||
result = oidcService.addUser(user, SystemConstant.INIT_PASSWORD, null); | |||
if (0 != result.getCode()) { | |||
log.info("添加用户业务接口:添加oidc数据失败:{}", result.getMsg()); | |||
return result; | |||
} | |||
this.buildUser(user, tenantId); | |||
Integer rowCount = userMapper.insert(user); | |||
if (rowCount <= 0) { | |||
return JsonResult.error(AddUserCodeEnum.ADD_IS_FAILED.getCode(), AddUserCodeEnum.ADD_IS_FAILED.getMsg()); | |||
@@ -78,6 +72,7 @@ public class AddUserService { | |||
/** | |||
* 检查参数 | |||
* | |||
* @param tenantId | |||
* @param user | |||
* @return | |||
@@ -104,9 +99,9 @@ public class AddUserService { | |||
return JsonResult.error(AddUserCodeEnum.PHONE_NOT_IMATCH.getCode(), AddUserCodeEnum.PHONE_NOT_IMATCH.getMsg()); | |||
} | |||
Integer count = userMapper.selectCount(new LambdaQueryWrapper<User>() | |||
.eq(User::getTenantId, tenantId) | |||
.eq(User::getUsername, user.getUsername()) | |||
.eq(User::getMark, 1)); | |||
.eq(User::getTenantId, tenantId) | |||
.eq(User::getUsername, user.getUsername()) | |||
.eq(User::getMark, 1)); | |||
if (count > 0) { | |||
return JsonResult.error(AddUserCodeEnum.USERNAME_ALREADY_EXISTS.getCode(), AddUserCodeEnum.USERNAME_ALREADY_EXISTS.getMsg()); | |||
} | |||
@@ -121,7 +116,6 @@ public class AddUserService { | |||
} | |||
/** | |||
* | |||
* 构建实体字段数据 | |||
* | |||
* @param user | |||
@@ -135,15 +129,12 @@ public class AddUserService { | |||
// 默认密码 | |||
user.setPassword(CommonUtils.password(SystemConstant.INIT_PASSWORD)); | |||
} | |||
if (StringUtils.isNotNull(user.getAvatar()) && user.getAvatar().contains(CommonConfig.imageURL)) { | |||
user.setAvatar(user.getAvatar().replaceAll(CommonConfig.imageURL, "")); | |||
} else { | |||
//添加默认头像 | |||
user.setAvatar("imagedir/n1o1j9ndau_1670488066287.png"); | |||
} | |||
//id | |||
user.setCityName(""); | |||
user.setTenantId(tenantId); | |||
user.setCreateUser(CurrentUserUtil.getUserId()); |
@@ -2,12 +2,14 @@ package com.tuoheng.admin.service.user.init; | |||
import cn.hutool.core.util.ObjectUtil; | |||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||
import com.tuoheng.admin.entity.Role; | |||
import com.tuoheng.admin.entity.Tenant; | |||
import com.tuoheng.admin.entity.User; | |||
import com.tuoheng.admin.mapper.RoleMapper; | |||
import com.tuoheng.admin.mapper.TenantMapper; | |||
import com.tuoheng.admin.mapper.UserMapper; | |||
import com.tuoheng.admin.request.user.UserInitRequest; | |||
import com.tuoheng.admin.service.third.oidc.OidcService; | |||
import com.tuoheng.common.core.utils.CommonUtils; | |||
import com.tuoheng.common.core.utils.DateUtils; | |||
@@ -18,7 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
/** | |||
* 初始化系统用户业务层处理 | |||
* 初始化系统租户用户业务层处理 | |||
* | |||
* @author wanjing | |||
* @team tuoheng | |||
@@ -41,57 +43,84 @@ public class SystemUserInitService { | |||
private OidcService oidcService; | |||
/** | |||
* 添加用户数据 | |||
* 添加租户用户数据 | |||
* | |||
* @return | |||
*/ | |||
public JsonResult init(String username, String password) { | |||
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, password, tenantId); | |||
JsonResult result = this.check(username, password, tenantId); | |||
public JsonResult init(UserInitRequest request) { | |||
log.info("进入初始化系统租户用户业务接口"); | |||
JsonResult result = this.check(request, null); | |||
if (0 != result.getCode()) { | |||
log.info("初始化系统用户业务接口:校验失败:{}", result.getMsg()); | |||
log.info("初始化系统租户用户业务接口:校验失败:{}", result.getMsg()); | |||
return result; | |||
} | |||
Tenant tenant = null; | |||
if (1 == request.getTenantFlag()) { | |||
tenant = this.addTenant(request); | |||
} | |||
User user = this.buildUser(request); | |||
// 添加oidc | |||
result = oidcService.addUser(user, password, tenant); | |||
result = oidcService.addUser(user, request.getPassword(), tenant); | |||
if (0 != result.getCode()) { | |||
log.info("初始化系统用户业务接口:添加oidc数据失败:{}", result.getMsg()); | |||
log.info("初始化系统租户用户业务接口:添加oidc数据失败:{}", result.getMsg()); | |||
return result; | |||
} | |||
// 此次不能放在上面,要等oicd添加完之后,防止tenant不为空的话,创建都是租户 | |||
if (0 == request.getTenantFlag()) { | |||
tenant = tenantMapper.selectOne(Wrappers.<Tenant>lambdaQuery() | |||
.eq(Tenant::getMark, 1) | |||
.eq(Tenant::getCode, request.getTenantCode())); | |||
if (ObjectUtil.isNotNull(tenant)) { | |||
user.setTenantId(tenant.getId()); | |||
} else { | |||
log.info("租户不存在,tenantCode:{}", request.getTenantCode()); | |||
} | |||
} | |||
Integer rowCount = userMapper.insert(user); | |||
if (rowCount <= 0) { | |||
return JsonResult.error(400, "初始化系统用户失败"); | |||
return JsonResult.error(400, "初始化系统租户用户失败"); | |||
} | |||
return JsonResult.success(); | |||
} | |||
/** | |||
* 构建实体字段数据 | |||
* 添加租户 | |||
*/ | |||
private User buildUser(String username, String password, String tenantId) { | |||
if (StringUtils.isEmpty(username)) { | |||
username = "THJS"; | |||
private Tenant addTenant(UserInitRequest request) { | |||
// 如果是租户先去创建 | |||
Tenant tenant = new Tenant(); | |||
tenant.setName(request.getName()); | |||
tenant.setUsername(request.getName()); | |||
tenant.setCode(request.getTenantCode()); | |||
tenant.setAirportUrl(request.getAirportUrl()); | |||
Tenant tenantTmp = tenantMapper.selectOne(Wrappers.<Tenant>lambdaQuery() | |||
.eq(Tenant::getMark, 1) | |||
.eq(Tenant::getStatus, 1) | |||
.eq(Tenant::getName, request.getName()) | |||
.eq(Tenant::getCode, request.getTenantCode())); | |||
if (ObjectUtil.isNull(tenantTmp)) { | |||
tenantMapper.insert(tenant); | |||
} | |||
return tenant; | |||
} | |||
/** | |||
* 构建实体字段数据 | |||
*/ | |||
private User buildUser(UserInitRequest request) { | |||
User user = new User(); | |||
user.setCode("gs"); | |||
user.setUsername(username); | |||
user.setUsername(request.getName()); | |||
user.setRealname("超级管理员"); | |||
user.setNickname("超级管理员"); | |||
user.setCityName(""); | |||
user.setPassword(CommonUtils.password(password)); | |||
user.setPassword(CommonUtils.password(request.getPassword())); | |||
user.setAvatar("imagedir/n1o1j9ndau_1670488066287.png"); | |||
user.setClientId("1,2"); | |||
user.setRoleId(1); | |||
user.setDeptId(""); | |||
user.setStatus(1); | |||
user.setTenantId(tenantId); | |||
user.setCreateTime(DateUtils.now()); | |||
return user; | |||
} | |||
@@ -99,27 +128,24 @@ public class SystemUserInitService { | |||
/** | |||
* 检查参数 | |||
* | |||
* @param username | |||
* @param password | |||
* @param request | |||
* @param | |||
* | |||
* @return | |||
*/ | |||
private JsonResult check(String username, String password, String tenantId) { | |||
if (StringUtils.isEmpty(username)) { | |||
private JsonResult check(UserInitRequest request, String tenantId) { | |||
if (StringUtils.isEmpty(request.getName())) { | |||
return JsonResult.error(400, "用户名为空"); | |||
} | |||
if (StringUtils.isEmpty(password)) { | |||
if (StringUtils.isEmpty(request.getPassword())) { | |||
return JsonResult.error(400, "密码为空"); | |||
} | |||
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, "系统中已经存在超级管理员"); | |||
if (StringUtils.isEmpty(request.getTenantCode())) { | |||
return JsonResult.error(400, "租户编号"); | |||
} | |||
count = userMapper.selectCount(new LambdaQueryWrapper<User>() | |||
Integer count = userMapper.selectCount(new LambdaQueryWrapper<User>() | |||
.eq(User::getTenantId, tenantId) | |||
.eq(User::getUsername, username) | |||
.eq(User::getUsername, request.getName()) | |||
.eq(User::getMark, 1)); | |||
if (count > 0) { | |||
return JsonResult.error(400, "系统中已经存在该用户名"); |
@@ -64,6 +64,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |||
</where> | |||
</update> | |||
<update id="updateDeptIdByIdList" parameterType="hashmap"> | |||
update th_oauth_user | |||
<trim prefix="SET" suffixOverrides=","> | |||
<if test="deptId != null"> dept_d = #{deptId}, </if> | |||
<if test="updateUser != null and updateUser != ''"> update_user = #{updateUser}, </if> | |||
<if test="updateTime != null"> update_time = #{updateTime}, </if> | |||
</trim> | |||
<where> | |||
<if test="idList != null and idList.size() > 0"> | |||
and id in | |||
<foreach collection="idList" item="id" separator="," open="(" close=")"> | |||
#{id} | |||
</foreach> | |||
</if> | |||
</where> | |||
</update> | |||
<select id="selectPageList" parameterType="com.tuoheng.admin.request.user.QueryUserPageListRequest" resultMap="UserResult"> | |||
select <include refid="selectUserVo"/> | |||
from th_oauth_user |