123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package com.emato.cuspay.biz.merch;
- import com.emato.cuspay.biz.CuspayBiz;
- import com.emato.cuspay.common.contant.MerchNoticeDict;
- import com.emato.cuspay.dao.mapper.merch.MerchNotiMapper;
- import com.emato.cuspay.entity.merch.MerchNoti;
- import com.emato.cuspay.support.msg.resp.ResponseMessage;
- import com.emato.cuspay.support.msg.resp.ResponseStatus;
- import com.emato.cuspay.util.OkHttpUtils;
- import com.google.common.collect.Lists;
- import com.google.common.collect.Maps;
- import com.google.gson.Gson;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import java.io.IOException;
- import java.util.List;
- import java.util.Map;
- /**
- * 商户通知请求回执
- * @author hyq
- * @version 1.0
- * 2018-05-23 09:12
- */
- @Component
- public class MerchantNoticeBiz implements CuspayBiz{
- private static final Logger logger = LoggerFactory.getLogger(MerchantNoticeBiz.class);
- @Value("${db.merch.notice.limit}")
- private Integer limit;
- @Autowired
- private MerchNotiMapper merchNotiMapper;
- public void biz() {
- limit = (limit != null && limit > 0) ? limit : 20;
- List<MerchNoti> notis = merchNotiMapper.selectMerchNotis(limit);
- if (notis == null || notis.isEmpty()) {
- logger.info("未查到商户通知数据");
- return;
- }
- //记录通知状态
- List<MerchNoti> noticeList = Lists.newArrayList();
- notis.forEach(noti->{
- if (noti.getNotifyUrl() == null) {
- logger.error("商户通知回调接口为空");
- noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_3.getItem()); //发送失败
- noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());
- noticeList.add(noti);
- return;
- }
- if (noti.getNotiCount() == null || noti.getNotiCount() <= 0) {
- logger.error("通知商户次数为空");
- noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_3.getItem()); //发送失败
- noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());
- noticeList.add(noti);
- return;
- }
- //组装需要请求的参数
- String jsonStr = createJsonByNoti(noti);
- logger.info("商户通知回调参数Parameters:"+ jsonStr);
- int counter = 0;
- while (counter < noti.getNotiCount()) {
- ++counter;
- Request request = OkHttpUtils.buildRequest(noti.getNotifyUrl(),
- RequestBody.create(OkHttpUtils.JSON, jsonStr));
- String result = null;
- try{
- result = OkHttpUtils.post(request);
- noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_2.getItem()); //通知发送成功
- noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());//停止通知
- break;
- }catch (IOException e) {
- logger.error("商户回调通知异常", e);
- noti.setNotiStatue(MerchNoticeDict.NoticeStatus.i_3.getItem()); //发送失败
- noti.setIsStoped(MerchNoticeDict.IsStopStatus.i_1.getItem());
- }
- }
- noticeList.add(noti);
- });
- try {
- int result = merchNotiMapper.updateBatch(noticeList);
- if (result < 0) {
- logger.error("更新商户通知数据失败" + noticeList);
- }
- } catch (Exception e) {
- logger.error("更新商户通知数据失败", e);
- throw new RuntimeException("更新商户通知数据失败",e);
- }
- }
- private String createJsonByNoti(MerchNoti noti) {
- Map<String, String> request = Maps.newHashMap();
- request.put("merchErpOrderSn",noti.getMerchErpOrderSn());
- request.put("merchSn", noti.getMerchSn());
- request.put("allPaySn",noti.getAllPaySn());
- request.put("allPayNo",noti.getAllPayNo());
- request.put("allMerchId",noti.getAllMerchId());
- request.put("allSubOrderNo",noti.getAllSubOrderNo());
- request.put("buyerPayerCheck",noti.getBuyerPayerCheck());
- request.put("payChnlFlag",noti.getPayChnlFlag());
- request.put("code",noti.getCode());
- request.put("msg",noti.getMsg());
- request.put("cusDeclStatus", noti.getCusDeclStatus());
- request.put("thirdPartyMerchCode",noti.getThirdPartyMerchCode());
- request.put("thirdPartyMerchName",noti.getThirdPartyMerchName());
- request.put("platNo", noti.getPlatSn());
- request.put("platName", noti.getPlatName());
- request.put("merchOrderSn",noti.getAllSubOrderId());
- ResponseMessage responseMessage = new ResponseMessage.Builder()
- .setCode(ResponseStatus.SUCCESS.getItem())
- .setData(request).build();
- return new Gson().toJson(responseMessage);
- }
- }
|