From 7cf6b5cea2417b188170408bff5eb004f5ec6686 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Wed, 29 Nov 2023 10:55:44 +0800 Subject: [PATCH] Optimize user management api --- .../api/converter/permission/UserConverter.kt | 19 +++++++ .../api/mapper/permission/UserMapper.kt | 8 +-- .../api/service/permission/IUserService.kt | 5 +- .../permission/impl/UserDetailsServiceImpl.kt | 2 +- .../permission/impl/UserServiceImpl.kt | 12 ++--- .../api/vo/permission/UserWithInfoVo.kt | 54 +++++++++++++++++++ .../permission/UserWithPasswordRoleInfoVo.kt | 2 +- .../mapper/permission/UserMapper.xml | 44 ++++++--------- 8 files changed, 103 insertions(+), 43 deletions(-) create mode 100644 src/main/kotlin/top/fatweb/api/vo/permission/UserWithInfoVo.kt diff --git a/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt b/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt index a64c869..14fa247 100644 --- a/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/permission/UserConverter.kt @@ -9,6 +9,7 @@ import top.fatweb.api.param.authentication.LoginParam import top.fatweb.api.param.authentication.UserAddParam import top.fatweb.api.param.authentication.UserUpdateParam import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.permission.UserWithInfoVo import top.fatweb.api.vo.permission.UserWithPasswordRoleInfoVo import top.fatweb.api.vo.permission.UserWithPowerInfoVo import top.fatweb.api.vo.permission.UserWithRoleInfoVo @@ -81,6 +82,24 @@ object UserConverter { } ) + fun userToUserWithInfoVo(user: User) = UserWithInfoVo( + id = user.id, + username = user.username, + locking = user.locking?.let { it == 1 }, + expiration = user.expiration, + credentialsExpiration = user.credentialsExpiration, + enable = user.enable?.let { it == 1 }, + currentLoginTime = user.currentLoginTime, + currentLoginIp = user.currentLoginIp, + lastLoginTime = user.lastLoginTime, + lastLoginIp = user.lastLoginIp, + createTime = user.createTime, + updateTime = user.updateTime, + userInfo = user.userInfo?.let { + UserInfoConverter.userInfoToUserInfoVo(it) + } + ) + fun userToUserWithPasswordRoleInfoVo(user: User) = UserWithPasswordRoleInfoVo( id = user.id, username = user.username, diff --git a/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt index ebf38d5..c8eca89 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/permission/UserMapper.kt @@ -14,13 +14,13 @@ import top.fatweb.api.entity.permission.User */ @Mapper interface UserMapper : BaseMapper { - fun getOneWithPowerInfoByUsername(@Param("username")username: String): User? + fun selectOneWithPowerInfoByUsername(@Param("username")username: String): User? fun selectPage(page: IPage, searchValue: String?, searchRegex: Boolean): IPage - fun getWithRoleInfoByList(userIds: List): List? + fun selectListWithRoleInfoByIds(userIds: List): List? - fun getOneWithRoleInfo(id: Long): User? + fun selectOneWithRoleInfoById(id: Long): User? - fun getListWithRoleInfo(): List + fun selectListWithInfo(): List } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt b/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt index a9cfc1c..22a569e 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/IUserService.kt @@ -7,6 +7,7 @@ import top.fatweb.api.param.authentication.UserDeleteParam import top.fatweb.api.param.authentication.UserGetParam import top.fatweb.api.param.authentication.UserUpdateParam import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.permission.UserWithInfoVo import top.fatweb.api.vo.permission.UserWithPasswordRoleInfoVo import top.fatweb.api.vo.permission.UserWithPowerInfoVo import top.fatweb.api.vo.permission.UserWithRoleInfoVo @@ -18,7 +19,7 @@ import top.fatweb.api.vo.permission.UserWithRoleInfoVo * @since 1.0.0 */ interface IUserService : IService { - fun getUserWithPower(username: String): User? + fun getUserWithPowerByUsername(username: String): User? fun getInfo(): UserWithPowerInfoVo? @@ -26,7 +27,7 @@ interface IUserService : IService { fun getOne(id: Long): UserWithRoleInfoVo? - fun getList(): List + fun getList(): List fun add(userAddParam: UserAddParam): UserWithPasswordRoleInfoVo? diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserDetailsServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserDetailsServiceImpl.kt index ce02807..01efa6c 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserDetailsServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserDetailsServiceImpl.kt @@ -15,7 +15,7 @@ import top.fatweb.api.service.permission.IUserService @Service class UserDetailsServiceImpl(val userService: IUserService) : UserDetailsService { override fun loadUserByUsername(username: String): UserDetails { - val user = userService.getUserWithPower(username) + val user = userService.getUserWithPowerByUsername(username) user ?: let { throw Exception("Username not found") } return LoginUser(user) diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt index 6fb6f6a..e317679 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/UserServiceImpl.kt @@ -41,8 +41,8 @@ class UserServiceImpl( private val userRoleService: IUserRoleService, private val userGroupService: IUserGroupService ) : ServiceImpl(), IUserService { - override fun getUserWithPower(username: String): User? { - val user = baseMapper.getOneWithPowerInfoByUsername(username) + override fun getUserWithPowerByUsername(username: String): User? { + val user = baseMapper.selectOneWithPowerInfoByUsername(username) user ?: let { return null } if (user.id == 0L) { @@ -56,7 +56,7 @@ class UserServiceImpl( } override fun getInfo() = WebUtil.getLoginUsername() - ?.let { username -> getUserWithPower(username)?.let { UserConverter.userToUserWithPowerInfoVo(it) } } + ?.let { username -> getUserWithPowerByUsername(username)?.let { UserConverter.userToUserWithPowerInfoVo(it) } } override fun getPage(userGetParam: UserGetParam?): PageVo { val userIdsPage = Page(userGetParam?.currentPage ?: 1, userGetParam?.pageSize ?: 20) @@ -67,16 +67,16 @@ class UserServiceImpl( baseMapper.selectPage(userIdsPage, userGetParam?.searchValue, userGetParam?.searchRegex ?: false) val userPage = Page(userIdsIPage.current, userIdsIPage.size, userIdsIPage.total) if (userIdsIPage.total > 0) { - userPage.setRecords(baseMapper.getWithRoleInfoByList(userIdsIPage.records)) + userPage.setRecords(baseMapper.selectListWithRoleInfoByIds(userIdsIPage.records)) } return UserConverter.userPageToUserWithRoleInfoPageVo(userPage) } override fun getOne(id: Long) = - baseMapper.getOneWithRoleInfo(id)?.let { UserConverter.userToUserWithRoleInfoVo(it) } + baseMapper.selectOneWithRoleInfoById(id)?.let { UserConverter.userToUserWithRoleInfoVo(it) } - override fun getList() = baseMapper.getListWithRoleInfo().map { UserConverter.userToUserWithRoleInfoVo(it) } + override fun getList() = baseMapper.selectListWithInfo().map { UserConverter.userToUserWithInfoVo(it) } @Transactional override fun add(userAddParam: UserAddParam): UserWithPasswordRoleInfoVo? { diff --git a/src/main/kotlin/top/fatweb/api/vo/permission/UserWithInfoVo.kt b/src/main/kotlin/top/fatweb/api/vo/permission/UserWithInfoVo.kt new file mode 100644 index 0000000..058c593 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/permission/UserWithInfoVo.kt @@ -0,0 +1,54 @@ +package top.fatweb.api.vo.permission + +import com.fasterxml.jackson.databind.annotation.JsonSerialize +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer +import io.swagger.v3.oas.annotations.media.Schema +import java.time.LocalDateTime + +/** + * User with information value object + * + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + */ +@Schema(description = "用户信息返回参数") +data class UserWithInfoVo( + @JsonSerialize(using = ToStringSerializer::class) + val id: Long?, + + @Schema(description = "用户名", example = "User") + val username: String?, + + @Schema(description = "是否锁定", example = "false") + val locking: Boolean?, + + @Schema(description = "过期时间", example = "1900-01-01T00:00:00.000Z") + val expiration: LocalDateTime?, + + @Schema(description = "认证过期时间", example = "1900-01-01T00:00:00.000Z") + val credentialsExpiration: LocalDateTime?, + + @Schema(description = "是否启用", example = "true") + val enable: Boolean?, + + @Schema(description = "当前登录时间", example = "1900-01-01T00:00:00.000Z") + val currentLoginTime: LocalDateTime?, + + @Schema(description = "当前登录 IP", example = "1.1.1.1") + val currentLoginIp: String?, + + @Schema(description = "最后登录时间", example = "1900-01-01T00:00:00.000Z") + val lastLoginTime: LocalDateTime?, + + @Schema(description = "最后登录 IP", example = "1.1.1.1") + val lastLoginIp: String?, + + @Schema(description = "创建时间", example = "1900-01-01T00:00:00.000Z") + val createTime: LocalDateTime?, + + @Schema(description = "修改时间", example = "1900-01-01T00:00:00.000Z") + val updateTime: LocalDateTime?, + + @Schema(description = "用户资料") + val userInfo: UserInfoVo? +) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/vo/permission/UserWithPasswordRoleInfoVo.kt b/src/main/kotlin/top/fatweb/api/vo/permission/UserWithPasswordRoleInfoVo.kt index df16848..f551cf3 100644 --- a/src/main/kotlin/top/fatweb/api/vo/permission/UserWithPasswordRoleInfoVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/permission/UserWithPasswordRoleInfoVo.kt @@ -6,7 +6,7 @@ import io.swagger.v3.oas.annotations.media.Schema import java.time.LocalDateTime /** - * User with role information value object + * User with password role information value object * * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 diff --git a/src/main/resources/mapper/permission/UserMapper.xml b/src/main/resources/mapper/permission/UserMapper.xml index 6eb6207..ab7442d 100644 --- a/src/main/resources/mapper/permission/UserMapper.xml +++ b/src/main/resources/mapper/permission/UserMapper.xml @@ -1,7 +1,7 @@ - select t_user.id as user_id, t_user.username as user_username, t_user.password as user_password, @@ -69,7 +69,7 @@ - select t_user.id as user_id, t_user.username as user_username, t_user.password as user_password, @@ -107,10 +107,10 @@ from t_user left join (select * from t_user_info where deleted = 0) as tui on t_user.id = tui.user_id left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id - left join (select * from t_group where deleted = 0 and enable = 1) as tg on tg.id = tug.group_id + left join (select * from t_group where deleted = 0) as tg on tg.id = tug.group_id left join (select * from t_role_group where deleted = 0) as trg on tg.id = trg.group_id left join (select * from t_user_role where deleted = 0) as tur on t_user.id = tur.user_id - left join (select * from t_role where deleted = 0 and enable = 1) as tr + left join (select * from t_role where deleted = 0) as tr on tr.id = trg.role_id or tr.id = tur.role_id - select t_user.id as user_id, t_user.username as user_username, t_user.password as user_password, @@ -158,16 +158,16 @@ from t_user left join (select * from t_user_info where deleted = 0) as tui on t_user.id = tui.user_id left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id - left join (select * from t_group where deleted = 0 and enable = 1) as tg on tg.id = tug.group_id + left join (select * from t_group where deleted = 0) as tg on tg.id = tug.group_id left join (select * from t_role_group where deleted = 0) as trg on tg.id = trg.group_id left join (select * from t_user_role where deleted = 0) as tur on t_user.id = tur.user_id - left join (select * from t_role where deleted = 0 and enable = 1) as tr + left join (select * from t_role where deleted = 0) as tr on tr.id = trg.role_id or tr.id = tur.role_id where t_user.deleted = 0 and t_user.id = #{id} - select t_user.id as user_id, t_user.username as user_username, t_user.password as user_password, @@ -191,25 +191,9 @@ tui.create_time as user_info_create_time, tui.update_time as user_info_update_time, tui.deleted as user_info_deleted, - tui.version as user_info_version, - tr.id as role_id, - tr.name as role_name, - tr.enable as role_enable, - tr.deleted as role_deleted, - tr.version as role_version, - tg.id as group_id, - tg.name as group_name, - tg.enable as group_enable, - tg.deleted as group_deleted, - tg.version as group_version + tui.version as user_info_version from t_user left join (select * from t_user_info where deleted = 0) as tui on t_user.id = tui.user_id - left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id - left join (select * from t_group where deleted = 0 and enable = 1) as tg on tg.id = tug.group_id - left join (select * from t_role_group where deleted = 0) as trg on tg.id = trg.group_id - left join (select * from t_user_role where deleted = 0) as tur on t_user.id = tur.user_id - left join (select * from t_role where deleted = 0 and enable = 1) as tr - on tr.id = trg.role_id or tr.id = tur.role_id where t_user.deleted = 0 @@ -230,17 +214,19 @@ - - + + + + + - - +