Complete core functions #9

Merged
FatttSnake merged 171 commits from FatttSnake into dev 2024-02-23 11:56:35 +08:00
10 changed files with 208 additions and 38 deletions
Showing only changes of commit e681d9d7b7 - Show all commits

View File

@@ -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<PageVo<RoleWithPowerVo>> {
return ResponseResult.databaseSuccess(
data = RoleConverter.rolePageToRoleWithPowerPageVo(
roleService.getPage(roleGetParam)
)
data = roleService.getPage(roleGetParam)
)
}
@Operation(summary = "获取单个角色")
@GetMapping("/{id}")
fun getOne(@PathVariable id: Long): ResponseResult<RoleWithPowerVo> {
return ResponseResult.databaseSuccess(
data = roleService.getOne(id)
)
}
@Operation(summary = "添加角色")
@PostMapping
fun add(@Valid @RequestBody roleAddParam: RoleAddParam): ResponseResult<RoleVo> {
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<RoleVo> {
return roleService.update(roleUpdateParam)?.let {
ResponseResult.databaseSuccess(
ResponseCode.DATABASE_UPDATE_SUCCESS, data = it
)
} ?: let { ResponseResult.databaseFail(ResponseCode.DATABASE_UPDATE_FILED) }
}
@Operation(summary = "修改角色状态")

View File

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

View File

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

View File

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

View File

@@ -15,5 +15,11 @@ import top.fatweb.api.entity.permission.Role
*/
@Mapper
interface RoleMapper : BaseMapper<Role> {
fun selectPage(page: IPage<Role>): IPage<Role>
fun selectPage(page: IPage<Long>): IPage<Long>
fun getWithPowerByList(roleIds: List<Long>): List<Role>?
fun selectOne(id: Long): Role?
fun getPowerList(id: Long): List<Long?>
}

View File

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

View File

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

View File

@@ -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
/**
* <p>
@@ -16,9 +19,13 @@ import top.fatweb.api.param.authentication.RoleGetParam
* @since 2023-10-25
*/
interface IRoleService : IService<Role> {
fun getPage(roleGetParam: RoleGetParam?): IPage<Role>
fun getPage(roleGetParam: RoleGetParam?): PageVo<RoleWithPowerVo>
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
}

View File

@@ -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
/**
* <p>
@@ -28,39 +32,95 @@ import top.fatweb.api.util.PageUtil
class RoleServiceImpl(
private val powerRoleService: IPowerRoleService
) : ServiceImpl<RoleMapper, Role>(), IRoleService {
override fun getPage(roleGetParam: RoleGetParam?): IPage<Role> {
val rolePage = Page<Role>(roleGetParam?.currentPage ?: 1, roleGetParam?.pageSize ?: 20)
override fun getPage(roleGetParam: RoleGetParam?): PageVo<RoleWithPowerVo> {
val roleIdsPage = Page<Long>(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<Role>(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<Long> = 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<Long> = 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<Long>()
val removePowerIds = HashSet<Long>()
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))
}

View File

@@ -1,7 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.fatweb.api.mapper.permission.RoleMapper">
<select id="selectPage" resultMap="roleWithPowerMap">
<select id="selectPage" resultType="long">
select id
from t_role
where deleted = 0
</select>
<select id="getWithPowerByList" resultMap="roleWithPowerMap">
select distinct t_role.id as role_id,
t_role.name as role_name,
t_role.enable as role_enable,
t_role.deleted as role_deleted,
t_role.version as role_version,
tm.id as module_id,
tm.name as module_name,
tm.power_id as module_power_id,
tmn.id as menu_id,
tmn.name as menu_name,
tmn.url as menu_url,
tmn.power_id as menu_power_id,
tmn.parent_id as menu_parent_id,
tmn.module_id as menu_module_id,
te.id as element_id,
te.name as element_name,
te.power_id as element_power_id,
te.parent_id as element_parent_id,
te.menu_id as element_menu_id,
t.id as operation_id,
t.name as operation_name,
t.code as operation_code,
t.power_id as operation_power_id,
t.element_id as operation_element_id
from (select * from t_role where deleted = 0) as t_role
left join (select * from t_power_role where deleted = 0) as tpr on t_role.id = tpr.role_id
left join t_power tp on tp.id = tpr.power_id
left join t_module tm on tp.id = tm.power_id
left join t_menu tmn on tp.id = tmn.power_id
left join t_element te on tp.id = te.power_id
left join t_operation t on tp.id = t.power_id
<where>
<foreach collection="roleIds" item="item" index="index" open="and t_role.id in (" separator="," close=")"
nullable="true">
#{item}
</foreach>
</where>
</select>
<select id="selectOne" resultMap="roleWithPowerMap">
select distinct t_role.id as role_id,
t_role.name as role_name,
t_role.enable as role_enable,
@@ -33,6 +79,14 @@
left join t_menu tmn on tp.id = tmn.power_id
left join t_element te on tp.id = te.power_id
left join t_operation t on tp.id = t.power_id
where t_role.id = #{id}
</select>
<select id="getPowerList" resultType="long">
select tpr.power_id
from t_role
left join (select * from t_power_role where deleted = 0) as tpr on t_role.id = tpr.role_id
where t_role.id = #{id}
</select>
<resultMap id="roleMap" type="role">