Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api for role business #55

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions api/src/main/java/com/wansensoft/api/role/SysRoleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wansensoft.dto.role.AddOrUpdateRoleDTO;
import com.wansensoft.dto.role.RoleListDTO;
import com.wansensoft.service.role.ISysRoleService;
import com.wansensoft.service.role.KtSysRoleService;
Expand All @@ -38,13 +39,10 @@ public class SysRoleController {

private final ISysMenuService menuService;

private final ISysRoleService roleService;

private final KtSysRoleService ktSysRoleService;

public SysRoleController(ISysMenuService menuService, ISysRoleService roleService, KtSysRoleService ktSysRoleService) {
public SysRoleController(ISysMenuService menuService, KtSysRoleService ktSysRoleService) {
this.menuService = menuService;
this.roleService = roleService;
this.ktSysRoleService = ktSysRoleService;
}

Expand All @@ -68,4 +66,13 @@ public Response<String> updateStatus(@RequestParam(value = "id") String id, @Req
return ktSysRoleService.updateStatus(id, status);
}

@PostMapping("addOrUpdateRole")
public Response<String> addOrUpdateRole(@RequestBody AddOrUpdateRoleDTO addOrUpdateRoleDTO) {
return ktSysRoleService.addOrUpdateRole(addOrUpdateRoleDTO);
}

@PostMapping("deleteRole")
public Response<String> deleteRole(@RequestParam(value = "id") String id) {
return ktSysRoleService.deleteRole(id);
}
}
4 changes: 2 additions & 2 deletions domain/src/main/java/com/wansensoft/dto/PageSizeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class PageSizeDTO {
/**
* 查询列表总记录数
*/
int page = 0;
Long page = 0L;

/**
* 每页显示条数,默认10
*/
int pageSize = 10;
Long pageSize = 10L;

}
22 changes: 22 additions & 0 deletions domain/src/main/java/com/wansensoft/dto/role/AddOrUpdateRoleDTO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wansensoft.dto.role

import com.wansensoft.dto.PageSizeDTO
import lombok.Data
import lombok.EqualsAndHashCode

@Data
@EqualsAndHashCode(callSuper = true)
class AddOrUpdateRoleDTO : PageSizeDTO() {

var id : Long? = null

var roleName: String? = null

var type: String? = null

var priceLimit: Int? = null

var status: Int? = null

var description: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;

import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
Expand All @@ -32,6 +34,7 @@
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_role")

public class SysRole implements Serializable {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wansensoft.service.role

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.IService
import com.wansensoft.dto.role.AddOrUpdateRoleDTO
import com.wansensoft.dto.role.RoleListDTO
import com.wansensoft.entities.role.SysRole
import com.wansensoft.utils.response.Response
Expand All @@ -14,4 +15,8 @@ interface KtSysRoleService : IService<SysRole> {
fun rolePageList(roleListDTO: RoleListDTO?) : Response<Page<RoleVO>>

fun updateStatus(id: String?, status: Int?) : Response<String>

fun addOrUpdateRole(addOrUpdateRoleDTO : AddOrUpdateRoleDTO?) : Response<String>

fun deleteRole(id: String?): Response<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package com.wansensoft.service.role
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.wansensoft.dto.role.AddOrUpdateRoleDTO
import com.wansensoft.dto.role.RoleListDTO
import com.wansensoft.entities.role.SysRole
import com.wansensoft.mappers.role.SysRoleMapper
import com.wansensoft.utils.SnowflakeIdUtil
import com.wansensoft.utils.constants.CommonConstants
import com.wansensoft.utils.enums.BaseCodeEnum
import com.wansensoft.utils.enums.RoleCodeEnum
import com.wansensoft.utils.response.Response
import com.wansensoft.vo.RoleVO
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils
import java.time.LocalDateTime

@Service
open class KtSysRoleServiceImpl(private val roleMapper: SysRoleMapper)
Expand All @@ -21,7 +25,9 @@ open class KtSysRoleServiceImpl(private val roleMapper: SysRoleMapper)
override fun roleList(): Response<List<RoleVO>> {
val roles = ArrayList<RoleVO>()

val sysRoles = lambdaQuery().list()
val sysRoles = lambdaQuery()
.eq(SysRole::getDeleteFlag, CommonConstants.NOT_DELETED)
.list()
sysRoles.forEach { item ->
val roleVo = RoleVO()
BeanUtils.copyProperties(item, roleVo)
Expand All @@ -32,34 +38,38 @@ open class KtSysRoleServiceImpl(private val roleMapper: SysRoleMapper)
}

override fun rolePageList(roleListDTO: RoleListDTO?): Response<Page<RoleVO>> {
val result = Page<RoleVO>()
val listVo = ArrayList<RoleVO>()

val rolePage = roleListDTO?.let { Page<SysRole>(roleListDTO.page.toLong(), it.pageSize.toLong()) }
val roleWrapper = LambdaQueryWrapper<SysRole>()

roleWrapper.eq(StringUtils.hasText(roleListDTO!!.roleName), SysRole::getRoleName, roleListDTO.roleName)

roleWrapper.eq(roleListDTO.status != null, SysRole::getStatus, roleListDTO.status)
val rolePage = roleListDTO?.let { Page<SysRole>(it.page, it.pageSize) }
val roleWrapper = LambdaQueryWrapper<SysRole>().apply {
roleListDTO?.roleName?.let { eq(SysRole::getRoleName, it) }
roleListDTO?.status?.let { eq(SysRole::getStatus, it) }
eq(SysRole::getDeleteFlag, CommonConstants.NOT_DELETED)
}

roleMapper.selectPage(rolePage, roleWrapper)
if (rolePage!!.records.isNotEmpty()) {
rolePage.records.forEach { role ->
val roleVo = RoleVO()
BeanUtils.copyProperties(role, roleVo)
listVo.add(roleVo)
val result = rolePage?.run {
roleMapper.selectPage(this, roleWrapper)
if (records.isNotEmpty()) {
val listVo = records.map { role ->
RoleVO().apply {
BeanUtils.copyProperties(role, this)
}
}
Page<RoleVO>().apply {
records = listVo
total = this@run.total
pages = this@run.pages
size = this@run.size
}
} else {
Page<RoleVO>()
}
}
result.records = listVo
result.total = rolePage.total
result.size = rolePage.size
result.pages = rolePage.pages
} ?: Page<RoleVO>()

return Response.responseData(result)
}


override fun updateStatus(id: String?, status: Int?): Response<String> {
if (!StringUtils.hasText(id) && status == null) {
if (id.isNullOrBlank() || status == null) {
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL)
}

Expand All @@ -68,10 +78,72 @@ open class KtSysRoleServiceImpl(private val roleMapper: SysRoleMapper)
.set(SysRole::getStatus, status)
.update()

if (!updateResult) {
return Response.responseMsg(RoleCodeEnum.UPDATE_ROLE_STATUS_ERROR)
return if (updateResult) {
Response.responseMsg(RoleCodeEnum.UPDATE_ROLE_STATUS_SUCCESS)
} else {
Response.responseMsg(RoleCodeEnum.UPDATE_ROLE_STATUS_ERROR)
}
}

override fun addOrUpdateRole(addOrUpdateRoleDTO: AddOrUpdateRoleDTO?): Response<String> {
return addOrUpdateRoleDTO?.let { dto ->
if (dto.id == null) {
// add
val sysRole = SysRole().apply {
id = SnowflakeIdUtil.nextId()
roleName = dto.roleName
type = dto.type
priceLimit = dto.priceLimit
status = dto.status
description = dto.description
createTime = LocalDateTime.now()
}

val saveResult = save(sysRole)
if (!saveResult) {
Response.responseMsg(RoleCodeEnum.ADD_ROLE_ERROR)
} else {
Response.responseMsg(RoleCodeEnum.ADD_ROLE_SUCCESS)
}
} else {
// update
val updateResult = lambdaUpdate().apply {
eq(SysRole::getId, dto.id)
set(SysRole::getRoleName, dto.roleName)
set(SysRole::getType, dto.type)
set(SysRole::getStatus, dto.status)
set(SysRole::getPriceLimit, dto.priceLimit)
set(SysRole::getDescription, dto.description)
set(SysRole::getUpdateTime, LocalDateTime.now())
}.update()

return Response.responseMsg(RoleCodeEnum.UPDATE_ROLE_STATUS_SUCCESS)
if (!updateResult) {
Response.responseMsg(RoleCodeEnum.UPDATE_ROLE_ERROR)
} else {
Response.responseMsg(RoleCodeEnum.UPDATE_ROLE_SUCCESS)
}
}
} ?: Response.responseMsg(BaseCodeEnum.PARAMETER_NULL)
}

override fun deleteRole(id: String?): Response<String> {
id?.let { roleId ->
if (roleId.isBlank()) {
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL)
}
val deleteResult = lambdaUpdate()
.eq(SysRole::getId, roleId)
.set(SysRole::getDeleteFlag, CommonConstants.DELETED)
.update()

return if (deleteResult) {
Response.responseMsg(RoleCodeEnum.DELETE_ROLE_SUCCESS)
} else {
Response.responseMsg(RoleCodeEnum.DELETE_ROLE_ERROR)
}
}
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL)
}


}
14 changes: 13 additions & 1 deletion utils/src/main/java/com/wansensoft/utils/enums/RoleCodeEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
@Getter
public enum RoleCodeEnum {

ADD_ROLE_SUCCESS("A0004", "添加角色成功"),

ADD_ROLE_ERROR("A0207", "添加角色失败"),

UPDATE_ROLE_STATUS_SUCCESS("A0005", "角色状态修改成功"),

UPDATE_ROLE_STATUS_ERROR("A0208", "角色状态修改失败");
UPDATE_ROLE_STATUS_ERROR("A0208", "角色状态修改失败"),

UPDATE_ROLE_SUCCESS("A0006", "修改角色资料成功"),

UPDATE_ROLE_ERROR("A0209", "修改角色资料失败"),

DELETE_ROLE_SUCCESS("A0007", "删除角色成功"),

DELETE_ROLE_ERROR("A0210", "删除角色失败");

/**
* 响应状态码
Expand Down