| | |
| | | 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; |
| | |
| | | import com.doumee.core.utils.DateUtil; |
| | | import com.doumee.core.utils.Utils; |
| | | import com.doumee.core.wx.WxMiniConfig; |
| | | import com.doumee.core.wx.WxPayProperties; |
| | | import com.doumee.dao.business.WxBillDetailMapper; |
| | | import com.doumee.dao.business.WxBillMapper; |
| | | import com.doumee.dao.business.model.Goodsorder; |
| | | 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; |
| | |
| | | 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.checkerframework.checker.units.qual.C; |
| | | 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实现 |
| | |
| | | |
| | | @Autowired |
| | | private WxBillMapper wxBillMapper; |
| | | @Autowired |
| | | private WxBillDetailMapper wxBillDetailMapper; |
| | | @Override |
| | | public String create(WxBill wxBill) { |
| | | wxBillMapper.insert(wxBill); |
| | |
| | | return wxBillMapper.selectCount(wrapper); |
| | | } |
| | | @Override |
| | | public void getWxBill() { |
| | | @Transactional(rollbackFor = {BusinessException.class,Exception.class}) |
| | | public void getWxBill(String billDate) { |
| | | // 获取交易账单数据 |
| | | WxPayDownloadBillRequest request = new WxPayDownloadBillRequest(); |
| | | // String billDate = DateUtil.beforeDateToStr(1); |
| | | Date ydate = DateUtil.addDaysToDate(new Date(), -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())); |
| | | |
| | | // getTotalAndIncome(bill,ydate);//统计结算金额和自行车收入 |
| | | //总结算金额(收款金额-收款手续费-退款金额-退款手续费(负数)) |
| | | bill.setTotal(bill.getSumSuccessFee().subtract(bill.getSumCmmsAmt()).subtract(bill.getSumRefundFee()).subtract(bill.getSumRefundCmmsAmt())); |
| | | // //自行车收入(收款金额-收款手续费-退款金额-退款手续费(负数)) |
| | | // bill.setBikeFee(bill.getSumSuccessFee().subtract(bill.getSumRefundFee())); |
| | | wxBillMapper.insert(bill); |
| | | if(detailList.size()>0){ |
| | | wxBillDetailMapper.insertBatch(detailList); |
| | | } |
| | | } |
| | | private void getTotalAndIncome(WxBill bill,Date date) { |
| | | QueryWrapper<Goodsorder> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.apply("to_char(create_date, 'yyyy-MM-dd') = {0}", date); |
| | | queryWrapper.lambda().eq(Goodsorder::getIsdeleted, Constants.ZERO); |
| | | queryWrapper.lambda().eq(Goodsorder::getStatus, Constants.goodsorderStatus.over);//已結算 |
| | | queryWrapper.select("sum(money) as totalMoney","sum(close_money) as totalCloseMoney"); |
| | | } |
| | | |
| | | 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()));//代金券金额 |
| | | 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())); //申请退款时间 |
| | | //计算自行车收入,匹配来自自行车小程序的所有支付成功和退款成功的金额,作为自行车收入(累计收款金额-累计退款成功金额) |
| | | if(StringUtils.equals(detail.getAppid(), WxMiniConfig.wxPayService.getConfig().getAppId())){ |
| | | //自行车收入累计收款金额 |
| | | bill.setBikeFee(bill.getBikeFee().add(detail.getSettlementTotalFee())); |
| | | if(StringUtils.equals(detail.getRefundSuccessStatus(),"SUCCESS")){ |
| | | //如果退款成功,扣除退款金额 |
| | | bill.setBikeFee(bill.getBikeFee().subtract(detail.getApplyRefundFee())); |
| | | } |
| | | } |
| | | 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)); |
| | | } |
| | | } |