@Component
public class McoServiceContext {
private static final Logger logger = LoggerFactory.getLogger(McoServiceContext.class);
/**
* 修改产品信息service注册表
*/
private Map<OuterPlatformCodeEnum, ProductsUpdateService> productsUpdateServiceMap;
/**
* 订单相关service注册表
*/
private Map<OuterPlatformCodeEnum, OrdersDealService> ordersDealServiceMap;
@PostConstruct
private void init() {
productsUpdateServiceMap = new HashMap<>();
ordersDealServiceMap = new HashMap<>();
initProductsUpdateService();
initOrdersDealService();
}
/**
* 初始化修改产品service
*/
private void initProductsUpdateService() {
Map<String, ProductsUpdateService> result = SpringBeanUtil.getApplicationContext().getBeansOfType(ProductsUpdateService.class);
if (CollectionUtils.isEmpty(result)) {
logger.warn("初始化获取失败,没有发现策略实现 bean。");
throw new RuntimeException("初始化配送单分发策略失败,没有对应的策略实现");
}
for (Map.Entry<String, ProductsUpdateService> entry : result.entrySet()) {
String beanName = entry.getKey();
ProductsUpdateService value = entry.getValue();
OuterPlatformCodeEnum outerPlatformCodeEnum = value.operateType();
if (null != outerPlatformCodeEnum){
productsUpdateServiceMap.put(outerPlatformCodeEnum, value);
}else{
logger.info("初始化,operateType is null,bean:{}", beanName);
}
}
}
/**
* @Author: wangwenying10
* @Description: 订单service
* @Date: 2021/8/12 20:14
* @Param: []
* @Return: void
*/
private void initOrdersDealService() {
Map<String, OrdersDealService> result = SpringBeanUtil.getApplicationContext().getBeansOfType(OrdersDealService.class);
if (CollectionUtils.isEmpty(result)) {
logger.warn("初始化获取失败,没有发现Service实现 bean。");
throw new RuntimeException("初始化配送单分发Service失败,没有对应的Service实现");
}
for (Map.Entry<String, OrdersDealService> entry : result.entrySet()) {
String beanName = entry.getKey();
OrdersDealService value = entry.getValue();
OuterPlatformCodeEnum outerPlatformCodeEnum = value.operateType();
if (null != outerPlatformCodeEnum){
ordersDealServiceMap.put(outerPlatformCodeEnum, value);
}else{
logger.info("初始化,operateType is null,bean:{}", beanName);
}
}
}
/**
* @Author: wangwenying10
* @Description: 获取产品修改service
* @Date: 2021/8/6 18:25
* @Param: [outerPlatformCodeEnum]
* @Return: cn.com.gome.scot.alamein.mco.pop.job.client.ProductsUpdateService
*/
public ProductsUpdateService getProductsUpdateService(OuterPlatformCodeEnum outerPlatformCodeEnum) {
if (outerPlatformCodeEnum == null) {
logger.error("[McoServiceContext.getProductsUpdateService] the input param is null, return [null].");
return null;
}
return productsUpdateServiceMap.get(outerPlatformCodeEnum);
}
public OrdersDealService getOrdersDealService(OuterPlatformCodeEnum outerPlatformCodeEnum) {
if (outerPlatformCodeEnum == null) {
logger.error("[McoServiceContext.getOrdersDealService] the input param is null, return [null].");
return null;
}
return ordersDealServiceMap.get(outerPlatformCodeEnum);
}
}
使用
// 3、调用具体平台服务类
OuterPlatformCodeEnum platformEnum = OuterPlatformCodeEnum.getByCode(response.getData().getExtChannelCode());
ProductsUpdateService service = serviceContext.getProductsUpdateService(platformEnum);
service.pushProductsPrice(response.getData(), req);