请求资方时,是各种不同的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);