Finish GroupController

This commit is contained in:
2023-11-15 18:07:23 +08:00
parent c921c56e46
commit fd9dcc01f8
11 changed files with 392 additions and 20 deletions

View File

@@ -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
/**
* <p>
@@ -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<PageVo<GroupVo>> {
fun get(@Valid groupGetParam: GroupGetParam?): ResponseResult<PageVo<GroupWithRoleVo>> {
return ResponseResult.databaseSuccess(
data = GroupConverter.groupPageToGroupPageVo(
groupService.getPage(groupGetParam)
)
data = groupService.getPage(groupGetParam)
)
}
@Operation(summary = "获取单个用户组")
@GetMapping("/{id}")
fun getOne(@PathVariable id: Long): ResponseResult<GroupWithRoleVo> {
return ResponseResult.databaseSuccess(
data = groupService.getOne(id)
)
}
@Operation(summary = "获取用户组列表")
@GetMapping("/list")
fun list(): ResponseResult<List<GroupVo>> {
return ResponseResult.databaseSuccess(
data = groupService.listAll()
)
}
@Operation(summary = "添加用户组")
@PostMapping
fun add(@Valid @RequestBody groupAddParam: GroupAddParam): ResponseResult<GroupVo> {
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<GroupVo> {
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<Nothing> {
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<Nothing> {
groupService.deleteOne(id)
return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS)
}
@Operation(summary = "批量删除角色")
@DeleteMapping
fun deleteList(@Valid @RequestBody groupDeleteParam: GroupDeleteParam): ResponseResult<Nothing> {
groupService.delete(groupDeleteParam)
return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS)
}
}

View File

@@ -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<Group>): PageVo<GroupVo> = 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<Group>): PageVo<GroupWithRoleVo> = 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
}
}

View File

@@ -15,5 +15,9 @@ import top.fatweb.api.entity.permission.Group
*/
@Mapper
interface GroupMapper : BaseMapper<Group> {
fun selectPage(page: IPage<Group>): IPage<Group>
fun selectPage(page: IPage<Long>, searchName: String?, searchRegex: Boolean): IPage<Long>
fun getWithRoleByList(groupIds: List<Long>): List<Group>?
fun selectOne(id: Long): Group?
}

View File

@@ -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<Long>? = null
)

View File

@@ -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
)

View File

@@ -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<Long>
)

View File

@@ -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<Long>? = null
)

View File

@@ -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
/**
* <p>
@@ -14,5 +16,19 @@ import top.fatweb.api.param.authentication.GroupGetParam
* @since 2023-10-25
*/
interface IGroupService : IService<Group> {
fun getPage(groupGetParam: GroupGetParam?): IPage<Group>
fun getPage(groupGetParam: GroupGetParam?): PageVo<GroupWithRoleVo>
fun getOne(id: Long): GroupWithRoleVo?
fun listAll(): List<GroupVo>
fun add(groupAddParam: GroupAddParam): GroupVo?
fun update(groupUpdateParam: GroupUpdateParam): GroupVo?
fun changeStatus(groupChangeStatusParam: GroupChangeStatusParam): Boolean
fun deleteOne(id: Long)
fun delete(groupDeleteParam: GroupDeleteParam)
}

View File

@@ -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
/**
* <p>
@@ -19,12 +26,104 @@ import top.fatweb.api.util.PageUtil
* @since 2023-10-25
*/
@Service
class GroupServiceImpl : ServiceImpl<GroupMapper, Group>(), IGroupService {
override fun getPage(groupGetParam: GroupGetParam?): IPage<Group> {
val groupPage = Page<Group>(groupGetParam?.currentPage ?: 1, groupGetParam?.pageSize ?: 20)
class GroupServiceImpl(
private val roleGroupService: IRoleGroupService
) : ServiceImpl<GroupMapper, Group>(), IGroupService {
override fun getPage(groupGetParam: GroupGetParam?): PageVo<GroupWithRoleVo> {
val groupIdsPage = Page<Long>(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<Group>(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<GroupVo> {
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<Long>()
val removeRoleIds = HashSet<Long>()
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))
}
}

View File

@@ -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<RoleVo>?
)