MerchantNoticeBiz.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.emato.cuspay.biz.merch;
  2. import com.emato.cuspay.biz.CuspayBiz;
  3. import com.emato.cuspay.common.contant.MerchNoticeDict;
  4. import com.emato.cuspay.dao.mapper.merch.MerchNotiMapper;
  5. import com.emato.cuspay.entity.merch.MerchNoti;
  6. import com.emato.cuspay.support.msg.resp.ResponseMessage;
  7. import com.emato.cuspay.support.msg.resp.ResponseStatus;
  8. import com.emato.cuspay.util.OkHttpUtils;
  9. import com.google.common.collect.Lists;
  10. import com.google.common.collect.Maps;
  11. import com.google.gson.Gson;
  12. import okhttp3.Request;
  13. import okhttp3.RequestBody;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.stereotype.Component;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * 商户通知请求回执
  24. * @author hyq
  25. * @version 1.0
  26. * 2018-05-23 09:12
  27. */
  28. @Component
  29. public class MerchantNoticeBiz implements CuspayBiz{
  30. private static final Logger logger = LoggerFactory.getLogger(MerchantNoticeBiz.class);
  31. @Value("${db.merch.notice.limit}")
  32. private Integer limit;
  33. @Autowired
  34. private MerchNotiMapper merchNotiMapper;
  35. public void biz() {
  36. limit = (limit != null && limit > 0) ? limit : 20;
  37. List<MerchNoti> notis = merchNotiMapper.selectMerchNotis(limit);
  38. if (notis == null || notis.isEmpty()) {
  39. logger.info("未查到商户通知数据");
  40. return;
  41. }
  42. //记录通知状态
  43. List<MerchNoti> noticeList = Lists.newArrayList();
  44. notis.forEach(noti->{
  45. if (noti.getNotifyUrl() == null) {
  46. logger.error("商户通知回调接口为空");
  47. noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_3.getItem()); //发送失败
  48. noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());
  49. noticeList.add(noti);
  50. return;
  51. }
  52. if (noti.getNotiCount() == null || noti.getNotiCount() <= 0) {
  53. logger.error("通知商户次数为空");
  54. noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_3.getItem()); //发送失败
  55. noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());
  56. noticeList.add(noti);
  57. return;
  58. }
  59. //组装需要请求的参数
  60. String jsonStr = createJsonByNoti(noti);
  61. logger.info("商户通知回调参数Parameters:"+ jsonStr);
  62. int counter = 0;
  63. while (counter < noti.getNotiCount()) {
  64. ++counter;
  65. Request request = OkHttpUtils.buildRequest(noti.getNotifyUrl(),
  66. RequestBody.create(OkHttpUtils.JSON, jsonStr));
  67. String result = null;
  68. try{
  69. result = OkHttpUtils.post(request);
  70. noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_2.getItem()); //通知发送成功
  71. noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());//停止通知
  72. break;
  73. }catch (IOException e) {
  74. logger.error("商户回调通知异常", e);
  75. noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_3.getItem()); //发送失败
  76. noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());
  77. }
  78. }
  79. noticeList.add(noti);
  80. });
  81. try {
  82. int result = merchNotiMapper.updateBatch(noticeList);
  83. if (result < 0) {
  84. logger.error("更新商户通知数据失败" + noticeList);
  85. }
  86. } catch (Exception e) {
  87. logger.error("更新商户通知数据失败", e);
  88. throw new RuntimeException("更新商户通知数据失败",e);
  89. }
  90. }
  91. private String createJsonByNoti(MerchNoti noti) {
  92. Map<String, String> request = Maps.newHashMap();
  93. request.put("merchErpOrderSn",noti.getMerchErpOrderSn());
  94. request.put("merchSn", noti.getMerchSn());
  95. request.put("allPaySn",noti.getAllPaySn());
  96. request.put("allPayNo",noti.getAllPayNo());
  97. request.put("allMerchId",noti.getAllMerchId());
  98. request.put("allSubOrderNo",noti.getAllSubOrderNo());
  99. request.put("buyerPayerCheck",noti.getBuyerPayerCheck());
  100. request.put("payChnlFlag",noti.getPayChnlFlag());
  101. request.put("code",noti.getCode());
  102. request.put("msg",noti.getMsg());
  103. request.put("cusDeclStatus", noti.getCusDeclStatus());
  104. request.put("thirdPartyMerchCode",noti.getThirdPartyMerchCode());
  105. request.put("thirdPartyMerchName",noti.getThirdPartyMerchName());
  106. request.put("platNo", noti.getPlatSn());
  107. request.put("platName", noti.getPlatName());
  108. request.put("merchOrderSn",noti.getAllSubOrderId());
  109. ResponseMessage responseMessage = new ResponseMessage.Builder()
  110. .setCode(ResponseStatus.SUCCESS.getItem())
  111. .setData(request).build();
  112. return new Gson().toJson(responseMessage);
  113. }
  114. }