From f9691d544e62d6c04dbfe45d05a6c7bc5e004291 Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期五, 29 十二月 2023 11:52:29 +0800 Subject: [PATCH] 服务商 --- server/services/src/main/java/com/doumee/service/business/impl/WxBillServiceImpl.java | 311 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 289 insertions(+), 22 deletions(-) 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..c0032c1 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,12 @@ 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; @@ -18,12 +23,18 @@ import com.github.binarywang.wxpay.bean.result.WxPayBillInfo; import com.github.binarywang.wxpay.bean.result.WxPayBillResult; import com.github.binarywang.wxpay.exception.WxPayException; +import lombok.Synchronized; +import org.apache.commons.lang3.StringUtils; +import org.checkerframework.checker.units.qual.C; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; 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.math.RoundingMode; +import java.util.*; /** * Service瀹炵幇 @@ -35,6 +46,8 @@ @Autowired private WxBillMapper wxBillMapper; + @Autowired + private WxBillDetailMapper wxBillDetailMapper; @Override public String create(WxBill wxBill) { wxBillMapper.insert(wxBill); @@ -83,7 +96,7 @@ @Override public WxBill findOne(WxBill wxBill) { QueryWrapper<WxBill> wrapper = new QueryWrapper<>(wxBill); - return wxBillMapper.selectOne(wrapper); + return wxBillMapper.selectOne(wrapper.last(" limit 1")); } @Override @@ -91,7 +104,71 @@ QueryWrapper<WxBill> wrapper = new QueryWrapper<>(wxBill); return wxBillMapper.selectList(wrapper); } - + @Override + public List<WxBill> getTotalBill(WxBill param) { + //鑾峰彇姹囨�荤粺璁� + WxBill bill = getTotalDataByDate(param); + WxBill bill1 = initBillData();//璁板綍浜ゆ槗瀹炴敹缁熻鏁版嵁 + bill1.setSumBill(bill.getSumBill());//璁㈠崟鏁伴噺 + bill1.setSumTotalFee(bill.getSumTotalFee());//浜ゆ槗閲戦 + bill1.setSumCmmsAmt(bill.getSumCmmsAmt());//浜ゆ槗鎵嬬画璐� + bill1.setSumSuccessFee(bill.getSumSuccessFee());//鎴愬姛浜ゆ槗閲戦 + + WxBill bill2 = initBillData();//璁板綍閫�娆剧粺璁℃暟鎹� + bill2.setSumRefundBill(bill.getSumRefundBill());//閫�娆剧瑪鏁� + bill2.setSumRefundCmmsAmt(bill.getSumRefundCmmsAmt());//閫�娆炬墜缁垂 + bill2.setSumRefundFee(bill.getSumRefundFee());//閫�娆鹃噾棰� + + WxBill bill3 = initBillData(); + bill3.setSumBill(bill.getSumBill()); + bill3.setSumSuccessFee(bill.getSumSuccessFee()); + bill3.setSumCmmsAmt(bill.getSumCmmsAmt()); + bill3.setSumTotalFee(bill.getSumTotalFee()); + bill3.setSumRefundBill(bill.getSumRefundBill());//閫�娆剧瑪鏁� + bill3.setSumRefundCmmsAmt(Constants.formatDecimalNum(bill.getSumRefundCmmsAmt()).add(Constants.formatDecimalNum(bill.getSumCmmsAmt())));//鎵嬬画璐� + bill3.setSumRefundFee(bill.getSumRefundFee());//閫�娆鹃噾棰� + bill3.setTotal(bill.getTotal()); + + QueryWrapper<WxBillDetail> queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().ge(param.getStartDate() !=null,WxBillDetail::getPid,DateUtil.getShortDateStr(param.getStartDate())); + queryWrapper.lambda().le(param.getEndDate() !=null,WxBillDetail::getPid,DateUtil.getShortDateStr(param.getEndDate())); + queryWrapper.lambda().ne(WxBillDetail::getAppid, WxMiniConfig.wxPayV2Service.getConfig().getSubAppId());//闈炶嚜琛岃溅鏀跺叆缁熻 + queryWrapper.lambda().groupBy(WxBillDetail::getBillStatus); + queryWrapper.select( "count(id) as sum_bill" + ,"sum(settlement_total_fee) as settlement_total_fee" + ,"sum(refund_fee) as refund_fee" + ,"sum(cmms_amt) as cmms_amt" ,"bill_status"); + List<WxBillDetail> detailList = wxBillDetailMapper.selectList(queryWrapper); + WxBill bill4 = initBillData();//璁板綍浜ゆ槗瀹炴敹缁熻鏁版嵁 + if(detailList!=null){ + for(WxBillDetail detail : detailList){ + if(StringUtils.equals(detail.getBillStatus(), "SUCCESS")){ + //瀹炴敹鏁版嵁 + bill4.setSumBill( detail.getSumBill());//浜ゆ槗绗旀暟 + bill4.setSumSuccessFee(Constants.formatDecimalNum(detail.getSettlementTotalFee()));//浜ゆ槗閲戦 + bill4.setSumCmmsAmt(Constants.formatDecimalNum(detail.getCmmsAmt()));//浜ゆ槗鎵嬬画璐� + } else if(StringUtils.equals(detail.getBillStatus(), "REFUND") || StringUtils.equals(detail.getBillStatus(), "REVOKED")){ + //閫�娆炬暟鎹� + bill4.setSumRefundBill(Constants.formatIntegerNum(bill4.getSumRefundBill())+detail.getSumBill());//閫�娆剧瑪鏁� + bill4.setSumRefundFee(Constants.formatDecimalNum(bill4.getSumRefundFee()).add(Constants.formatDecimalNum(detail.getRefundFee()))); + bill4.setSumRefundCmmsAmt(Constants.formatDecimalNum(bill4.getSumRefundCmmsAmt()).add(Constants.formatDecimalNum(Constants.formatDecimalNum(detail.getCmmsAmt()))));//閫�娆炬墜缁垂 + } + } + //缁撶畻閲戦 + bill4.setTotal(bill4.getSumSuccessFee() + .subtract(bill4.getSumRefundFee()) + .subtract(bill4.getSumCmmsAmt()) + .subtract(bill4.getSumRefundCmmsAmt())); + } + + List<WxBill> list = new ArrayList<>(); + list.add(bill1);//瀹炴敹浜ゆ槗 + list.add(bill2);//閫�娆炬眹鎬� + list.add(bill3);//瀹炴敹閫�娆炬�昏 + list.add(bill4);//闈炶嚜琛岃溅鏀跺叆 + return list; + } + @Override public PageData<WxBill> findPage(PageWrap<WxBill> pageWrap) { IPage<WxBill> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); @@ -133,38 +210,228 @@ if (pageWrap.getModel().getBikeFee() != null) { queryWrapper.lambda().eq(WxBill::getBikeFee, pageWrap.getModel().getBikeFee()); } - for(PageWrap.SortData sortData: pageWrap.getSorts()) { + queryWrapper.lambda().ge(pageWrap.getModel().getStartDate() !=null,WxBill::getId,DateUtil.getShortDateStr(pageWrap.getModel().getStartDate())); + queryWrapper.lambda().le(pageWrap.getModel().getEndDate() !=null,WxBill::getId,DateUtil.getShortDateStr(pageWrap.getModel().getEndDate())); + //鎸夌収鏃堕棿鍗囧簭鏄剧ず + queryWrapper.lambda().orderByAsc(WxBill::getId); + /* for(PageWrap.SortData sortData: pageWrap.getSorts()) { if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { queryWrapper.orderByDesc(sortData.getProperty()); } else { queryWrapper.orderByAsc(sortData.getProperty()); } - } - return PageData.from(wxBillMapper.selectPage(page, queryWrapper)); + }*/ + return PageData.from(wxBillMapper.selectPage(page, queryWrapper),getTotalDataByDate(pageWrap.getModel())); } + private WxBill getTotalDataByDate(WxBill model) { + QueryWrapper<WxBill> param = new QueryWrapper<>(); + param.lambda().ge(model.getStartDate() !=null,WxBill::getId,DateUtil.getShortDateStr(model.getStartDate())); + param.lambda().le(model.getEndDate() !=null,WxBill::getId,DateUtil.getShortDateStr(model.getEndDate())); + param.select("sum(sum_bill) as sum_bill" + ,"sum(sum_refund_bill) as sum_refund_bill" + ,"sum(sum_success_fee) as sum_success_fee" + ,"sum(sum_refund_fee) as sum_refund_fee" + ,"sum(sum_cmms_amt) as sum_cmms_amt" + ,"sum(sum_total_fee) as sum_total_fee" + ,"sum(sum_apply_refund_fee) as sum_apply_refund_fee" + ,"sum(sum_total_fee) as sum_total_fee" + ,"sum(total) as total" + ,"sum(sum_refund_cmms_amt) as sum_refund_cmms_amt" + ,"sum(bike_fee) as bike_fee" + ); + WxBill bill = wxBillMapper.selectOne(param.last("limit 1")); + if(bill == null){ + bill = initBillData(); + + } + return bill; + } + + public static WxBill initBillData(){ + WxBill bill = new WxBill(); + 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)); + bill.setTotal(new BigDecimal(0.00)); + bill.setBikeFee(new BigDecimal(0.00)); + return bill; + } @Override public long count(WxBill wxBill) { QueryWrapper<WxBill> wrapper = new QueryWrapper<>(wxBill); return wxBillMapper.selectCount(wrapper); } @Override - public void getWxBill() { - 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); - } + @Transactional(rollbackFor = {BusinessException.class,Exception.class}) + @Synchronized//鍗曚緥鎵ц + public void getWxBill(Date ydate ) { + isValidDate(ydate); + // 鑾峰彇浜ゆ槗璐﹀崟鏁版嵁 + WxPayDownloadBillRequest request = new WxPayDownloadBillRequest(); + String billDate = DateUtil.getShortDateStr(ydate); +// Date ydate = DateUtil.addDaysToDate(new Date(), -1); + request.setBillDate(billDate); + request.setBillType("SUCCESS"); + request.setSubMchId(WxMiniConfig.wxProperties.getSubMchId()); - } catch (WxPayException e) { - e.printStackTrace(); + WxPayBillResult response = null; + try { + response = WxMiniConfig.wxPayV2Service.downloadBill(request); + }catch (WxPayException e){ + } + request.setBillType("REFUND"); + //璇锋眰閫�娆惧崟浜ゆ槗璁板綍姹囨�绘暟鎹� + WxPayBillResult responseRefund = null; + try { + responseRefund = WxMiniConfig.wxPayV2Service.downloadBill(request); + }catch (WxPayException e){ + + } + WxBill bill = initBillData(); + //鏃ユ湡浣滀负涓婚敭 + bill.setId(billDate); + 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.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.setSumRefundFee(formatStringToDecimal(responseRefund.getTotalRefundFee())); + //閫�娆炬�婚噾棰� + bill.setSumApplyRefundFee(formatStringToDecimal(responseRefund.getTotalAppliedRefundFee())); + 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 boolean isValidDate(Date ydate) { + if(ydate == null ){ + //鏃堕棿涓嶈兘涓虹┖ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + int days = DateUtil.daysBetweenDates(new Date(),ydate); + if(days< 1){ + //鍙兘鍚屾鏄ㄥぉ涔嬪墠鐨勬暟鎹� + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + if( days == 1){ + //鍙兘鍚屾鏄ㄥぉ10涔嬪墠鐨勬暟鎹� + Calendar caln = Calendar.getInstance(); + caln.setTime(ydate); + if(caln.get(Calendar.HOUR_OF_DAY) > 10){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝鏈�杩戝悓姝ユ椂闂村彧鑳芥埅姝㈠埌鏄ㄥぉ10鐐瑰墠锛�"); + } + } + return true; + } + + 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); + if((StringUtils.equals(info.getTradeType(),"REFUND" )||StringUtils.equals(info.getTradeType(),"REVOKED" )) && !StringUtils.equals(info.getRefundState(),"SUCCESS" )){ + //濡傛灉鏄潪鎴愬姛鐨勯��娆炬暟鎹紝涓嶈繘琛屽悓姝� + continue; + } + 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())); //鐢宠閫�娆炬椂闂� + //璁$畻鑷杞︽敹鍏ワ紝鍖归厤鏉ヨ嚜鑷杞﹀皬绋嬪簭鐨勬墍鏈夋敮浠樻垚鍔熷拰閫�娆炬垚鍔熺殑閲戦锛屼綔涓鸿嚜琛岃溅鏀跺叆锛堢疮璁℃敹娆鹃噾棰�-绱閫�娆炬垚鍔熼噾棰濓級 + if(StringUtils.equals(detail.getAppid(), WxMiniConfig.wxPayV2Service.getConfig().getSubAppId())){ + //鑷杞︽敹鍏ョ疮璁℃敹娆鹃噾棰�(鏀粯鎴愬姛鎬婚噾棰�-閫�娆炬�婚噾棰濓級 + if(StringUtils.equals(info.getTradeState(),"SUCCESS")){ + //濡傛灉鏄氦鏄� + bill.setBikeFee(bill.getBikeFee().add(detail.getSettlementTotalFee())); + bill.setBikeFee(bill.getBikeFee().subtract(detail.getCmmsAmt()) ); + }else if(StringUtils.equals(info.getTradeState(),"REFUND" ) || StringUtils.equals(info.getTradeState(),"REVOKED" )){ + //濡傛灉閫�娆炬垚鍔燂紝鎵i櫎閫�娆鹃噾棰� + bill.setBikeFee(bill.getBikeFee().subtract(detail.getRefundFee()) ); + bill.setBikeFee(bill.getBikeFee().subtract(detail.getCmmsAmt()) ); + } + } + 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)); } } -- Gitblit v1.9.3