多条件组合检查设计
开发遇到多条件组合检查,但是我们可能需要多种组合比如
有一条数据会有甲,乙,丙,丁检查。
但是在某种场景下只检查甲乙;另一种检查丙丁;
如何设计?
可以将这些检查给个二进制数。
甲: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;
@Component
public class PositionVerificationHandeler {
@Autowired
private PositionService positionService;
@Autowired
private StaffMiddleProviderService staffMiddleProviderService;
@Autowired
private PositionController positionController;
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);
}
if (hasState(option, 32)) {
for (CompanyPositionVO position : positions) {
resultMap.put(position.getPositionName(), resultMap.get(position.getId()));
resultMap.remove(position.getId());
}
}
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;
}
}