mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-17 18:46:36 +08:00
commit:同步2.0版本(添加指定审批人功能)
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.orange.demo.upmsservice.config;
|
||||
|
||||
import com.orange.demo.common.core.constant.ApplicationConstant;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 表示数据源类型的常量对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public final class DataSourceType {
|
||||
|
||||
public static final int MAIN = 0;
|
||||
/**
|
||||
* 对于多数据源服务,操作日志的数据源类型是固定值。如果有冲突,可以直接修改
|
||||
* ApplicationConstant.OPERATION_LOG_DATASOURCE_TYPE的值。
|
||||
* 如果保存SysOperationLog操作日志的数据和其他业务位于同库,为了便于今后的
|
||||
* 迁移,这里也尽量要给其配置单独的数据源类型,今后数据库拆分时,可以直接修改
|
||||
* 该值对应的配置项即可。
|
||||
*/
|
||||
public static final int OPERATION_LOG = ApplicationConstant.OPERATION_LOG_DATASOURCE_TYPE;
|
||||
|
||||
private static final Map<String, Integer> TYPE_MAP = new HashMap<>(2);
|
||||
static {
|
||||
TYPE_MAP.put("main", MAIN);
|
||||
TYPE_MAP.put("operation-log", OPERATION_LOG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据名称获取字典类型。
|
||||
*
|
||||
* @param name 数据源在配置中的名称。
|
||||
* @return 返回可用于多数据源切换的数据源类型。
|
||||
*/
|
||||
public static Integer getDataSourceTypeByName(String name) {
|
||||
return TYPE_MAP.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有构造函数,明确标识该常量类的作用。
|
||||
*/
|
||||
private DataSourceType() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.orange.demo.upmsservice.config;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
import com.orange.demo.common.core.config.DynamicDataSource;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 多数据源配置对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@MapperScan(value = {"com.orange.demo.*.dao", "com.orange.demo.common.*.dao"})
|
||||
public class MultiDataSourceConfig {
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.druid.main")
|
||||
public DataSource mainDataSource() {
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认生成的用于保存操作日志的数据源,可根据需求修改。
|
||||
* 这里我们还是非常推荐给操作日志使用独立的数据源,这样便于今后的数据迁移。
|
||||
*/
|
||||
@Bean(initMethod = "init", destroyMethod = "close")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.druid.operation-log")
|
||||
public DataSource operationLogDataSource() {
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public DynamicDataSource dataSource() {
|
||||
Map<Object, Object> targetDataSources = new HashMap<>(1);
|
||||
targetDataSources.put(DataSourceType.MAIN, mainDataSource());
|
||||
targetDataSources.put(DataSourceType.OPERATION_LOG, operationLogDataSource());
|
||||
// 如果当前工程支持在线表单,这里请务必保证upms数据表所在数据库为缺省数据源。
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
dynamicDataSource.setTargetDataSources(targetDataSources);
|
||||
dynamicDataSource.setDefaultTargetDataSource(mainDataSource());
|
||||
return dynamicDataSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
package com.orange.demo.upmsservice.controller;
|
||||
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
import io.swagger.annotations.Api;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.page.PageMethod;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.orange.demo.common.core.validator.UpdateGroup;
|
||||
import com.orange.demo.common.core.constant.ErrorCodeEnum;
|
||||
import com.orange.demo.common.core.object.*;
|
||||
import com.orange.demo.common.core.util.MyModelUtil;
|
||||
import com.orange.demo.common.core.util.MyCommonUtil;
|
||||
import com.orange.demo.common.core.util.MyPageUtil;
|
||||
import com.orange.demo.common.core.annotation.MyRequestBody;
|
||||
import com.orange.demo.common.log.annotation.OperationLog;
|
||||
import com.orange.demo.common.log.model.constant.SysOperationLogType;
|
||||
import com.orange.demo.upmsapi.dto.SysDataPermDto;
|
||||
import com.orange.demo.upmsapi.dto.SysUserDto;
|
||||
import com.orange.demo.upmsapi.vo.SysDataPermVo;
|
||||
import com.orange.demo.upmsapi.vo.SysUserVo;
|
||||
import com.orange.demo.upmsservice.model.SysDataPerm;
|
||||
import com.orange.demo.upmsservice.model.SysUser;
|
||||
import com.orange.demo.upmsservice.service.SysDataPermService;
|
||||
import com.orange.demo.upmsservice.service.SysUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.groups.Default;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据权限接口控制器对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Api(tags = "数据权限管理接口")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sysDataPerm")
|
||||
public class SysDataPermController {
|
||||
|
||||
@Autowired
|
||||
private SysDataPermService sysDataPermService;
|
||||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
|
||||
/**
|
||||
* 添加新数据权限。
|
||||
*
|
||||
* @param sysDataPermDto 新增对象。
|
||||
* @param deptIdListString 数据权限关联的部门Id列表,多个之间逗号分隔。
|
||||
* @return 应答结果对象。包含新增数据权限对象的主键Id。
|
||||
*/
|
||||
@ApiOperationSupport(ignoreParameters = {
|
||||
"sysDataPermDto.dataPermId",
|
||||
"sysDataPermDto.createTimeStart",
|
||||
"sysDataPermDto.createTimeEnd",
|
||||
"sysDataPermDto.searchString"})
|
||||
@OperationLog(type = SysOperationLogType.ADD)
|
||||
@PostMapping("/add")
|
||||
public ResponseResult<Long> add(
|
||||
@MyRequestBody SysDataPermDto sysDataPermDto, @MyRequestBody String deptIdListString) {
|
||||
String errorMessage = MyCommonUtil.getModelValidationError(sysDataPermDto);
|
||||
if (errorMessage != null) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
|
||||
}
|
||||
SysDataPerm sysDataPerm = MyModelUtil.copyTo(sysDataPermDto, SysDataPerm.class);
|
||||
CallResult result = sysDataPermService.verifyRelatedData(sysDataPerm, deptIdListString);
|
||||
if (!result.isSuccess()) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, result.getErrorMessage());
|
||||
}
|
||||
Set<Long> deptIdSet = null;
|
||||
if (result.getData() != null) {
|
||||
deptIdSet = result.getData().getObject("deptIdSet", new TypeReference<Set<Long>>(){});
|
||||
}
|
||||
sysDataPermService.saveNew(sysDataPerm, deptIdSet);
|
||||
return ResponseResult.success(sysDataPerm.getDataPermId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据权限。
|
||||
*
|
||||
* @param sysDataPermDto 更新的数据权限对象。
|
||||
* @param deptIdListString 数据权限关联的部门Id列表,多个之间逗号分隔。
|
||||
* @return 应答结果对象。
|
||||
*/
|
||||
@ApiOperationSupport(ignoreParameters = {
|
||||
"sysDataPermDto.createTimeStart",
|
||||
"sysDataPermDto.createTimeEnd",
|
||||
"sysDataPermDto.searchString"})
|
||||
@OperationLog(type = SysOperationLogType.UPDATE)
|
||||
@PostMapping("/update")
|
||||
public ResponseResult<Void> update(
|
||||
@MyRequestBody SysDataPermDto sysDataPermDto, @MyRequestBody String deptIdListString) {
|
||||
String errorMessage = MyCommonUtil.getModelValidationError(sysDataPermDto, Default.class, UpdateGroup.class);
|
||||
if (errorMessage != null) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
|
||||
}
|
||||
SysDataPerm originalSysDataPerm = sysDataPermService.getById(sysDataPermDto.getDataPermId());
|
||||
if (originalSysDataPerm == null) {
|
||||
errorMessage = "数据验证失败,当前数据权限并不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
|
||||
}
|
||||
SysDataPerm sysDataPerm = MyModelUtil.copyTo(sysDataPermDto, SysDataPerm.class);
|
||||
CallResult result = sysDataPermService.verifyRelatedData(sysDataPerm, deptIdListString);
|
||||
if (!result.isSuccess()) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, result.getErrorMessage());
|
||||
}
|
||||
Set<Long> deptIdSet = null;
|
||||
if (result.getData() != null) {
|
||||
deptIdSet = result.getData().getObject("deptIdSet", new TypeReference<Set<Long>>(){});
|
||||
}
|
||||
if (!sysDataPermService.update(sysDataPerm, originalSysDataPerm, deptIdSet)) {
|
||||
errorMessage = "更新失败,数据不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据权限。
|
||||
*
|
||||
* @param dataPermId 待删除数据权限主键Id。
|
||||
* @return 应答数据结果。
|
||||
*/
|
||||
@OperationLog(type = SysOperationLogType.DELETE)
|
||||
@PostMapping("/delete")
|
||||
public ResponseResult<Void> delete(@MyRequestBody Long dataPermId) {
|
||||
if (MyCommonUtil.existBlankArgument(dataPermId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
|
||||
}
|
||||
if (!sysDataPermService.remove(dataPermId)) {
|
||||
String errorMessage = "数据操作失败,数据权限不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据权限列表。
|
||||
*
|
||||
* @param sysDataPermDtoFilter 数据权限查询过滤对象。
|
||||
* @param orderParam 排序参数。
|
||||
* @param pageParam 分页参数。
|
||||
* @return 应答结果对象。包含数据权限列表。
|
||||
*/
|
||||
@PostMapping("/list")
|
||||
public ResponseResult<MyPageData<SysDataPermVo>> list(
|
||||
@MyRequestBody SysDataPermDto sysDataPermDtoFilter,
|
||||
@MyRequestBody MyOrderParam orderParam,
|
||||
@MyRequestBody MyPageParam pageParam) {
|
||||
if (pageParam != null) {
|
||||
PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
|
||||
}
|
||||
SysDataPerm filter = MyModelUtil.copyTo(sysDataPermDtoFilter, SysDataPerm.class);
|
||||
String orderBy = MyOrderParam.buildOrderBy(orderParam, SysDataPerm.class);
|
||||
List<SysDataPerm> dataPermList = sysDataPermService.getSysDataPermList(filter, orderBy);
|
||||
List<SysDataPermVo> dataPermVoList = MyModelUtil.copyCollectionTo(dataPermList, SysDataPermVo.class);
|
||||
long totalCount = 0L;
|
||||
if (dataPermList instanceof Page) {
|
||||
totalCount = ((Page<SysDataPerm>) dataPermList).getTotal();
|
||||
}
|
||||
return ResponseResult.success(MyPageUtil.makeResponseData(dataPermVoList, totalCount));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看单条数据权限详情。
|
||||
*
|
||||
* @param dataPermId 数据权限的主键Id。
|
||||
* @return 应答结果对象,包含数据权限的详情。
|
||||
*/
|
||||
@GetMapping("/view")
|
||||
public ResponseResult<SysDataPermVo> view(@RequestParam Long dataPermId) {
|
||||
if (MyCommonUtil.existBlankArgument(dataPermId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
|
||||
}
|
||||
SysDataPerm sysDataPerm =
|
||||
sysDataPermService.getByIdWithRelation(dataPermId, MyRelationParam.full());
|
||||
if (sysDataPerm == null) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
|
||||
}
|
||||
SysDataPermVo sysDataPermVo = MyModelUtil.copyTo(sysDataPerm, SysDataPermVo.class);
|
||||
return ResponseResult.success(sysDataPermVo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取不包含指定数据权限Id的用户列表。
|
||||
* 用户和数据权限是多对多关系,当前接口将返回没有赋值指定DataPermId的用户列表。可用于给数据权限添加新用户。
|
||||
*
|
||||
* @param dataPermId 数据权限主键Id。
|
||||
* @param sysUserDtoFilter 用户数据的过滤对象。
|
||||
* @param orderParam 排序参数。
|
||||
* @param pageParam 分页参数。
|
||||
* @return 应答结果对象,包含用户列表数据。
|
||||
*/
|
||||
@PostMapping("/listNotInDataPermUser")
|
||||
public ResponseResult<MyPageData<SysUserVo>> listNotInDataPermUser(
|
||||
@MyRequestBody Long dataPermId,
|
||||
@MyRequestBody SysUserDto sysUserDtoFilter,
|
||||
@MyRequestBody MyOrderParam orderParam,
|
||||
@MyRequestBody MyPageParam pageParam) {
|
||||
ResponseResult<Void> verifyResult = this.doDataPermUserVerify(dataPermId);
|
||||
if (!verifyResult.isSuccess()) {
|
||||
return ResponseResult.errorFrom(verifyResult);
|
||||
}
|
||||
if (pageParam != null) {
|
||||
PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
|
||||
}
|
||||
SysUser filter = MyModelUtil.copyTo(sysUserDtoFilter, SysUser.class);
|
||||
String orderBy = MyOrderParam.buildOrderBy(orderParam, SysUser.class);
|
||||
List<SysUser> userList =
|
||||
sysUserService.getNotInSysUserListByDataPermId(dataPermId, filter, orderBy);
|
||||
List<SysUserVo> userVoList = MyModelUtil.copyCollectionTo(userList, SysUserVo.class);
|
||||
return ResponseResult.success(MyPageUtil.makeResponseData(userVoList));
|
||||
}
|
||||
|
||||
/**
|
||||
* 拥有指定数据权限的用户列表。
|
||||
*
|
||||
* @param dataPermId 数据权限Id。
|
||||
* @param sysUserDtoFilter 用户过滤对象。
|
||||
* @param orderParam 排序参数。
|
||||
* @param pageParam 分页参数。
|
||||
* @return 应答结果对象,包含用户列表数据。
|
||||
*/
|
||||
@PostMapping("/listDataPermUser")
|
||||
public ResponseResult<MyPageData<SysUserVo>> listDataPermUser(
|
||||
@MyRequestBody Long dataPermId,
|
||||
@MyRequestBody SysUserDto sysUserDtoFilter,
|
||||
@MyRequestBody MyOrderParam orderParam,
|
||||
@MyRequestBody MyPageParam pageParam) {
|
||||
ResponseResult<Void> verifyResult = this.doDataPermUserVerify(dataPermId);
|
||||
if (!verifyResult.isSuccess()) {
|
||||
return ResponseResult.errorFrom(verifyResult);
|
||||
}
|
||||
if (pageParam != null) {
|
||||
PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
|
||||
}
|
||||
SysUser filter = MyModelUtil.copyTo(sysUserDtoFilter, SysUser.class);
|
||||
String orderBy = MyOrderParam.buildOrderBy(orderParam, SysUser.class);
|
||||
List<SysUser> userList =
|
||||
sysUserService.getSysUserListByDataPermId(dataPermId, filter, orderBy);
|
||||
List<SysUserVo> userVoList = MyModelUtil.copyCollectionTo(userList, SysUserVo.class);
|
||||
return ResponseResult.success(MyPageUtil.makeResponseData(userVoList));
|
||||
}
|
||||
|
||||
private ResponseResult<Void> doDataPermUserVerify(Long dataPermId) {
|
||||
if (MyCommonUtil.existBlankArgument(dataPermId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
|
||||
}
|
||||
if (!sysDataPermService.existId(dataPermId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.INVALID_RELATED_RECORD_ID);
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 为指定数据权限添加用户列表。该操作可同时给一批用户赋值数据权限,并在同一事务内完成。
|
||||
*
|
||||
* @param dataPermId 数据权限主键Id。
|
||||
* @param userIdListString 逗号分隔的用户Id列表。
|
||||
* @return 应答结果对象。
|
||||
*/
|
||||
@OperationLog(type = SysOperationLogType.ADD_M2M)
|
||||
@PostMapping("/addDataPermUser")
|
||||
public ResponseResult<Void> addDataPermUser(
|
||||
@MyRequestBody Long dataPermId, @MyRequestBody String userIdListString) {
|
||||
if (MyCommonUtil.existBlankArgument(dataPermId, userIdListString)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
|
||||
}
|
||||
Set<Long> userIdSet =
|
||||
Arrays.stream(userIdListString.split(",")).map(Long::valueOf).collect(Collectors.toSet());
|
||||
if (!sysDataPermService.existId(dataPermId)
|
||||
|| !sysUserService.existUniqueKeyList("userId", userIdSet)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.INVALID_RELATED_RECORD_ID);
|
||||
}
|
||||
sysDataPermService.addDataPermUserList(dataPermId, userIdSet);
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 为指定用户移除指定数据权限。
|
||||
*
|
||||
* @param dataPermId 指定数据权限主键Id。
|
||||
* @param userId 指定用户主键Id。
|
||||
* @return 应答数据结果。
|
||||
*/
|
||||
@OperationLog(type = SysOperationLogType.DELETE_M2M)
|
||||
@PostMapping("/deleteDataPermUser")
|
||||
public ResponseResult<Void> deleteDataPermUser(
|
||||
@MyRequestBody Long dataPermId, @MyRequestBody Long userId) {
|
||||
if (MyCommonUtil.existBlankArgument(dataPermId, userId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
|
||||
}
|
||||
if (!sysDataPermService.removeDataPermUser(dataPermId, userId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,365 @@
|
||||
package com.orange.demo.upmsservice.controller;
|
||||
|
||||
import cn.jimmyshi.beanquery.BeanQuery;
|
||||
import com.orange.demo.common.log.annotation.OperationLog;
|
||||
import com.orange.demo.common.log.model.constant.SysOperationLogType;
|
||||
import com.github.pagehelper.page.PageMethod;
|
||||
import com.orange.demo.upmsservice.model.*;
|
||||
import com.orange.demo.upmsservice.service.*;
|
||||
import com.orange.demo.upmsapi.dto.*;
|
||||
import com.orange.demo.upmsapi.vo.*;
|
||||
import com.orange.demo.common.core.object.*;
|
||||
import com.orange.demo.common.core.util.*;
|
||||
import com.orange.demo.common.core.constant.*;
|
||||
import com.orange.demo.common.core.base.controller.BaseController;
|
||||
import com.orange.demo.common.core.base.service.IBaseService;
|
||||
import com.orange.demo.common.core.annotation.MyRequestBody;
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
import io.swagger.annotations.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 部门管理操作控制器类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Api(tags = "部门管理管理接口")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sysDept")
|
||||
public class SysDeptController extends BaseController<SysDept, SysDeptVo, Long> {
|
||||
|
||||
@Autowired
|
||||
private SysDeptService sysDeptService;
|
||||
|
||||
@Override
|
||||
protected IBaseService<SysDept, Long> service() {
|
||||
return sysDeptService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增部门管理数据。
|
||||
*
|
||||
* @param sysDeptDto 新增对象。
|
||||
* @return 应答结果对象,包含新增对象主键Id。
|
||||
*/
|
||||
@ApiOperationSupport(ignoreParameters = {"sysDeptDto.deptId"})
|
||||
@OperationLog(type = SysOperationLogType.ADD)
|
||||
@PostMapping("/add")
|
||||
public ResponseResult<Long> add(@MyRequestBody SysDeptDto sysDeptDto) {
|
||||
String errorMessage = MyCommonUtil.getModelValidationError(sysDeptDto, false);
|
||||
if (errorMessage != null) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
|
||||
}
|
||||
SysDept sysDept = MyModelUtil.copyTo(sysDeptDto, SysDept.class);
|
||||
// 验证父Id的数据合法性
|
||||
SysDept parentSysDept = null;
|
||||
if (MyCommonUtil.isNotBlankOrNull(sysDept.getParentId())) {
|
||||
parentSysDept = sysDeptService.getById(sysDept.getParentId());
|
||||
if (parentSysDept == null) {
|
||||
errorMessage = "数据验证失败,关联的父节点并不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_PARENT_ID_NOT_EXIST, errorMessage);
|
||||
}
|
||||
}
|
||||
sysDept = sysDeptService.saveNew(sysDept, parentSysDept);
|
||||
return ResponseResult.success(sysDept.getDeptId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新部门管理数据。
|
||||
*
|
||||
* @param sysDeptDto 更新对象。
|
||||
* @return 应答结果对象。
|
||||
*/
|
||||
@OperationLog(type = SysOperationLogType.UPDATE)
|
||||
@PostMapping("/update")
|
||||
public ResponseResult<Void> update(@MyRequestBody SysDeptDto sysDeptDto) {
|
||||
String errorMessage = MyCommonUtil.getModelValidationError(sysDeptDto, true);
|
||||
if (errorMessage != null) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
|
||||
}
|
||||
SysDept sysDept = MyModelUtil.copyTo(sysDeptDto, SysDept.class);
|
||||
SysDept originalSysDept = sysDeptService.getById(sysDept.getDeptId());
|
||||
if (originalSysDept == null) {
|
||||
// NOTE: 修改下面方括号中的话述
|
||||
errorMessage = "数据验证失败,当前 [数据] 并不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
|
||||
}
|
||||
// 验证父Id的数据合法性
|
||||
if (MyCommonUtil.isNotBlankOrNull(sysDept.getParentId())
|
||||
&& ObjectUtils.notEqual(sysDept.getParentId(), originalSysDept.getParentId())) {
|
||||
SysDept parentSysDept = sysDeptService.getById(sysDept.getParentId());
|
||||
if (parentSysDept == null) {
|
||||
errorMessage = "数据验证失败,关联的父节点并不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_PARENT_ID_NOT_EXIST, errorMessage);
|
||||
}
|
||||
}
|
||||
if (!sysDeptService.update(sysDept, originalSysDept)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除部门管理数据。
|
||||
*
|
||||
* @param deptId 删除对象主键Id。
|
||||
* @return 应答结果对象。
|
||||
*/
|
||||
@OperationLog(type = SysOperationLogType.DELETE)
|
||||
@PostMapping("/delete")
|
||||
public ResponseResult<Void> delete(@MyRequestBody Long deptId) {
|
||||
String errorMessage;
|
||||
if (MyCommonUtil.existBlankArgument(deptId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
|
||||
}
|
||||
// 验证关联Id的数据合法性
|
||||
SysDept originalSysDept = sysDeptService.getById(deptId);
|
||||
if (originalSysDept == null) {
|
||||
// NOTE: 修改下面方括号中的话述
|
||||
errorMessage = "数据验证失败,当前 [对象] 并不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
|
||||
}
|
||||
if (sysDeptService.hasChildren(deptId)) {
|
||||
// NOTE: 修改下面方括号中的话述
|
||||
errorMessage = "数据验证失败,当前 [对象存在子对象],请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.HAS_CHILDREN_DATA, errorMessage);
|
||||
}
|
||||
if (sysDeptService.hasChildrenUser(deptId)) {
|
||||
errorMessage = "数据验证失败,请先移除部门用户数据后,再删除当前部门!";
|
||||
return ResponseResult.error(ErrorCodeEnum.HAS_CHILDREN_DATA, errorMessage);
|
||||
}
|
||||
if (!sysDeptService.remove(deptId)) {
|
||||
errorMessage = "数据操作失败,删除的对象不存在,请刷新后重试!";
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出符合过滤条件的部门管理列表。
|
||||
*
|
||||
* @param sysDeptDtoFilter 过滤对象。
|
||||
* @param orderParam 排序参数。
|
||||
* @param pageParam 分页参数。
|
||||
* @return 应答结果对象,包含查询结果集。
|
||||
*/
|
||||
@PostMapping("/list")
|
||||
public ResponseResult<MyPageData<SysDeptVo>> list(
|
||||
@MyRequestBody SysDeptDto sysDeptDtoFilter,
|
||||
@MyRequestBody MyOrderParam orderParam,
|
||||
@MyRequestBody MyPageParam pageParam) {
|
||||
if (pageParam != null) {
|
||||
PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
|
||||
}
|
||||
SysDept sysDeptFilter = MyModelUtil.copyTo(sysDeptDtoFilter, SysDept.class);
|
||||
String orderBy = MyOrderParam.buildOrderBy(orderParam, SysDept.class);
|
||||
List<SysDept> sysDeptList = sysDeptService.getSysDeptListWithRelation(sysDeptFilter, orderBy);
|
||||
return ResponseResult.success(MyPageUtil.makeResponseData(sysDeptList, SysDept.INSTANCE));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看指定部门管理对象详情。
|
||||
*
|
||||
* @param deptId 指定对象主键Id。
|
||||
* @return 应答结果对象,包含对象详情。
|
||||
*/
|
||||
@GetMapping("/view")
|
||||
public ResponseResult<SysDeptVo> view(@RequestParam Long deptId) {
|
||||
if (MyCommonUtil.existBlankArgument(deptId)) {
|
||||
return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
|
||||
}
|
||||
SysDept sysDept = sysDeptService.getByIdWithRelation(deptId, MyRelationParam.full());
|
||||
if (sysDept == null) {
|
||||
return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
|
||||
}
|
||||
SysDeptVo sysDeptVo = SysDept.INSTANCE.fromModel(sysDept);
|
||||
return ResponseResult.success(sysDeptVo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以字典形式返回全部部门管理数据集合。字典的键值为[deptId, deptName]。
|
||||
* 白名单接口,登录用户均可访问。
|
||||
*
|
||||
* @param filter 过滤对象。
|
||||
* @return 应答结果对象,包含字典形式的数据集合。
|
||||
*/
|
||||
@GetMapping("/listDict")
|
||||
public ResponseResult<List<Map<String, Object>>> listDict(SysDept filter) {
|
||||
List<SysDept> resultList = sysDeptService.getListByFilter(filter);
|
||||
return ResponseResult.success(
|
||||
BeanQuery.select("parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字典Id集合,获取查询后的字典数据。
|
||||
*
|
||||
* @param dictIds 字典Id集合。
|
||||
* @return 应答结果对象,包含字典形式的数据集合。
|
||||
*/
|
||||
@PostMapping("/listDictByIds")
|
||||
public ResponseResult<List<Map<String, Object>>> listDictByIds(
|
||||
@MyRequestBody(elementType = Long.class) List<Long> dictIds) {
|
||||
List<SysDept> resultList = sysDeptService.getInList(new HashSet<>(dictIds));
|
||||
return ResponseResult.success(
|
||||
BeanQuery.select("parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据父主键Id,以字典的形式返回其下级数据列表。
|
||||
* 白名单接口,登录用户均可访问。
|
||||
*
|
||||
* @param parentId 父主键Id。
|
||||
* @return 按照字典的形式返回下级数据列表。
|
||||
*/
|
||||
@GetMapping("/listDictByParentId")
|
||||
public ResponseResult<List<Map<String, Object>>> listDictByParentId(@RequestParam(required = false) Long parentId) {
|
||||
List<SysDept> resultList = sysDeptService.getListByParentId("parentId", parentId);
|
||||
return ResponseResult.success(
|
||||
BeanQuery.select("parentId as parentId", "deptId as id", "deptName as name").executeFrom(resultList));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键Id集合,获取数据对象集合。仅限于微服务间远程接口调用。
|
||||
*
|
||||
* @param deptIds 主键Id集合。
|
||||
* @param withDict 是否包含字典关联。
|
||||
* @return 应答结果对象,包含主对象集合。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "listByIds")
|
||||
@PostMapping("/listByIds")
|
||||
public ResponseResult<List<SysDeptVo>> listByIds(
|
||||
@RequestParam Set<Long> deptIds, @RequestParam Boolean withDict) {
|
||||
return super.baseListByIds(deptIds, withDict, SysDept.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键Id,获取数据对象。仅限于微服务间远程接口调用。
|
||||
*
|
||||
* @param deptId 主键Id。
|
||||
* @param withDict 是否包含字典关联。
|
||||
* @return 应答结果对象,包含主对象数据。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "getById")
|
||||
@PostMapping("/getById")
|
||||
public ResponseResult<SysDeptVo> getById(
|
||||
@RequestParam Long deptId, @RequestParam Boolean withDict) {
|
||||
return super.baseGetById(deptId, withDict, SysDept.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数列表中指定的主键Id集合,是否全部存在。仅限于微服务间远程接口调用。
|
||||
*
|
||||
* @param deptIds 主键Id集合。
|
||||
* @return 应答结果对象,包含true全部存在,否则false。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "existIds")
|
||||
@PostMapping("/existIds")
|
||||
public ResponseResult<Boolean> existIds(@RequestParam Set<Long> deptIds) {
|
||||
return super.baseExistIds(deptIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数列表中指定的主键Id是否存在。仅限于微服务间远程接口调用。
|
||||
*
|
||||
* @param deptId 主键Id。
|
||||
* @return 应答结果对象,包含true表示存在,否则false。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "existId")
|
||||
@PostMapping("/existId")
|
||||
public ResponseResult<Boolean> existId(@RequestParam Long deptId) {
|
||||
return super.baseExistId(deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键Id删除数据。
|
||||
*
|
||||
* @param deptId 主键Id。
|
||||
* @return 删除数量。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "deleteById")
|
||||
@PostMapping("/deleteById")
|
||||
public ResponseResult<Integer> deleteById(@RequestParam Long deptId) throws Exception {
|
||||
SysDept filter = new SysDept();
|
||||
filter.setDeptId(deptId);
|
||||
return super.baseDeleteBy(filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除符合过滤条件的数据。
|
||||
*
|
||||
* @param filter 过滤对象。
|
||||
* @return 删除数量。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "deleteBy")
|
||||
@PostMapping("/deleteBy")
|
||||
public ResponseResult<Integer> deleteBy(@RequestBody SysDeptDto filter) throws Exception {
|
||||
return super.baseDeleteBy(MyModelUtil.copyTo(filter, SysDept.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 复杂的查询调用,包括(in list)过滤,对象条件过滤,分页和排序等。主要用于微服务间远程过程调用。
|
||||
*
|
||||
* @param queryParam 查询参数。
|
||||
* @return 分页数据集合对象。如MyQueryParam参数的分页属性为空,则不会执行分页操作,只是基于MyPageData对象返回数据结果。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "listBy")
|
||||
@PostMapping("/listBy")
|
||||
public ResponseResult<MyPageData<SysDeptVo>> listBy(@RequestBody MyQueryParam queryParam) {
|
||||
return super.baseListBy(queryParam, SysDept.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 复杂的查询调用,包括(in list)过滤,对象条件过滤,分页和排序等。主要用于微服务间远程过程调用。
|
||||
*
|
||||
* @param queryParam 查询参数。
|
||||
* @return 分页数据集合对象。如MyQueryParam参数的分页属性为空,则不会执行分页操作,只是基于MyPageData对象返回数据结果。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "listMapBy")
|
||||
@PostMapping("/listMapBy")
|
||||
public ResponseResult<MyPageData<Map<String, Object>>> listMapBy(@RequestBody MyQueryParam queryParam) {
|
||||
return super.baseListMapBy(queryParam, SysDept.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 复杂的查询调用,仅返回单体记录。主要用于微服务间远程过程调用。
|
||||
*
|
||||
* @param queryParam 查询参数。
|
||||
* @return 应答结果对象,包含符合查询过滤条件的对象结果集。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "getBy")
|
||||
@PostMapping("/getBy")
|
||||
public ResponseResult<SysDeptVo> getBy(@RequestBody MyQueryParam queryParam) {
|
||||
return super.baseGetBy(queryParam, SysDept.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取远程主对象中符合查询条件的数据数量。主要用于微服务间远程过程调用。
|
||||
*
|
||||
* @param queryParam 查询参数。
|
||||
* @return 应答结果对象,包含结果数量。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "countBy")
|
||||
@PostMapping("/countBy")
|
||||
public ResponseResult<Integer> countBy(@RequestBody MyQueryParam queryParam) {
|
||||
return super.baseCountBy(queryParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取远程对象中符合查询条件的分组聚合计算Map列表。
|
||||
*
|
||||
* @param aggregationParam 聚合参数。
|
||||
* @return 应该结果对象,包含聚合计算后的分组Map列表。
|
||||
*/
|
||||
@ApiOperation(hidden = true, value = "aggregateBy")
|
||||
@PostMapping("/aggregateBy")
|
||||
public ResponseResult<List<Map<String, Object>>> aggregateBy(@RequestBody MyAggregationParam aggregationParam) {
|
||||
return super.baseAggregateBy(aggregationParam);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.orange.demo.upmsservice.controller;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.page.PageMethod;
|
||||
import io.swagger.annotations.Api;
|
||||
import com.orange.demo.common.core.annotation.MyRequestBody;
|
||||
import com.orange.demo.common.core.object.*;
|
||||
import com.orange.demo.common.core.util.MyModelUtil;
|
||||
import com.orange.demo.common.core.util.MyPageUtil;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
import com.orange.demo.common.log.service.SysOperationLogService;
|
||||
import com.orange.demo.upmsapi.dto.SysOperationLogDto;
|
||||
import com.orange.demo.upmsapi.vo.SysOperationLogVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作日志接口控制器对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Api(tags = "操作日志接口")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sysOperationLog")
|
||||
public class SysOperationLogController {
|
||||
|
||||
@Autowired
|
||||
private SysOperationLogService operationLogService;
|
||||
|
||||
/**
|
||||
* 数据权限列表。
|
||||
*
|
||||
* @param sysOperationLogDtoFilter 操作日志查询过滤对象。
|
||||
* @param orderParam 排序参数。
|
||||
* @param pageParam 分页参数。
|
||||
* @return 应答结果对象。包含操作日志列表。
|
||||
*/
|
||||
@PostMapping("/list")
|
||||
public ResponseResult<MyPageData<SysOperationLogVo>> list(
|
||||
@MyRequestBody SysOperationLogDto sysOperationLogDtoFilter,
|
||||
@MyRequestBody MyOrderParam orderParam,
|
||||
@MyRequestBody MyPageParam pageParam) {
|
||||
if (pageParam != null) {
|
||||
PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
|
||||
}
|
||||
SysOperationLog filter = MyModelUtil.copyTo(sysOperationLogDtoFilter, SysOperationLog.class);
|
||||
String orderBy = MyOrderParam.buildOrderBy(orderParam, SysOperationLog.class);
|
||||
List<SysOperationLog> operationLogList = operationLogService.getSysOperationLogList(filter, orderBy);
|
||||
List<SysOperationLogVo> operationLogVoList = MyModelUtil.copyCollectionTo(operationLogList, SysOperationLogVo.class);
|
||||
long totalCount = 0L;
|
||||
if (operationLogList instanceof Page) {
|
||||
totalCount = ((Page<SysOperationLog>) operationLogList).getTotal();
|
||||
}
|
||||
return ResponseResult.success(MyPageUtil.makeResponseData(operationLogVoList, totalCount));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.orange.demo.upmsservice.dao;
|
||||
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.upmsservice.model.SysDataPermDept;
|
||||
|
||||
/**
|
||||
* 数据权限与部门关系数据访问操作接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysDataPermDeptMapper extends BaseDaoMapper<SysDataPermDept> {
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.orange.demo.upmsservice.dao;
|
||||
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.upmsservice.model.SysDataPerm;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据权限数据访问操作接口。
|
||||
* NOTE: 该对象一定不能被 @EnableDataPerm 注解标注,否则会导致无限递归。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysDataPermMapper extends BaseDaoMapper<SysDataPerm> {
|
||||
|
||||
/**
|
||||
* 获取数据权限列表。
|
||||
*
|
||||
* @param sysDataPermFilter 过滤对象。
|
||||
* @param orderBy 排序字符串。
|
||||
* @return 过滤后的数据权限列表。
|
||||
*/
|
||||
List<SysDataPerm> getSysDataPermList(
|
||||
@Param("sysDataPermFilter") SysDataPerm sysDataPermFilter, @Param("orderBy") String orderBy);
|
||||
|
||||
/**
|
||||
* 获取指定用户的数据权限列表。
|
||||
*
|
||||
* @param userId 用户Id。
|
||||
* @return 数据权限列表。
|
||||
*/
|
||||
List<SysDataPerm> getSysDataPermListByUserId(@Param("userId") Long userId);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.orange.demo.upmsservice.dao;
|
||||
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.upmsservice.model.SysDataPermUser;
|
||||
|
||||
/**
|
||||
* 数据权限与用户关系数据访问操作接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysDataPermUserMapper extends BaseDaoMapper<SysDataPermUser> {
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.orange.demo.upmsservice.dao;
|
||||
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.upmsservice.model.SysDept;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 部门管理数据操作访问接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysDeptMapper extends BaseDaoMapper<SysDept> {
|
||||
|
||||
/**
|
||||
* 批量插入对象列表。
|
||||
*
|
||||
* @param sysDeptList 新增对象列表。
|
||||
*/
|
||||
void insertList(List<SysDept> sysDeptList);
|
||||
|
||||
/**
|
||||
* 获取过滤后的对象列表。
|
||||
*
|
||||
* @param inFilterColumn 参与(In-list)过滤的数据表列。
|
||||
* @param inFilterValues 参与(In-list)过滤的数据表列值集合。
|
||||
* @param sysDeptFilter 过滤对象。
|
||||
* @param orderBy 排序字符串,order by从句的参数。
|
||||
* @return 对象列表。
|
||||
*/
|
||||
<M> List<SysDept> getSysDeptList(
|
||||
@Param("inFilterColumn") String inFilterColumn,
|
||||
@Param("inFilterValues") Set<M> inFilterValues,
|
||||
@Param("sysDeptFilter") SysDept sysDeptFilter,
|
||||
@Param("orderBy") String orderBy);
|
||||
|
||||
/**
|
||||
* 获取对象列表,过滤条件中包含like和between条件,以及指定属性的(in list)过滤条件。
|
||||
*
|
||||
* @param inFilterColumn 参与(In-list)过滤的数据表列。
|
||||
* @param inFilterValues 参与(In-list)过滤的数据表列值集合。
|
||||
* @param sysDeptFilter 过滤对象。
|
||||
* @return 对象列表。
|
||||
*/
|
||||
<M> Integer getSysDeptCount(
|
||||
@Param("inFilterColumn") String inFilterColumn,
|
||||
@Param("inFilterValues") Set<M> inFilterValues,
|
||||
@Param("sysDeptFilter") SysDept sysDeptFilter);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.orange.demo.upmsservice.dao;
|
||||
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.upmsservice.model.SysDeptRelation;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 部门关系树关联关系表访问接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysDeptRelationMapper extends BaseDaoMapper<SysDeptRelation> {
|
||||
|
||||
/**
|
||||
* 将myDeptId的所有子部门,与其父部门parentDeptId解除关联关系。
|
||||
*
|
||||
* @param parentDeptId myDeptId的父部门Id。
|
||||
* @param myDeptId 当前部门。
|
||||
*/
|
||||
void removeBetweenChildrenAndParents(
|
||||
@Param("parentDeptId") Long parentDeptId, @Param("myDeptId") Long myDeptId);
|
||||
|
||||
/**
|
||||
* 批量插入部门关联数据。
|
||||
* 由于目前版本(3.4.1)的Mybatis Plus没有提供真正的批量插入,为了保证效率需要自己实现。
|
||||
* 目前我们仅仅给出MySQL的insert list实现作为参考,其他数据库需要自行修改。
|
||||
*
|
||||
* @param deptRelationList 部门关联关系数据列表。
|
||||
*/
|
||||
void insertList(List<SysDeptRelation> deptRelationList);
|
||||
|
||||
/**
|
||||
* 批量插入当前部门的所有父部门列表,包括自己和自己的关系。
|
||||
*
|
||||
* @param parentDeptId myDeptId的父部门Id。
|
||||
* @param myDeptId 当前部门。
|
||||
*/
|
||||
void insertParentList(@Param("parentDeptId") Long parentDeptId, @Param("myDeptId") Long myDeptId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?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="com.orange.demo.upmsservice.dao.SysDataPermDeptMapper">
|
||||
<resultMap id="BaseResultMap" type="com.orange.demo.upmsservice.model.SysDataPermDept">
|
||||
<id column="data_perm_id" jdbcType="BIGINT" property="dataPermId"/>
|
||||
<id column="dept_id" jdbcType="BIGINT" property="deptId"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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="com.orange.demo.upmsservice.dao.SysDataPermMapper">
|
||||
<resultMap id="BaseResultMap" type="com.orange.demo.upmsservice.model.SysDataPerm">
|
||||
<id column="data_perm_id" jdbcType="BIGINT" property="dataPermId"/>
|
||||
<result column="data_perm_name" jdbcType="VARCHAR" property="dataPermName"/>
|
||||
<result column="rule_type" jdbcType="INTEGER" property="ruleType"/>
|
||||
<result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
|
||||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
|
||||
<result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
|
||||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
|
||||
<result column="deleted_flag" jdbcType="INTEGER" property="deletedFlag"/>
|
||||
</resultMap>
|
||||
|
||||
<resultMap id="BaseResultMapEx" type="com.orange.demo.upmsservice.model.SysDataPerm" extends="BaseResultMap">
|
||||
<collection property="dataPermDeptList" column="data_perm_id" javaType="ArrayList"
|
||||
ofType="com.orange.demo.upmsservice.model.SysDataPermDept" notNullColumn="dept_id"
|
||||
resultMap="com.orange.demo.upmsservice.dao.SysDataPermDeptMapper.BaseResultMap">
|
||||
</collection>
|
||||
</resultMap>
|
||||
|
||||
<sql id="filterRef">
|
||||
<if test="sysDataPermFilter != null">
|
||||
<if test="sysDataPermFilter.ruleType != null">
|
||||
AND zz_sys_data_perm.rule_type = #{sysDataPermFilter.ruleType}
|
||||
</if>
|
||||
<if test="sysDataPermFilter.searchString != null and sysDataPermFilter.searchString != ''">
|
||||
<bind name= "safeSearchString" value= "'%' + sysDataPermFilter.searchString + '%'" />
|
||||
AND IFNULL(zz_sys_data_perm.data_perm_name, '') LIKE #{safeSearchString}
|
||||
</if>
|
||||
</if>
|
||||
AND zz_sys_data_perm.deleted_flag = ${@com.orange.demo.common.core.constant.GlobalDeletedFlag@NORMAL}
|
||||
</sql>
|
||||
|
||||
<select id="getSysDataPermList" resultMap="BaseResultMap" parameterType="com.orange.demo.upmsservice.model.SysDataPerm">
|
||||
SELECT
|
||||
zz_sys_data_perm.*
|
||||
FROM
|
||||
zz_sys_data_perm
|
||||
<where>
|
||||
<include refid="filterRef"/>
|
||||
</where>
|
||||
<if test="orderBy != null and orderBy != ''">
|
||||
ORDER BY ${orderBy}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="getSysDataPermListByUserId" resultMap="BaseResultMapEx" parameterType="com.orange.demo.upmsservice.model.SysDataPerm">
|
||||
SELECT
|
||||
zz_sys_data_perm.*,
|
||||
zz_sys_data_perm_dept.*
|
||||
FROM
|
||||
zz_sys_data_perm_user
|
||||
INNER JOIN
|
||||
zz_sys_data_perm ON zz_sys_data_perm_user.data_perm_id = zz_sys_data_perm.data_perm_id
|
||||
LEFT JOIN
|
||||
zz_sys_data_perm_dept ON zz_sys_data_perm.data_perm_id = zz_sys_data_perm_dept.data_perm_id
|
||||
<where>
|
||||
AND zz_sys_data_perm_user.user_id = #{userId}
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?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="com.orange.demo.upmsservice.dao.SysDataPermUserMapper">
|
||||
<resultMap id="BaseResultMap" type="com.orange.demo.upmsservice.model.SysDataPermUser">
|
||||
<id column="data_perm_id" jdbcType="BIGINT" property="dataPermId"/>
|
||||
<id column="user_id" jdbcType="BIGINT" property="userId"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
@@ -0,0 +1,89 @@
|
||||
<?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="com.orange.demo.upmsservice.dao.SysDeptMapper">
|
||||
<resultMap id="BaseResultMap" type="com.orange.demo.upmsservice.model.SysDept">
|
||||
<id column="dept_id" jdbcType="BIGINT" property="deptId"/>
|
||||
<result column="dept_name" jdbcType="VARCHAR" property="deptName"/>
|
||||
<result column="show_order" jdbcType="INTEGER" property="showOrder"/>
|
||||
<result column="parent_id" jdbcType="BIGINT" property="parentId"/>
|
||||
<result column="deleted_flag" jdbcType="INTEGER" property="deletedFlag"/>
|
||||
<result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
|
||||
<result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
|
||||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
|
||||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
|
||||
</resultMap>
|
||||
|
||||
<insert id="insertList">
|
||||
INSERT INTO zz_sys_dept
|
||||
(dept_id,
|
||||
dept_name,
|
||||
show_order,
|
||||
parent_id,
|
||||
deleted_flag,
|
||||
create_user_id,
|
||||
update_user_id,
|
||||
create_time,
|
||||
update_time)
|
||||
VALUES
|
||||
<foreach collection="list" index="index" item="item" separator="," >
|
||||
(#{item.deptId},
|
||||
#{item.deptName},
|
||||
#{item.showOrder},
|
||||
#{item.parentId},
|
||||
#{item.deletedFlag},
|
||||
#{item.createUserId},
|
||||
#{item.updateUserId},
|
||||
#{item.createTime},
|
||||
#{item.updateTime})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<!-- 如果有逻辑删除字段过滤,请写到这里 -->
|
||||
<sql id="filterRef">
|
||||
<!-- 这里必须加上全包名,否则当filterRef被其他Mapper.xml包含引用的时候,就会调用Mapper.xml中的该SQL片段 -->
|
||||
<include refid="com.orange.demo.upmsservice.dao.SysDeptMapper.inputFilterRef"/>
|
||||
AND zz_sys_dept.deleted_flag = ${@com.orange.demo.common.core.constant.GlobalDeletedFlag@NORMAL}
|
||||
</sql>
|
||||
|
||||
<!-- 这里仅包含调用接口输入的主表过滤条件 -->
|
||||
<sql id="inputFilterRef">
|
||||
<if test="sysDeptFilter != null">
|
||||
<if test="sysDeptFilter.deptName != null and sysDeptFilter.deptName != ''">
|
||||
<bind name = "safeSysDeptDeptName" value = "'%' + sysDeptFilter.deptName + '%'" />
|
||||
AND zz_sys_dept.dept_name LIKE #{safeSysDeptDeptName}
|
||||
</if>
|
||||
<if test="sysDeptFilter.parentId != null">
|
||||
AND zz_sys_dept.parent_id = #{sysDeptFilter.parentId}
|
||||
</if>
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<select id="getSysDeptList" resultMap="BaseResultMap" parameterType="com.orange.demo.upmsservice.model.SysDept">
|
||||
SELECT * FROM zz_sys_dept
|
||||
<where>
|
||||
<if test="inFilterColumn != null and inFilterColumn != '' and inFilterValues != null and inFilterValues.size > 0">
|
||||
AND ${inFilterColumn} IN
|
||||
<foreach collection="inFilterValues" item="item" open="(" separator="," close=")">
|
||||
'${item}'
|
||||
</foreach>
|
||||
</if>
|
||||
<include refid="filterRef"/>
|
||||
</where>
|
||||
<if test="orderBy != null and orderBy != ''">
|
||||
ORDER BY ${orderBy}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="getSysDeptCount" resultType="java.lang.Integer" parameterType="com.orange.demo.upmsservice.model.SysDept">
|
||||
SELECT COUNT(1) FROM zz_sys_dept
|
||||
<where>
|
||||
<if test="inFilterColumn != null and inFilterColumn != '' and inFilterValues != null and inFilterValues.size > 0">
|
||||
AND ${inFilterColumn} IN
|
||||
<foreach collection="inFilterValues" item="item" open="(" separator="," close=")">
|
||||
'${item}'
|
||||
</foreach>
|
||||
</if>
|
||||
<include refid="filterRef"/>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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="com.orange.demo.upmsservice.dao.SysDeptRelationMapper">
|
||||
<resultMap id="BaseResultMap" type="com.orange.demo.upmsservice.model.SysDeptRelation">
|
||||
<id column="parent_dept_id" jdbcType="BIGINT" property="parentDeptId"/>
|
||||
<id column="dept_id" jdbcType="BIGINT" property="deptId"/>
|
||||
</resultMap>
|
||||
|
||||
<delete id="removeBetweenChildrenAndParents">
|
||||
DELETE a FROM zz_sys_dept_relation a
|
||||
INNER JOIN zz_sys_dept_relation b ON a.dept_id = b.dept_id
|
||||
WHERE a.parent_dept_id = #{parentDeptId} AND b.parent_dept_id = #{myDeptId}
|
||||
</delete>
|
||||
|
||||
<insert id="insertList">
|
||||
INSERT INTO zz_sys_dept_relation(parent_dept_id, dept_id) VALUES
|
||||
<foreach collection="list" index="index" item="item" separator=",">
|
||||
(#{item.parentDeptId}, #{item.deptId})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<insert id="insertParentList">
|
||||
INSERT INTO zz_sys_dept_relation(parent_dept_id, dept_id)
|
||||
SELECT t.parent_dept_id, #{myDeptId} FROM zz_sys_dept_relation t
|
||||
WHERE t.dept_id = #{parentDeptId}
|
||||
UNION ALL
|
||||
SELECT #{myDeptId}, #{myDeptId}
|
||||
</insert>
|
||||
</mapper>
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.orange.demo.upmsservice.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.orange.demo.common.core.annotation.RelationManyToMany;
|
||||
import com.orange.demo.common.core.base.model.BaseModel;
|
||||
import com.orange.demo.common.core.base.mapper.BaseModelMapper;
|
||||
import com.orange.demo.common.core.util.MyCommonUtil;
|
||||
import com.orange.demo.upmsapi.vo.SysDataPermVo;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 数据权限实体对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName(value = "zz_sys_data_perm")
|
||||
public class SysDataPerm extends BaseModel {
|
||||
|
||||
/**
|
||||
* 主键Id。
|
||||
*/
|
||||
@TableId(value = "data_perm_id")
|
||||
private Long dataPermId;
|
||||
|
||||
/**
|
||||
* 显示名称。
|
||||
*/
|
||||
@TableField(value = "data_perm_name")
|
||||
private String dataPermName;
|
||||
|
||||
/**
|
||||
* 数据权限规则类型(0: 全部可见 1: 只看自己 2: 只看本部门 3: 本部门及子部门 4: 多部门及子部门 5: 自定义部门列表)。
|
||||
*/
|
||||
@TableField(value = "rule_type")
|
||||
private Integer ruleType;
|
||||
|
||||
/**
|
||||
* 逻辑删除标记字段(1: 正常 -1: 已删除)。
|
||||
*/
|
||||
@TableLogic
|
||||
@TableField(value = "deleted_flag")
|
||||
private Integer deletedFlag;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String deptIdListString;
|
||||
|
||||
@RelationManyToMany(
|
||||
relationMapperName = "sysDataPermDeptMapper",
|
||||
relationMasterIdField = "dataPermId",
|
||||
relationModelClass = SysDataPermDept.class)
|
||||
@TableField(exist = false)
|
||||
private List<SysDataPermDept> dataPermDeptList;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String searchString;
|
||||
|
||||
public void setSearchString(String searchString) {
|
||||
this.searchString = MyCommonUtil.replaceSqlWildcard(searchString);
|
||||
}
|
||||
|
||||
@Mapper
|
||||
public interface SysDataPermModelMapper extends BaseModelMapper<SysDataPermVo, SysDataPerm> {
|
||||
/**
|
||||
* 转换VO对象到实体对象。
|
||||
*
|
||||
* @param sysDataPermVo 域对象。
|
||||
* @return 实体对象。
|
||||
*/
|
||||
@Mapping(target = "dataPermDeptList", expression = "java(mapToBean(sysDataPermVo.getDataPermDeptList(), com.orange.demo.upmsservice.model.SysDataPermDept.class))")
|
||||
@Override
|
||||
SysDataPerm toModel(SysDataPermVo sysDataPermVo);
|
||||
/**
|
||||
* 转换实体对象到VO对象。
|
||||
*
|
||||
* @param sysDataPerm 实体对象。
|
||||
* @return 域对象。
|
||||
*/
|
||||
@Mapping(target = "dataPermDeptList", expression = "java(beanToMap(sysDataPerm.getDataPermDeptList(), false))")
|
||||
@Override
|
||||
SysDataPermVo fromModel(SysDataPerm sysDataPerm);
|
||||
}
|
||||
public static final SysDataPermModelMapper INSTANCE = Mappers.getMapper(SysDataPerm.SysDataPermModelMapper.class);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.orange.demo.upmsservice.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 数据权限与部门关联实体对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@ToString(of = {"deptId"})
|
||||
@TableName(value = "zz_sys_data_perm_dept")
|
||||
public class SysDataPermDept {
|
||||
|
||||
/**
|
||||
* 数据权限Id。
|
||||
*/
|
||||
@TableField(value = "data_perm_id")
|
||||
private Long dataPermId;
|
||||
|
||||
/**
|
||||
* 关联部门Id。
|
||||
*/
|
||||
@TableField(value = "dept_id")
|
||||
private Long deptId;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.orange.demo.upmsservice.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 数据权限与用户关联实体对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@TableName(value = "zz_sys_data_perm_user")
|
||||
public class SysDataPermUser {
|
||||
|
||||
/**
|
||||
* 数据权限Id。
|
||||
*/
|
||||
@TableField(value = "data_perm_id")
|
||||
private Long dataPermId;
|
||||
|
||||
/**
|
||||
* 用户Id。
|
||||
*/
|
||||
@TableField(value = "user_id")
|
||||
private Long userId;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.orange.demo.upmsservice.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.orange.demo.upmsapi.vo.SysDeptVo;
|
||||
import com.orange.demo.common.core.base.model.BaseModel;
|
||||
import com.orange.demo.common.core.base.mapper.BaseModelMapper;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.mapstruct.*;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* SysDept实体对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName(value = "zz_sys_dept")
|
||||
public class SysDept extends BaseModel {
|
||||
|
||||
/**
|
||||
* 部门Id。
|
||||
*/
|
||||
@TableId(value = "dept_id")
|
||||
private Long deptId;
|
||||
|
||||
/**
|
||||
* 部门名称。
|
||||
*/
|
||||
@TableField(value = "dept_name")
|
||||
private String deptName;
|
||||
|
||||
/**
|
||||
* 显示顺序。
|
||||
*/
|
||||
@TableField(value = "show_order")
|
||||
private Integer showOrder;
|
||||
|
||||
/**
|
||||
* 父部门Id。
|
||||
*/
|
||||
@TableField(value = "parent_id")
|
||||
private Long parentId;
|
||||
|
||||
/**
|
||||
* 逻辑删除标记字段(1: 正常 -1: 已删除)。
|
||||
*/
|
||||
@TableLogic
|
||||
@TableField(value = "deleted_flag")
|
||||
private Integer deletedFlag;
|
||||
|
||||
@Mapper
|
||||
public interface SysDeptModelMapper extends BaseModelMapper<SysDeptVo, SysDept> {
|
||||
}
|
||||
public static final SysDeptModelMapper INSTANCE = Mappers.getMapper(SysDeptModelMapper.class);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.orange.demo.upmsservice.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 部门关联实体对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@TableName(value = "zz_sys_dept_relation")
|
||||
public class SysDeptRelation {
|
||||
|
||||
/**
|
||||
* 上级部门Id。
|
||||
*/
|
||||
@TableField(value = "parent_dept_id")
|
||||
private Long parentDeptId;
|
||||
|
||||
/**
|
||||
* 部门Id。
|
||||
*/
|
||||
@TableField(value = "dept_id")
|
||||
private Long deptId;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.orange.demo.upmsservice.service;
|
||||
|
||||
import com.orange.demo.common.core.base.service.IBaseService;
|
||||
import com.orange.demo.common.core.object.CallResult;
|
||||
import com.orange.demo.upmsservice.model.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 数据权限数据服务接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysDataPermService extends IBaseService<SysDataPerm, Long> {
|
||||
|
||||
/**
|
||||
* 保存新增的数据权限对象。
|
||||
*
|
||||
* @param dataPerm 新增的数据权限对象。
|
||||
* @param deptIdSet 关联的部门Id列表。
|
||||
* @return 新增后的数据权限对象。
|
||||
*/
|
||||
SysDataPerm saveNew(SysDataPerm dataPerm, Set<Long> deptIdSet);
|
||||
|
||||
/**
|
||||
* 更新数据权限对象。
|
||||
*
|
||||
* @param dataPerm 更新的数据权限对象。
|
||||
* @param originalDataPerm 原有的数据权限对象。
|
||||
* @param deptIdSet 关联的部门Id列表。
|
||||
* @return 更新成功返回true,否则false。
|
||||
*/
|
||||
boolean update(SysDataPerm dataPerm, SysDataPerm originalDataPerm, Set<Long> deptIdSet);
|
||||
|
||||
/**
|
||||
* 删除指定数据权限。
|
||||
*
|
||||
* @param dataPermId 数据权限主键Id。
|
||||
* @return 删除成功返回true,否则false。
|
||||
*/
|
||||
boolean remove(Long dataPermId);
|
||||
|
||||
/**
|
||||
* 获取数据权限列表。
|
||||
*
|
||||
* @param filter 数据权限过滤对象。
|
||||
* @param orderBy 排序参数。
|
||||
* @return 数据权限查询列表。
|
||||
*/
|
||||
List<SysDataPerm> getSysDataPermList(SysDataPerm filter, String orderBy);
|
||||
|
||||
/**
|
||||
* 将指定会话的数据权限集合从缓存中移除。
|
||||
*
|
||||
* @param sessionId 会话Id。
|
||||
*/
|
||||
void removeDataPermCache(String sessionId);
|
||||
|
||||
/**
|
||||
* 将指定用户的指定会话的数据权限集合存入缓存。
|
||||
*
|
||||
* @param sessionId 会话Id。
|
||||
* @param userId 用户主键Id。
|
||||
* @param deptId 用户所属部门主键Id。
|
||||
* @return 查询并缓存后的数据权限集合。返回格式为,Map<RuleType, DeptIdString>。
|
||||
*/
|
||||
Map<Integer, String> putDataPermCache(String sessionId, Long userId, Long deptId);
|
||||
|
||||
/**
|
||||
* 获取指定用户Id的数据权限列表。并基于权限规则类型进行了一级分组。
|
||||
*
|
||||
* @param userId 指定的用户Id。
|
||||
* @param deptId 用户所属部门主键Id。
|
||||
* @return 合并优化后的数据权限列表。返回格式为,Map<RuleType, DeptIdString>。
|
||||
*/
|
||||
Map<Integer, String> getSysDataPermListByUserId(Long userId, Long deptId);
|
||||
|
||||
/**
|
||||
* 添加用户和数据权限之间的多对多关联关系。
|
||||
*
|
||||
* @param dataPermId 数据权限Id。
|
||||
* @param userIdSet 关联的用户Id列表。
|
||||
*/
|
||||
void addDataPermUserList(Long dataPermId, Set<Long> userIdSet);
|
||||
|
||||
/**
|
||||
* 移除用户和数据权限之间的多对多关联关系。
|
||||
*
|
||||
* @param dataPermId 数据权限主键Id。
|
||||
* @param userId 用户主键Id。
|
||||
* @return true移除成功,否则false。
|
||||
*/
|
||||
boolean removeDataPermUser(Long dataPermId, Long userId);
|
||||
|
||||
/**
|
||||
* 验证数据权限对象关联菜单数据是否都合法。
|
||||
*
|
||||
* @param dataPerm 与数据权限关联的菜单数据列表。
|
||||
* @param deptIdListString 与数据权限关联的部门Id列表。
|
||||
* @return 验证结果。
|
||||
*/
|
||||
CallResult verifyRelatedData(SysDataPerm dataPerm, String deptIdListString);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.orange.demo.upmsservice.service;
|
||||
|
||||
import com.orange.demo.upmsservice.model.*;
|
||||
import com.orange.demo.common.core.base.service.IBaseService;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 部门管理数据操作服务接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysDeptService extends IBaseService<SysDept, Long> {
|
||||
|
||||
/**
|
||||
* 保存新增的部门对象。
|
||||
*
|
||||
* @param sysDept 新增的部门对象。
|
||||
* @param parentSysDept 上级部门对象。
|
||||
* @return 新增后的部门对象。
|
||||
*/
|
||||
SysDept saveNew(SysDept sysDept, SysDept parentSysDept);
|
||||
|
||||
/**
|
||||
* 更新部门对象。
|
||||
*
|
||||
* @param sysDept 更新的部门对象。
|
||||
* @param originalSysDept 原有的部门对象。
|
||||
* @return 更新成功返回true,否则false。
|
||||
*/
|
||||
boolean update(SysDept sysDept, SysDept originalSysDept);
|
||||
|
||||
/**
|
||||
* 删除指定数据。
|
||||
*
|
||||
* @param deptId 主键Id。
|
||||
* @return 成功返回true,否则false。
|
||||
*/
|
||||
boolean remove(Long deptId);
|
||||
|
||||
/**
|
||||
* 获取单表查询结果。由于没有关联数据查询,因此在仅仅获取单表数据的场景下,效率更高。
|
||||
* 如果需要同时获取关联数据,请移步(getSysDeptListWithRelation)方法。
|
||||
*
|
||||
* @param filter 过滤对象。
|
||||
* @param orderBy 排序参数。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
List<SysDept> getSysDeptList(SysDept filter, String orderBy);
|
||||
|
||||
/**
|
||||
* 获取主表的查询结果,查询条件中包括主表过滤对象和指定字段的(in list)过滤。
|
||||
* 由于没有关联数据查询,因此在仅仅获取单表数据的场景下,效率更高。
|
||||
* 如果需要同时获取关联数据,请移步(getSysDeptListWithRelation)方法。
|
||||
*
|
||||
* @param inFilterField (In-list)指定的字段(Java成员字段,而非数据列名)。
|
||||
* @param inFilterValues inFilterField指定字段的(In-list)数据列表。
|
||||
* @param filter 过滤对象。
|
||||
* @param orderBy 排序参数。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
<M> List<SysDept> getSysDeptList(String inFilterField, Set<M> inFilterValues, SysDept filter, String orderBy);
|
||||
|
||||
/**
|
||||
* 获取主表的查询结果,以及主表关联的字典数据和一对一从表数据,以及一对一从表的字典数据。
|
||||
* 如果仅仅需要获取主表数据,请移步(getSysDeptList),以便获取更好的查询性能。
|
||||
*
|
||||
* @param filter 主表过滤对象。
|
||||
* @param orderBy 排序对象。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
List<SysDept> getSysDeptListWithRelation(SysDept filter, String orderBy);
|
||||
|
||||
/**
|
||||
* 获取主表的查询结果,查询条件中包括主表过滤对象和指定字段的(in list)过滤。
|
||||
* 同时还包含主表关联的字典数据和一对一从表数据,以及一对一从表的字典数据。
|
||||
* 如果仅仅需要获取主表数据,请移步(getSysDeptList),以便获取更好的查询性能。
|
||||
*
|
||||
* @param inFilterField (In-list)指定的字段(Java成员字段,而非数据列名)。
|
||||
* @param inFilterValues inFilterField指定字段的(In-list)数据列表。
|
||||
* @param filter 主表过滤对象。
|
||||
* @param orderBy 排序对象。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
<M> List<SysDept> getSysDeptListWithRelation(
|
||||
String inFilterField, Set<M> inFilterValues, SysDept filter, String orderBy);
|
||||
|
||||
/**
|
||||
* 判断指定对象是否包含下级对象。
|
||||
*
|
||||
* @param deptId 主键Id。
|
||||
* @return 存在返回true,否则false。
|
||||
*/
|
||||
boolean hasChildren(Long deptId);
|
||||
|
||||
/**
|
||||
* 判断指定部门Id是否包含用户对象。
|
||||
*
|
||||
* @param deptId 部门主键Id。
|
||||
* @return 存在返回true,否则false。
|
||||
*/
|
||||
boolean hasChildrenUser(Long deptId);
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
package com.orange.demo.upmsservice.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.orange.demo.common.core.base.service.BaseService;
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.core.constant.GlobalDeletedFlag;
|
||||
import com.orange.demo.common.core.object.CallResult;
|
||||
import com.orange.demo.common.core.util.MyModelUtil;
|
||||
import com.orange.demo.common.core.util.RedisKeyUtil;
|
||||
import com.orange.demo.common.sequence.wrapper.IdGeneratorWrapper;
|
||||
import com.orange.demo.common.datafilter.constant.DataPermRuleType;
|
||||
import com.orange.demo.upmsservice.dao.SysDataPermDeptMapper;
|
||||
import com.orange.demo.upmsservice.dao.SysDataPermMapper;
|
||||
import com.orange.demo.upmsservice.dao.SysDataPermUserMapper;
|
||||
import com.orange.demo.upmsservice.service.SysDataPermService;
|
||||
import com.orange.demo.upmsservice.service.SysDeptService;
|
||||
import com.orange.demo.upmsservice.model.*;
|
||||
import com.orange.demo.upmsservice.config.ApplicationConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据权限数据服务类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Slf4j
|
||||
@Service("sysDataPermService")
|
||||
public class SysDataPermServiceImpl extends BaseService<SysDataPerm, Long> implements SysDataPermService {
|
||||
|
||||
@Autowired
|
||||
private SysDataPermMapper sysDataPermMapper;
|
||||
@Autowired
|
||||
private SysDataPermDeptMapper sysDataPermDeptMapper;
|
||||
@Autowired
|
||||
private SysDataPermUserMapper sysDataPermUserMapper;
|
||||
@Autowired
|
||||
private SysDeptService sysDeptService;
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
@Autowired
|
||||
private ApplicationConfig applicationConfig;
|
||||
@Autowired
|
||||
private IdGeneratorWrapper idGenerator;
|
||||
|
||||
/**
|
||||
* 返回主对象的Mapper对象。
|
||||
*
|
||||
* @return 主对象的Mapper对象。
|
||||
*/
|
||||
@Override
|
||||
protected BaseDaoMapper<SysDataPerm> mapper() {
|
||||
return sysDataPermMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存新增的数据权限对象。
|
||||
*
|
||||
* @param dataPerm 新增的数据权限对象。
|
||||
* @param deptIdSet 关联的部门Id列表。
|
||||
* @return 新增后的数据权限对象。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public SysDataPerm saveNew(SysDataPerm dataPerm, Set<Long> deptIdSet) {
|
||||
dataPerm.setDataPermId(idGenerator.nextLongId());
|
||||
dataPerm.setDeletedFlag(GlobalDeletedFlag.NORMAL);
|
||||
MyModelUtil.fillCommonsForInsert(dataPerm);
|
||||
sysDataPermMapper.insert(dataPerm);
|
||||
this.insertRelationData(dataPerm, deptIdSet);
|
||||
return dataPerm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据权限对象。
|
||||
*
|
||||
* @param dataPerm 更新的数据权限对象。
|
||||
* @param originalDataPerm 原有的数据权限对象。
|
||||
* @param deptIdSet 关联的部门Id列表。
|
||||
* @return 更新成功返回true,否则false。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean update(SysDataPerm dataPerm, SysDataPerm originalDataPerm, Set<Long> deptIdSet) {
|
||||
MyModelUtil.fillCommonsForUpdate(dataPerm, originalDataPerm);
|
||||
UpdateWrapper<SysDataPerm> uw = this.createUpdateQueryForNullValue(dataPerm, dataPerm.getDataPermId());
|
||||
if (sysDataPermMapper.update(dataPerm, uw) != 1) {
|
||||
return false;
|
||||
}
|
||||
SysDataPermDept dataPermDept = new SysDataPermDept();
|
||||
dataPermDept.setDataPermId(dataPerm.getDataPermId());
|
||||
sysDataPermDeptMapper.delete(new QueryWrapper<>(dataPermDept));
|
||||
this.insertRelationData(dataPerm, deptIdSet);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定数据权限。
|
||||
*
|
||||
* @param dataPermId 数据权限主键Id。
|
||||
* @return 删除成功返回true,否则false。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean remove(Long dataPermId) {
|
||||
if (sysDataPermMapper.deleteById(dataPermId) != 1) {
|
||||
return false;
|
||||
}
|
||||
SysDataPermDept dataPermDept = new SysDataPermDept();
|
||||
dataPermDept.setDataPermId(dataPermId);
|
||||
sysDataPermDeptMapper.delete(new QueryWrapper<>(dataPermDept));
|
||||
SysDataPermUser dataPermUser = new SysDataPermUser();
|
||||
dataPermUser.setDataPermId(dataPermId);
|
||||
sysDataPermUserMapper.delete(new QueryWrapper<>(dataPermUser));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据权限列表。
|
||||
*
|
||||
* @param filter 数据权限过滤对象。
|
||||
* @param orderBy 排序参数。
|
||||
* @return 数据权限查询列表。
|
||||
*/
|
||||
@Override
|
||||
public List<SysDataPerm> getSysDataPermList(SysDataPerm filter, String orderBy) {
|
||||
return sysDataPermMapper.getSysDataPermList(filter, orderBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定会话的数据权限集合从缓存中移除。
|
||||
*
|
||||
* @param sessionId 会话Id。
|
||||
*/
|
||||
@Override
|
||||
public void removeDataPermCache(String sessionId) {
|
||||
String sessionPermKey = RedisKeyUtil.makeSessionDataPermIdKey(sessionId);
|
||||
redissonClient.getBucket(sessionPermKey).deleteAsync();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定用户的指定会话的数据权限集合存入缓存。
|
||||
*
|
||||
* @param sessionId 会话Id。
|
||||
* @param userId 用户主键Id。
|
||||
* @param deptId 用户所属部门主键Id。
|
||||
* @return 查询并缓存后的数据权限集合。返回格式为,Map<RuleType, DeptIdString>。
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, String> putDataPermCache(String sessionId, Long userId, Long deptId) {
|
||||
Map<Integer, String> dataPermMap = this.getSysDataPermListByUserId(userId, deptId);
|
||||
if (dataPermMap.size() > 0) {
|
||||
String dataPermSessionKey = RedisKeyUtil.makeSessionDataPermIdKey(sessionId);
|
||||
RBucket<String> bucket = redissonClient.getBucket(dataPermSessionKey);
|
||||
bucket.set(JSON.toJSONString(dataPermMap),
|
||||
applicationConfig.getDataPermExpiredSeconds(), TimeUnit.SECONDS);
|
||||
}
|
||||
return dataPermMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定用户Id的数据权限列表。并基于权限规则类型进行了一级分组。
|
||||
*
|
||||
* @param userId 指定的用户Id。
|
||||
* @param deptId 用户所属部门主键Id。
|
||||
* @return 合并优化后的数据权限列表。返回格式为,Map<RuleType, DeptIdString>。
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, String> getSysDataPermListByUserId(Long userId, Long deptId) {
|
||||
List<SysDataPerm> dataPermList = sysDataPermMapper.getSysDataPermListByUserId(userId);
|
||||
dataPermList.forEach(dataPerm -> {
|
||||
if (CollectionUtils.isNotEmpty(dataPerm.getDataPermDeptList())) {
|
||||
Set<Long> deptIdSet = dataPerm.getDataPermDeptList().stream()
|
||||
.map(SysDataPermDept::getDeptId).collect(Collectors.toSet());
|
||||
dataPerm.setDeptIdListString(StringUtils.join(deptIdSet, ","));
|
||||
}
|
||||
});
|
||||
// 为了更方便进行后续的合并优化处理,这里再基于规则类型进行分组。ruleMap的key是规则类型。
|
||||
Map<Integer, List<SysDataPerm>> ruleMap =
|
||||
dataPermList.stream().collect(Collectors.groupingBy(SysDataPerm::getRuleType));
|
||||
Map<Integer, String> resultMap = new HashMap<>(ruleMap.size());
|
||||
// 如有有ALL存在,就可以直接退出了,没有必要在处理后续的规则了。
|
||||
if (ruleMap.containsKey(DataPermRuleType.TYPE_ALL)) {
|
||||
resultMap.put(DataPermRuleType.TYPE_ALL, "null");
|
||||
return resultMap;
|
||||
}
|
||||
// 这里优先合并最复杂的多部门及子部门场景。
|
||||
String deptIds = processMultiDeptAndChildren(ruleMap, deptId);
|
||||
if (deptIds != null) {
|
||||
resultMap.put(DataPermRuleType.TYPE_MULTI_DEPT_AND_CHILD_DEPT, deptIds);
|
||||
}
|
||||
// 合并当前部门及子部门的优化
|
||||
if (ruleMap.get(DataPermRuleType.TYPE_DEPT_AND_CHILD_DEPT) != null) {
|
||||
// 需要与仅仅当前部门规则进行合并。
|
||||
ruleMap.remove(DataPermRuleType.TYPE_DEPT_ONLY);
|
||||
resultMap.put(DataPermRuleType.TYPE_DEPT_AND_CHILD_DEPT, "null");
|
||||
}
|
||||
// 合并自定义部门了。
|
||||
deptIds = processMultiDept(ruleMap, deptId);
|
||||
if (deptIds != null) {
|
||||
resultMap.put(DataPermRuleType.TYPE_CUSTOM_DEPT_LIST, deptIds);
|
||||
}
|
||||
// 最后处理当前部门和当前用户。
|
||||
if (ruleMap.get(DataPermRuleType.TYPE_DEPT_ONLY) != null) {
|
||||
resultMap.put(DataPermRuleType.TYPE_DEPT_ONLY, "null");
|
||||
}
|
||||
if (ruleMap.get(DataPermRuleType.TYPE_USER_ONLY) != null) {
|
||||
resultMap.put(DataPermRuleType.TYPE_USER_ONLY, "null");
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
private String processMultiDeptAndChildren(Map<Integer, List<SysDataPerm>> ruleMap, Long deptId) {
|
||||
List<SysDataPerm> parentDeptList = ruleMap.get(DataPermRuleType.TYPE_MULTI_DEPT_AND_CHILD_DEPT);
|
||||
if (parentDeptList == null) {
|
||||
return null;
|
||||
}
|
||||
Set<Long> deptIdSet = new HashSet<>();
|
||||
for (SysDataPerm parentDept : parentDeptList) {
|
||||
deptIdSet.addAll(Arrays.stream(StringUtils.split(
|
||||
parentDept.getDeptIdListString(), ",")).map(Long::valueOf).collect(Collectors.toSet()));
|
||||
}
|
||||
// 在合并所有的多父部门Id之后,需要判断是否有本部门及子部门的规则。如果有,就继续合并。
|
||||
if (ruleMap.containsKey(DataPermRuleType.TYPE_DEPT_AND_CHILD_DEPT)) {
|
||||
// 如果多父部门列表中包含当前部门,那么可以直接删除该规则了,如果没包含,就加入到多部门的DEPT_ID的IN LIST中。
|
||||
deptIdSet.add(deptId);
|
||||
ruleMap.remove(DataPermRuleType.TYPE_DEPT_AND_CHILD_DEPT);
|
||||
}
|
||||
// 需要与仅仅当前部门规则进行合并。
|
||||
if (ruleMap.containsKey(DataPermRuleType.TYPE_DEPT_ONLY)) {
|
||||
if (deptIdSet.contains(deptId)) {
|
||||
ruleMap.remove(DataPermRuleType.TYPE_DEPT_ONLY);
|
||||
}
|
||||
}
|
||||
return StringUtils.join(deptIdSet, ',');
|
||||
}
|
||||
|
||||
private String processMultiDept(Map<Integer, List<SysDataPerm>> ruleMap, Long deptId) {
|
||||
List<SysDataPerm> customDeptList = ruleMap.get(DataPermRuleType.TYPE_CUSTOM_DEPT_LIST);
|
||||
if (customDeptList == null) {
|
||||
return null;
|
||||
}
|
||||
Set<Long> deptIdSet = new HashSet<>();
|
||||
for (SysDataPerm customDept : customDeptList) {
|
||||
deptIdSet.addAll(Arrays.stream(StringUtils.split(
|
||||
customDept.getDeptIdListString(), ",")).map(Long::valueOf).collect(Collectors.toSet()));
|
||||
}
|
||||
if (ruleMap.containsKey(DataPermRuleType.TYPE_DEPT_ONLY)) {
|
||||
deptIdSet.add(deptId);
|
||||
ruleMap.remove(DataPermRuleType.TYPE_DEPT_ONLY);
|
||||
}
|
||||
return StringUtils.join(deptIdSet, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加用户和数据权限之间的多对多关联关系。
|
||||
*
|
||||
* @param dataPermId 数据权限Id。
|
||||
* @param userIdSet 关联的用户Id列表。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void addDataPermUserList(Long dataPermId, Set<Long> userIdSet) {
|
||||
for (Long userId : userIdSet) {
|
||||
SysDataPermUser dataPermUser = new SysDataPermUser();
|
||||
dataPermUser.setDataPermId(dataPermId);
|
||||
dataPermUser.setUserId(userId);
|
||||
sysDataPermUserMapper.insert(dataPermUser);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除用户和数据权限之间的多对多关联关系。
|
||||
*
|
||||
* @param dataPermId 数据权限主键Id。
|
||||
* @param userId 用户主键Id。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean removeDataPermUser(Long dataPermId, Long userId) {
|
||||
SysDataPermUser dataPermUser = new SysDataPermUser();
|
||||
dataPermUser.setDataPermId(dataPermId);
|
||||
dataPermUser.setUserId(userId);
|
||||
return sysDataPermUserMapper.delete(new QueryWrapper<>(dataPermUser)) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证数据权限对象关联菜单数据是否都合法。
|
||||
*
|
||||
* @param dataPerm 与数据权限关联的菜单数据列表。
|
||||
* @param deptIdListString 与数据权限关联的部门Id列表。
|
||||
* @return 验证结果。
|
||||
*/
|
||||
@Override
|
||||
public CallResult verifyRelatedData(SysDataPerm dataPerm, String deptIdListString) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
if (dataPerm.getRuleType() == DataPermRuleType.TYPE_MULTI_DEPT_AND_CHILD_DEPT
|
||||
|| dataPerm.getRuleType() == DataPermRuleType.TYPE_CUSTOM_DEPT_LIST) {
|
||||
if (StringUtils.isBlank(deptIdListString)) {
|
||||
return CallResult.error("数据验证失败,部门列表不能为空!");
|
||||
}
|
||||
Set<Long> deptIdSet = Arrays.stream(StringUtils.split(
|
||||
deptIdListString, ",")).map(Long::valueOf).collect(Collectors.toSet());
|
||||
if (!sysDeptService.existAllPrimaryKeys(deptIdSet)) {
|
||||
return CallResult.error("数据验证失败,存在不合法的部门数据,请刷新后重试!");
|
||||
}
|
||||
jsonObject.put("deptIdSet", deptIdSet);
|
||||
}
|
||||
return CallResult.ok(jsonObject);
|
||||
}
|
||||
|
||||
private void insertRelationData(SysDataPerm dataPerm, Set<Long> deptIdSet) {
|
||||
if (CollectionUtils.isNotEmpty(deptIdSet)) {
|
||||
for (Long deptId : deptIdSet) {
|
||||
SysDataPermDept dataPermDept = new SysDataPermDept();
|
||||
dataPermDept.setDataPermId(dataPerm.getDataPermId());
|
||||
dataPermDept.setDeptId(deptId);
|
||||
sysDataPermDeptMapper.insert(dataPermDept);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
package com.orange.demo.upmsservice.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.orange.demo.upmsservice.service.*;
|
||||
import com.orange.demo.upmsservice.dao.*;
|
||||
import com.orange.demo.upmsservice.model.*;
|
||||
import com.orange.demo.common.core.util.*;
|
||||
import com.orange.demo.common.core.object.MyRelationParam;
|
||||
import com.orange.demo.common.core.constant.GlobalDeletedFlag;
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.core.base.service.BaseService;
|
||||
import com.orange.demo.common.sequence.wrapper.IdGeneratorWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import com.github.pagehelper.Page;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 部门管理数据操作服务类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Slf4j
|
||||
@Service("sysDeptService")
|
||||
public class SysDeptServiceImpl extends BaseService<SysDept, Long> implements SysDeptService {
|
||||
|
||||
@Autowired
|
||||
private SysDeptMapper sysDeptMapper;
|
||||
@Autowired
|
||||
private SysDeptRelationMapper sysDeptRelationMapper;
|
||||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
@Autowired
|
||||
private SysDataPermDeptMapper sysDataPermDeptMapper;
|
||||
@Autowired
|
||||
private IdGeneratorWrapper idGenerator;
|
||||
|
||||
/**
|
||||
* 返回当前Service的主表Mapper对象。
|
||||
*
|
||||
* @return 主表Mapper对象。
|
||||
*/
|
||||
@Override
|
||||
protected BaseDaoMapper<SysDept> mapper() {
|
||||
return sysDeptMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存新增的部门对象。
|
||||
*
|
||||
* @param sysDept 新增的部门对象。
|
||||
* @param parentSysDept 上级部门对象。
|
||||
* @return 新增后的部门对象。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public SysDept saveNew(SysDept sysDept, SysDept parentSysDept) {
|
||||
sysDept.setDeptId(idGenerator.nextLongId());
|
||||
sysDept.setDeletedFlag(GlobalDeletedFlag.NORMAL);
|
||||
MyModelUtil.fillCommonsForInsert(sysDept);
|
||||
sysDeptMapper.insert(sysDept);
|
||||
// 同步插入部门关联关系数据
|
||||
if (parentSysDept == null) {
|
||||
sysDeptRelationMapper.insert(new SysDeptRelation(sysDept.getDeptId(), sysDept.getDeptId()));
|
||||
} else {
|
||||
sysDeptRelationMapper.insertParentList(parentSysDept.getDeptId(), sysDept.getDeptId());
|
||||
}
|
||||
return sysDept;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新部门对象。
|
||||
*
|
||||
* @param sysDept 更新的部门对象。
|
||||
* @param originalSysDept 原有的部门对象。
|
||||
* @return 更新成功返回true,否则false。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean update(SysDept sysDept, SysDept originalSysDept) {
|
||||
MyModelUtil.fillCommonsForUpdate(sysDept, originalSysDept);
|
||||
UpdateWrapper<SysDept> uw = this.createUpdateQueryForNullValue(sysDept, sysDept.getDeptId());
|
||||
if (sysDeptMapper.update(sysDept, uw) == 0) {
|
||||
return false;
|
||||
}
|
||||
if (ObjectUtils.notEqual(sysDept.getParentId(), originalSysDept.getParentId())) {
|
||||
this.updateParentRelation(sysDept, originalSysDept);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void updateParentRelation(SysDept sysDept, SysDept originalSysDept) {
|
||||
// 1. 在删除当前部门与原有父部门的关联关系之前,先将原有的所有父部门Id缓存。
|
||||
List<Long> originalParentIdList = new LinkedList<>();
|
||||
if (originalSysDept.getParentId() != null) {
|
||||
SysDept originalParentDept = getById(originalSysDept.getParentId());
|
||||
while (originalParentDept != null) {
|
||||
originalParentIdList.add(originalParentDept.getDeptId());
|
||||
if (originalParentDept.getParentId() == null) {
|
||||
break;
|
||||
}
|
||||
originalParentDept = getById(originalParentDept.getParentId());
|
||||
}
|
||||
}
|
||||
// 删除其子部门与其原有父部门之间的关联关系。
|
||||
for (Long parentDeptId : originalParentIdList) {
|
||||
sysDeptRelationMapper.removeBetweenChildrenAndParents(parentDeptId, sysDept.getDeptId());
|
||||
}
|
||||
// 2. 将当前部门与原有的父部门列表解除关系。
|
||||
SysDeptRelation filter = new SysDeptRelation();
|
||||
filter.setDeptId(sysDept.getDeptId());
|
||||
sysDeptRelationMapper.delete(new QueryWrapper<>(filter));
|
||||
// 3. 将当前部门和新的父部门列表建立关联关系。
|
||||
// 在插入与新父部门的关联关系
|
||||
List<SysDeptRelation> deptRelationList = new LinkedList<>();
|
||||
// 先插入自己和自己的关系。
|
||||
deptRelationList.add(new SysDeptRelation(sysDept.getDeptId(), sysDept.getDeptId()));
|
||||
SysDept parentSysDept = null;
|
||||
if (sysDept.getParentId() != null) {
|
||||
parentSysDept = getById(sysDept.getParentId());
|
||||
}
|
||||
List<Long> newParentIdList = new LinkedList<>();
|
||||
// 再插入直接父部门,以及父部门的父部门,并向上以此类推。
|
||||
while (parentSysDept != null) {
|
||||
newParentIdList.add(parentSysDept.getDeptId());
|
||||
deptRelationList.add(
|
||||
new SysDeptRelation(parentSysDept.getDeptId(), sysDept.getDeptId()));
|
||||
if (parentSysDept.getParentId() == null) {
|
||||
break;
|
||||
}
|
||||
parentSysDept = getById(parentSysDept.getParentId());
|
||||
}
|
||||
sysDeptRelationMapper.insertList(deptRelationList);
|
||||
// 4. 将当前部门的子部门与其新的父部门建立关联关系
|
||||
QueryWrapper<SysDeptRelation> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq(MyModelUtil.mapToColumnName("parentDeptId", SysDeptRelation.class), sysDept.getDeptId());
|
||||
queryWrapper.ne(MyModelUtil.mapToColumnName("deptId", SysDeptRelation.class), sysDept.getDeptId());
|
||||
List<SysDeptRelation> childRelationList = sysDeptRelationMapper.selectList(queryWrapper);
|
||||
List<SysDeptRelation> newChildrenAndParentList = new LinkedList<>();
|
||||
for (Long newParentId : newParentIdList) {
|
||||
for (SysDeptRelation childDeptRelation : childRelationList) {
|
||||
newChildrenAndParentList.add(new SysDeptRelation(newParentId, childDeptRelation.getDeptId()));
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(newChildrenAndParentList)) {
|
||||
sysDeptRelationMapper.insertList(newChildrenAndParentList);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定数据。
|
||||
*
|
||||
* @param deptId 主键Id。
|
||||
* @return 成功返回true,否则false。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean remove(Long deptId) {
|
||||
if (sysDeptMapper.deleteById(deptId) == 0) {
|
||||
return false;
|
||||
}
|
||||
// 这里删除当前部门及其父部门的关联关系。
|
||||
// 当前部门和子部门的关系无需在这里删除,因为包含子部门时不能删除父部门。
|
||||
SysDeptRelation deptRelation = new SysDeptRelation();
|
||||
deptRelation.setDeptId(deptId);
|
||||
sysDeptRelationMapper.delete(new QueryWrapper<>(deptRelation));
|
||||
SysDataPermDept dataPermDept = new SysDataPermDept();
|
||||
dataPermDept.setDeptId(deptId);
|
||||
sysDataPermDeptMapper.delete(new QueryWrapper<>(dataPermDept));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单表查询结果。由于没有关联数据查询,因此在仅仅获取单表数据的场景下,效率更高。
|
||||
* 如果需要同时获取关联数据,请移步(getSysDeptListWithRelation)方法。
|
||||
*
|
||||
* @param filter 过滤对象。
|
||||
* @param orderBy 排序参数。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
@Override
|
||||
public List<SysDept> getSysDeptList(SysDept filter, String orderBy) {
|
||||
return sysDeptMapper.getSysDeptList(null, null, filter, orderBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主表的查询结果,查询条件中包括主表过滤对象和指定字段的(in list)过滤。
|
||||
* 由于没有关联数据查询,因此在仅仅获取单表数据的场景下,效率更高。
|
||||
* 如果需要同时获取关联数据,请移步(getSysDeptListWithRelation)方法。
|
||||
*
|
||||
* @param inFilterField (In-list)指定的字段(Java成员字段,而非数据列名)。
|
||||
* @param inFilterValues inFilterField指定字段的(In-list)数据列表。
|
||||
* @param filter 过滤对象。
|
||||
* @param orderBy 排序参数。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
@Override
|
||||
public <M> List<SysDept> getSysDeptList(
|
||||
String inFilterField, Set<M> inFilterValues, SysDept filter, String orderBy) {
|
||||
String inFilterColumn = MyModelUtil.mapToColumnName(inFilterField, SysDept.class);
|
||||
return sysDeptMapper.getSysDeptList(inFilterColumn, inFilterValues, filter, orderBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主表的查询结果,以及主表关联的字典数据和一对一从表数据,以及一对一从表的字典数据。
|
||||
* 该查询会涉及到一对一从表的关联过滤,或一对多从表的嵌套关联过滤,因此性能不如单表过滤。
|
||||
* 如果仅仅需要获取主表数据,请移步(getSysDeptList),以便获取更好的查询性能。
|
||||
*
|
||||
* @param filter 主表过滤对象。
|
||||
* @param orderBy 排序对象。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
@Override
|
||||
public List<SysDept> getSysDeptListWithRelation(SysDept filter, String orderBy) {
|
||||
List<SysDept> resultList = sysDeptMapper.getSysDeptList(null, null, filter, orderBy);
|
||||
// 在缺省生成的代码中,如果查询结果resultList不是Page对象,说明没有分页,那么就很可能是数据导出接口调用了当前方法。
|
||||
// 为了避免一次性的大量数据关联,规避因此而造成的系统运行性能冲击,这里手动进行了分批次读取,开发者可按需修改该值。
|
||||
int batchSize = resultList instanceof Page ? 0 : 1000;
|
||||
this.buildRelationForDataList(resultList, MyRelationParam.normal(), batchSize);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主表的查询结果,查询条件中包括主表过滤对象和指定字段的(in list)过滤。
|
||||
* 同时还包含主表关联的字典数据和一对一从表数据,以及一对一从表的字典数据。
|
||||
* 如果仅仅需要获取主表数据,请移步(getSysDeptList),以便获取更好的查询性能。
|
||||
*
|
||||
* @param inFilterField (In-list)指定的字段(Java成员字段,而非数据列名)。
|
||||
* @param inFilterValues inFilterField指定字段的(In-list)数据列表。
|
||||
* @param filter 主表过滤对象。
|
||||
* @param orderBy 排序对象。
|
||||
* @return 查询结果集。
|
||||
*/
|
||||
@Override
|
||||
public <M> List<SysDept> getSysDeptListWithRelation(
|
||||
String inFilterField, Set<M> inFilterValues, SysDept filter, String orderBy) {
|
||||
String inFilterColumn = MyModelUtil.mapToColumnName(inFilterField, SysDept.class);
|
||||
List<SysDept> resultList =
|
||||
sysDeptMapper.getSysDeptList(inFilterColumn, inFilterValues, filter, orderBy);
|
||||
// 在缺省生成的代码中,如果查询结果resultList不是Page对象,说明没有分页,那么就很可能是数据导出接口调用了当前方法。
|
||||
// 为了避免一次性的大量数据关联,规避因此而造成的系统运行性能冲击,这里手动进行了分批次读取,开发者可按需修改该值。
|
||||
int batchSize = resultList instanceof Page ? 0 : 1000;
|
||||
this.buildRelationForDataList(resultList, MyRelationParam.dictOnly(), batchSize);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定对象是否包含下级对象。
|
||||
*
|
||||
* @param deptId 主键Id。
|
||||
* @return 存在返回true,否则false。
|
||||
*/
|
||||
@Override
|
||||
public boolean hasChildren(Long deptId) {
|
||||
SysDept filter = new SysDept();
|
||||
filter.setParentId(deptId);
|
||||
return getCountByFilter(filter) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定部门Id是否包含用户对象。
|
||||
*
|
||||
* @param deptId 部门主键Id。
|
||||
* @return 存在返回true,否则false。
|
||||
*/
|
||||
@Override
|
||||
public boolean hasChildrenUser(Long deptId) {
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setDeptId(deptId);
|
||||
return sysUserService.getCountByFilter(sysUser) > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user