// 定义函数式接口 @FunctionalInterface interface MapFunction<A, B, C, D, E, F, R> { R apply(A a, B b, C c, D d, E e, F f); } /** * 定义的策略map */ private Map<String, MapFunction<Integer, String, String, String, String, Boolean, IndicatorEntityDTO>> ruleMap; // 策略map初始化 @PostConstruct public void combineTargetRuleInit() { ruleMap.put(CombineTargetRuleEnum.RECENT.getCode(), (deviceId, tableName, fieldName, startDate, endDate, isFirst) -> combineTargetRuleService.getRecentValue(deviceId, tableName, fieldName, startDate, endDate, isFirst)); ruleMap.put(CombineTargetRuleEnum.FARTHEST.getCode(), (deviceId, tableName, fieldName, startDate, endDate, isFirst) -> combineTargetRuleService.getFarthestValue(deviceId, tableName, fieldName, startDate, endDate, isFirst)); ruleMap.put(CombineTargetRuleEnum.AVG.getCode(), (deviceId, tableName, fieldName, startDate, endDate, isFirst) -> combineTargetRuleService.getAvgValue(deviceId, tableName, fieldName, startDate, endDate)); ruleMap.put(CombineTargetRuleEnum.MAX.getCode(), (deviceId, tableName, fieldName, startDate, endDate, isFirst) -> combineTargetRuleService.getMaxValue(deviceId, tableName, fieldName, startDate, endDate)); ruleMap.put(CombineTargetRuleEnum.MIN.getCode(), (deviceId, tableName, fieldName, startDate, endDate, isFirst) -> combineTargetRuleService.getMinValue(deviceId, tableName, fieldName, startDate, endDate)); } /** * 实现业务逻辑 */ private IndicatorEntityDTO getRelatedTargetValue(Date keyTargetTime, CombineTargetEntity combineTargetEntity, Integer deviceId) { // 获取时间范围内的所有数据,如果主指标先到,时间范围则取主指标之后,如果后到,时间范围则取主指标之前 String fieldName = DataConversionUtil.humpToUnderline(combineTargetEntity.getRelatedTarget()); String tableName = Constant.DFS_RECORD_DEVICE_ + fieldName; // 如果开始和结束时间都为0,则取最新的一条数据,否则按配置的时间单位参数进行查询 if (combineTargetEntity.getStart() == 0 && combineTargetEntity.getEnd() == 0) { return indicatorService.getNewVal(tableName, fieldName, deviceId); } else { String startDate = null; String endDate = null; int start = combineTargetEntity.getFirst() ? combineTargetEntity.getStart() : -combineTargetEntity.getEnd(); int end = combineTargetEntity.getFirst() ? combineTargetEntity.getEnd() : -combineTargetEntity.getStart(); switch (combineTargetEntity.getUnit()) { case Constant.HOUR: startDate = DateUtil.format(DateUtil.addHour(keyTargetTime, start), DateUtil.DATETIME_FORMAT); endDate = DateUtil.format(DateUtil.addHour(keyTargetTime, end), DateUtil.DATETIME_FORMAT); break; case Constant.MINUTE: startDate = DateUtil.format(DateUtil.addMin(keyTargetTime, start), DateUtil.DATETIME_FORMAT); endDate = DateUtil.format(DateUtil.addMin(keyTargetTime, end), DateUtil.DATETIME_FORMAT); break; case Constant.SECOND: startDate = DateUtil.format(DateUtil.addSec(keyTargetTime, start), DateUtil.DATETIME_FORMAT); endDate = DateUtil.format(DateUtil.addSec(keyTargetTime, end), DateUtil.DATETIME_FORMAT); break; default: break; } // 时间范围内的数据根据规则进行数据绑定 return ruleMap.get(combineTargetEntity.getStrategy()).apply(deviceId, tableName, fieldName, startDate, endDate, combineTargetEntity.getFirst()); } }
注解说明:
@FunctionalInterface
@PostConstruct
详解:
策略模式之Map+函数式接口