diff --git a/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt b/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt index 1627f13..0a5a579 100644 --- a/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/permission/GroupController.kt @@ -3,15 +3,23 @@ package top.fatweb.api.controller.permission 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.DeleteMapping import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PatchMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.PutMapping +import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import top.fatweb.api.converter.permission.GroupConverter +import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult -import top.fatweb.api.param.authentication.GroupGetParam +import top.fatweb.api.param.authentication.* import top.fatweb.api.service.permission.IGroupService import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.permission.GroupVo +import top.fatweb.api.vo.permission.GroupWithRoleVo /** *

@@ -27,13 +35,71 @@ import top.fatweb.api.vo.permission.GroupVo class GroupController( val groupService: IGroupService ) { - @Operation(summary = "获取用户组列表") + @Operation(summary = "获取用户组") @GetMapping - fun get(@Valid groupGetParam: GroupGetParam?): ResponseResult> { + fun get(@Valid groupGetParam: GroupGetParam?): ResponseResult> { return ResponseResult.databaseSuccess( - data = GroupConverter.groupPageToGroupPageVo( - groupService.getPage(groupGetParam) - ) + data = groupService.getPage(groupGetParam) ) } + + @Operation(summary = "获取单个用户组") + @GetMapping("/{id}") + fun getOne(@PathVariable id: Long): ResponseResult { + return ResponseResult.databaseSuccess( + data = groupService.getOne(id) + ) + } + + @Operation(summary = "获取用户组列表") + @GetMapping("/list") + fun list(): ResponseResult> { + return ResponseResult.databaseSuccess( + data = groupService.listAll() + ) + } + + @Operation(summary = "添加用户组") + @PostMapping + fun add(@Valid @RequestBody groupAddParam: GroupAddParam): ResponseResult { + return groupService.add(groupAddParam)?.let { + ResponseResult.databaseSuccess( + ResponseCode.DATABASE_INSERT_SUCCESS, data = it + ) + } ?: let { ResponseResult.databaseFail(ResponseCode.DATABASE_INSERT_FAILED) } + } + + @Operation(summary = "修改用户组") + @PutMapping + fun update(@Valid @RequestBody groupUpdateParam: GroupUpdateParam): ResponseResult { + return groupService.update(groupUpdateParam)?.let { + ResponseResult.databaseSuccess( + ResponseCode.DATABASE_UPDATE_SUCCESS, data = it + ) + } ?: let { ResponseResult.databaseFail(ResponseCode.DATABASE_UPDATE_FILED) } + } + + @Operation(summary = "修改用户组状态") + @PatchMapping + fun changStatus(@Valid @RequestBody groupChangeStatusParam: GroupChangeStatusParam): ResponseResult { + return if (groupService.changeStatus(groupChangeStatusParam)) { + ResponseResult.databaseSuccess(ResponseCode.DATABASE_UPDATE_SUCCESS) + } else { + ResponseResult.databaseFail(ResponseCode.DATABASE_UPDATE_FILED) + } + } + + @Operation(summary = "删除角色") + @DeleteMapping("/{id}") + fun delete(@PathVariable id: Long): ResponseResult { + groupService.deleteOne(id) + return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS) + } + + @Operation(summary = "批量删除角色") + @DeleteMapping + fun deleteList(@Valid @RequestBody groupDeleteParam: GroupDeleteParam): ResponseResult { + groupService.delete(groupDeleteParam) + return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS) + } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/converter/permission/GroupConverter.kt b/src/main/kotlin/top/fatweb/api/converter/permission/GroupConverter.kt index 6d55910..bc66d32 100644 --- a/src/main/kotlin/top/fatweb/api/converter/permission/GroupConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/permission/GroupConverter.kt @@ -2,8 +2,13 @@ package top.fatweb.api.converter.permission import com.baomidou.mybatisplus.core.metadata.IPage import top.fatweb.api.entity.permission.Group +import top.fatweb.api.entity.permission.Role +import top.fatweb.api.param.authentication.GroupAddParam +import top.fatweb.api.param.authentication.GroupChangeStatusParam +import top.fatweb.api.param.authentication.GroupUpdateParam import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.permission.GroupVo +import top.fatweb.api.vo.permission.GroupWithRoleVo object GroupConverter { fun groupToGroupVo(group: Group) = GroupVo( @@ -14,13 +19,40 @@ object GroupConverter { updateTime = group.updateTime ) - fun groupPageToGroupPageVo(groupPage: IPage): PageVo = PageVo( + fun groupToGroupWithRoleVo(group: Group) = GroupWithRoleVo( + id = group.id, + name = group.name, + enable = group.enable == 1, + createTime = group.createTime, + updateTime = group.updateTime, + roles = group.roles?.map { RoleConverter.roleToRoleVo(it) } + ) + + fun groupPageToGroupWithRolePageVo(groupPage: IPage): PageVo = PageVo( total = groupPage.total, pages = groupPage.pages, size = groupPage.size, current = groupPage.current, records = groupPage.records.map { - groupToGroupVo(it) + groupToGroupWithRoleVo(it) } ) + + fun groupAddParamToGroup(groupAddParam: GroupAddParam) = Group().apply { + name = groupAddParam.name + enable = if (groupAddParam.enable == true) 1 else 0 + roles = groupAddParam.roleIds?.map { Role().apply { id = it } } + } + + fun groupUpdateParamToGroup(groupUpdateParam: GroupUpdateParam) = Group().apply { + id = groupUpdateParam.id + name = groupUpdateParam.name + enable = if (groupUpdateParam.enable == true) 1 else 0 + roles = groupUpdateParam.roleIds?.map { Role().apply { id = it } } + } + + fun groupChangeStatusParamToGroup(groupChangeStatusParam: GroupChangeStatusParam) = Group().apply { + id = groupChangeStatusParam.id + enable = if (groupChangeStatusParam.enable) 1 else 0 + } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/mapper/permission/GroupMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/permission/GroupMapper.kt index f42b78a..a9b500e 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/permission/GroupMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/permission/GroupMapper.kt @@ -15,5 +15,9 @@ import top.fatweb.api.entity.permission.Group */ @Mapper interface GroupMapper : BaseMapper { - fun selectPage(page: IPage): IPage + fun selectPage(page: IPage, searchName: String?, searchRegex: Boolean): IPage + + fun getWithRoleByList(groupIds: List): List? + + fun selectOne(id: Long): Group? } diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/GroupAddParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/GroupAddParam.kt new file mode 100644 index 0000000..309bbf2 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/authentication/GroupAddParam.kt @@ -0,0 +1,17 @@ +package top.fatweb.api.param.authentication + +import io.swagger.v3.oas.annotations.media.Schema +import jakarta.validation.constraints.NotBlank + +@Schema(description = "用户组添加请求参数") +data class GroupAddParam( + @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 roleIds: List? = null +) diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/GroupChangeStatusParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/GroupChangeStatusParam.kt new file mode 100644 index 0000000..5f18624 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/authentication/GroupChangeStatusParam.kt @@ -0,0 +1,16 @@ +package top.fatweb.api.param.authentication + +import io.swagger.v3.oas.annotations.media.Schema +import jakarta.validation.constraints.Min +import jakarta.validation.constraints.NotNull + +@Schema(description = "用户组更改状态请求参数") +data class GroupChangeStatusParam( + @Schema(description = "用户组 ID") + @field:Min(0) + val id: Long, + + @Schema(description = "启用", allowableValues = ["true", "false"]) + @field:NotNull + val enable: Boolean +) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/GroupDeleteParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/GroupDeleteParam.kt new file mode 100644 index 0000000..c68e60f --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/authentication/GroupDeleteParam.kt @@ -0,0 +1,9 @@ +package top.fatweb.api.param.authentication + +import io.swagger.v3.oas.annotations.media.Schema + +@Schema(description = "用户组删除请求参数") +data class GroupDeleteParam( + @Schema(description = "用户组 ID 列表") + val ids: List +) diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/GroupUpdateParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/GroupUpdateParam.kt new file mode 100644 index 0000000..5b4a686 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/authentication/GroupUpdateParam.kt @@ -0,0 +1,22 @@ +package top.fatweb.api.param.authentication + +import io.swagger.v3.oas.annotations.media.Schema +import jakarta.validation.constraints.Min +import jakarta.validation.constraints.NotBlank + +@Schema(description = "用户组更新请求参数") +data class GroupUpdateParam( + @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 roleIds: List? = null +) diff --git a/src/main/kotlin/top/fatweb/api/service/permission/IGroupService.kt b/src/main/kotlin/top/fatweb/api/service/permission/IGroupService.kt index 73c88b3..e49b315 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/IGroupService.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/IGroupService.kt @@ -1,9 +1,11 @@ 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.Group -import top.fatweb.api.param.authentication.GroupGetParam +import top.fatweb.api.param.authentication.* +import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.permission.GroupVo +import top.fatweb.api.vo.permission.GroupWithRoleVo /** *

@@ -14,5 +16,19 @@ import top.fatweb.api.param.authentication.GroupGetParam * @since 2023-10-25 */ interface IGroupService : IService { - fun getPage(groupGetParam: GroupGetParam?): IPage + fun getPage(groupGetParam: GroupGetParam?): PageVo + + fun getOne(id: Long): GroupWithRoleVo? + + fun listAll(): List + + fun add(groupAddParam: GroupAddParam): GroupVo? + + fun update(groupUpdateParam: GroupUpdateParam): GroupVo? + + fun changeStatus(groupChangeStatusParam: GroupChangeStatusParam): Boolean + + fun deleteOne(id: Long) + + fun delete(groupDeleteParam: GroupDeleteParam) } diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt index 811464d..5628dfd 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/GroupServiceImpl.kt @@ -1,14 +1,21 @@ 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 +import org.springframework.transaction.annotation.Transactional +import top.fatweb.api.converter.permission.GroupConverter import top.fatweb.api.entity.permission.Group +import top.fatweb.api.entity.permission.RoleGroup import top.fatweb.api.mapper.permission.GroupMapper -import top.fatweb.api.param.authentication.GroupGetParam +import top.fatweb.api.param.authentication.* import top.fatweb.api.service.permission.IGroupService +import top.fatweb.api.service.permission.IRoleGroupService import top.fatweb.api.util.PageUtil +import top.fatweb.api.vo.PageVo +import top.fatweb.api.vo.permission.GroupVo +import top.fatweb.api.vo.permission.GroupWithRoleVo /** *

@@ -19,12 +26,104 @@ import top.fatweb.api.util.PageUtil * @since 2023-10-25 */ @Service -class GroupServiceImpl : ServiceImpl(), IGroupService { - override fun getPage(groupGetParam: GroupGetParam?): IPage { - val groupPage = Page(groupGetParam?.currentPage ?: 1, groupGetParam?.pageSize ?: 20) +class GroupServiceImpl( + private val roleGroupService: IRoleGroupService +) : ServiceImpl(), IGroupService { + override fun getPage(groupGetParam: GroupGetParam?): PageVo { + val groupIdsPage = Page(groupGetParam?.currentPage ?: 1, groupGetParam?.pageSize ?: 20) - PageUtil.setPageSort(groupGetParam, groupPage) + PageUtil.setPageSort(groupGetParam, groupIdsPage) - return baseMapper.selectPage(groupPage) + val groupIdsIPage = + baseMapper.selectPage(groupIdsPage, groupGetParam?.searchName, groupGetParam?.searchRegex ?: false) + val groupPage = Page(groupIdsPage.current, groupIdsIPage.size, groupIdsIPage.total) + if (groupIdsIPage.total > 0) { + groupPage.setRecords(baseMapper.getWithRoleByList(groupIdsIPage.records)) + } + + return GroupConverter.groupPageToGroupWithRolePageVo(groupPage) + } + + override fun getOne(id: Long): GroupWithRoleVo? { + return baseMapper.selectOne(id)?.let { GroupConverter.groupToGroupWithRoleVo(it) } ?: let { null } + } + + override fun listAll(): List { + val groups = this.list() + + return groups.map { GroupConverter.groupToGroupVo(it) } + } + + @Transactional + override fun add(groupAddParam: GroupAddParam): GroupVo? { + val group = GroupConverter.groupAddParamToGroup(groupAddParam) + if (baseMapper.insert(group) == 1) { + if (group.roles.isNullOrEmpty()) { + return GroupConverter.groupToGroupVo(group) + } + + if (roleGroupService.saveBatch(group.roles!!.map { + RoleGroup().apply { + groupId = group.id + roleId = it.id + } + })) { + return GroupConverter.groupToGroupVo(group) + } + } + + return null + } + + @Transactional + override fun update(groupUpdateParam: GroupUpdateParam): GroupVo? { + val group = GroupConverter.groupUpdateParamToGroup(groupUpdateParam) + val oldRoleList = roleGroupService.list( + KtQueryWrapper(RoleGroup()).select(RoleGroup::roleId).eq(RoleGroup::groupId, groupUpdateParam.id) + ).map { it.roleId } + val addRoleIds = HashSet() + val removeRoleIds = HashSet() + groupUpdateParam.roleIds?.forEach { addRoleIds.add(it) } + oldRoleList.forEach { + if (it != null) { + removeRoleIds.add(it) + } + } + removeRoleIds.removeAll(addRoleIds) + oldRoleList.toSet().let { addRoleIds.removeAll(it) } + + baseMapper.updateById(group) + + removeRoleIds.forEach { + roleGroupService.remove( + KtQueryWrapper(RoleGroup()).eq( + RoleGroup::groupId, groupUpdateParam.id + ).eq(RoleGroup::roleId, it) + ) + } + + addRoleIds.forEach { + roleGroupService.save(RoleGroup().apply { + groupId = groupUpdateParam.id + roleId = it + }) + } + + return GroupConverter.groupToGroupVo(group) + } + + override fun changeStatus(groupChangeStatusParam: GroupChangeStatusParam): Boolean { + return updateById(GroupConverter.groupChangeStatusParamToGroup(groupChangeStatusParam)) + } + + @Transactional + override fun deleteOne(id: Long) { + this.delete(GroupDeleteParam(listOf(id))) + } + + @Transactional + override fun delete(groupDeleteParam: GroupDeleteParam) { + baseMapper.deleteBatchIds(groupDeleteParam.ids) + roleGroupService.remove(KtQueryWrapper(RoleGroup()).`in`(RoleGroup::groupId, groupDeleteParam.ids)) } } diff --git a/src/main/kotlin/top/fatweb/api/vo/permission/GroupWithRoleVo.kt b/src/main/kotlin/top/fatweb/api/vo/permission/GroupWithRoleVo.kt new file mode 100644 index 0000000..06b85a3 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/permission/GroupWithRoleVo.kt @@ -0,0 +1,26 @@ +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 + +data class GroupWithRoleVo( + @JsonSerialize(using = ToStringSerializer::class) + val id: Long?, + + @Schema(description = "用户组名", example = "Role") + val name: String?, + + @Schema(description = "启用", example = "true") + val enable: Boolean?, + + @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 roles: List? +) diff --git a/src/main/resources/mapper/permission/GroupMapper.xml b/src/main/resources/mapper/permission/GroupMapper.xml index 0f08352..84d4cb4 100644 --- a/src/main/resources/mapper/permission/GroupMapper.xml +++ b/src/main/resources/mapper/permission/GroupMapper.xml @@ -1,8 +1,69 @@ - + select id + from t_group + + deleted = 0 + + + + and t_group.name regexp #{searchName} + + + and t_group.name like concat('%' ,#{searchName}, '%') + + + + + + + + @@ -14,4 +75,8 @@ + + + +