泛化controller层请求

constant 定义枚举类

package com.jiniutech.common.openacclink.constant;

/**
 * EnterStrategyEnum
 *
 * @author zhangLinwei
 * @since 2021/11/8 21:38
 */
public enum OpenAccountLinkStrategyEnum {

    /**
     * 华福
     */
    HUAFU("mp-service-huafu");

    private String type;

    OpenAccountLinkStrategyEnum(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

yml 文件内加入默认项

impl:
  default:
    information: mp-service-huafu

service 定义方法

接口抽象类:

package com.jiniutech.common.openacclink.service;

/**
 * IOpenAccountLinkService
 *
 * @author zhangLinwei
 * @since 2021/11/8 21:56
 */
public interface IOpenAccountLinkService {

    /**
     * 获取开户链接
     *
     * @return
     */
    public String getGenericOpenAccLink(Integer userId, Integer branchNo);
}

 接口实现类:

package com.jiniutech.common.openacclink.service.impl;

import com.jiniutech.common.annotation.HandlerBrokerStrategy;
import com.jiniutech.common.openacclink.constant.OpenAccountLinkStrategyEnum;
import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * OpenAccountLinkService
 *
 * @author zhangLinwei
 * @since 2021/11/8 21:55
 */
@Service
@HandlerBrokerStrategy(value = OpenAccountLinkStrategyEnum.HUAFU)
public class OpenAccountLinkService implements IOpenAccountLinkService {

    @Value("${open-account-link.huafu.default}")
    private String defaultLink;
    @Value("${open-account-link.huafu.staff}")
    private String linkWithStaff;

    @Override
    public String getGenericOpenAccLink(Integer userId, Integer branchNo) {
        if (userId == null) {
            return defaultLink;
        } else {
            return linkWithStaff + "&yddh=" + userId + "&branchno=" + (branchNo != null ? branchNo : 0);
        }
    }
}

config 策略容器获取bean实体、策略加载

package com.jiniutech.common.openacclink.config;

import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * OpenAccLinkStrategyTypeConfig
 *
 * @author zhangLinwei
 * @since 2021/11/8 22:11
 */
@Component
public class BrokerStrategyTypeConfig {

    @Autowired
    private ApplicationContext applicationContext;

    public static Map<String, Class<IOpenAccountLinkService>> linkStrategyBeanMap = new HashMap();

    /**
     * 根据类型获取校验类型的bean
     *
     * @param type type
     * @return
     */
    public IOpenAccountLinkService getOpenAccountLinkService(String type) {
        Class<IOpenAccountLinkService> strategyClass = linkStrategyBeanMap.get(type);
        if (strategyClass == null) {
            throw new IllegalArgumentException("没有对应的校验类型");
        }
        //从容器中获取对应的策略Bean
        return applicationContext.getBean(strategyClass);
    }
}
package com.jiniutech.common.openacclink.config;

import com.jiniutech.common.annotation.HandlerBrokerStrategy;
import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * 策略加载类
 * @author: zlw
 * @create: 2021-11-09
 */
@Configuration
public class HandlerBrokerProcessor implements ApplicationContextAware {

    /**
     * 获取所有的策略Beanclass 加入HandlerOrderContext属性中
     * @param applicationContext applicationContext
     * @throws BeansException beansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, Object> orderStrategyMap = applicationContext.getBeansWithAnnotation(HandlerBrokerStrategy.class);
        orderStrategyMap.forEach((k, v) -> {
            if (v instanceof IOpenAccountLinkService) {
                Class<IOpenAccountLinkService> orderStrategyClass = (Class<IOpenAccountLinkService>) v.getClass();
                String type = orderStrategyClass.getAnnotation(HandlerBrokerStrategy.class).value().getType();
                //将class加入map中,type作为key
                BrokerStrategyTypeConfig.linkStrategyBeanMap.put(type, orderStrategyClass);
            }
        });
    }
}

controller 层方法实现

package com.jiniutech.common.openacclink.controller;

import com.jiniutech.common.openacclink.config.BrokerStrategyTypeConfig;
import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * OpenAccountLink
 *
 * @author zhangLinwei
 * @since 2021/11/8 21:52
 */
@Slf4j
@RestController
@RequestMapping("/openAccountLink")
@Api(value = "开户链接服务", description = "开户链接服务")
public class OpenAccountLinkController {
    @Resource
    private BrokerStrategyTypeConfig brokerStrategyTypeConfig;

    @Value("${impl.default.information}")
    private String defaultInfoImpl;

    @GetMapping("/generic-get-openAccLink")
    @ApiOperation(
            value = "(泛化)获取开户链接",
            notes = "(泛化)获取开户链接",
            response = String.class,
            httpMethod = "GET")
    public String getOpenAccLink(@ApiParam("所属券商") @RequestParam(required = false) String informationImpl,
                                 @ApiParam("投顾的userId(非必传,不传就是默认自行开户)") @RequestParam(required = false) Integer userId,
                                 @ApiParam("投顾的分公司号(非必传)") @RequestParam(required = false) Integer branchNo) {
        IOpenAccountLinkService openAccountLinkService = brokerStrategyTypeConfig
                .getOpenAccountLinkService(StringUtils.isNotBlank(informationImpl) ? informationImpl : defaultInfoImpl);
        return openAccountLinkService.getGenericOpenAccLink(userId, branchNo);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值