package com.gk.hotwork.specialWork.service.impl;
|
|
import cn.hutool.core.util.IdUtil;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.gk.hotwork.Domain.Exception.E;
|
import com.gk.hotwork.Domain.Vo.ResultVO;
|
import com.gk.hotwork.Domain.co.ContextCacheUser;
|
import com.gk.hotwork.Domain.dto.DepInfoRPCRespDTO;
|
import com.gk.hotwork.Domain.co.ContextCacheUser;
|
import com.gk.hotwork.Domain.Exception.E;
|
import com.gk.hotwork.Domain.Enum.ResultCodes;
|
import com.gk.hotwork.Domain.Exception.BusinessException;
|
import com.gk.hotwork.Domain.Vo.PageQuery;
|
import com.gk.hotwork.Domain.Vo.ResultVO;
|
import com.gk.hotwork.Domain.Vo.SearchResultVO;
|
import com.gk.hotwork.Domain.dto.UserInfoRPCRespDTO;
|
import com.gk.hotwork.Service.Middle.AccountAuthService;
|
import com.gk.hotwork.Service.Middle.AccountDepartmentService;
|
import com.gk.hotwork.specialWork.entity.*;
|
import com.gk.hotwork.specialWork.entity.ApprovalRule;
|
import com.gk.hotwork.specialWork.entity.ApprovalRuleStep;
|
import com.gk.hotwork.specialWork.entity.ApprovalRuleUnit;
|
import com.gk.hotwork.specialWork.enums.*;
|
import com.gk.hotwork.specialWork.enums.ApprovalStepTypeEnum;
|
import com.gk.hotwork.specialWork.enums.RuleContinueTimeUnitEnum;
|
import com.gk.hotwork.specialWork.enums.WorkTypeEnum;
|
import com.gk.hotwork.specialWork.model.bo.ApprovalRuleBO;
|
import com.gk.hotwork.specialWork.model.dto.req.*;
|
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleRespDTO;
|
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleStepRespDTO;
|
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleUnitItemRespDTO;
|
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleUnitRespDTO;
|
import com.gk.hotwork.specialWork.model.query.db.*;
|
import com.gk.hotwork.specialWork.service.RuleService;
|
import com.gk.hotwork.specialWork.service.baseService.*;
|
import org.redisson.api.RLock;
|
import org.redisson.api.RedissonClient;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.time.LocalDateTime;
|
import java.util.*;
|
import java.util.concurrent.TimeUnit;
|
import java.util.stream.Collectors;
|
|
|
@Service("ruleService")
|
public class RuleServiceImpl implements RuleService {
|
|
@Autowired
|
private ApprovalRuleService approvalRuleService;
|
|
@Autowired
|
private ApprovalRuleStepService approvalRuleStepService;
|
|
@Autowired
|
private ApprovalRuleUnitItemService approvalRuleUnitItemService;
|
|
@Autowired
|
private ApprovalRuleUnitService approvalRuleUnitService;
|
|
@Autowired
|
private ApprovalRuleStandService approvalRuleStandService;
|
|
@Autowired
|
private ApprovalRuleItemMeasureService approvalRuleItemMeasureService;
|
|
@Autowired
|
private RedissonClient redissonClient;
|
|
@Autowired
|
private AccountAuthService accountUserService;
|
|
@Autowired
|
private AccountDepartmentService accountDepartmentService;
|
|
|
@Override
|
@Transactional
|
public ResultVO save(ContextCacheUser currentUser, ApprovalRuleAddReqDTO ruleReqDTO) {
|
|
//加分布式锁,防止重复创建规则
|
RLock lock = redissonClient.getLock("LOCK_RULE_INSERT");
|
try {
|
lock.lock(10, TimeUnit.SECONDS);
|
//部门验证
|
ResultVO<DepInfoRPCRespDTO> resultVO = accountDepartmentService.getDepInfoByDepId(null, ruleReqDTO.getDepId());
|
DepInfoRPCRespDTO depInfoByDepId = (DepInfoRPCRespDTO) resultVO.getData();
|
if (null == depInfoByDepId) {
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门不存在!");
|
}
|
//作业类型验证
|
if (!WorkTypeEnum.checkWorkType(ruleReqDTO.getWorkType())) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "作业类型不合法!");
|
}
|
//作业等级验证
|
if (null != ruleReqDTO.getWorkLevel()) {//如果作业级别不为空
|
if (!WorkLevelEnum.checkWorkLevel(ruleReqDTO.getWorkLevel())) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "作业级别不合法!");
|
}
|
}
|
//验证该部门下是否创建审批流程
|
ApprovalRuleListDbQuery query = new ApprovalRuleListDbQuery();
|
query.setDepId(ruleReqDTO.getDepId());
|
query.setWorkType(ruleReqDTO.getWorkType());
|
query.setWorkLevel(ruleReqDTO.getWorkLevel());
|
List<ApprovalRule> ruleList = approvalRuleService.listRule(query);
|
if (ruleList.size() > 0) {
|
throw new BusinessException(E.DATA_DATABASE_EXIST, "该部门下该项已存在,不可重复");
|
}
|
//填充规则信息
|
ApprovalRule rule = new ApprovalRule();
|
rule.setRuleId(IdUtil.getSnowflake(0, 0).nextId());
|
rule.setDepId(ruleReqDTO.getDepId());
|
rule.setRuleName(ruleReqDTO.getRuleName());
|
rule.setWorkType(ruleReqDTO.getWorkType());
|
if (null == ruleReqDTO.getWorkLevel()) {
|
rule.setWorkLevel((byte) 0);
|
} else {
|
rule.setWorkLevel(ruleReqDTO.getWorkLevel());
|
}
|
rule.setCreateUid(currentUser.getUid());
|
rule.setCreateUname(currentUser.getRealName());
|
|
//层级List
|
List<ApprovalRuleStep> saveStepList = new ArrayList<>();
|
//单元List
|
List<ApprovalRuleUnit> saveUnitList = new ArrayList<>();
|
//审批项List
|
List<ApprovalRuleUnitItem> saveUnitItemList = new ArrayList<>();
|
|
//层级
|
List<ApprovalRuleStepAddReqDTO> stepReqDTOList = ruleReqDTO.getStepList();
|
if (stepReqDTOList != null && stepReqDTOList.size() > 0) {
|
List<Long> uIdList = new ArrayList<>();
|
Set<Long> mIdList = new HashSet<>();
|
Set<Long> sIdList = new HashSet<>();
|
//循环出数据用于后面验证
|
|
stepReqDTOList.forEach(step -> {
|
//单元
|
List<ApprovalRuleUnitAddReqDTO> units = step.getUnitList();
|
if (null != units && units.size() > 0) {
|
units.forEach(unit -> {
|
uIdList.add(unit.getBindUid());
|
});
|
}
|
|
//审批项
|
List<ApprovalRuleUnitItemAddReqDTO> items = step.getItemList();
|
if (null != items && items.size() > 0) {
|
items.forEach(item -> {
|
if (null != item.getMeasureId()) {
|
mIdList.add(item.getMeasureId());
|
}
|
if (null != item.getStandId()) {
|
sIdList.add(item.getStandId());
|
}
|
});
|
}
|
|
|
});
|
//用于后面验证
|
List<UserInfoRPCRespDTO> userList = new ArrayList<>();
|
List<ApprovalRuleItemMeasureDO> measureList = new ArrayList<>();
|
List<ApprovalRuleItemStandDO> standList = new ArrayList<>();
|
if (uIdList.size() > 0) {
|
userList = (List<UserInfoRPCRespDTO>) accountUserService.listUserInfoByUids(uIdList).getData();
|
}
|
if (mIdList.size() > 0) {
|
measureList = approvalRuleItemMeasureService.listItemMeasureByIds(mIdList);
|
}
|
if (sIdList.size() > 0) {
|
standList = approvalRuleStandService.listItemStandByIds(sIdList);
|
}
|
|
//检查序号是否存在重复
|
List<Integer> collect = stepReqDTOList.stream().map(ApprovalRuleStepAddReqDTO::getStepSerial).distinct().collect(Collectors.toList());
|
if (stepReqDTOList.size() > collect.size()) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "排序号不可重复!");
|
}
|
//按照升序排序
|
Collections.sort(stepReqDTOList, new Comparator<ApprovalRuleStepAddReqDTO>() {
|
@Override
|
public int compare(ApprovalRuleStepAddReqDTO o1, ApprovalRuleStepAddReqDTO o2) {
|
//升序
|
return o1.getStepSerial().compareTo(o2.getStepSerial());
|
}
|
});
|
//循环层级
|
Long stepId = null;
|
for (ApprovalRuleStepAddReqDTO stepReqDTO : stepReqDTOList) {
|
//填充层级
|
ApprovalRuleStep step = new ApprovalRuleStep();
|
|
if (!ApprovalStepTypeEnum.checkStepType(stepReqDTO.getType())) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "审批类型层级类型不合法!");
|
}
|
//如果是分析人审批,有效时间,以及有效时间单位必须传
|
if (stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())) {
|
if (null == stepReqDTO.getContinueTime() || null == stepReqDTO.getContinueTimeUnit()) {
|
throw new BusinessException(E.DATA_PARAM_NULL, "当前是分析审批人层级,有效时间或者有效时间单位不可为空!");
|
}
|
if (!RuleContinueTimeUnitEnum.checkTimeUnitType(stepReqDTO.getType())) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "有效时间单位不合法!");
|
}
|
//如果是分析人审批审批项不可为空
|
if(null == stepReqDTO.getItemList() || stepReqDTO.getItemList().size() == 0 ){
|
throw new BusinessException(E.DATA_PARAM_NULL,"分析审批人层级,请添加审批项!");
|
}
|
step.setAuditType(stepReqDTO.getAuditType());
|
}
|
//如果是多人审批并行审核不可为空
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())){
|
if(null == stepReqDTO.getAuditType() ){
|
throw new BusinessException(E.DATA_PARAM_NULL,"多人审批时审核类型不可为空!");
|
}
|
if(null == AuditTypeEnum.parse(stepReqDTO.getAuditType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"审核类型不合法!");
|
}
|
step.setAuditType(stepReqDTO.getAuditType());
|
}
|
//填充规则id
|
step.setStepId(IdUtil.getSnowflake(0, 0).nextId());
|
step.setRuleId(rule.getRuleId());
|
step.setStepName(stepReqDTO.getStepName());
|
step.setStepSerial(stepReqDTO.getStepSerial());
|
step.setType(stepReqDTO.getType());
|
step.setContinueTime(stepReqDTO.getContinueTime());
|
step.setContinueTimeUnit(stepReqDTO.getContinueTimeUnit());
|
step.setGmtCreate(LocalDateTime.now());
|
step.setStatus(RuleStepStatusEnum.VALID.getCode());
|
step.setCreateUname(currentUser.getRealName());
|
step.setCreateUid(currentUser.getUid());
|
if (null != stepId) {
|
step.setPreStepId(stepId);
|
}
|
saveStepList.add(step);
|
stepId = step.getStepId();
|
|
//单元
|
List<ApprovalRuleUnitAddReqDTO> unitReqDTOList = stepReqDTO.getUnitList();
|
//遍历审批单元
|
if (unitReqDTOList != null && unitReqDTOList.size() > 0) {
|
//单人审批
|
if (stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_SINGLE_PERSON.getType())) {
|
if (unitReqDTOList.size() > 1) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "单人审批类型或者分析审批类型时不可设置多人!");
|
}
|
}
|
//分析人审批有可能是单人,也可能是多人
|
//多人审批
|
if (stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())) {
|
if (unitReqDTOList.size() < 2) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "多人审批类型时,需设置多人!");
|
}
|
}
|
for (ApprovalRuleUnitAddReqDTO unitReqDTO : unitReqDTOList) {
|
List<UserInfoRPCRespDTO> uList = userList.stream().filter(user -> user.getUid().equals(unitReqDTO.getBindUid())).collect(Collectors.toList());
|
if (uList.size() == 0) {
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "审批人员不存在!");
|
}
|
//填充单元数据
|
ApprovalRuleUnit unit = new ApprovalRuleUnit();
|
unit.setUnitId(IdUtil.getSnowflake(0, 0).nextId());
|
unit.setRuleId(rule.getRuleId());
|
unit.setStepId(step.getStepId());
|
unit.setBindDepId(ruleReqDTO.getDepId());
|
unit.setBindDepName(depInfoByDepId.getDepName());
|
unit.setBindUid(unitReqDTO.getBindUid());
|
unit.setBindUname(uList.get(0).getRealName());
|
unit.setGmtCreate(LocalDateTime.now());
|
unit.setStatus(RuleUnitStatusEnum.VALID.getCode());
|
unit.setCreateUid(currentUser.getUid());
|
unit.setCreateUname(currentUser.getRealName());
|
saveUnitList.add(unit);
|
|
}
|
} else {
|
throw new BusinessException(E.DATA_PARAM_NULL, step.getStepName() + "未选择审批人!");
|
}
|
|
|
//审批项
|
List<ApprovalRuleUnitItemAddReqDTO> itemReqDTOList = stepReqDTO.getItemList();
|
//遍历审批项
|
if (itemReqDTOList != null && itemReqDTOList.size() > 0) {
|
for (ApprovalRuleUnitItemAddReqDTO item : itemReqDTOList) {
|
//验证类型是否合法
|
if (null == RuleItemTypeEnum.parse(item.getType())) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法!");
|
}
|
|
//填充审批项数据
|
ApprovalRuleUnitItem itemVo = new ApprovalRuleUnitItem();
|
itemVo.setId(IdUtil.getSnowflake(0, 0).nextId());
|
itemVo.setItemName(item.getItemName());
|
itemVo.setRuleId(rule.getRuleId());
|
itemVo.setStepId(step.getStepId());
|
itemVo.setType(item.getType());
|
itemVo.setStatus(RuleItemStatusEnum.VALID.getCode());
|
itemVo.setGmtCreate(LocalDateTime.now());
|
itemVo.setCreateUid(currentUser.getUid());
|
itemVo.setCreateUname(currentUser.getRealName());
|
|
//是否分析人审批
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())){//分析人
|
//数值
|
if(item.getType().equals(RuleItemTypeEnum.NUMERIC.getCode())) {
|
//验证标准是否为空
|
if(null == item.getStandId()){
|
throw new BusinessException(E.DATA_PARAM_NULL,"分析人审批层级和审批项类型不对应,未关联标准项!");
|
}
|
//验证审批标准是否存在
|
List<ApprovalRuleItemStandDO> sList = standList.stream().filter(stand -> stand.getId().equals(item.getStandId())).collect(Collectors.toList());
|
if(sList.size() == 0){
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"标准不存在,请联系管理员!");
|
}
|
itemVo.setStandId(item.getStandId());
|
}
|
//选项
|
if(item.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
|
throw new BusinessException(E.DATA_PARAM_NULL,"审批项类型与分析人类型不一致,请重新选择!");
|
}
|
//填空暂时不操作
|
if(item.getType().equals(RuleItemTypeEnum.FILL.getCode())){
|
throw new BusinessException(E.DATA_PARAM_NULL,"分析人层级暂不支持填空,请重新选择!");
|
}
|
|
}else{//非分析人
|
//验证安全措施是否为空
|
if(null == item.getMeasureId()){
|
throw new BusinessException(E.DATA_PARAM_NULL,"多人或单人审批层级和审批项类型不对应,未关联安全措施项!");
|
}
|
//验证安全措施类型是否存在
|
List<ApprovalRuleItemMeasureDO> mList = measureList.stream().filter(measure -> measure.getId().equals(item.getMeasureId())).collect(Collectors.toList());
|
if(mList.size() == 0){
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"安全措施不存在,请联系管理员!");
|
}
|
//选项
|
if(item.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
|
//验证审批项类型是否与安全措施一致
|
if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
|
}
|
}
|
//填空
|
if(item.getType().equals(RuleItemTypeEnum.FILL.getCode())){
|
//验证审批项类型是否与安全措施一致
|
if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
|
}
|
}
|
itemVo.setMeasureId(item.getMeasureId());
|
}
|
saveUnitItemList.add(itemVo);
|
}
|
}
|
}
|
}
|
//插入规则
|
approvalRuleService.saveRuleInfo(rule);
|
//批量插入层级
|
if (saveStepList.size() > 0) {
|
approvalRuleStepService.saveStepList(saveStepList);
|
}
|
//批量插入单元
|
if (saveUnitList.size() > 0) {
|
approvalRuleUnitService.saveUnitList(saveUnitList);
|
}
|
//批量插入审批项
|
if (saveUnitItemList.size() > 0) {
|
approvalRuleUnitItemService.batchSaveItemList(saveUnitItemList);
|
}
|
|
//创建成功,释放锁
|
lock.unlock();
|
|
} catch (BusinessException e) {
|
e.printStackTrace();
|
throw new BusinessException(e.getCode(), e.getMessage());
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new BusinessException(ResultCodes.SERVER_ERROR);
|
} finally {
|
if (lock.isLocked()) {
|
lock.unlock();
|
}
|
}
|
|
return new ResultVO(ResultCodes.OK);
|
}
|
|
@Override
|
@Transactional
|
public ResultVO update(ContextCacheUser currentUser,ApprovalRuleModReqDTO ruleReqDTO) {
|
|
//主键
|
if(null == ruleReqDTO.getRuleId()){
|
throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
|
}
|
//部门验证
|
ResultVO<DepInfoRPCRespDTO> resultVO = accountDepartmentService.getDepInfoByDepId(null,ruleReqDTO.getDepId());
|
DepInfoRPCRespDTO depInfoByDepId= (DepInfoRPCRespDTO)resultVO.getData();
|
if(null == depInfoByDepId){
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"部门不存在!");
|
}
|
|
|
//根据规则id获取所有层级
|
List<ApprovalRuleStep> stepList = approvalRuleStepService.listByRuleId(ruleReqDTO.getRuleId());
|
//根据规则id获取所有单元
|
List<ApprovalRuleUnit> unitList = approvalRuleUnitService.listByRuleId(ruleReqDTO.getRuleId());
|
//根据规则id获取所有审批项
|
List<ApprovalRuleUnitItem> unitItemList = approvalRuleUnitItemService.listByRuleId(ruleReqDTO.getRuleId());
|
|
//填充规则信息
|
ApprovalRule rule = new ApprovalRule();
|
rule.setRuleId(ruleReqDTO.getRuleId());
|
rule.setRuleName(ruleReqDTO.getRuleName());
|
rule.setModifiedUid(currentUser.getUid());
|
rule.setModifiedUname(currentUser.getRealName());
|
|
//层级List
|
List<ApprovalRuleStep> saveStepList = new ArrayList<>();
|
List<ApprovalRuleStep> updateStepList = new ArrayList<>();
|
//单元List
|
List<ApprovalRuleUnit> saveUnitList = new ArrayList<>();
|
//审批项List
|
List<ApprovalRuleUnitItem> saveUnitItemList = new ArrayList<>();
|
List<ApprovalRuleUnitItem> updateUnitItemList = new ArrayList<>();
|
|
//层级
|
List<ApprovalRuleStepModReqDTO> stepModReqDTOList = ruleReqDTO.getStepList();
|
if(stepModReqDTOList != null && stepModReqDTOList.size()>0) {
|
|
List<Long> uIdList = new ArrayList<>();
|
Set<Long> mIdList = new HashSet<>();
|
Set<Long> sIdList = new HashSet<>();
|
//循环出数据用于后面验证
|
stepModReqDTOList.forEach(step ->{
|
//单元
|
List<ApprovalRuleUnitModReqDTO> units = step.getUnitList();
|
if(null != units && units.size()>0){
|
units.forEach(unit ->{
|
uIdList.add(unit.getBindUid());
|
});
|
}
|
|
//审批项
|
List<ApprovalRuleUnitItemModReqDTO> items = step.getItemList();
|
if(null != items && items.size()>0){
|
items.forEach(item ->{
|
if (null != item.getMeasureId()){
|
mIdList.add(item.getMeasureId());
|
}
|
if (null != item.getStandId()){
|
sIdList.add(item.getStandId());
|
}
|
});
|
}
|
|
|
});
|
//用于后面验证
|
List<UserInfoRPCRespDTO> userList = new ArrayList<>();
|
List<ApprovalRuleItemMeasureDO> measureList = new ArrayList<>();
|
List<ApprovalRuleItemStandDO> standList = new ArrayList<>();
|
if(uIdList.size()>0){
|
userList= (List<UserInfoRPCRespDTO>)accountUserService.listUserInfoByUids(uIdList).getData();
|
}
|
if(mIdList.size()>0){
|
measureList = approvalRuleItemMeasureService.listItemMeasureByIds(mIdList);
|
}
|
if(sIdList.size()>0){
|
standList = approvalRuleStandService.listItemStandByIds(sIdList);
|
}
|
//检查序号是否存在重复
|
List<Integer> collect = stepModReqDTOList.stream().map(ApprovalRuleStepModReqDTO::getStepSerial).distinct().collect(Collectors.toList());
|
if (stepModReqDTOList.size() > collect.size()) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "排序号不可重复!");
|
}
|
//按照升序排序
|
Collections.sort(stepModReqDTOList, new Comparator<ApprovalRuleStepModReqDTO>() {
|
@Override
|
public int compare(ApprovalRuleStepModReqDTO o1, ApprovalRuleStepModReqDTO o2) {
|
//升序
|
return o1.getStepSerial().compareTo(o2.getStepSerial());
|
}
|
});
|
|
//过滤出要删除的层级 做删除以及解绑操作
|
List<ApprovalRuleStep> deleteOldStepList = stepList.stream().parallel().filter(a ->
|
stepModReqDTOList.stream().noneMatch(b ->
|
a.getStepId().equals(b.getStepId())))
|
.collect(Collectors.toList());
|
List<Long> deleteOldStepIdList = deleteOldStepList.stream().map(step -> step.getStepId()).collect(Collectors.toList());
|
if(deleteOldStepIdList != null && deleteOldStepIdList.size()>0){
|
//删除关联的审批项
|
approvalRuleUnitItemService.updateStatusByStepIds(deleteOldStepIdList);
|
//删除关联的单元
|
approvalRuleUnitService.updateStatusByStepIds(deleteOldStepIdList);
|
//先删除原有层级
|
approvalRuleStepService.updateStatusByStepIds(deleteOldStepIdList);
|
}
|
|
Long stepId = null;
|
//循环层级 更新的层级
|
for (ApprovalRuleStepModReqDTO stepReqDTO : stepModReqDTOList) {
|
//填充层级数据
|
ApprovalRuleStep step = new ApprovalRuleStep();
|
|
//校验
|
if (!ApprovalStepTypeEnum.checkStepType(stepReqDTO.getType())) {
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "审批类型层级类型不合法!");
|
}
|
//如果是分析人审批,有效时间,以及有效时间单位必须传
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())){
|
if(null == stepReqDTO.getContinueTime() || null == stepReqDTO.getContinueTimeUnit()){
|
throw new BusinessException(E.DATA_PARAM_NULL,"当前是分析审批人层级,有效时间或者有效时间单位不可为空!");
|
}
|
if(!RuleContinueTimeUnitEnum.checkTimeUnitType(stepReqDTO.getType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"有效时间单位不合法!");
|
}
|
//如果是分析人审批审批项不可为空
|
if(null == stepReqDTO.getItemList() || stepReqDTO.getItemList().size() == 0 ){
|
throw new BusinessException(E.DATA_PARAM_NULL,"分析审批人层级,请添加审批项!");
|
}
|
step.setAuditType(AuditTypeEnum.CONCURRENT_JOINT_TRIAL.getType());
|
}
|
//如果是多人审批并行审核不可为空
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())){
|
if(null == stepReqDTO.getAuditType() ){
|
throw new BusinessException(E.DATA_PARAM_NULL,"多人审批时并行审核类型不可为空!");
|
}
|
if(null == AuditTypeEnum.parse(stepReqDTO.getAuditType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"审核类型不合法!");
|
}
|
step.setAuditType(stepReqDTO.getAuditType());
|
}
|
//单人审核默认为空
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_SINGLE_PERSON.getType())){
|
step.setAuditType(null);
|
}
|
List<ApprovalRuleUnit> oldUnitList = new ArrayList<>();
|
List<ApprovalRuleUnitItem> oldUnitItemList = new ArrayList<>();
|
|
//填充规则id
|
step.setRuleId(rule.getRuleId());
|
step.setStepName(stepReqDTO.getStepName());
|
step.setStepSerial(stepReqDTO.getStepSerial());
|
step.setType(stepReqDTO.getType());
|
step.setContinueTime(stepReqDTO.getContinueTime());
|
step.setContinueTimeUnit(stepReqDTO.getContinueTimeUnit());
|
|
//判断是否第一个节点
|
if (null != stepId) {
|
step.setPreStepId(stepId);
|
}
|
//层级主键不为空 做更新操作
|
if(null != stepReqDTO.getStepId()){
|
step.setModifiedUid(currentUser.getUid());
|
step.setModifiedUname(currentUser.getRealName());
|
step.setStepId(stepReqDTO.getStepId());
|
updateStepList.add(step);
|
|
//过滤出层级原有的单元人员
|
oldUnitList = unitList.stream().filter(unit -> unit.getStepId().equals(stepReqDTO.getStepId())).collect(Collectors.toList());
|
//过滤出层级原有的审批项
|
oldUnitItemList = unitItemList.stream().filter(item -> item.getStepId().equals(stepReqDTO.getStepId())).collect(Collectors.toList());
|
}else{
|
step.setStepId(IdUtil.getSnowflake(0,0).nextId());
|
step.setStatus(RuleStepStatusEnum.VALID.getCode());
|
step.setGmtCreate(LocalDateTime.now());
|
step.setCreateUid(currentUser.getUid());
|
step.setCreateUname(currentUser.getRealName());
|
saveStepList.add(step);
|
|
}
|
//赋值
|
stepId = step.getStepId();
|
|
//单元
|
List<ApprovalRuleUnitModReqDTO> unitModReqDTOList = stepReqDTO.getUnitList();
|
if(unitModReqDTOList != null && unitModReqDTOList.size()>0){
|
//单人审批
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_SINGLE_PERSON.getType())){
|
if(unitModReqDTOList.size()>1){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"单人审批类型或者分析审批类型时不可设置多人!");
|
}
|
}
|
//多人审批
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())){
|
if(unitModReqDTOList.size() < 2){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"多人审批类型时,需设置多人!");
|
}
|
}
|
//过滤出原来存在的单元
|
List<ApprovalRuleUnit> undeleteOldUnitList = oldUnitList.stream().parallel().filter(oldUnit -> unitModReqDTOList.stream()
|
.anyMatch(newUnit-> newUnit.getBindUid().equals(oldUnit.getBindUid()))).collect(Collectors.toList());
|
//过滤出要删除的单元 做删除操作
|
List<ApprovalRuleUnit> deleteOldUnitList = oldUnitList.stream().parallel().filter(a ->
|
unitModReqDTOList.stream().noneMatch(b ->
|
a.getBindUid().equals(b.getBindUid())))
|
.collect(Collectors.toList());
|
if(deleteOldUnitList.size()>0 && deleteOldUnitList != null){
|
List<Long> deleteOldUnitIdList = deleteOldUnitList.stream().map(unit->unit.getUnitId()).collect(Collectors.toList());
|
//删除审批单元
|
approvalRuleUnitService.updateStatusByUnitIds(deleteOldUnitIdList);
|
}
|
|
//遍历单元
|
for (ApprovalRuleUnitModReqDTO unitReqDTO : unitModReqDTOList) {
|
List<UserInfoRPCRespDTO> uList = userList.stream().filter(user -> user.getUid().equals(unitReqDTO.getBindUid())).collect(Collectors.toList());
|
if (uList.size() == 0) {
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "审批人员不存在!");
|
}
|
List<ApprovalRuleUnit> selectUnit = undeleteOldUnitList.stream().filter(u -> u.getBindUid().equals(unitReqDTO.getBindUid())).collect(Collectors.toList());
|
if(selectUnit.size() == 0){ //如果非原有单元,则插入
|
ApprovalRuleUnit unit = new ApprovalRuleUnit();
|
unit.setUnitId(IdUtil.getSnowflake(0,0).nextId());
|
unit.setRuleId(rule.getRuleId());
|
unit.setStepId(step.getStepId());
|
unit.setBindDepId(ruleReqDTO.getDepId());
|
unit.setBindDepName(depInfoByDepId.getDepName());
|
unit.setBindUid(unitReqDTO.getBindUid());
|
unit.setBindUname(uList.get(0).getRealName());
|
unit.setGmtCreate(LocalDateTime.now());
|
unit.setStatus(RuleUnitStatusEnum.VALID.getCode());
|
unit.setCreateUid(currentUser.getUid());
|
unit.setCreateUname(currentUser.getRealName());
|
saveUnitList.add(unit);
|
}
|
}
|
|
}else{
|
throw new BusinessException(E.DATA_PARAM_NULL, step.getStepName()+"未选择审批人!");
|
}
|
//审批项
|
List<ApprovalRuleUnitItemModReqDTO> itemModReqDTOList = stepReqDTO.getItemList();
|
if(itemModReqDTOList != null){
|
//过滤出要删除的审批项 做删除操作
|
List<ApprovalRuleUnitItem> deleteOldItemList = oldUnitItemList.stream().parallel().filter(a ->
|
itemModReqDTOList.stream().noneMatch(b ->
|
a.getId().equals(b.getId())))
|
.collect(Collectors.toList());
|
if(deleteOldItemList.size()>0 && deleteOldItemList != null){
|
List<Long> deleteOldUnitIdList = deleteOldItemList.stream().map(item->item.getId()).collect(Collectors.toList());
|
//删除审批项
|
approvalRuleUnitItemService.updateStatusByIds(deleteOldUnitIdList);
|
|
}
|
//遍历审批项
|
for (ApprovalRuleUnitItemModReqDTO itemReqDTO : itemModReqDTOList) {
|
//验证类型是否合法
|
if(null == RuleItemTypeEnum.parse(itemReqDTO.getType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"审批项类型不合法!");
|
}
|
|
ApprovalRuleUnitItem itemVo = new ApprovalRuleUnitItem();
|
itemVo.setItemName(itemReqDTO.getItemName());
|
itemVo.setRuleId(rule.getRuleId());
|
itemVo.setStepId(step.getStepId());
|
itemVo.setType(itemReqDTO.getType());
|
//是否分析人审批
|
if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())){//分析人
|
//数值
|
if(itemReqDTO.getType().equals(RuleItemTypeEnum.NUMERIC.getCode())) {
|
//验证标准是否为空
|
if(null == itemReqDTO.getStandId()){
|
throw new BusinessException(E.DATA_PARAM_NULL,"分析人审批层级未关联标准项!");
|
}
|
//验证审批标准是否存在
|
List<ApprovalRuleItemStandDO> sList = standList.stream().filter(stand -> stand.getId().equals(itemReqDTO.getStandId())).collect(Collectors.toList());
|
if(sList.size() == 0){
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"标准不存在,请联系管理员!");
|
}
|
itemVo.setStandId(itemReqDTO.getStandId());
|
}
|
|
//选项
|
if(itemReqDTO.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
|
throw new BusinessException(E.DATA_PARAM_NULL,"审批项类型与分析人类型不一致,请重新选择!");
|
}
|
//填空
|
if(itemReqDTO.getType().equals(RuleItemTypeEnum.FILL.getCode())){
|
throw new BusinessException(E.DATA_PARAM_NULL,"分析人层级暂不支持填空,请重新选择!");
|
}
|
//填空暂不操作
|
|
}else{//非分析人
|
//验证安全措施是否为空
|
if(null == itemReqDTO.getMeasureId()){
|
throw new BusinessException(E.DATA_PARAM_NULL,"多人或单人审批层级未关联安全措施项!");
|
}
|
//验证安全措施类型是否存在
|
List<ApprovalRuleItemMeasureDO> mList = measureList.stream().filter(measure -> measure.getId().equals(itemReqDTO.getMeasureId())).collect(Collectors.toList());
|
if(mList.size() == 0){
|
throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"安全措施不存在,请联系管理员!");
|
}
|
//选项
|
if(itemReqDTO.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
|
//验证审批项类型是否与安全措施一致
|
if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
|
}
|
}
|
//填空
|
if(itemReqDTO.getType().equals(RuleItemTypeEnum.FILL.getCode())){
|
//验证审批项类型是否与安全措施一致
|
if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())){
|
throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
|
}
|
}
|
itemVo.setMeasureId(itemReqDTO.getMeasureId());
|
}
|
//主键为空 则新增
|
if(null == itemReqDTO.getId()){
|
itemVo.setId(IdUtil.getSnowflake(0,0).nextId());
|
itemVo.setStatus(RuleItemStatusEnum.VALID.getCode());
|
itemVo.setGmtCreate(LocalDateTime.now());
|
itemVo.setCreateUid(currentUser.getUid());
|
itemVo.setCreateUname(currentUser.getRealName());
|
saveUnitItemList.add(itemVo);
|
}else{ //修改操作
|
//填充主键
|
itemVo.setId(itemReqDTO.getId());
|
itemVo.setGmtModified(LocalDateTime.now());
|
itemVo.setModifiedUid(currentUser.getUid());
|
itemVo.setModifiedUname(currentUser.getRealName());
|
updateUnitItemList.add(itemVo);
|
}
|
}
|
}
|
}
|
}
|
//更新规则
|
approvalRuleService.updateRule(rule);
|
|
if(saveStepList.size()>0){
|
//批量插入层级
|
approvalRuleStepService.saveStepList(saveStepList);
|
}
|
if(updateStepList.size()>0){
|
//批量更新层级
|
approvalRuleStepService.updateBatchStep(updateStepList);
|
}
|
if(saveUnitList.size()>0){
|
//批量插入单元
|
approvalRuleUnitService.saveUnitList(saveUnitList);
|
}
|
if(saveUnitItemList.size()>0){
|
//批量插入审批项
|
approvalRuleUnitItemService.batchSaveItemList(saveUnitItemList);
|
}
|
if(updateUnitItemList.size()>0){
|
//批量更新审批项
|
approvalRuleUnitItemService.updateBatchItem(updateUnitItemList);
|
}
|
return new ResultVO<>(ResultCodes.OK);
|
}
|
|
@Override
|
@Transactional
|
public ResultVO removeBatchRule(DeleteForm deleteForm) {
|
|
//删除规则表中数据
|
approvalRuleService.updateStutsByRuleIds(deleteForm.getIds());
|
|
//删除关联该规则的层级
|
approvalRuleStepService.updateStatusByRuleIds(deleteForm.getIds());
|
|
//删除单元
|
approvalRuleUnitService.updateStatusByRuleIds(deleteForm.getIds());
|
|
//删除审批项
|
approvalRuleUnitItemService.updateStatusByRuleIds(deleteForm.getIds());
|
|
return new ResultVO<>(ResultCodes.OK);
|
}
|
|
|
|
@Override
|
public SearchResultVO<List<ApprovalRuleRespDTO>> listRuleByPage(PageQuery<ApprovalRuleListDbQuery> pageQuery) {
|
Page<ApprovalRule> page = new Page<>(pageQuery.getPageIndex(),pageQuery.getPageSize());
|
List<ApprovalRule> approvalRules = approvalRuleService.listRuleByPage(page,pageQuery.getSearchParams());
|
//获取层级列表
|
List<ApprovalRuleStep> stepList = approvalRuleStepService.listStepByNoConditions();
|
//获取单元列表
|
List<ApprovalRuleUnit> unitList = approvalRuleUnitService.listByNoConditions();
|
//获取审批项列表
|
List<ApprovalRuleUnitItem> itemList = approvalRuleUnitItemService.listByNoConditions();
|
//获取对应的审批标准
|
//获取安全措施列表
|
|
List<ApprovalRuleRespDTO> ruleRespList = new ArrayList<>();
|
for (ApprovalRule rule : approvalRules) {
|
//复制
|
ApprovalRuleRespDTO ruleRespDTO = new ApprovalRuleRespDTO();
|
BeanUtils.copyProperties(rule,ruleRespDTO);
|
//刷选层级
|
List<ApprovalRuleStep> selectStepList = stepList.stream().filter(step -> step.getRuleId().equals(rule.getRuleId())).collect(Collectors.toList());
|
List<ApprovalRuleStepRespDTO> stepRespList = new ArrayList<>();
|
for (ApprovalRuleStep step : selectStepList) {
|
//复制
|
ApprovalRuleStepRespDTO stepRespDTO = new ApprovalRuleStepRespDTO();
|
BeanUtils.copyProperties(step,stepRespDTO);
|
//过滤单元
|
List<ApprovalRuleUnit> selectUnitList = unitList.stream().filter(unit -> unit.getStepId().equals(step.getStepId())).collect(Collectors.toList());
|
List<ApprovalRuleUnitRespDTO> unitRespDTOList = new ArrayList<>();
|
for (ApprovalRuleUnit unit: selectUnitList) {
|
//复制
|
ApprovalRuleUnitRespDTO unitRespDTO = new ApprovalRuleUnitRespDTO();
|
BeanUtils.copyProperties(unit,unitRespDTO);
|
unitRespDTOList.add(unitRespDTO);
|
}
|
//过滤审批项
|
List<ApprovalRuleUnitItem> selectItemList = itemList.stream().filter(item -> item.getStepId().equals(step.getStepId())).collect(Collectors.toList());
|
List<ApprovalRuleUnitItemRespDTO> itemRespDTOList = new ArrayList<>();
|
for (ApprovalRuleUnitItem item: selectItemList) {
|
//复制
|
ApprovalRuleUnitItemRespDTO itemRespDTO = new ApprovalRuleUnitItemRespDTO();
|
BeanUtils.copyProperties(item,itemRespDTO);
|
itemRespDTOList.add(itemRespDTO);
|
}
|
stepRespDTO.setUnitList(unitRespDTOList);
|
stepRespDTO.setItemList(itemRespDTOList);
|
stepRespList.add(stepRespDTO);
|
}
|
ruleRespDTO.setStepList(stepRespList);
|
ruleRespList.add(ruleRespDTO);
|
}
|
|
//需要获取层级、单元、审批项、以及审批规则 等后面封装进去
|
return new SearchResultVO<>(true,
|
page.getCurrent(),
|
page.getSize(),
|
page.getPages(),
|
page.getTotal(),
|
ruleRespList,
|
ResultCodes.OK);
|
}
|
|
@Override
|
public ApprovalRuleBO recursiveQueryRule(Long depId, Byte workType, Byte workLevel) {
|
ApprovalRuleBO ruleBO = null;
|
// rpc获取部门
|
if (depId == null) {
|
return null;
|
}
|
ResultVO<DepInfoRPCRespDTO> rpcResult = accountDepartmentService.getDepInfoByDepId(null,depId);
|
|
if (rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
|
if (rpcResult.getData() != null) {
|
DepInfoRPCRespDTO department = (DepInfoRPCRespDTO)rpcResult.getData();
|
ApprovalRule rule = approvalRuleService.getApprovalRuleInfo(depId, workType, workLevel);
|
if (rule == null) {
|
ruleBO = recursiveQueryRule(department.getParentDepId(), workType, workLevel);
|
}else{
|
ruleBO = new ApprovalRuleBO();
|
ruleBO.setDepId(rule.getDepId());
|
ruleBO.setDepName(department.getDepName());
|
ruleBO.setEid(rule.getEid());
|
ruleBO.setRuleId(rule.getRuleId());
|
ruleBO.setRuleName(rule.getRuleName());
|
ruleBO.setStatus(rule.getStatus());
|
ruleBO.setWorkLevel(rule.getWorkLevel());
|
ruleBO.setWorkType(rule.getWorkType());
|
}
|
}
|
}else{
|
throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
|
}
|
|
|
return ruleBO;
|
|
|
}
|
}
|