diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserServiceApi.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserServiceApi.java index d673ee569..b4569d386 100644 --- a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserServiceApi.java +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/SysUserServiceApi.java @@ -179,4 +179,15 @@ public interface SysUserServiceApi { */ List batchGetUserSimpleInfoList(Set userIdList); + /** + * 简单添加用户,基于一些基本信息,直接添加用户,简化版添加用户 + * + * @param addUserDTO 添加用户的请求 + * @param orgId 用户所属的组织机构id + * @param positionId 用户职位id + * @author fengshuonan + * @since 2024/5/23 23:48 + */ + void simpleAddUser(AddUserDTO addUserDTO, Long orgId, Long positionId); + } diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/pojo/user/AddUserDTO.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/pojo/user/AddUserDTO.java new file mode 100644 index 000000000..c726579de --- /dev/null +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/sys/api/pojo/user/AddUserDTO.java @@ -0,0 +1,45 @@ +package cn.stylefeng.roses.kernel.sys.api.pojo.user; + +import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription; +import lombok.Data; + +/** + * 简单添加用户的请求 + * + * @author fengshuonan + * @since 2024/5/23 23:47 + */ +@Data +public class AddUserDTO { + + /** + * 账号 + */ + @ChineseDescription("账号") + private String account; + + /** + * 昵称 + */ + @ChineseDescription("昵称") + private String nickName; + + /** + * 邮箱 + */ + @ChineseDescription("邮箱") + private String email; + + /** + * 密码,加密方式为MD5 + */ + @ChineseDescription("密码,加密方式为MD5") + private String password; + + /** + * 密码盐 + */ + @ChineseDescription("密码盐") + private String passwordSalt; + +} diff --git a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/factory/SysUserCreateFactory.java b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/factory/SysUserCreateFactory.java index 23f5db38b..c2cc6be1a 100644 --- a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/factory/SysUserCreateFactory.java +++ b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/factory/SysUserCreateFactory.java @@ -2,8 +2,13 @@ package cn.stylefeng.roses.kernel.sys.modular.user.factory; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.rule.enums.StatusEnum; +import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum; +import cn.stylefeng.roses.kernel.sys.api.pojo.user.AddUserDTO; import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUser; +import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserOrg; import cn.stylefeng.roses.kernel.sys.modular.user.pojo.request.SysUserRequest; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; /** * 用户信息填充,用于创建和修改用户时,添加一些基础信息 @@ -46,4 +51,50 @@ public class SysUserCreateFactory { } } + /** + * 通过添加用户的dto创建用户 + * + * @author fengshuonan + * @since 2024/5/23 23:51 + */ + public static SysUser createSimpleAddUser(AddUserDTO addUserDTO) { + SysUser sysUser = new SysUser(); + + // 设置主键 + sysUser.setUserId(IdWorker.getId()); + + // 设置账号 + sysUser.setAccount(addUserDTO.getAccount()); + + // 设置昵称 + sysUser.setNickName(addUserDTO.getNickName()); + + // 设置邮箱 + sysUser.setEmail(addUserDTO.getEmail()); + + // 设置密码 + sysUser.setPassword(addUserDTO.getPassword()); + + // 设置密码盐 + sysUser.setPasswordSalt(addUserDTO.getPasswordSalt()); + + return sysUser; + } + + /** + * 创建一个用户的所属机构,假设用户只有一个机构信息 + * + * @author fengshuonan + * @since 2024/5/24 0:02 + */ + public static SysUserOrg createOneUserOrg(Long userId, Long orgId, Long positionId) { + SysUserOrg sysUserOrg = new SysUserOrg(); + sysUserOrg.setUserId(userId); + sysUserOrg.setOrgId(orgId); + sysUserOrg.setPositionId(positionId); + sysUserOrg.setMainFlag(YesOrNotEnum.Y.getCode()); + sysUserOrg.setStatusFlag(StatusEnum.ENABLE.getCode()); + return sysUserOrg; + } + } diff --git a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java index ac5122d39..3c7e28344 100644 --- a/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java +++ b/kernel-s-system/system-business-hr/src/main/java/cn/stylefeng/roses/kernel/sys/modular/user/service/impl/SysUserServiceImpl.java @@ -2,6 +2,7 @@ package cn.stylefeng.roses.kernel.sys.modular.user.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.extra.spring.SpringUtil; import cn.stylefeng.roses.kernel.auth.api.TenantCodeGetApi; @@ -794,6 +795,33 @@ public class SysUserServiceImpl extends ServiceImpl impl return simpleUserDTOS; } + @Override + public void simpleAddUser(AddUserDTO addUserDTO, Long orgId, Long positionId) { + + SysUser simpleUser = SysUserCreateFactory.createSimpleAddUser(addUserDTO); + + // 设置用户默认头像 + simpleUser.setAvatar(FileConstants.DEFAULT_AVATAR_FILE_ID); + + this.save(simpleUser); + + // 记录一个密码修改记录 + securityConfigService.recordPasswordEditLog(simpleUser.getUserId(), simpleUser.getPassword(), simpleUser.getPasswordSalt()); + + // 更新用户的任职信息,用户有且只有一个所属机构 + sysUserOrgService.updateUserOrg(simpleUser.getUserId(), + ListUtil.of( + SysUserCreateFactory.createOneUserOrg(simpleUser.getUserId(), orgId, positionId))); + + // 添加用户一个默认角色 + sysUserRoleService.bindUserDefaultRole(simpleUser.getUserId()); + + // 记录日志 + BusinessLogUtil.setLogTitle("新增用户,用户账号:" + simpleUser.getAccount()); + BusinessLogUtil.addContent("新增用户账号信息:", simpleUser.getAccount()); + BusinessLogUtil.addContent("用户详细信息如下:\n", simpleUser); + } + /** * 获取信息 *