284 lines
11 KiB
Java
284 lines
11 KiB
Java
package com.cdzy.user.controller;
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
import com.cdzy.common.model.JsonResult;
|
|
import com.cdzy.common.model.PageParam;
|
|
import com.cdzy.common.model.StaffDto;
|
|
import com.cdzy.user.model.dto.RoleListQueryDto;
|
|
import com.cdzy.user.model.dto.ResRolesDto;
|
|
import com.cdzy.user.model.dto.RolePermissionsDto;
|
|
import com.cdzy.user.model.dto.RolesDto;
|
|
import com.cdzy.user.model.entity.RolePermissions;
|
|
import com.cdzy.user.model.entity.Roles;
|
|
import com.cdzy.user.model.entity.Staff;
|
|
import com.cdzy.user.service.PermissionsService;
|
|
import com.cdzy.user.service.RolePermissionsService;
|
|
import com.cdzy.user.service.RolesService;
|
|
import com.cdzy.user.service.StaffService;
|
|
import com.mybatisflex.core.paginate.Page;
|
|
import com.mybatisflex.core.query.QueryMethods;
|
|
import com.mybatisflex.core.query.QueryWrapper;
|
|
import jakarta.annotation.Resource;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static com.cdzy.user.model.entity.table.OrganizationsTableDef.ORGANIZATIONS;
|
|
import static com.cdzy.user.model.entity.table.PermissionsTableDef.PERMISSIONS;
|
|
import static com.cdzy.user.model.entity.table.RolePermissionsTableDef.ROLE_PERMISSIONS;
|
|
import static com.cdzy.user.model.entity.table.RolesTableDef.ROLES;
|
|
import static com.cdzy.user.model.entity.table.StaffRolesTableDef.STAFF_ROLES;
|
|
import static com.cdzy.user.model.entity.table.StaffTableDef.STAFF;
|
|
|
|
/**
|
|
* 角色控制层。
|
|
*
|
|
* @author attiya
|
|
* @since 2025-03-14
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/roles")
|
|
public class RolesController {
|
|
|
|
@Resource
|
|
private RolesService rolesService;
|
|
|
|
@Autowired
|
|
private PermissionsService permissionsService;
|
|
@Resource
|
|
private StaffService staffService;
|
|
|
|
@Autowired
|
|
private RolePermissionsService rolePermissionsService;
|
|
|
|
/**
|
|
* 添加。
|
|
*
|
|
* @param roles 角色信息
|
|
* @return {@code true} 添加成功,{@code false} 添加失败
|
|
*/
|
|
@PostMapping("save")
|
|
public JsonResult<?> save(@RequestBody Roles roles) {
|
|
rolesService.save(roles);
|
|
return JsonResult.success();
|
|
}
|
|
|
|
/**
|
|
* 根据主键删除。
|
|
*
|
|
* @param id 主键
|
|
* @return {@code true} 删除成功,{@code false} 删除失败
|
|
*/
|
|
@DeleteMapping("remove/{id}")
|
|
public JsonResult<?> remove(@PathVariable("id") Long id) {
|
|
rolesService.removeById(id);
|
|
return JsonResult.success();
|
|
}
|
|
|
|
/**
|
|
* 根据主键更新。
|
|
*
|
|
* @param roles 角色信息
|
|
* @return {@code true} 更新成功,{@code false} 更新失败
|
|
*/
|
|
@PostMapping("update")
|
|
public JsonResult<?> update(@RequestBody Roles roles) {
|
|
rolesService.updateById(roles);
|
|
return JsonResult.success();
|
|
}
|
|
|
|
/**
|
|
* 查询所有。
|
|
*
|
|
* @return 所有数据
|
|
*/
|
|
@GetMapping("list")
|
|
public JsonResult<?> list() {
|
|
List<Roles> list = rolesService.list();
|
|
return JsonResult.success(list);
|
|
}
|
|
|
|
/**
|
|
* 根据主键获取详细信息。
|
|
*
|
|
* @param id 主键
|
|
* @return 详情
|
|
*/
|
|
@GetMapping("getInfo/{id}")
|
|
public JsonResult<?> getInfo(@PathVariable("id") Long id) {
|
|
Roles roles = rolesService.getById(id);
|
|
return JsonResult.success(roles);
|
|
}
|
|
|
|
/**
|
|
* 分页查询。
|
|
*
|
|
* @param pageParam 分页对象
|
|
* @return 分页对象
|
|
*/
|
|
@PostMapping("page")
|
|
public JsonResult<?> page(@Validated PageParam pageParam) {
|
|
Page<Roles> page = rolesService.page(pageParam.getPage());
|
|
return JsonResult.success(page);
|
|
}
|
|
|
|
/**
|
|
* 获取当前登陆用户的所有角色码。
|
|
*
|
|
* @return 分页对象
|
|
*/
|
|
@GetMapping("roleCodeList")
|
|
public JsonResult<?> listByStaffId(@RequestParam("staffId") String staffId) {
|
|
QueryWrapper queryWrapper = QueryWrapper.create().select(ROLES.ROLE_CODE).leftJoin(STAFF_ROLES).on(STAFF_ROLES.ROLE_ID.eq(ROLES.ROLE_CODE)).where(STAFF_ROLES.STAFF_ID.eq(staffId));
|
|
List<String> list = rolesService.listAs(queryWrapper, String.class);
|
|
return JsonResult.success(list);
|
|
}
|
|
|
|
/**
|
|
* 分页查询角色列表
|
|
*
|
|
* @param roleListQueryDto
|
|
* @return
|
|
*/
|
|
@PostMapping("pageQueryRoles")
|
|
public JsonResult<?> pageQueryRoles(@RequestBody @Validated RoleListQueryDto roleListQueryDto) {
|
|
|
|
String tokenValue = StpUtil.getTokenValue();
|
|
Object loginId = StpUtil.getLoginIdByToken(tokenValue);
|
|
QueryWrapper queryStaff = QueryWrapper.create().where(STAFF.STAFF_ID.eq(loginId));
|
|
StaffDto staff = staffService.getOneAs(queryStaff, StaffDto.class);
|
|
|
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
|
.from(ROLES).select(STAFF.STAFF_ID, ROLES.ALL_COLUMNS, ORGANIZATIONS.ORG_NAME)
|
|
.leftJoin(STAFF_ROLES).on(ROLES.ROLE_ID.eq(STAFF_ROLES.ROLE_ID))
|
|
.leftJoin(STAFF).on(STAFF.STAFF_ID.eq(STAFF_ROLES.STAFF_ID))
|
|
.leftJoin(ORGANIZATIONS).on(STAFF.ORG_ID.eq(ORGANIZATIONS.ORG_ID))
|
|
.where(ROLES.ORG_ID.eq(staff.getOrgId())).and(ROLES.ROLE_NAME.eq(roleListQueryDto.getRoleName(),
|
|
StringUtils.hasText(roleListQueryDto.getRoleName())));
|
|
|
|
QueryWrapper countQuery = QueryWrapper.create()
|
|
.select(QueryMethods.count(STAFF.STAFF_ID.getName()).as("staff_count"))
|
|
.select(ROLES.ROLE_NAME.getName(), ROLES.ROLE_ID.getName(),
|
|
ROLES.ROLE_DESCRIPTION.getName(), ROLES.ROLE_CODE.getName(),
|
|
ORGANIZATIONS.ORG_NAME.getName(), ORGANIZATIONS.ORG_ID.getName())
|
|
.groupBy(ORGANIZATIONS.ORG_NAME, ROLES.ROLE_NAME)
|
|
.from(queryWrapper).as("a");
|
|
|
|
Page<ResRolesDto> page = Page.of(roleListQueryDto.getPageNum(), roleListQueryDto.getPageSize());
|
|
Page<ResRolesDto> resRolesDtoPage = rolesService.pageAs(page, countQuery, ResRolesDto.class);
|
|
List<ResRolesDto> records = resRolesDtoPage.getRecords();
|
|
//统计pc权限数量和小程序权限数量
|
|
for (ResRolesDto record : records) {
|
|
QueryWrapper caseQuery = QueryWrapper.create().select(QueryMethods.sum(QueryMethods.case_().when(PERMISSIONS.PERM_TYPE.eq(1)).then(1).else_(0).end()).as("web_count"), QueryMethods.sum(QueryMethods.case_().when(PERMISSIONS.PERM_TYPE.eq(2)).then(1).else_(0).end()).as("applet_count")).innerJoin(ROLE_PERMISSIONS).on(PERMISSIONS.PERM_ID.eq(ROLE_PERMISSIONS.PERM_ID)).where(ROLE_PERMISSIONS.ROLE_ID.eq(record.getRoleId())).where(PERMISSIONS.PERM_TYPE.ne("0"));
|
|
ResRolesDto oneAs = permissionsService.getOneAs(caseQuery, ResRolesDto.class);
|
|
if (oneAs == null) continue;
|
|
record.setWebCount(oneAs.getWebCount());
|
|
record.setAppletCount(oneAs.getAppletCount());
|
|
}
|
|
return JsonResult.success(resRolesDtoPage);
|
|
}
|
|
|
|
|
|
/**
|
|
* 角色信息新增
|
|
*
|
|
* @param rolesDto
|
|
* @return
|
|
*/
|
|
@Transactional
|
|
@PostMapping("rolePermissionsAdd")
|
|
public JsonResult<?> rolePermissionsAdd(@RequestBody RolesDto rolesDto) {
|
|
|
|
try {
|
|
Roles roles = new Roles();
|
|
String tokenValue = StpUtil.getTokenValue();
|
|
Object loginId = StpUtil.getLoginIdByToken(tokenValue);
|
|
Staff staff = staffService.getById((Serializable) loginId);
|
|
BeanUtils.copyProperties(rolesDto, roles);
|
|
roles.setOrgId(staff.getOrgId());
|
|
roles.setCreatedStaff(Long.parseLong(loginId.toString()));
|
|
rolesService.save(roles);
|
|
List<RolePermissionsDto> rolePermissionsDtos = rolesDto.getRolePermissions();
|
|
List<RolePermissions> rolePermissions = new ArrayList<>();
|
|
for (RolePermissionsDto rolePermissionsDto : rolePermissionsDtos) {
|
|
RolePermissions roleperm = new RolePermissions();
|
|
BeanUtils.copyProperties(rolePermissionsDto, roleperm);
|
|
roleperm.setRoleId(roles.getRoleId());
|
|
rolePermissions.add(roleperm);
|
|
}
|
|
if (!rolePermissions.isEmpty()) {
|
|
rolePermissionsService.saveBatch(rolePermissions);
|
|
}
|
|
return JsonResult.success("保存成功!");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return JsonResult.failed("保存失败!");
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 角色信息更新
|
|
*
|
|
* @param rolesDto
|
|
* @return
|
|
*/
|
|
@Transactional
|
|
@PostMapping("rolePermissionsUpdate")
|
|
public JsonResult<?> rolePermissionsUpdate(@RequestBody RolesDto rolesDto) {
|
|
|
|
try {
|
|
Roles roles = new Roles();
|
|
BeanUtils.copyProperties(rolesDto, roles);
|
|
rolesService.updateById(roles);
|
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
|
.eq(ROLE_PERMISSIONS.ROLE_ID.getName(), roles.getRoleId());
|
|
rolePermissionsService.remove(queryWrapper);
|
|
List<RolePermissionsDto> rolePermissionsDtos = rolesDto.getRolePermissions();
|
|
List<RolePermissions> rolePermissions = new ArrayList<>();
|
|
for (RolePermissionsDto rolePermissionsDto : rolePermissionsDtos) {
|
|
RolePermissions roleperm = new RolePermissions();
|
|
BeanUtils.copyProperties(rolePermissionsDto, roleperm);
|
|
roleperm.setRoleId(roles.getRoleId());
|
|
rolePermissions.add(roleperm);
|
|
}
|
|
if (!rolePermissions.isEmpty()) {
|
|
rolePermissionsService.saveBatch(rolePermissions);
|
|
}
|
|
return JsonResult.success("保存成功!");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return JsonResult.failed("保存失败!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 角色信息更新
|
|
*
|
|
* @param roleId
|
|
* @return
|
|
*/
|
|
@Transactional
|
|
@GetMapping("rolePermissionsDel/{roleId}")
|
|
public JsonResult<?> rolePermissionsDel(@PathVariable("roleId") Long roleId) {
|
|
|
|
try {
|
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
|
.eq(ROLE_PERMISSIONS.ROLE_ID.getName(), roleId);
|
|
rolePermissionsService.remove(queryWrapper);
|
|
rolesService.removeById(roleId);
|
|
return JsonResult.success("删除成功!");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return JsonResult.failed("删除失败!");
|
|
}
|
|
}
|
|
}
|