26-03-13-14:36:火力规则简单实现

This commit is contained in:
MHW
2026-03-13 14:36:31 +08:00
parent 8b3fe9b548
commit 99c100f2ac
31 changed files with 997 additions and 4 deletions

View File

@@ -0,0 +1,36 @@
package com.solution.rule.service;
import com.solution.rule.domain.FireRuleExecuteDTO;
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 fireRuleExecuteDTO
* @return
*/
List<PlatformWeaponAggregateVO> execute(FireRuleExecuteDTO fireRuleExecuteDTO);
List<PlatformWeaponAggregateVO> getWeapon();
/**
* 获取所有武器平台和组件
* @return
*/
List<PlatformComponentNamesVO> getPlatformComponentNames();
/**
* 获取通信组件的所有平台和组件
* @param scenarioId
* @return
*/
List<PlatformComponentNamesVO> getCommPlatformComponentNames(Integer scenarioId);
}

View File

@@ -0,0 +1,156 @@
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.FireRuleExecuteDTO;
import com.solution.rule.domain.RuleParam;
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 {
private static final long COMPONENT_QUANTITY_THRESHOLD = 1;
@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> execute(FireRuleExecuteDTO fireRuleExecuteDTO) {
if(ObjectUtil.isEmpty(fireRuleExecuteDTO)){
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
}
List<WeaponModelDTO> weaponModelDTOs = fireRuleExecuteDTO.getWeaponModelDTOs();
Integer sceneType = fireRuleExecuteDTO.getSceneType();
// 查数据库获取我方装备
List<ComponentCountVO> weapon = this.getModuleAndCount();
// 创建RuleParam并设置数据
RuleParam ruleParam = new RuleParam();
ruleParam.setWeaponModelDTOList(weaponModelDTOs);
ruleParam.setDatabaseWeapons(weapon);
// 执行策略
SceneStrategy strategy = strategyFactory.getStrategy(sceneType);
List<PlatformWeaponAggregateVO> result = strategy.execute(ruleParam);
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.setComponentCountVOS(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());
}
/**
* 获取通信组件的所有平台和组件
* @param scenarioId
* @return
*/
@Override
public List<PlatformComponentNamesVO> getCommPlatformComponentNames(Integer scenarioId) {
return ruleMapper.getCommPlatformComponentNames(scenarioId);
}
/**
* 获取所有组件以及数量
* @return
*/
private List<ComponentCountVO> getModuleAndCount(){
List<ComponentCountVO> componentCountVOS = ruleMapper.getModuleAndCount();
if(CollUtil.isEmpty(componentCountVOS)){
return new ArrayList<>();
}
return componentCountVOS;
}
}