多条件组合检查设计

多条件组合检查设计

	开发遇到多条件组合检查,但是我们可能需要多种组合比如
	有一条数据会有甲,乙,丙,丁检查。
	但是在某种场景下只检查甲乙;另一种检查丙丁;
	如何设计?
		可以将这些检查给个二进制数。
		甲:1
		乙:2
		丙:4
		丁:8
		后端控制检查的参数,比如传入3进行甲乙检查
		实例:
package com.ihr360.organization.middle.controller;

import com.google.common.collect.Sets;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.vo.PageDataInfo;
import com.ihr360.commons.vo.ResultVO;
import com.ihr360.organization.info.constant.CompanyPositionConstant;
import com.ihr360.organization.info.constant.CompanyPositionEnum;
import com.ihr360.organization.info.service.PositionService;
import com.ihr360.organization.info.vo.organization.CompanyPositionVO;
import com.ihr360.organization.middle.feign.StaffMiddleProviderService;
import com.ihr360.organization.middle.feign.vo.EntryFormListVO;
import com.ihr360.organization.middle.feign.vo.EntryFormVO;
import com.ihr360.organization.middle.feign.vo.StaffInfoBasicVO;
import com.ihr360.support.jpa.SearchField;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Classname PositionVerificationHandeler
 * @Description 通用职位验证处理
 * @Date 2021/12/17 下午1:32
 */
@Component
public class PositionVerificationHandeler {
    @Autowired
    private PositionService positionService;
    @Autowired
    private StaffMiddleProviderService staffMiddleProviderService;
    @Autowired
    private PositionController positionController;

    /**
     * 通用职位验证中心,提供职位状态,范围数据验证支持,组织,职位相关业务解决多次验证数据频繁查询
     *
     * @param companyId
     * @param throwException 是否抛出异常
     * @param type           msg类型
     * @param option         校验选项
     * @param positionIds    职位id
     * @return
     * @throws Ihr360Exception
     */
    public Map<String, String> checkPosition(@RequestParam("companyId") String companyId,
                                             @RequestParam(value = "throwException") Boolean throwException,
                                             @RequestParam(value = "type") String type,
                                             @RequestParam(value = "option") int option,
                                             @RequestBody List<String> positionIds) throws Ihr360Exception {
        List<CompanyPositionVO> positions = new ArrayList<>();
        Map<String, String> resultMap = new HashMap<>();
        if (hasState(option, 1) || hasState(option, 2)) {
            positions = Optional
                    .ofNullable(positionService.findByCompanyIdAndIdIn(companyId, positionIds, CompanyPositionEnum.ALL.getState()))
                    .orElse(Collections.emptyList());
            if (CollectionUtils.isEmpty(positions)) {
                throw new Ihr360Exception("", "职位不存在");
            }
        }

        List<StaffInfoBasicVO> staffList = Optional.ofNullable(staffMiddleProviderService.getStaffBasicInfosByPositionIdsAndStaffStatus(companyId, null, positionIds))
                .map(ResultVO::getData)
                .orElse(Collections.emptyList());

        //职位存在
        if (hasState(option, 1)) {
            resultMap = checkPositionIsExist(positions, positionIds, resultMap, type, throwException);
        }
        //职位停用
        if (hasState(option, 2)) {
            resultMap = checkPositionDisable(positions, positionIds, resultMap, type, throwException);
        }
        //在职
        if (hasState(option, 4)) {
            resultMap = checkPositionInServcieStaff(staffList, positionIds, resultMap, type, throwException);
        }
        //待入职
        if (hasState(option, 8)) {
            if (resultMap.size() == positionIds.size()) {
                return resultMap;
            }
            resultMap = checkPositionEntryStaff(positions, positionIds, resultMap, type, throwException, companyId);
        }
        //离职
        if (hasState(option, 16)) {
            resultMap = checkPositionQuitStaff(staffList, positionIds, resultMap, type, throwException);
        }
        //id 转 name
        if (hasState(option, 32)) {
            for (CompanyPositionVO position : positions) {
                resultMap.put(position.getPositionName(), resultMap.get(position.getId()));
                resultMap.remove(position.getId());
            }
        }

        //id 转 name 拼接 职位code
        if (hasState(option, 64)) {
            for (CompanyPositionVO position : positions) {
                String value = "";
                if (StringUtils.isNotBlank(position.getPositionCode())) {
                    value += "(" + position.getPositionCode() + ")";
                }
                resultMap.put(position.getPositionName(), value += resultMap.get(position.getId()));
                resultMap.remove(position.getId());
            }
        }

        //待调动
        if (hasState(option, 128)) {
            if (resultMap.size() == positionIds.size()) {
                return resultMap;
            }
            resultMap = checkPositionTransfer(positions, positionIds, resultMap, type, throwException, companyId);
        }
        //被职级体系引用
        if (hasState(option,256)){
            resultMap =  checkPositionRefenceGradeSystem(positionIds,resultMap, type,throwException, companyId);
        }
        return resultMap;
    }

    private Map<String, String> checkPositionRefenceGradeSystem(List<String> positionIds, Map<String, String> resultMap, String type, Boolean throwException, String companyId) {
        if (positionIds.size() == resultMap.size()) {
            return resultMap;
        }
        String msg = String.format(CompanyPositionConstant.MSG_GRADE_SYSTEM,type);
        Set<String> positionIdSet = positionService.validateDelete(companyId, positionIds);
        if (throwException && CollectionUtils.isNotEmpty(positionIdSet)){
            throw new Ihr360Exception(null,StringUtils.join(positionIdSet,",")+msg);
        }
        for (String positionId : positionIdSet) {
            resultMap.put(positionId, msg);
        }
        return resultMap;

    }

    private Map<String, String> checkPositionIsExist(List<CompanyPositionVO> positions, List<String> positionId, Map<String, String> map, String type, Boolean throwException) {
        List<String> collect = positions.stream().map(CompanyPositionVO::getId).collect(Collectors.toList());
        String msg = String.format(CompanyPositionConstant.MSG_NOT_FIND, type);
        for (String s : positionId) {
            if (!collect.contains(s)) {
                if (throwException) {
                    throw new Ihr360Exception("", msg);
                }
                map.put(s, msg);
            }
        }
        return map;
    }

    private Map<String, String> checkPositionDisable(List<CompanyPositionVO> positions, List<String> positionId, Map<String, String> map, String type, Boolean throwException) {
        List<String> collect = positions.stream().filter(companyPositionVO -> CompanyPositionEnum.DISABLE.getCode() == companyPositionVO.getPositionState()).map(CompanyPositionVO::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return map;
        }
        String msg = String.format(CompanyPositionConstant.MSG_DISABLE, type);
        for (String s : positionId) {
            if (collect.contains(s)) {
                if (throwException) {
                    throw new Ihr360Exception("", msg);
                }
                map.put(s, msg);
            }
        }
        return map;
    }

    private Map<String, String> checkPositionInServcieStaff(List<StaffInfoBasicVO> stafflist, List<String> positionId, Map<String, String> map, String type, Boolean throwException) {
        List<String> collect = stafflist.stream()
                .filter(item -> CompanyPositionConstant.IN_SERVICE.equals(item.getStaffStatus()))
                .map(StaffInfoBasicVO::getPositionId)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return map;
        }
        String msg = String.format(CompanyPositionConstant.MSG_IN_SERVCIE, type);
        for (String s : positionId) {
            if (collect.contains(s)) {
                if (throwException) {
                    throw new Ihr360Exception("", msg);
                }
                map.put(s, msg);
            }
        }
        return map;
    }

    private Map<String, String> checkPositionEntryStaff(List<CompanyPositionVO> positions, List<String> positionId, Map<String, String> map, String type, Boolean throwException, String companyId) {
        String msg = String.format(CompanyPositionConstant.MSG_ENTRY, type);
        List<String> names = positions.stream().map(CompanyPositionVO::getPositionName).collect(Collectors.toList());
        SearchField searchField = new SearchField();
        searchField.setFieldName(EntryFormVO.FieldName.POSITION_NAME);
        searchField.setFieldValue(StringUtils.join(names, ","));
        searchField.setFieldType(SearchField.IN);
        EntryFormListVO entryFormListVO = new EntryFormListVO();
        entryFormListVO.setSearchFields(Arrays.asList(searchField));
        entryFormListVO.setPage(1);
        entryFormListVO.setPageSize(9999999);
        List<EntryFormVO> entryFormVOS = Optional.ofNullable(staffMiddleProviderService.searchEntryForms(companyId, entryFormListVO))
                .map(ResultVO::getData)
                .map(PageDataInfo::getDataList)
                .orElse(Collections.emptyList());

        for (EntryFormVO entryFormVO : entryFormVOS) {
            if (entryFormVO.getEntryStaffInfo().equals(0) && entryFormVO.getAbandonEntry().equals(0)) {
        for (CompanyPositionVO companyPositionVO : positions) {
                    if (companyPositionVO.getPositionName().equals(entryFormVO.getPositionName())) {
                    if (throwException) {
                        throw new Ihr360Exception("", msg);
                    } else {
                        map.put(companyPositionVO.getId(), msg);
                    }
                }
            }
        }
        }
        return map;
    }

    private Map<String, String> checkPositionQuitStaff(List<StaffInfoBasicVO> stafflist, List<String> positionId, Map<String, String> map, String type, Boolean throwException) {
        List<String> collect = stafflist
                .stream().filter(item -> item.getStaffStatus().equals(CompanyPositionConstant.QUIT))
                .map(StaffInfoBasicVO::getPositionId)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return map;
        }
        String msg = String.format(CompanyPositionConstant.MSG_QUIT, type);
        for (String s : positionId) {
            if (collect.contains(s)) {
                if (throwException) {
                    throw new Ihr360Exception("", msg);
                }
                map.put(s, msg);
            }
        }
        return map;
    }

    private Map<String, String> checkPositionTransfer(List<CompanyPositionVO> positions, List<String> positionIds, Map<String, String> resultMap, String type, Boolean throwException, String companyId) {
        Set<String> ids = Optional.ofNullable(staffMiddleProviderService.getTransferFormPositionIds(companyId, positionIds))
                .map(ResultVO::getData)
                .orElse(Sets.newHashSet());
        if (CollectionUtils.isEmpty(ids)) {
            return resultMap;
        }
        String msg = String.format(CompanyPositionConstant.MSG_TRANSFER, type);
        for (String s : positionIds) {
            if (ids.contains(s)) {
                if (throwException) {
                    throw new Ihr360Exception("", msg);
                }
                resultMap.put(s, msg);
            }
        }
        return resultMap;
    }

    public static boolean hasState(int states, int value) {
        return (states & value) != 0;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值