diff --git a/mallinkSchedule/src/main/java/com/iformall/schedule/SharingOrderResultSchedule.java b/mallinkSchedule/src/main/java/com/iformall/schedule/SharingOrderResultSchedule.java new file mode 100644 index 000000000..1dccd434d --- /dev/null +++ b/mallinkSchedule/src/main/java/com/iformall/schedule/SharingOrderResultSchedule.java @@ -0,0 +1,65 @@ +package com.iformall.schedule; + +import com.github.pagehelper.PageInfo; +import com.iformall.domain.po.WxPayOrder; +import com.iformall.domain.po.WxProfitSharingOrder; +import com.iformall.enums.EnumProfitSharingStatus; +import com.iformall.mapper.*; +import com.iformall.service.WxPayOrderService; +import com.iformall.service.WxProfitSharingOrderService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class SharingOrderResultSchedule { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private WxProfitSharingOrderService wxProfitSharingOrderService; + + @Autowired + private WxProfitSharingOrderMapper wxProfitSharingOrderMapper; + + @Autowired + private WxPayOrderMapper wxPayOrderMapper; + + @Scheduled(cron = "0 15 0 * * ?") // 每天凌晨00:15更新分账结果 + //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 + public void sharingOrderResultSchedule() { + + WxProfitSharingOrder wxProfitSharingOrder = new WxProfitSharingOrder(); + wxProfitSharingOrder.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getCode()); + List list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder); + for(WxProfitSharingOrder sharingOrder : list) { + try { + WxPayOrder wxPayOrder = wxPayOrderMapper.selectByPrimaryKey(sharingOrder.getOrderId()); + wxProfitSharingOrderService.querySharingOrder(wxPayOrder); + } catch (Exception e){ + logger.error("状态更新失败:" + EnumProfitSharingStatus.PROFIT_SHARING_ACCEPTED.getMessage() + + " ID:" +sharingOrder.getId() + + " MSG:" + e.getMessage()); + } + } + + wxProfitSharingOrder.setSharingStatus(EnumProfitSharingStatus.PROFIT_SHARING_PROCESSING.getCode()); + list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder); + for(WxProfitSharingOrder sharingOrder : list) { + try { + WxPayOrder wxPayOrder = wxPayOrderMapper.selectByPrimaryKey(sharingOrder.getOrderId()); + wxProfitSharingOrderService.querySharingOrder(wxPayOrder); + } catch (Exception e){ + logger.error("状态更新失败:" + EnumProfitSharingStatus.PROFIT_SHARING_PROCESSING.getMessage() + + " ID:" +sharingOrder.getId() + + " MSG:" + e.getMessage()); + } + } + + } + +} \ No newline at end of file diff --git a/mallinkService/src/main/java/com/iformall/domain/po/WxProfitSharingResult.java b/mallinkService/src/main/java/com/iformall/domain/po/WxProfitSharingResult.java index 1d01fa3a6..ae1789b1e 100644 --- a/mallinkService/src/main/java/com/iformall/domain/po/WxProfitSharingResult.java +++ b/mallinkService/src/main/java/com/iformall/domain/po/WxProfitSharingResult.java @@ -71,6 +71,11 @@ public class WxProfitSharingResult implements Serializable { /*微信返回的失败原因**/ @io.swagger.annotations.ApiModelProperty(value="微信返回的失败原因",name="failedReason") private String failedReason; + /*描述**/ + @io.swagger.annotations.ApiModelProperty(value="描述",name="description") + private String description; + + public Long getSharingOrderId() { return sharingOrderId; } @@ -136,6 +141,13 @@ public class WxProfitSharingResult implements Serializable { this.tenantId = tenantId; } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } public static enum Field { diff --git a/mallinkService/src/main/java/com/iformall/pay/WxProfitSharing.java b/mallinkService/src/main/java/com/iformall/pay/WxProfitSharing.java index 31febe6ac..b5a5dc711 100644 --- a/mallinkService/src/main/java/com/iformall/pay/WxProfitSharing.java +++ b/mallinkService/src/main/java/com/iformall/pay/WxProfitSharing.java @@ -36,8 +36,8 @@ public class WxProfitSharing { * @param params * @return */ - public static String queryOrder(Map params) { - return doPost(PROFIT_SHARING_QUERY_URL, params); + public static String queryOrder(Map params, String certPath, String certPass) { + return doPostSSL(PROFIT_SHARING_QUERY_URL, params, certPath, certPass); } diff --git a/mallinkService/src/main/java/com/iformall/pay/WxProfitSharingQueryP.java b/mallinkService/src/main/java/com/iformall/pay/WxProfitSharingQueryP.java index 2ed46c2a0..60cd395c6 100644 --- a/mallinkService/src/main/java/com/iformall/pay/WxProfitSharingQueryP.java +++ b/mallinkService/src/main/java/com/iformall/pay/WxProfitSharingQueryP.java @@ -14,7 +14,7 @@ public class WxProfitSharingQueryP implements Serializable { private String sign; // 签名 private String sign_type; // 签名方法 private String transaction_id ; // 支付订单号 - private String out_trade_no; // 商户分账单号 + private String out_order_no; // 商户分账单号 public String getMch_id() { @@ -62,10 +62,10 @@ public class WxProfitSharingQueryP implements Serializable { this.transaction_id = transaction_id; } - public String getOut_trade_no() { return out_trade_no; } + public String getOut_order_no() { return out_order_no; } - public void setOut_trade_no(String out_trade_no) { - this.out_trade_no = out_trade_no; + public void setOut_order_no(String out_order_no) { + this.out_order_no = out_order_no; } } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java index 639774e84..191980e63 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxProfitSharingOrderServiceImpl.java @@ -20,6 +20,7 @@ import com.iformall.pay.WxProfitSharingQueryP; import com.iformall.service.WxProfitSharingOrderService; import com.iformall.utils.BeanUtils; import com.iformall.utils.Utility; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -121,7 +122,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ @Override public void test() { - createSharingOrder(wxPayOrderMapper.selectByPrimaryKey(new Long(190403470006681600L))); + querySharingOrder(wxPayOrderMapper.selectByPrimaryKey(new Long(220465483168612352L))); } private WxAppinfo getAppinfo(WxPayOrder wxPayOrder) { @@ -129,12 +130,12 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ WxCUser wxCUser = wxCUserMapper.selectByPrimaryKey(wxPayOrder.getcUserId()); if (wxCUser == null) - throw new MallinkException(ErrorCode.USER_IS_EMPTY.getCode(), ErrorCode.USER_IS_EMPTY.getMessage()); + throw new MallinkException(ErrorCode.USER_IS_EMPTY); wxAppinfo.setAppId(wxCUser.getAppId()); wxAppinfo = wxAppinfoMapper.selectOne(wxAppinfo); if (wxAppinfo == null) - throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND.getCode(), ErrorCode.APP_ID_NOT_FOUND.getMessage()); + throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); return wxAppinfo; } @@ -190,7 +191,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ wxProfitSharingReceiver.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode()); List wxProfitSharingReceiverList = wxProfitSharingReceiverMapper.findList(wxProfitSharingReceiver); if (wxProfitSharingReceiverList.size()<=0 || wxProfitSharingReceiverList.size() > 50) { - throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RECEIVER_INVALID.getMessage()); + throw new MallinkException(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID); } JSONArray receivers = new JSONArray(); @@ -231,7 +232,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ record.setErrorMsg(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()); record.setUpdateTime(new Date()); wxProfitSharingOrderMapper.updateByPrimaryKey(record); - return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(), ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getMessage()+e.getMessage()); + return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED); } logger.info("response: " + response); Map returnMap = WxPayment.xmlToMap(response); @@ -249,7 +250,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ record.setErrorMsg(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); record.setUpdateTime(new Date()); wxProfitSharingOrderMapper.updateByPrimaryKey(record); - return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); + return new ResultData(ErrorCode.PROFIT_SHARING_RETURN_INVALID.getCode()); } String result_code = returnMap.get("result_code"); @@ -272,6 +273,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ } @Override + @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) public ResultData querySharingOrder(WxPayOrder wxPayOrder) { WxProfitSharingOrder record = new WxProfitSharingOrder(); @@ -283,7 +285,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ record.setOrderId(wxPayOrder.getId()); record = wxProfitSharingOrderMapper.selectOne(record); if (record == null) - return new ResultData(ErrorCode.ORDER_IS_NOT_FIND.getCode(), ErrorCode.ORDER_IS_NOT_FIND.getMessage()); + return new ResultData(ErrorCode.ORDER_IS_NOT_FIND); //分账查询提交 WxProfitSharingQueryP wxProfitSharingQueryP = new WxProfitSharingQueryP();; @@ -291,12 +293,12 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ wxProfitSharingQueryP.setSub_mch_id(payAccount.getSubMchId()); wxProfitSharingQueryP.setNonce_str(Utility.generate32UUID()); wxProfitSharingQueryP.setTransaction_id(wxPayOrder.getTransactionId()); - wxProfitSharingQueryP.setOut_trade_no(record.getId().toString()); + wxProfitSharingQueryP.setOut_order_no(record.getId().toString()); wxProfitSharingQueryP.setSign_type("HMAC-SHA256"); String response; try { - wxProfitSharingQueryP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getApiKey())); - response = WxProfitSharing.pushOrder(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getCertPath(), payAccount.getMchId()); + wxProfitSharingQueryP.setSign(WxPayment.createSignHMAC(BeanUtils.toStringMap(wxProfitSharingQueryP),payAccount.getApiKey())); + response = WxProfitSharing.queryOrder(BeanUtils.toStringMap(wxProfitSharingQueryP), payAccount.getCertPath(), payAccount.getMchId()); }catch (Exception e){ return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getCode(), ErrorCode.PROFIT_SHARING_QUERY_REQUEST_FAILED.getMessage()+e.getMessage()); } @@ -307,12 +309,7 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ } if (!WxPayment.verifyNotifyHMAC(returnMap,payAccount.getApiKey())){ - return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); - } - - String out_order_no = returnMap.get("out_order_no"); - if (!out_order_no.equals(record.getId().toString())){ - return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID.getCode(), ErrorCode.PROFIT_SHARING_RETURN_INVALID.getMessage()); + return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID); } String result_code = returnMap.get("result_code"); @@ -320,6 +317,11 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_APPLY_FAILED.getCode(), returnMap.get("result_msg")); } + String out_order_no = returnMap.get("out_order_no"); + if (StringUtils.isBlank(out_order_no) || !out_order_no.equals(record.getId().toString())){ + return new ResultData(ErrorCode.PROFIT_SHARING_QUERY_RETURN_INVALID); + } + record.setSharingStatus((Integer) statusMap.get(returnMap.get("status"))); record.setUpdateTime(new Date()); wxProfitSharingOrderMapper.updateByPrimaryKey(record); @@ -331,30 +333,63 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ result.setSharingOrderId(record.getId()); List wxProfitSharingResultList = wxProfitSharingResultMapper.findList(result); - - WxProfitSharingReceiver wxProfitSharingReceiver; - WxProfitSharingResult wxProfitSharingResult; - - for(int i=0; i list = wxProfitSharingResultMapper.findList(wxProfitSharingResult); + if (list.size() > 0) { + wxProfitSharingResult = list.get(0); + } + + wxProfitSharingResult.setPayAmount(Integer.valueOf(res.getString("amount"))); + wxProfitSharingResult.setFinishTime(res.getString("finish_time")); + wxProfitSharingResult.setSharingStatus(resultStatusMap.getIntValue(res.getString("result"))); + wxProfitSharingResult.setFailedReason(res.getString("fail_reason")); + wxProfitSharingResult.setDescription(des.toJSONString()); + if (wxProfitSharingResult.getId() != null){ + wxProfitSharingResult.setUpdateTime(new Date()); + wxProfitSharingResultMapper.updateByPrimaryKeySelective(wxProfitSharingResult); + } + else { + wxProfitSharingResult.setCreateTime(new Date()); + wxProfitSharingResult.setUpdateTime(new Date()); + wxProfitSharingResult.setId(IdWorker.get().nextId()); + wxProfitSharingResultMapper.insertSelective(wxProfitSharingResult); + } + } + } return new ResultData(returnMap); } } diff --git a/mallinkService/src/main/resources/mapper/WxProfitSharingOrderMapper.xml b/mallinkService/src/main/resources/mapper/WxProfitSharingOrderMapper.xml index b6842cc29..a742ab098 100644 --- a/mallinkService/src/main/resources/mapper/WxProfitSharingOrderMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxProfitSharingOrderMapper.xml @@ -10,7 +10,7 @@ - + diff --git a/mallinkService/src/main/resources/mapper/WxProfitSharingResultMapper.xml b/mallinkService/src/main/resources/mapper/WxProfitSharingResultMapper.xml index a91c65c09..07d1c5978 100644 --- a/mallinkService/src/main/resources/mapper/WxProfitSharingResultMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxProfitSharingResultMapper.xml @@ -13,10 +13,11 @@ + - `id`,`tenant_id`,`merchant_id`,`sharing_order_id`,`sharing_receiver_id`,`pay_amount`,`update_time`,`create_time`,`sharing_status`,`finish_time`,`failed_reason` + `id`,`tenant_id`,`merchant_id`,`sharing_order_id`,`sharing_receiver_id`,`pay_amount`,`update_time`,`create_time`,`sharing_status`,`finish_time`,`failed_reason`,`description`