【泛型小解】

请求资方时,是各种不同的model,为了统一管理,用泛型很正常
定义一个接口

public interface IQueryService {
    /**
     * 是否支持该交易
     *
     * @param txnType the txn type
     * @return the boolean
     */
    boolean supports(String txnType);


    /**
     * 交易结果查询
     *
     * @param request the request
     * @return the gate txn query response
     */
    GateTxnQueryResponse query(GateTxnQueryRequest request);

}

定一个一个抽象类

public abstract class AbstractQueryService<Req extends BaseQueryReq, T> implements IQueryService {

    @Override
    public GateTxnQueryResponse query(GateTxnQueryRequest request) {
        // 预处理 如果有校验逻辑之类的 可以放到这里
        preRequest(request); 
        // 构建请求
        Req req = buildReq(request); 
        ZbbankReq<Req> zbReq = new ZbbankReq<>(req);
        zbReq.setMemberId(request.getMemberId());  
        // 访问 银行网关服务
        BaseResp<T> resp = zbGateService.doInvoke(zbReq, getReqType(), getRespClassType()); 
        // 构建resp返回
        GateTxnQueryResponse response = doBuildResp(resp);
        response.setGateReqPayload(JsonUtil.toJson(zbReq)); 
        if (resp != null) {
            // 返回结果需要处理的
            afterResponse(request, response, resp);
        } 
        return response;
    } 
      protected abstract Req buildReq(GateTxnQueryRequest req);

具体的实现类:

@Service
public class CreditQueryServiceImpl extends AbstractQueryService<CreditResultQueryReq, CreditResultQueryResp> {
@Override
	public boolean supports(String txnType) {
		return GateTxnTypes.CREDIT_QUERY.getGateTxnTypeCode().equals(txnType);
	}
	@Override
	protected CreditResultQueryReq buildReq(GateTxnQueryRequest req) {
		ZbCashGateTxnCtrl creditTxnCtrl = zbCashGateTxnCtrlDao.getByTxnId(req.getTxnId());
		CreditResultQueryReq request = new CreditResultQueryReq();
		if (null != creditTxnCtrl) {
			request.setRequestNo(creditTxnCtrl.getRequestNo());
		}
		return request;
	}
	@Override
	public GateTxnQueryResponse query(GateTxnQueryRequest request) {
		//执行资方查询
		return super.query(request);
	}
protected void updateResponse(ZbCashGateTxnCtrl txnCtrl, BaseResp<CreditResultQueryResp> zbResp, GateTxnQueryResponse resp) {
		CreditResultQueryResp result = zbResp.getResult();
		if (CreditStatuses.REFUSE.equals(result.getCreditStatus())) { 
			boolean repeatFlag = loanPreSupport.judgeRepeatUnionCredit(txnCtrl.getExtAttrMap());
			if(repeatFlag){
				resp.setGateRespCode(ZbRespCodes.SUCCESS);
			}
		}
	}
}

具体的调用:

    @Autowired
    private List<IQueryService> queryServiceList;
       public GateTxnQueryResponse queryTxn(GateTxnQueryRequest req) throws AppBizException {
     for (IQueryService queryService : queryServiceList) {
            if (queryService.supports(req.getGateTxnType())) {
                GateTxnQueryResponse response = queryService.query(req); 
                return response;
            }
        }
        }

为了方便看输入输出,也可以这样, 对于无法确定传入,上面的例子这个确定了从传入,只需要将传入转化为自己需要的Req

public interface GateTxnProcessor<R, T> {

    T processTxn(String txnId, R paymentTxnRequest) throws AppBizException;
}
public abstract class AbstractGateTxnProcessor<R, T extends BaseGateResponse> implements GateTxnProcessor<R, T> { 
    
    @Override
    public final T processTxn(String txnId, R input) throws AppBizException {
        PayTxnCtrl payTxnCtrl = payTxnCtrlDAO.getUnique(new QueryPayTxnCtrlCond(txnId));
        T  gateTxnResponse = processTxnInternal(txnId, input, payTxnCtrl); 
       //需要翻译
        txnRespCode = translate(payTxnCtrl.getGateId(), queryResponse.getGateRespCode()); 
        processAfterTxn(txnId, gateTxnResponse);  
        return gateTxnResponse;
    }

    protected abstract T processTxnInternal(String txnId, R input, PayTxnCtrl payTxnCtrl) throws AppBizException;

    protected abstract void processAfterTxn(String txnId, T gateTxnResponse);
// 具体实现类
public class GateApplyTxnProcessor extends AbstractGateTxnProcessor<PaymentApplyTxnRequest, GateApplyTxnResponse> {}

// 具体调用
	@Autowired
	private GateTxnProcessor<PaymentApplyTxnRequest, GateApplyTxnResponse> gateApplyTxnProcessor;
	 PaymentApplyTxnRequest request =new  PaymentApplyTxnRequest ()
		GateApplyTxnResponse gateApplyTxnResponse = gateApplyTxnProcessor.processTxn(payTxnCtrl.getTxnId(), request);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值