From 6089e88cde34ef3519312d3f563fc3809e138948 Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期三, 18 十月 2023 11:32:20 +0800
Subject: [PATCH] bug

---
 server/services/src/main/java/com/doumee/service/business/impl/GoodsorderServiceImpl.java |   44 ++++++++++++++++++++++++--------------------
 1 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/server/services/src/main/java/com/doumee/service/business/impl/GoodsorderServiceImpl.java b/server/services/src/main/java/com/doumee/service/business/impl/GoodsorderServiceImpl.java
index ec04d88..eea7414 100644
--- a/server/services/src/main/java/com/doumee/service/business/impl/GoodsorderServiceImpl.java
+++ b/server/services/src/main/java/com/doumee/service/business/impl/GoodsorderServiceImpl.java
@@ -44,6 +44,7 @@
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shiro.SecurityUtils;
 import org.checkerframework.checker.units.qual.A;
+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;
@@ -494,28 +495,34 @@
     public void backGoodsorder(String orderId, BigDecimal money,String reason) {
         LoginUserInfo principal = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
         Goodsorder goodsorder = goodsorderMapper.selectById(orderId);
+        if(goodsorder == null){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "瀵逛笉璧凤紝璁㈠崟淇℃伅涓嶅瓨鍦紝璇峰埛鏂伴〉闈㈤噸璇曪紒");
+        }
+         if (Constants.formatIntegerNum(goodsorder.getStatus()) != Constants.GOODSORDER_STATUS.CLOSE.getKey()){
+                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝璇ヨ鍗曡缁撶畻鍚庡啀閫�娆緙");
+        }
         QueryWrapper<Refund> wrapper = new QueryWrapper<>();
         wrapper.lambda()
                 .eq(Refund::getObjId,orderId)
-                .orderByDesc(Refund::getCreateDate)
-                .last("limit 1");
-        Refund lastRefund = refundMapper.selectOne(wrapper);
-        if (Objects.isNull(lastRefund)){
-            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"鏀硅鍗曡缁撶畻鍚庡湪閫�娆�");
-        }
-        BigDecimal canBalance = lastRefund.getCanBalance();
+                .eq(Refund::getStatus,Constants.TWO)
+                .eq(Refund::getIsdeleted,Constants.ZERO) ;
+        wrapper.select("sum(money) as money").last(" limit 1");
+        Refund total = refundMapper.selectOne(wrapper);
+        BigDecimal refundMoney = total == null?new BigDecimal(0):Constants.formatDecimalNum(total.getMoney());
+        BigDecimal canBalance =Constants.formatDecimalNum(goodsorder.getMoney()).subtract(money);
         if(canBalance.compareTo(money) > Constants.ZERO){
             RefundDTO refundDTO = new RefundDTO();
             refundDTO.setOrderId(orderId);
-            refundDTO.setCanBalance(lastRefund.getCanBalance().subtract(lastRefund.getMoney()));
+            refundDTO.setCanBalance(canBalance);
             refundDTO.setRefundAmount(money);
             refundDTO.setTotalAmount(goodsorder.getMoney());
             refundDTO.setMemberId(goodsorder.getMemberId());
+            refundDTO.setCreator(principal.getId());
             refundDTO.setReason(reason);
             refundDTO.setType(Constants.REFUND_TYPE.BACK.getKey());
             Refund refund = wxMiniUtilService.wxRefund(refundDTO);
         }else {
-            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"閫�娆鹃噾棰濅笉鍏佽瓒呰繃"+canBalance);
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝閫�娆鹃噾棰濅笉鍏佽瓒呰繃"+canBalance);
         }
     }
 
@@ -529,20 +536,16 @@
         QueryWrapper<Refund> wrapper = new QueryWrapper<>();
         wrapper.lambda()
                 .eq(Refund::getObjId,orderId)
-                .orderByDesc(Refund::getCreateDate);
-        List<Refund> refunds = refundMapper.selectList(wrapper);
+                .eq(Refund::getStatus,Constants.TWO)
+                .eq(Refund::getIsdeleted,Constants.ZERO) ;
+        wrapper.select("sum(money) as money").last(" limit 1");
+        Refund total = refundMapper.selectOne(wrapper);
         GoodsorderCanBanlanceDTO goodsorderCanBanlanceDTO = new GoodsorderCanBanlanceDTO();
         goodsorderCanBanlanceDTO.setGoodsorderMoney(goodsorder.getMoney());
         goodsorderCanBanlanceDTO.setCloseMoney(goodsorder.getCloseMoney());
-        if (!CollectionUtils.isEmpty(refunds)){
-            BigDecimal reduce = refunds.stream().map(s -> s.getMoney()).reduce(new BigDecimal("0"), (a, b) -> b.add(a));
-            Refund refund = refunds.stream().findFirst().orElse(null);
-            goodsorderCanBanlanceDTO.setHasRefundMoney(reduce);
-            goodsorderCanBanlanceDTO.setCanBanlanceMoney(refund.getCanBalance().subtract(refund.getMoney()));
-        }else {
-            goodsorderCanBanlanceDTO.setHasRefundMoney(new BigDecimal("0"));
-            goodsorderCanBanlanceDTO.setCanBanlanceMoney(goodsorder.getMoney());
-        }
+        BigDecimal reduce =  total!=null?Constants.formatDecimalNum(total.getMoney()):new BigDecimal(0);
+        goodsorderCanBanlanceDTO.setHasRefundMoney(reduce);
+        goodsorderCanBanlanceDTO.setCanBanlanceMoney(Constants.formatDecimalNum(goodsorder.getMoney()).subtract(goodsorderCanBanlanceDTO.getHasRefundMoney()));
         return goodsorderCanBanlanceDTO;
     }
 
@@ -673,6 +676,7 @@
         goodsorder.setId(Constants.getUUID());
         goodsorder.setCreateDate(new Date());
         goodsorder.setIsdeleted(Constants.ZERO);
+        goodsorder.setCode(goodsorder.getId());
         goodsorder.setMemberId(memberId);
         goodsorder.setMoney(new BigDecimal(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.RENT_DEPOSIT).getCode()));
         goodsorder.setStatus(Constants.goodsorderStatus.waitPay);

--
Gitblit v1.9.3