From e681d9d7b73bb71d3de9c0aed71d8ce78d13aa4c Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Mon, 13 Nov 2023 17:52:08 +0800 Subject: [PATCH] Finish role add and edit --- .../controller/permission/RoleController.kt | 37 +++++--- .../api/converter/permission/RoleConverter.kt | 8 ++ .../fatweb/api/entity/common/ResponseCode.kt | 3 +- .../fatweb/api/handler/ExceptionHandler.kt | 2 +- .../api/mapper/permission/RoleMapper.kt | 8 +- .../authentication/RoleChangeStatusParam.kt | 4 +- .../param/authentication/RoleUpdateParam.kt | 21 +++++ .../api/service/permission/IRoleService.kt | 13 ++- .../permission/impl/RoleServiceImpl.kt | 94 +++++++++++++++---- .../mapper/permission/RoleMapper.xml | 56 ++++++++++- 10 files changed, 208 insertions(+), 38 deletions(-) create mode 100644 src/main/kotlin/top/fatweb/api/param/authentication/RoleUpdateParam.kt diff --git a/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt b/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt index d2b9543..e1b4721 100644 --- a/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/permission/RoleController.kt @@ -4,12 +4,12 @@ import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag import jakarta.validation.Valid import org.springframework.web.bind.annotation.* -import top.fatweb.api.converter.permission.RoleConverter import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.param.authentication.RoleAddParam import top.fatweb.api.param.authentication.RoleChangeStatusParam import top.fatweb.api.param.authentication.RoleGetParam +import top.fatweb.api.param.authentication.RoleUpdateParam import top.fatweb.api.service.permission.IRoleService import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.permission.RoleVo @@ -31,23 +31,36 @@ class RoleController( @GetMapping fun get(roleGetParam: RoleGetParam?): ResponseResult> { return ResponseResult.databaseSuccess( - data = RoleConverter.rolePageToRoleWithPowerPageVo( - roleService.getPage(roleGetParam) - ) + data = roleService.getPage(roleGetParam) + ) + } + + @Operation(summary = "获取单个角色") + @GetMapping("/{id}") + fun getOne(@PathVariable id: Long): ResponseResult { + return ResponseResult.databaseSuccess( + data = roleService.getOne(id) ) } @Operation(summary = "添加角色") @PostMapping fun add(@Valid @RequestBody roleAddParam: RoleAddParam): ResponseResult { - return roleService.add(roleAddParam) - ?.let { - ResponseResult.databaseSuccess( - ResponseCode.DATABASE_INSERT_SUCCESS, - data = RoleConverter.roleToRoleVo(it) - ) - } - ?: let { ResponseResult.databaseFail(ResponseCode.DATABASE_INSERT_FAILED) } + return roleService.add(roleAddParam)?.let { + ResponseResult.databaseSuccess( + ResponseCode.DATABASE_INSERT_SUCCESS, data = it + ) + } ?: let { ResponseResult.databaseFail(ResponseCode.DATABASE_INSERT_FAILED) } + } + + @Operation(summary = "修改角色") + @PutMapping + fun update(@Valid @RequestBody roleUpdateParam: RoleUpdateParam): ResponseResult { + return roleService.update(roleUpdateParam)?.let { + ResponseResult.databaseSuccess( + ResponseCode.DATABASE_UPDATE_SUCCESS, data = it + ) + } ?: let { ResponseResult.databaseFail(ResponseCode.DATABASE_UPDATE_FILED) } } @Operation(summary = "修改角色状态") diff --git a/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt b/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt index 8fc3b84..c072157 100644 --- a/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/permission/RoleConverter.kt @@ -5,6 +5,7 @@ import top.fatweb.api.entity.permission.Power import top.fatweb.api.entity.permission.Role import top.fatweb.api.param.authentication.RoleAddParam import top.fatweb.api.param.authentication.RoleChangeStatusParam +import top.fatweb.api.param.authentication.RoleUpdateParam import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.permission.RoleVo import top.fatweb.api.vo.permission.RoleWithPowerVo @@ -42,6 +43,13 @@ object RoleConverter { powers = roleAddParam.powerIds?.map { Power().apply { id = it } } } + fun roleUpdateParamToRole(roleUpdateParam: RoleUpdateParam) = Role().apply { + id = roleUpdateParam.id + name = roleUpdateParam.name + enable = if (roleUpdateParam.enable == true) 1 else 0 + powers = roleUpdateParam.powerIds?.map { Power().apply { id = it } } + } + fun roleChangeStatusParamToRole(roleChangeStatusParam: RoleChangeStatusParam) = Role().apply { id = roleChangeStatusParam.id enable = if (roleChangeStatusParam.enable) 1 else 0 diff --git a/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt b/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt index 222e39c..18d621e 100644 --- a/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt +++ b/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt @@ -28,7 +28,8 @@ enum class ResponseCode(val code: Int) { DATABASE_UPDATE_FILED(BusinessCode.DATABASE, 25), DATABASE_DELETE_SUCCESS(BusinessCode.DATABASE, 30), DATABASE_DELETE_FILED(BusinessCode.DATABASE, 35), - DATABASE_EXECUTE_ERROR(BusinessCode.DATABASE, 40); + DATABASE_EXECUTE_ERROR(BusinessCode.DATABASE, 40), + DATABASE_DUPLICATE_KEY(BusinessCode.DATABASE, 45); constructor(businessCode: BusinessCode, code: Int) : this(businessCode.code * 100 + code) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt index afe00a1..6e0b0d9 100644 --- a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt +++ b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt @@ -79,7 +79,7 @@ class ExceptionHandler { is DuplicateKeyException -> { logger.debug(e.localizedMessage, e) - ResponseResult.fail(ResponseCode.DATABASE_INSERT_FAILED, "Duplicate key", null) + ResponseResult.fail(ResponseCode.DATABASE_DUPLICATE_KEY, "Duplicate key", null) } else -> { diff --git a/src/main/kotlin/top/fatweb/api/mapper/permission/RoleMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/permission/RoleMapper.kt index 3c6bc49..b75bd5f 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/permission/RoleMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/permission/RoleMapper.kt @@ -15,5 +15,11 @@ import top.fatweb.api.entity.permission.Role */ @Mapper interface RoleMapper : BaseMapper { - fun selectPage(page: IPage): IPage + fun selectPage(page: IPage): IPage + + fun getWithPowerByList(roleIds: List): List? + + fun selectOne(id: Long): Role? + + fun getPowerList(id: Long): List } diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt index eec8de2..3034009 100644 --- a/src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/authentication/RoleChangeStatusParam.kt @@ -3,12 +3,12 @@ package top.fatweb.api.param.authentication import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.databind.ser.std.ToStringSerializer import io.swagger.v3.oas.annotations.media.Schema +import jakarta.validation.constraints.Min import jakarta.validation.constraints.NotNull -import jakarta.validation.constraints.Pattern data class RoleChangeStatusParam( @Schema(description = "角色 ID") - @field:Pattern(regexp = "^\\d+$", message = "ID must be number") + @field:Min(0) @JsonSerialize(using = ToStringSerializer::class) val id: Long, diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/RoleUpdateParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/RoleUpdateParam.kt new file mode 100644 index 0000000..6d8548b --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/authentication/RoleUpdateParam.kt @@ -0,0 +1,21 @@ +package top.fatweb.api.param.authentication + +import io.swagger.v3.oas.annotations.media.Schema +import jakarta.validation.constraints.Min +import jakarta.validation.constraints.NotBlank + +data class RoleUpdateParam( + @Schema(description = "角色 ID") + @field:Min(0) + val id: Long, + + @Schema(description = "角色名称") + @field:NotBlank(message = "Name can not be blank") + val name: String?, + + @Schema(description = "启用", allowableValues = ["true", "false"]) + val enable: Boolean? = true, + + @Schema(description = "权限 ID 列表") + val powerIds: List? = null +) diff --git a/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt b/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt index 5eec26c..c8348cf 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/IRoleService.kt @@ -1,11 +1,14 @@ package top.fatweb.api.service.permission -import com.baomidou.mybatisplus.core.metadata.IPage import com.baomidou.mybatisplus.extension.service.IService import top.fatweb.api.entity.permission.Role import top.fatweb.api.param.authentication.RoleAddParam import top.fatweb.api.param.authentication.RoleChangeStatusParam import top.fatweb.api.param.authentication.RoleGetParam +import top.fatweb.api.param.authentication.RoleUpdateParam +import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.permission.RoleVo +import top.fatweb.api.vo.permission.RoleWithPowerVo /** *

@@ -16,9 +19,13 @@ import top.fatweb.api.param.authentication.RoleGetParam * @since 2023-10-25 */ interface IRoleService : IService { - fun getPage(roleGetParam: RoleGetParam?): IPage + fun getPage(roleGetParam: RoleGetParam?): PageVo - fun add(roleAddParam: RoleAddParam): Role? + fun getOne(id: Long): RoleWithPowerVo? + + fun add(roleAddParam: RoleAddParam): RoleVo? + + fun update(roleUpdateParam: RoleUpdateParam): RoleVo? fun changeStatus(roleChangeStatusParam: RoleChangeStatusParam): Boolean } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt index 6a8e745..1d72988 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/RoleServiceImpl.kt @@ -1,6 +1,6 @@ package top.fatweb.api.service.permission.impl -import com.baomidou.mybatisplus.core.metadata.IPage +import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper import com.baomidou.mybatisplus.extension.plugins.pagination.Page import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl import org.springframework.stereotype.Service @@ -12,9 +12,13 @@ import top.fatweb.api.mapper.permission.RoleMapper import top.fatweb.api.param.authentication.RoleAddParam import top.fatweb.api.param.authentication.RoleChangeStatusParam import top.fatweb.api.param.authentication.RoleGetParam +import top.fatweb.api.param.authentication.RoleUpdateParam import top.fatweb.api.service.permission.IPowerRoleService import top.fatweb.api.service.permission.IRoleService import top.fatweb.api.util.PageUtil +import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.permission.RoleVo +import top.fatweb.api.vo.permission.RoleWithPowerVo /** *

@@ -28,39 +32,95 @@ import top.fatweb.api.util.PageUtil class RoleServiceImpl( private val powerRoleService: IPowerRoleService ) : ServiceImpl(), IRoleService { - override fun getPage(roleGetParam: RoleGetParam?): IPage { - val rolePage = Page(roleGetParam?.currentPage ?: 1, roleGetParam?.pageSize ?: 20) + override fun getPage(roleGetParam: RoleGetParam?): PageVo { + val roleIdsPage = Page(roleGetParam?.currentPage ?: 1, roleGetParam?.pageSize ?: 20) - PageUtil.setPageSort(roleGetParam, rolePage) + PageUtil.setPageSort(roleGetParam, roleIdsPage) - return baseMapper.selectPage(rolePage) + val roleIdsIPage = baseMapper.selectPage(roleIdsPage) + + val rolePage = Page(roleIdsPage.current, roleIdsIPage.size, roleIdsIPage.total) + rolePage.setRecords(baseMapper.getWithPowerByList(roleIdsIPage.records)) + + + return RoleConverter.rolePageToRoleWithPowerPageVo(rolePage) + } + + override fun getOne(id: Long): RoleWithPowerVo? { + return baseMapper.selectOne(id)?.let { RoleConverter.roleToRoleWithPowerVo(it) } ?: let { null } } @Transactional - override fun add(roleAddParam: RoleAddParam): Role? { + override fun add(roleAddParam: RoleAddParam): RoleVo? { + val fullPowerIds: HashSet = hashSetOf() + roleAddParam.powerIds?.forEach { + fullPowerIds.add(it) + fullPowerIds.add(it / 100 * 100) + fullPowerIds.add(it / 10000 * 10000) + fullPowerIds.add(it / 1000000 * 1000000) + } val role = RoleConverter.roleAddParamToRole(roleAddParam) if (baseMapper.insert(role) == 1) { - if (roleAddParam.powerIds.isNullOrEmpty()) { - return role + if (fullPowerIds.isEmpty()) { + return RoleConverter.roleToRoleVo(role) } - if (powerRoleService.saveBatch( - roleAddParam.powerIds.map { - PowerRole().apply { - roleId = role.id - powerId = it - } + if (powerRoleService.saveBatch(fullPowerIds.map { + PowerRole().apply { + roleId = role.id + powerId = it } - ) - ) { - return role + })) { + return RoleConverter.roleToRoleVo(role) } } return null } + @Transactional + override fun update(roleUpdateParam: RoleUpdateParam): RoleVo? { + val fullPowerIds: HashSet = hashSetOf() + roleUpdateParam.powerIds?.forEach { + fullPowerIds.add(it) + fullPowerIds.add(it / 100 * 100) + fullPowerIds.add(it / 10000 * 10000) + fullPowerIds.add(it / 1000000 * 1000000) + } + val role = RoleConverter.roleUpdateParamToRole(roleUpdateParam) + val oldPowerList = baseMapper.getPowerList(roleUpdateParam.id) + val addPowerIds = HashSet() + val removePowerIds = HashSet() + fullPowerIds.forEach { addPowerIds.add(it) } + oldPowerList.forEach { + if (it != null) { + removePowerIds.add(it) + } + } + removePowerIds.removeAll(addPowerIds) + oldPowerList.toSet().let { addPowerIds.removeAll(it) } + + baseMapper.updateById(role) + + removePowerIds.forEach { + powerRoleService.remove( + KtQueryWrapper(PowerRole()).eq( + PowerRole::roleId, roleUpdateParam.id + ).eq(PowerRole::powerId, it) + ) + } + + addPowerIds.forEach { + powerRoleService.save(PowerRole().apply { + roleId = roleUpdateParam.id + powerId = it + }) + } + + return RoleConverter.roleToRoleVo(role) + } + override fun changeStatus(roleChangeStatusParam: RoleChangeStatusParam): Boolean { return updateById(RoleConverter.roleChangeStatusParamToRole(roleChangeStatusParam)) } diff --git a/src/main/resources/mapper/permission/RoleMapper.xml b/src/main/resources/mapper/permission/RoleMapper.xml index 2f9b5ac..6caefa2 100644 --- a/src/main/resources/mapper/permission/RoleMapper.xml +++ b/src/main/resources/mapper/permission/RoleMapper.xml @@ -1,7 +1,53 @@ - + select id + from t_role + where deleted = 0 + + + + + + +