对接资方时,肯定会有多个实现类很正常

我们提供三种解决方法:

  • 1.1抽象类:
// 辅助类
public interface BeanSupport {
    boolean support(String var1);
}
// 抽象业务类
public interface LoanCallBack extends BeanSupport {
    /**
     * 回调通知
     *
     * @param loanApplyRecord the loan apply record
     * @return the call back resp
     */
    CallBackResp callBackNotify(LoanApplyRecord loanApplyRecord);
}



  • 1.2实现类的容器
@Component
public class SupportFactoryUtils implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    public SupportFactoryUtils() {
    }

    public static <T extends BeanSupport> T get(String supportType, Class<T> cls) {
        Map<String, T> beanMap = applicationContext.getBeansOfType(cls);
        Iterator i$ = beanMap.values().iterator();

        BeanSupport t;
        do {
            if (!i$.hasNext()) {
                return null;
            }

            t = (BeanSupport)i$.next();
        } while(!t.support(supportType));

        return t;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SupportFactoryUtils.applicationContext = applicationContext;
    }
}

  • 1.3实现类
@Component
@Slf4j
public class FqlLoanCallBack implements LoanCallBack {

 


    @Override
    public CallBackResp callBackNotify(LoanApplyRecord loanApplyRecord) {
        
        return cashOrderResult;
    }

    @Override
    public boolean support(String type) {
        return ChannelFlowNames.CHANNEL_FQL.equals(type)|| ChannelCodes.FQL.equals(type);
    }
  

}

  • 1.4 具体使用
	LoanCallBack callBack = SupportFactoryUtils.get(loanApplyRecord.getChannelCode(), LoanCallBack.class);

第二种方案

  • 2.1抽象类
public interface IFundCompensatory {

    /**
     * 各资方处理代偿逻辑
     *
     * @param fundCompensatoryConfig
     */
//    @RpcMethod(async = true)
    void compensatory(FundCompensatoryReq fundCompensatoryReq);

    /**
     * @param fundCompensatoryConfig
     * @return
     */
    boolean support(FundCompensatoryReq fundCompensatoryReq);

    boolean singleCompensatory(FundCompensatoryReq fundCompensatoryReq);

}
  • 2.2抽象类兼容每个实现类入参出参不一致时
 
public abstract class AbstractFundCompensatoryProcessor<T> implements IFundCompensatory {

     
    @Override
    public void compensatory(FundCompensatoryReq compensatoryReq) {
        logger.info("Start to compensatory by compensatoryReq={}", JsonUtil.toJson(compensatoryReq));
           // step1 : 获取
            List<T> repaySchedules = preProcessRepaySchedule(compensatoryReq, page);
            List<CompensatoryResp> compensatoryResps = Lists.newArrayList();
            do {
                if (CollectionUtil.isEmpty(repaySchedules)) {
                    break;
                }
                logger.info("compensatory#preProcessRepaySchedule step2");
                // step2: 保存初始化记录
                saveInitCompensatoryRecord(repaySchedules, compensatoryReq, batchNo);
                logger.info("compensatory#preProcessRepaySchedule step3");
                page++;
                logger.info("compensatory#preProcessRepaySchedule page={}", page);
                repaySchedules.clear();
                repaySchedules = preProcessRepaySchedule(compensatoryReq, page);
            } while (CollectionUtil.isNotEmpty(repaySchedules));
            // step3: 根据代偿记录发起 请求
            QueryResult<CompensatoryRecord> compensatoryRecordQueryResult = queryInitCompensatoryRecord(batchNo);
          
    } 
    protected abstract BaseResp process(T repaySchedule, FundCompensatoryReq fundCompensatoryReq)
            throws AppBizException;
  
    protected abstract List<T> preProcessRepaySchedule(FundCompensatoryReq fundCompensatoryReq, int page); 
    
    protected abstract void saveInitCompensatoryRecord(List<T> repaySchedules, FundCompensatoryReq compensatoryReq, String batchNo);

   
    protected abstract QueryResult<CompensatoryRecord> queryInitCompensatoryRecord(String batchNo);

}

  • 2.3 具体的实现类
@Service
public class ThirdFundChargeBackProcessor extends AbstractFundCompensatoryProcessor<RepaySchedule> {

   

    @Override
    protected BaseResp process(ThirdRepaySchedule repaySchedule, FundCompensatoryReq fundCompensatoryReq) throws AppBizException {
        BaseResp resp = new BaseResp();
        return resp;
    }

   
  
    @Override
    public boolean support(FundCompensatoryReq fundCompensatoryReq) {
        //  
         
    }

    @Override
    public boolean singleCompensatory(FundCompensatoryReq fundCompensatoryReq) {
        return false;
    }
}
  • 2.5具体使用
@Service
public class ProcessCompensatoryImpl implements IProcessCompensatory {

   

    @Autowired
    private List<IFundCompensatory> fundCompensatories;

 

    @Override
    public void processCompensatory(Long id) {
    
 
        for (IFundCompensatory fundCompensatory : fundCompensatories) {
            if (!fundCompensatory.support(compensatoryReq)) {
                continue;
            }
            fundCompensatory.compensatory(compensatoryReq);
        }
    }

}

第三种: 如果实现类是一整套不一样的流程,那么就这样

  • 3.1定义整个流程的类
@Root
public class TxnFlowConfig {

	/**
	 * sdk调用
	 */
	@Element(name = "http-invoke", required = false)
	private HttpInvoke sdkInvoke;

	/**
	 * 批次任务
	 */
	@ElementList(name = "jobs", required = false)
	private List<Job> jobs;

	/**
	 * 交易流程定义
	 */
	@ElementList(inline = true, required = false)
	private List<TxnFlowDef> txnFlowDefs;

	/**
	 * 流程步骤定义
	 */
	@ElementList(inline = true, required = false)
	private List<TxnFlowStepDef> flowStepDefs;
}
  • 3.2将整个流程放入内存容器
@Service 
public class TxnFlowAdminService implements InitializingBean {
	private final Logger LOG = LoggerFactory.getLogger(this.getClass());
	/**
	 * 交易流程配置Map
	 */
	public final Map<String, TxnFlowConfig> txnFlowConfigMap = new ConcurrentHashMap<String, TxnFlowConfig>();
	 

	@Override
	public GateTxnFlowConfig getGateTxnFlowConfig(Long id) {
		if(id == null){
			return null;
		}
		return gateTxnFlowConfigDao.get(id);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
	}
 

}

  • 3.3使用
   TxnFlowDef txnFlowDef = txnFlowService.getTxnFlowDef(gateId, ctx);

版权声明:本文为sinat_27639721原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/sinat_27639721/article/details/119455221