欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

多条件组合检查设计

时间:2023-07-05
多条件组合检查设计

开发遇到多条件组合检查,但是我们可能需要多种组合比如有一条数据会有甲,乙,丙,丁检查。但是在某种场景下只检查甲乙;另一种检查丙丁;如何设计?可以将这些检查给个二进制数。甲: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;@Componentpublic class PositionVerificationHandeler { @Autowired private PositionService positionService; @Autowired private StaffMiddleProviderService staffMiddleProviderService; @Autowired private PositionController positionController; public Map checkPosition(@RequestParam("companyId") String companyId, @RequestParam(value = "throwException") Boolean throwException, @RequestParam(value = "type") String type, @RequestParam(value = "option") int option, @RequestBody List positionIds) throws Ihr360Exception { List positions = new ArrayList<>(); Map 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 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 checkPositionRefenceGradeSystem(List positionIds, Map resultMap, String type, Boolean throwException, String companyId) { if (positionIds.size() == resultMap.size()) { return resultMap; } String msg = String.format(CompanyPositionConstant.MSG_GRADE_SYSTEM,type); Set 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 checkPositionIsExist(List positions, List positionId, Map map, String type, Boolean throwException) { List 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 checkPositionDisable(List positions, List positionId, Map map, String type, Boolean throwException) { List 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 checkPositionInServcieStaff(List stafflist, List positionId, Map map, String type, Boolean throwException) { List 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 checkPositionEntryStaff(List positions, List positionId, Map map, String type, Boolean throwException, String companyId) { String msg = String.format(CompanyPositionConstant.MSG_ENTRY, type); List 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 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 checkPositionQuitStaff(List stafflist, List positionId, Map map, String type, Boolean throwException) { List 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 checkPositionTransfer(List positions, List positionIds, Map resultMap, String type, Boolean throwException, String companyId) { Set 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; }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。