From c6085dd1e5fbbe13eae825dda02819c52227596a Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期六, 07 十月 2023 18:15:58 +0800
Subject: [PATCH] 微信对账

---
 server/services/src/main/java/com/doumee/core/utils/DateUtil.java                     |   10 ++
 server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java               |   13 --
 server/services/src/main/java/com/doumee/dao/business/WxBillDetailMapper.java         |    3 
 server/services/src/main/java/com/doumee/dao/business/model/WxBill.java               |    2 
 server/services/src/main/resources/mappers/WxBillDetailMapper.xml                     |   76 +++++++++++++++
 server/services/src/main/java/com/doumee/service/business/impl/WxBillServiceImpl.java |  137 ++++++++++++++++++++++++---
 6 files changed, 213 insertions(+), 28 deletions(-)

diff --git a/server/services/src/main/java/com/doumee/core/utils/DateUtil.java b/server/services/src/main/java/com/doumee/core/utils/DateUtil.java
index f96e181..658895b 100644
--- a/server/services/src/main/java/com/doumee/core/utils/DateUtil.java
+++ b/server/services/src/main/java/com/doumee/core/utils/DateUtil.java
@@ -2298,6 +2298,16 @@
             return new Timestamp(System.currentTimeMillis());
         }
     }
+    public static Date getDateFromString2(String strDate) {
+        if (StringUtils.isEmpty(strDate)) {
+            return new Date(System.currentTimeMillis());
+        }
+        try {
+            return sdfLongTimePlus.parse(strDate);
+        } catch (Exception ex) {
+           return  null;
+        }
+    }
 
     // -----------------------------------------------------------------------
     public static Date parseFromFormats(String aValue) {
diff --git a/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java b/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java
index 4c42c9b..d0da6cd 100644
--- a/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java
+++ b/server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java
@@ -1,28 +1,19 @@
 package com.doumee.core.wx;
 
 import com.doumee.biz.system.SystemDictDataBiz;
-import com.doumee.core.constants.Constants;
 import com.doumee.core.constants.ResponseStatus;
 import com.doumee.core.exception.BusinessException;
 import com.doumee.core.utils.ID;
-import com.doumee.dao.business.ActionLogMapper;
 import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
-import com.github.binarywang.wxpay.bean.request.WxPayDownloadBillRequest;
 import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
-import com.github.binarywang.wxpay.bean.result.WxPayBillResult;
 import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
 import com.github.binarywang.wxpay.exception.WxPayException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.yaml.snakeyaml.scanner.Constant;
 
 import java.math.BigDecimal;
-import java.security.KeyFactory;
-import java.security.Signature;
-import java.security.spec.PKCS8EncodedKeySpec;
-import java.util.Base64;
 
 /**
  * 寰俊灏忕▼搴�-鍏叡鏂规硶
@@ -49,14 +40,12 @@
             WxPayRefundRequest request = new WxPayRefundRequest();
             request.setOutTradeNo(orderNo);
             request.setOutRefundNo(refNum);
-           // request.setTotalFee(2);
-          //  request.setRefundFee(1);
             request.setTotalFee(BaseWxPayRequest.yuanToFen(totalPrice.toString()));
             request.setRefundFee(BaseWxPayRequest.yuanToFen(refundPrice.toString()));
             WxPayRefundResult response = WxMiniConfig.wxPayService.refund(request);
             if ("SUCCESS".equals(response.getReturnCode()) && "SUCCESS".equals(response.getResultCode())) {
                 return refNum;
-            } else {
+            } else{
                 throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),response.getErrCode() + response.getErrCodeDes());
             }
         } catch (WxPayException e) {
diff --git a/server/services/src/main/java/com/doumee/dao/business/WxBillDetailMapper.java b/server/services/src/main/java/com/doumee/dao/business/WxBillDetailMapper.java
index 5ff6640..504bac3 100644
--- a/server/services/src/main/java/com/doumee/dao/business/WxBillDetailMapper.java
+++ b/server/services/src/main/java/com/doumee/dao/business/WxBillDetailMapper.java
@@ -3,10 +3,13 @@
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.doumee.dao.business.model.WxBillDetail;
 
+import java.util.List;
+
 /**
  * @author 姹熻箘韫�
  * @date 2023/09/27 18:08
  */
 public interface WxBillDetailMapper extends BaseMapper<WxBillDetail> {
 
+    void insertBatch(List<WxBillDetail> detailList);
 }
diff --git a/server/services/src/main/java/com/doumee/dao/business/model/WxBill.java b/server/services/src/main/java/com/doumee/dao/business/model/WxBill.java
index 4114441..017e5db 100644
--- a/server/services/src/main/java/com/doumee/dao/business/model/WxBill.java
+++ b/server/services/src/main/java/com/doumee/dao/business/model/WxBill.java
@@ -20,7 +20,7 @@
 public class WxBill {
 
     @ApiModelProperty(value = "")
-    @ExcelColumn(name="")
+    @ExcelColumn(name="涓婚敭锛屾寜鐓ф棩鏈焬yyyMMDD鏍煎紡瀛樺偍锛岄槻姝㈤噸澶�")
     private String id;
 
     @ApiModelProperty(value = "鎬讳氦鏄撳崟鏁�", example = "1")
diff --git a/server/services/src/main/java/com/doumee/service/business/impl/WxBillServiceImpl.java b/server/services/src/main/java/com/doumee/service/business/impl/WxBillServiceImpl.java
index f3856d5..d38bd2d 100644
--- a/server/services/src/main/java/com/doumee/service/business/impl/WxBillServiceImpl.java
+++ b/server/services/src/main/java/com/doumee/service/business/impl/WxBillServiceImpl.java
@@ -1,5 +1,6 @@
 package com.doumee.service.business.impl;
 
+import com.doumee.core.constants.Constants;
 import com.doumee.core.constants.ResponseStatus;
 import com.doumee.core.exception.BusinessException;
 import com.doumee.core.model.PageData;
@@ -7,8 +8,10 @@
 import com.doumee.core.utils.DateUtil;
 import com.doumee.core.utils.Utils;
 import com.doumee.core.wx.WxMiniConfig;
+import com.doumee.dao.business.WxBillDetailMapper;
 import com.doumee.dao.business.WxBillMapper;
 import com.doumee.dao.business.model.WxBill;
+import com.doumee.dao.business.model.WxBillDetail;
 import com.doumee.service.business.WxBillService;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -18,12 +21,14 @@
 import com.github.binarywang.wxpay.bean.result.WxPayBillInfo;
 import com.github.binarywang.wxpay.bean.result.WxPayBillResult;
 import com.github.binarywang.wxpay.exception.WxPayException;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
-import java.util.Date;
-import java.util.List;
+import java.math.BigDecimal;
+import java.util.*;
 
 /**
  * Service瀹炵幇
@@ -35,6 +40,8 @@
 
     @Autowired
     private WxBillMapper wxBillMapper;
+    @Autowired
+    private WxBillDetailMapper wxBillDetailMapper;
     @Override
     public String create(WxBill wxBill) {
         wxBillMapper.insert(wxBill);
@@ -149,22 +156,122 @@
         return wxBillMapper.selectCount(wrapper);
     }
     @Override
+    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
     public void getWxBill() {
+        // 鑾峰彇浜ゆ槗璐﹀崟鏁版嵁
+        WxPayDownloadBillRequest request = new WxPayDownloadBillRequest();
+        String billDate =  DateUtil.beforeDateToStr(1);
+        request.setBillDate(billDate);
+        request.setBillType("SUCCESS");
+        WxPayBillResult response = null;
         try {
-            // 鑾峰彇浜ゆ槗璐﹀崟鏁版嵁
-            WxPayDownloadBillRequest request = new WxPayDownloadBillRequest();
-            String billDate =  DateUtil.beforeDateToStr(1);
-            request.setBillDate(billDate);
-            request.setBillType("ALL");
-            WxPayBillResult response = WxMiniConfig.wxPayService.downloadBill(request);
-            if(response !=null){
-                List<WxPayBillInfo> detailList = response.getBillInfoList();
-            }else{
-                throw  new BusinessException(ResponseStatus.SERVER_ERROR);
-            }
+            response = WxMiniConfig.wxPayService.downloadBill(request);
+        }catch (WxPayException e){
 
-        } catch (WxPayException e) {
-            e.printStackTrace();
         }
+        request.setBillType("REFUND");
+        //璇锋眰閫�娆惧崟浜ゆ槗璁板綍姹囨�绘暟鎹�
+        WxPayBillResult responseRefund = null;
+        try {
+            responseRefund = WxMiniConfig.wxPayService.downloadBill(request);
+        }catch (WxPayException e){
+
+        }
+        WxBill bill = new WxBill();
+        //鏃ユ湡浣滀负涓婚敭
+        bill.setId(billDate);
+        bill.setSumBill(0);
+        bill.setSumTotalFee(new BigDecimal(0.00));
+        bill.setSumSuccessFee(new BigDecimal(0.00));
+        bill.setSumRefundFee(new BigDecimal(0.00));
+        bill.setSumCouponRefundFee(new BigDecimal(0.00));
+        bill.setSumApplyRefundFee(new BigDecimal(0.00));
+        bill.setSumCmmsAmt(new BigDecimal(0.00));
+        bill.setSumRefundBill(0);
+        bill.setSumRefundCmmsAmt(new BigDecimal(0.00));
+        List<WxBillDetail> detailList = new ArrayList<>();
+        if(response !=null){
+//                List<WxPayBillInfo> detailList = response.getBillInfoList();
+            int totalRefund = 0;
+            BigDecimal totalRefundAmt = new BigDecimal(0.00);
+            bill.setSumBill(Integer.parseInt(response.getTotalRecord()));
+            //璁㈠崟鎬婚噾棰�
+            bill.setSumTotalFee(formatStringToDecimal(response.getTotalAmount()));
+            //搴旂粨璁㈠崟鎬婚噾棰�
+            bill.setSumSuccessFee(formatStringToDecimal(response.getTotalFee()));
+            //閫�娆炬�婚噾棰�
+            bill.setSumRefundFee(formatStringToDecimal(response.getTotalFee()));
+            //閫�娆炬�婚噾棰�
+            bill.setSumApplyRefundFee(formatStringToDecimal(response.getTotalAppliedRefundFee()));
+            //浜ゆ槗鎬绘墜缁垂閲戦
+            bill.setSumCmmsAmt(formatStringToDecimal(response.getTotalPoundageFee()));
+            detailList.addAll(getDetialModelByInfo(bill,response.getBillInfoList()));
+        }
+        if(responseRefund != null){
+            //閫�娆惧崟鎬绘暟绱杩涘叆
+//                bill.setSumBill(bill.getSumBill()+(Integer.parseInt(response.getTotalRecord()));
+            bill.setSumRefundBill(Integer.parseInt(responseRefund.getTotalRecord()));//閫�娆剧殑璁㈠崟鏁�
+            bill.setSumRefundCmmsAmt(formatStringToDecimal(responseRefund.getTotalPoundageFee()));//閫�娆炬�绘墜缁垂
+            bill.setSumCouponRefundFee(formatStringToDecimal(responseRefund.getTotalCouponFee()));//閫�娆炬�绘墜缁垂
+            detailList.addAll(getDetialModelByInfo(bill,responseRefund.getBillInfoList()));
+        }
+        wxBillMapper.delete(new UpdateWrapper<WxBill>().lambda().eq( WxBill::getId, bill.getId()));
+        wxBillDetailMapper.delete(new UpdateWrapper<WxBillDetail>().lambda().eq( WxBillDetail::getPid, bill.getId()));
+        wxBillMapper.insert(bill);
+        if(detailList.size()>0){
+            wxBillDetailMapper.insertBatch(detailList);
+        }
+    }
+
+    private  List<WxBillDetail> getDetialModelByInfo(WxBill bill, List<WxPayBillInfo> billInfoList) {
+        List<WxBillDetail> detailList = new ArrayList<>();
+        if(billInfoList!=null){
+            for (int i = 0; i <billInfoList.size(); i++) {
+                WxPayBillInfo info =billInfoList.get(i);
+                WxBillDetail detail = new WxBillDetail();
+                detail.setId(UUID.randomUUID().toString());
+                detail.setPid(bill.getId());
+                detail.setBillDate(DateUtil.getDateFromString2(info.getTradeTime()));//浜ゆ槗鏃堕棿
+                detail.setAppid(info.getAppId());//鍏紬璐﹀彿ID
+                detail.setMchId(info.getMchId());//鍟嗘埛鍙�
+                detail.setSubMchid(info.getSubMchId());//鐗圭害鍟嗘埛鍙�
+                detail.setDeviceInfo(info.getDeviceInfo());//璁惧
+
+                detail.setTransactionId(info.getTransactionId());//寰俊璁㈠崟鍙�
+                detail.setOutTradeNo(info.getOutTradeNo());//鍟嗘埛璁㈠崟鍙�
+                detail.setOpenid(info.getOpenId());//鐢ㄦ埛鏍囪瘑
+                detail.setBillType(info.getTradeType());//浜ゆ槗绫诲瀷
+                detail.setBillStatus(info.getTradeState());//浜ゆ槗鐘舵��
+                detail.setBankType(info.getBankType());//浠樻閾惰
+                detail.setFeeType(info.getFeeType());//璐у竵绉嶇被
+                detail.setSettlementTotalFee(formatStringToDecimal(info.getTotalFee()));//搴旂粨璁㈠崟閲戦
+                detail.setCouponFee(formatStringToDecimal(info.getCouponFee()));//浠i噾鍒搁噾棰�
+                detail.setRefundId(info.getRefundId());//寰俊閫�娆惧崟鍙�
+                detail.setOutRefundNo(info.getOutRefundNo());//鍟嗘埛閫�娆惧崟鍙�
+                detail.setRefundFee(formatStringToDecimal(info.getSettlementRefundFee()));//閫�娆鹃噾棰�
+                detail.setCouponRefundFee(formatStringToDecimal(info.getCouponRefundFee()));//鍏呭�煎埜閫�娆鹃噾棰�
+                detail.setRefundType(info.getRefundChannel());//閫�娆剧被鍨�
+                detail.setRefundSuccessStatus(info.getRefundState());//閫�娆剧姸鎬�
+                detail.setBody(info.getBody());//鍟嗗搧鍚嶇О
+                detail.setAttach(info.getAttach());//鍟嗘埛鏁版嵁鍖�
+                detail.setCmmsAmt(formatStringToDecimal(info.getPoundage()));//鎵嬬画璐�
+                detail.setRate(formatStringToDecimal(info.getPoundageRate().replace("%", "")));//璐圭巼
+                detail.setTotalFee(formatStringToDecimal(info.getTotalAmount()));//璁㈠崟閲戦
+                detail.setApplyRefundFee(formatStringToDecimal(info.getAppliedRefundAmount()));//鐢宠閫�娆鹃噾棰�
+                detail.setRateRemark(info.getFeeRemark());//璐圭巼澶囨敞
+                detail.setRefundSuccessDate(DateUtil.getDateFromString2(info.getRefundSuccessTime()));//鎴愬姛閫�娆炬椂闂�
+                detail.setRefundApplyDate(DateUtil.getDateFromString2(info.getRefundTime())); //鐢宠閫�娆炬椂闂�
+
+                detailList.add(detail);
+            }
+        }
+        return detailList;
+    }
+
+    public static BigDecimal formatStringToDecimal(String str){
+        if(str == null){
+            return  new BigDecimal(0.00);
+        }
+        return new BigDecimal(Double.parseDouble(str));
     }
 }
diff --git a/server/services/src/main/resources/mappers/WxBillDetailMapper.xml b/server/services/src/main/resources/mappers/WxBillDetailMapper.xml
new file mode 100644
index 0000000..0f37d2e
--- /dev/null
+++ b/server/services/src/main/resources/mappers/WxBillDetailMapper.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.doumee.dao.business.WxBillDetailMapper">
+
+  <insert id="insertBatch">
+      insert into wx_bill_detail
+    ( "id" ,
+    "pid",
+    "bill_date",
+    "appid" ,
+    "mch_id" ,
+    "sub_mchid" ,
+    "device_info",
+    "transaction_id",
+    "out_trade_no",
+    "openid",
+    "bill_type",
+    "bill_status",
+    "bank_type",
+    "fee_type" ,
+    "settlement_total_fee",
+    "coupon_fee",
+    "refund_id" ,
+    "out_refund_no" ,
+    "refund_fee",
+    "coupon_refund_fee" ,
+    "refund_type",
+    "refund_apply_date" ,
+    "refund_success_date" ,
+    "refund_success_status" ,
+    "body",
+    "attach" ,
+    "cmms_amt",
+    "rate",
+    "total_fee",
+    "apply_refund_fee" ,
+    "rate_remark"
+    )
+    values
+    <foreach collection="list" item="item" separator=",">
+      (#{item.id},
+      #{item.billDate},
+      #{item.pid},
+      #{item.appid},
+      #{item.mchId},
+      #{item.subMchid},
+      #{item.deviceInfo},
+      #{item.transactionId},
+      #{item.outTradeNo},
+      #{item.openid},
+      #{item.billType},
+      #{item.billStatus},
+      #{item.bankType},
+      #{item.feeType},
+      #{item.settlementTotalFee},
+      #{item.couponFee},
+      #{item.refundId},
+      #{item.outRefundNo},
+      #{item.refundFee},
+      #{item.couponRefundFee},
+      #{item.refundType},
+      #{item.refundApplyDate},
+      #{item.refundSuccessDate},
+      #{item.refundSuccessStatus},
+      #{item.body},
+      #{item.attach},
+      #{item.cmmsAmt},
+      #{item.rate},
+      #{item.totalFee},
+      #{item.applyRefundFee},
+      #{item.rateRemark}
+      )
+    </foreach>
+  </insert>
+
+</mapper>

--
Gitblit v1.9.3