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

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