定义策略接口
package com.bee.supply.chain.finance.dataentry.strategy.macro;
import com.bee.supply.chain.finance.common.dto.Page;
import com.bee.supply.chain.finance.common.entity.ResponseResult;
import com.bee.supply.chain.finance.dataentry.dto.ChartCommonDTO;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import java.util.List;
/**
* @author chenxm66777123
* @version 1.0.0
* @ClassName MacroEconomicGrowthStrategy
* @Description 宏观经济策略接口
* @Date
*/
public interface MacroStrategy {
ResponseResult<List<ChartCommonDTO>> resolveEconomicData(SearchQuery searchQuery
, Integer key, Page page);
}
// 不同策略实现分别的实现不同
package com.bee.supply.chain.finance.dataentry.strategy.macro.impl;
import com.bee.supply.chain.finance.common.dto.Page;
import com.bee.supply.chain.finance.common.entity.ResCodeEnum;
import com.bee.supply.chain.finance.common.entity.ResponseResult;
import com.bee.supply.chain.finance.common.utils.DateUtils;
import com.bee.supply.chain.finance.common.utils.PageUtils;
import com.bee.supply.chain.finance.dataentry.constants.enums.EnumMenuTags;
import com.bee.supply.chain.finance.dataentry.dto.ChartCommonDTO;
import com.bee.supply.chain.finance.dataentry.entity.MacroSocialFixedInvestment;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroFactory;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName
* @Description
* @author chenxm66777123
* @Date
* @version 1.0.0
*/
@Component
public class SocialFixedInvestmentMacroStrategy implements MacroStrategy , InitializingBean {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public ResponseResult<List<ChartCommonDTO>> resolveEconomicData(SearchQuery searchQuery, Integer key, Page page) {
List<MacroSocialFixedInvestment> socialFixed = elasticsearchTemplate.queryForList(searchQuery,
MacroSocialFixedInvestment.class);
List<ChartCommonDTO> result = new ArrayList<>();
Long count = elasticsearchTemplate.count(searchQuery);
socialFixed.stream().forEach((x) -> {
result.add(new ChartCommonDTO().setId(x.getId()).
setData(x.getCompleteCumulative())
.setTime(DateUtils.format(x.getIndicatorData(), DateUtils.Y_M)));
});
return ResponseResult.buildResponseResult(ResCodeEnum.SUCCESS
, result, PageUtils.transToPage(PageUtils.transFromPage(page).setTotal(count.intValue())));
}
@Override
public void afterPropertiesSet() throws Exception {
MacroFactory.registerMacroStrategy(EnumMenuTags.EconomicGrowth.SOCIAL_FIXED_INVESTMENT.getKey()
, this);
}
}
package com.bee.supply.chain.finance.dataentry.strategy.macro.impl;
import com.bee.supply.chain.finance.common.dto.Page;
import com.bee.supply.chain.finance.common.entity.ResCodeEnum;
import com.bee.supply.chain.finance.common.entity.ResponseResult;
import com.bee.supply.chain.finance.common.utils.DateUtils;
import com.bee.supply.chain.finance.common.utils.PageUtils;
import com.bee.supply.chain.finance.dataentry.constants.enums.EnumMenuTags;
import com.bee.supply.chain.finance.dataentry.dto.ChartCommonDTO;
import com.bee.supply.chain.finance.dataentry.entity.MacroSocialFixedInvestment;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroFactory;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName
* @Description
* @author chenxm66777123
* @Date
* @version 1.0.0
*/
@Component
public class SocialFixedInvestmentSixtyMacroStrategy implements MacroStrategy , InitializingBean {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public ResponseResult<List<ChartCommonDTO>> resolveEconomicData(SearchQuery searchQuery, Integer key, Page page) {
List<MacroSocialFixedInvestment> socialFixed = elasticsearchTemplate.queryForList(searchQuery,
MacroSocialFixedInvestment.class);
List<ChartCommonDTO> result = new ArrayList<>();
Long count = elasticsearchTemplate.count(searchQuery);
socialFixed.stream().forEach((x) -> {
result.add(new ChartCommonDTO().setId(x.getId())
.setData(x.getCompleteCumulativeSixtyMonths())
.setTime(DateUtils.format(x.getIndicatorData(), DateUtils.Y_M)));
});
return ResponseResult.buildResponseResult(ResCodeEnum.SUCCESS
, result, PageUtils.transToPage(PageUtils.transFromPage(page).setTotal(count.intValue())));
}
@Override
public void afterPropertiesSet() throws Exception {
MacroFactory.registerMacroStrategy(EnumMenuTags.EconomicGrowth.SOCIAL_FIXED_INVESTMENT_60.getKey()
, this);
}
}
package com.bee.supply.chain.finance.dataentry.strategy.macro.impl;
import com.bee.supply.chain.finance.common.dto.Page;
import com.bee.supply.chain.finance.common.entity.ResCodeEnum;
import com.bee.supply.chain.finance.common.entity.ResponseResult;
import com.bee.supply.chain.finance.common.utils.DateUtils;
import com.bee.supply.chain.finance.common.utils.PageUtils;
import com.bee.supply.chain.finance.dataentry.constants.enums.EnumMenuTags;
import com.bee.supply.chain.finance.dataentry.dto.ChartCommonDTO;
import com.bee.supply.chain.finance.dataentry.entity.MacroSocialFixedInvestment;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroFactory;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName
* @Description
* @author chenxm66777123
* @Date
* @version 1.0.0
*/
@Component
public class SocialFixedInvestmentTwelveMacroStrategy implements MacroStrategy , InitializingBean {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public ResponseResult<List<ChartCommonDTO>> resolveEconomicData(SearchQuery searchQuery, Integer key, Page page) {
List<MacroSocialFixedInvestment> socialFixed = elasticsearchTemplate.queryForList(searchQuery,
MacroSocialFixedInvestment.class);
List<ChartCommonDTO> result = new ArrayList<>();
Long count = elasticsearchTemplate.count(searchQuery);
socialFixed.stream().forEach((x) -> {
result.add(new ChartCommonDTO().setId(x.getId())
.setData(x.getCompleteCumulativeTwelveMonths())
.setTime(DateUtils.format(x.getIndicatorData(), DateUtils.Y_M)));
});
return ResponseResult.buildResponseResult(ResCodeEnum.SUCCESS
, result, PageUtils.transToPage(PageUtils.transFromPage(page).setTotal(count.intValue())));
}
@Override
public void afterPropertiesSet() throws Exception {
MacroFactory.registerMacroStrategy(EnumMenuTags.EconomicGrowth.SOCIAL_FIXED_INVESTMENT_12.getKey()
, this);
}
}
package com.bee.supply.chain.finance.dataentry.strategy.macro.impl;
import com.bee.supply.chain.finance.common.dto.Page;
import com.bee.supply.chain.finance.common.entity.ResCodeEnum;
import com.bee.supply.chain.finance.common.entity.ResponseResult;
import com.bee.supply.chain.finance.common.utils.DateUtils;
import com.bee.supply.chain.finance.common.utils.PageUtils;
import com.bee.supply.chain.finance.dataentry.constants.enums.EnumMenuTags;
import com.bee.supply.chain.finance.dataentry.dto.ChartCommonDTO;
import com.bee.supply.chain.finance.dataentry.entity.MacroSocialFixedInvestment;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroFactory;
import com.bee.supply.chain.finance.dataentry.strategy.macro.MacroStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName
* @Description
* @author chenxm66777123
* @Date
* @version 1.0.0
*/
@Component
public class SocialFixedInvestmentTwentyFourMacroStrategy implements MacroStrategy , InitializingBean {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public ResponseResult<List<ChartCommonDTO>> resolveEconomicData(SearchQuery searchQuery, Integer key, Page page) {
List<MacroSocialFixedInvestment> socialFixed = elasticsearchTemplate.queryForList(searchQuery,
MacroSocialFixedInvestment.class);
List<ChartCommonDTO> result = new ArrayList<>();
Long count = elasticsearchTemplate.count(searchQuery);
socialFixed.stream().forEach((x) -> {
result.add(new ChartCommonDTO().setId(x.getId())
.setData(x.getCompleteCumulativeTwentyfourMonths())
.setTime(DateUtils.format(x.getIndicatorData(), DateUtils.Y_M)));
});
return ResponseResult.buildResponseResult(ResCodeEnum.SUCCESS
, result, PageUtils.transToPage(PageUtils.transFromPage(page).setTotal(count.intValue())));
}
@Override
public void afterPropertiesSet() throws Exception {
MacroFactory.registerMacroStrategy(EnumMenuTags.EconomicGrowth.SOCIAL_FIXED_INVESTMENT_24.getKey()
, this);
}
}
//定义静态工厂,将策略注入
package com.bee.supply.chain.finance.dataentry.strategy.macro;
import com.bee.supply.chain.finance.common.dto.Page;
import com.bee.supply.chain.finance.common.entity.ResponseResult;
import com.bee.supply.chain.finance.dataentry.dto.ChartCommonDTO;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author chenxm66777123
* @version 1.0.0
* @ClassName
* @Description
* @Date
*/
public class MacroFactory {
private static Map<Integer, MacroStrategy> macroStrategyMap = new HashMap<Integer, MacroStrategy>();
private MacroFactory() {
}
private static final MacroStrategy EMPTY = new EmptyMacroStrategy();
//获取
public static MacroStrategy getMacroStrategy(Integer key) {
MacroStrategy result = macroStrategyMap.get(key);
return result == null ? EMPTY : result;
}
//将对象注册到这里
public static void registerMacroStrategy(Integer key, MacroStrategy obj) {
macroStrategyMap.put(key, obj);
}
private static class EmptyMacroStrategy implements MacroStrategy {
@Override
public ResponseResult<List<ChartCommonDTO>>
resolveEconomicData(SearchQuery searchQuery, Integer key, Page page) {
return null;
}
}
}
// 根据选择执行不同的策略
//key可制作为枚举,每一个策略就是拥有一个key
MacroStrategy macroStrategy = MacroFactory.getMacroStrategy(key);
//执行处罚逻辑
return macroStrategy.resolveEconomicData(searchQuery, key, page);