火力规则:返回平台以及组件
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package com.solution.rule.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 平台挂载的组件实体类
|
||||
* 对应表 platform_component
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value = "平台组件对象", description = "平台所挂载的武器、雷达、通信等组件")
|
||||
public class PlatformComponent {
|
||||
|
||||
@ApiModelProperty(value = "组件ID,主键自增")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "组件名称(具体型号)")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "组件类型:weapon(武器)、radar(雷达)、comm(通信)")
|
||||
private String type;
|
||||
|
||||
@ApiModelProperty(value = "组件描述信息")
|
||||
private String description;
|
||||
|
||||
@ApiModelProperty(value = "所属平台ID,关联platform表")
|
||||
private Integer platformId;
|
||||
|
||||
@ApiModelProperty(value = "组件数量")
|
||||
private Long num;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.solution.rule.domain;
|
||||
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
@Data
|
||||
public class RuleParam {
|
||||
|
||||
private WeaponModelVO weaponModelVO;
|
||||
|
||||
private WeaponModelDTO weaponModelDTO;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.solution.rule.domain.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 规则请求参数
|
||||
*/
|
||||
@Data
|
||||
public class RequestDTO {
|
||||
|
||||
//编队数量
|
||||
private Long formationNum;
|
||||
|
||||
//武装直升机数量
|
||||
private Long gunshipNum;
|
||||
|
||||
//无人机数量
|
||||
private Long droneNum;
|
||||
|
||||
//单兵武器数量
|
||||
private Long singleWeaponNum;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.solution.rule.domain.dto;
|
||||
|
||||
import com.solution.rule.domain.PlatformComponent;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class WeaponModelDTO {
|
||||
|
||||
@ApiModelProperty("平台id")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty("平台名称")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty("平台描述")
|
||||
private String description;
|
||||
|
||||
@ApiModelProperty("平台组件")
|
||||
private List<PlatformComponent> components;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.solution.rule.domain.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
// 组件计数VO
|
||||
@Data
|
||||
@ApiModel("组件计数信息")
|
||||
public class ComponentCountVO {
|
||||
@ApiModelProperty("组件名称")
|
||||
private String componentName;
|
||||
|
||||
@ApiModelProperty("组件数量")
|
||||
private Long count;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.solution.rule.domain.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@ApiModel("平台组件名称聚合")
|
||||
public class PlatformComponentNamesVO {
|
||||
@ApiModelProperty("平台名称")
|
||||
private String platformName;
|
||||
|
||||
@ApiModelProperty("该平台下的组件名称列表(去重)")
|
||||
private List<String> componentNames;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.solution.rule.domain.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// 平台武器聚合VO
|
||||
@Data
|
||||
@ApiModel("平台武器聚合信息")
|
||||
public class PlatformWeaponAggregateVO {
|
||||
@ApiModelProperty("平台名称")
|
||||
private String platformName;
|
||||
|
||||
@ApiModelProperty("该平台下的组件列表")
|
||||
private List<ComponentCountVO> components;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.solution.rule.domain.vo;
|
||||
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class WeaponModelVO {
|
||||
|
||||
@ApiModelProperty("平台名称")
|
||||
private String platformName;
|
||||
|
||||
@ApiModelProperty("组件名称")
|
||||
private String componentName;
|
||||
|
||||
@ApiModelProperty("组件数量")
|
||||
private Long count;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.solution.rule.enums;
|
||||
|
||||
public enum SceneType {
|
||||
|
||||
DEFENSE(0, "防御"),
|
||||
AIRBORNE(1, "空降");
|
||||
|
||||
private final int code;
|
||||
private final String description;
|
||||
|
||||
SceneType(int code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据数字编码获取对应的枚举
|
||||
* @param code 前端传递的数字
|
||||
* @return 枚举实例
|
||||
* @throws IllegalArgumentException 如果找不到对应枚举
|
||||
*/
|
||||
public static SceneType fromCode(int code) {
|
||||
for (SceneType type : values()) {
|
||||
if (type.code == code) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("未知的场景类型编码: " + code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.solution.rule.handler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
|
||||
/**
|
||||
* 规则链抽象类
|
||||
*/
|
||||
public abstract class AbstractRuleChainHandler {
|
||||
|
||||
|
||||
private AbstractRuleChainHandler nextHandler;
|
||||
|
||||
|
||||
/**
|
||||
* 执行过滤方法
|
||||
* @param ruleParam
|
||||
* @return
|
||||
*/
|
||||
public abstract RuleParam doHandler(RuleParam ruleParam);
|
||||
|
||||
|
||||
/**
|
||||
* 执行下一个处理器
|
||||
* @param ruleParam
|
||||
* @return
|
||||
*/
|
||||
public RuleParam doNextHandler(RuleParam ruleParam){
|
||||
if(ObjectUtil.isEmpty(nextHandler) || ObjectUtil.isNotEmpty(ruleParam)){
|
||||
return ruleParam;
|
||||
}
|
||||
return nextHandler.doHandler(ruleParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置下游handler
|
||||
* @param nextHandler
|
||||
*/
|
||||
public void setNextHandler(AbstractRuleChainHandler nextHandler) {
|
||||
this.nextHandler = nextHandler;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.solution.rule.handler;
|
||||
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.solution.common.constant.ExceptionConstants;
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 封装规则处理链
|
||||
*/
|
||||
@Component
|
||||
public class RuleChainHandler {
|
||||
|
||||
@Resource
|
||||
private List<AbstractRuleChainHandler> chainHandlers;
|
||||
|
||||
private AbstractRuleChainHandler firstHandler;
|
||||
|
||||
/**
|
||||
* 组装处理链
|
||||
*/
|
||||
@PostConstruct
|
||||
private void constructChain(){
|
||||
if (CollUtil.isEmpty(chainHandlers)) {
|
||||
throw new RuntimeException(ExceptionConstants.NOT_FOUND_CARRIAGE_CHAIN_HANDLER);
|
||||
}
|
||||
this.firstHandler = chainHandlers.get(0);
|
||||
for (int i = 0; i < chainHandlers.size(); i++) {
|
||||
if(i == (chainHandlers.size() - 1)){
|
||||
chainHandlers.get(i).setNextHandler(null);
|
||||
}else {
|
||||
chainHandlers.get(i).setNextHandler(chainHandlers.get(i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public RuleParam findRuleParam(RuleParam ruleParam){
|
||||
return firstHandler.doHandler(ruleParam);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.solution.rule.handler;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.solution.common.constant.ExceptionConstants;
|
||||
import com.solution.common.constant.PlatformAndModuleConstants;
|
||||
import com.solution.rule.domain.PlatformComponent;
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 战斗机处理链
|
||||
*/
|
||||
@Component
|
||||
public class WarplaneHandler extends AbstractRuleChainHandler{
|
||||
|
||||
@Override
|
||||
public RuleParam doHandler(RuleParam ruleParam) {
|
||||
if(ObjectUtil.isEmpty(ruleParam) || ObjectUtil.isEmpty(ruleParam.getWeaponModelDTO())){
|
||||
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
|
||||
}
|
||||
WeaponModelDTO weaponModelDTO = ruleParam.getWeaponModelDTO();
|
||||
List<PlatformComponent> platformComponents = weaponModelDTO.getComponents().stream()
|
||||
.filter(component -> PlatformAndModuleConstants.F22.equals(component.getName()))
|
||||
.collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(platformComponents)) {
|
||||
throw new RuntimeException(ExceptionConstants.NOT_FOUND_F22_COMPONENT);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.solution.rule.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface ModelDetailMapper {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.solution.rule.mapper;
|
||||
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface RuleMapper {
|
||||
|
||||
/**
|
||||
* 获取所有武器平台和组件
|
||||
* @return
|
||||
*/
|
||||
List<WeaponModelVO> getWeapon();
|
||||
|
||||
List<WeaponModelVO> getPlatformComponentNames();
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.solution.rule.service;
|
||||
|
||||
import com.solution.rule.domain.dto.RequestDTO;
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.PlatformComponentNamesVO;
|
||||
import com.solution.rule.domain.vo.PlatformWeaponAggregateVO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public interface RuleService {
|
||||
|
||||
/**
|
||||
* 开始执行规则匹配
|
||||
* @param sceneType
|
||||
* @param weaponModelDTO
|
||||
* @return
|
||||
*/
|
||||
WeaponModelVO execute(Integer sceneType, WeaponModelDTO weaponModelDTO);
|
||||
|
||||
List<PlatformWeaponAggregateVO> getWeapon();
|
||||
|
||||
/**
|
||||
* 获取所有武器平台和组件
|
||||
* @return
|
||||
*/
|
||||
List<PlatformComponentNamesVO> getPlatformComponentNames();
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.solution.rule.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.solution.common.constant.ExceptionConstants;
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.ComponentCountVO;
|
||||
import com.solution.rule.domain.vo.PlatformComponentNamesVO;
|
||||
import com.solution.rule.domain.vo.PlatformWeaponAggregateVO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
import com.solution.rule.mapper.RuleMapper;
|
||||
import com.solution.rule.service.RuleService;
|
||||
import com.solution.rule.strategy.SceneStrategy;
|
||||
import com.solution.rule.strategy.SceneStrategyFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class RuleServiceImpl implements RuleService {
|
||||
|
||||
@Autowired
|
||||
private SceneStrategyFactory strategyFactory;
|
||||
|
||||
@Autowired
|
||||
private RuleMapper ruleMapper;
|
||||
|
||||
@Override
|
||||
public WeaponModelVO execute(Integer sceneType, WeaponModelDTO weaponModelDTO) {
|
||||
if(ObjectUtil.isNull(sceneType) || ObjectUtil.isEmpty(weaponModelDTO)){
|
||||
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
|
||||
}
|
||||
//TODO 查数据库获取我方装备
|
||||
List<PlatformWeaponAggregateVO> weapon = this.getWeapon();
|
||||
|
||||
SceneStrategy strategy = strategyFactory.getStrategy(sceneType);
|
||||
WeaponModelVO result = strategy.execute(weaponModelDTO);
|
||||
if(ObjectUtil.isEmpty(result)){
|
||||
throw new RuntimeException(ExceptionConstants.RESULT_EXCEPTION);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public List<PlatformWeaponAggregateVO> getWeapon() {
|
||||
List<WeaponModelVO> flatList = ruleMapper.getWeapon();
|
||||
if (CollUtil.isEmpty(flatList)) {
|
||||
throw new RuntimeException(ExceptionConstants.RESULT_EXCEPTION);
|
||||
}
|
||||
|
||||
Map<String, List<WeaponModelVO>> groupByPlatform = flatList.stream()
|
||||
.collect(Collectors.groupingBy(WeaponModelVO::getPlatformName));
|
||||
|
||||
List<PlatformWeaponAggregateVO> result = new ArrayList<>();
|
||||
for (Map.Entry<String, List<WeaponModelVO>> entry : groupByPlatform.entrySet()) {
|
||||
PlatformWeaponAggregateVO platformVO = new PlatformWeaponAggregateVO();
|
||||
platformVO.setPlatformName(entry.getKey());
|
||||
|
||||
List<ComponentCountVO> components = entry.getValue().stream()
|
||||
.map(item -> {
|
||||
ComponentCountVO comp = new ComponentCountVO();
|
||||
comp.setComponentName(item.getComponentName());
|
||||
comp.setCount(item.getCount());
|
||||
return comp;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
platformVO.setComponents(components);
|
||||
|
||||
result.add(platformVO);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有武器平台和组件
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<PlatformComponentNamesVO> getPlatformComponentNames() {
|
||||
List<WeaponModelVO> flatList = ruleMapper.getPlatformComponentNames();
|
||||
if (CollUtil.isEmpty(flatList)) {
|
||||
throw new RuntimeException(ExceptionConstants.RESULT_EXCEPTION);
|
||||
}
|
||||
|
||||
|
||||
Map<String, List<String>> groupByPlatform = flatList.stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
WeaponModelVO::getPlatformName,
|
||||
Collectors.mapping(WeaponModelVO::getComponentName, Collectors.toList())
|
||||
));
|
||||
|
||||
return groupByPlatform.entrySet().stream()
|
||||
.map(entry -> {
|
||||
PlatformComponentNamesVO vo = new PlatformComponentNamesVO();
|
||||
vo.setPlatformName(entry.getKey());
|
||||
vo.setComponentNames(entry.getValue());
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.solution.common.constant.ExceptionConstants;
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
import com.solution.rule.enums.SceneType;
|
||||
import com.solution.rule.handler.RuleChainHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
@Component
|
||||
public class AirborneStrategy implements SceneStrategy{
|
||||
|
||||
|
||||
@Autowired
|
||||
private RuleChainHandler ruleChainHandler;
|
||||
/**
|
||||
* 空降场景处理
|
||||
* @param weaponModelDTO
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public WeaponModelVO execute(WeaponModelDTO weaponModelDTO) {
|
||||
if(ObjectUtil.isEmpty(weaponModelDTO)){
|
||||
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
|
||||
}
|
||||
RuleParam ruleParam = new RuleParam();
|
||||
ruleParam.setWeaponModelDTO(weaponModelDTO);
|
||||
ruleParam = ruleChainHandler.findRuleParam(ruleParam);
|
||||
if(ObjectUtil.isEmpty(ruleParam.getWeaponModelVO())){
|
||||
throw new RuntimeException(ExceptionConstants.RESULT_EXCEPTION);
|
||||
}
|
||||
return ruleParam.getWeaponModelVO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SceneType getSceneType() {
|
||||
return SceneType.AIRBORNE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.solution.common.constant.ExceptionConstants;
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
import com.solution.rule.enums.SceneType;
|
||||
import com.solution.rule.handler.RuleChainHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DefenseStrategy implements SceneStrategy{
|
||||
|
||||
|
||||
@Autowired
|
||||
private RuleChainHandler ruleChainHandler;
|
||||
/**
|
||||
* 防御场景处理
|
||||
* @param weaponModelDTO
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public WeaponModelVO execute(WeaponModelDTO weaponModelDTO) {
|
||||
if(ObjectUtil.isEmpty(weaponModelDTO)){
|
||||
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
|
||||
}
|
||||
RuleParam ruleParam = new RuleParam();
|
||||
ruleParam.setWeaponModelDTO(weaponModelDTO);
|
||||
ruleParam = ruleChainHandler.findRuleParam(ruleParam);
|
||||
if(ObjectUtil.isEmpty(ruleParam.getWeaponModelVO())){
|
||||
throw new RuntimeException(ExceptionConstants.RESULT_EXCEPTION);
|
||||
}
|
||||
return ruleParam.getWeaponModelVO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SceneType getSceneType() {
|
||||
return SceneType.DEFENSE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import com.solution.rule.domain.dto.RequestDTO;
|
||||
import com.solution.rule.domain.dto.WeaponModelDTO;
|
||||
import com.solution.rule.domain.vo.WeaponModelVO;
|
||||
import com.solution.rule.enums.SceneType;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public interface SceneStrategy {
|
||||
|
||||
WeaponModelVO execute(WeaponModelDTO weaponModelDTO);
|
||||
|
||||
SceneType getSceneType();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import com.solution.rule.enums.SceneType;
|
||||
import com.solution.rule.strategy.SceneStrategy;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class SceneStrategyFactory {
|
||||
|
||||
@Autowired
|
||||
private List<SceneStrategy> strategyList;
|
||||
|
||||
private final Map<SceneType, SceneStrategy> strategyMap = new EnumMap<>(SceneType.class);
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
for (SceneStrategy strategy : strategyList) {
|
||||
SceneType type = strategy.getSceneType();
|
||||
if (strategyMap.containsKey(type)) {
|
||||
throw new IllegalStateException("重复的场景类型: " + type);
|
||||
}
|
||||
strategyMap.put(type, strategy);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据前端传递的数字编码获取对应的策略
|
||||
* @param code 前端传递的数字
|
||||
* @return 策略实现
|
||||
* @throws IllegalArgumentException 如果编码无效或策略未注册
|
||||
*/
|
||||
public SceneStrategy getStrategy(int code) {
|
||||
SceneType type = SceneType.fromCode(code);
|
||||
SceneStrategy strategy = strategyMap.get(type);
|
||||
if (strategy == null) {
|
||||
throw new IllegalArgumentException("未找到编码 " + code + " 对应的策略实现");
|
||||
}
|
||||
return strategy;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user