From bae7c6f283690edbbca35cbb85cb9cd0ee9c7af8 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Wed, 30 Jun 2021 14:56:12 +0800
Subject: [PATCH] 20210630 积分拨付

---
 gc-shop/src/main/java/com/xzx/gc/shop/service/OrderService.java |  149 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 143 insertions(+), 6 deletions(-)

diff --git a/gc-shop/src/main/java/com/xzx/gc/shop/service/OrderService.java b/gc-shop/src/main/java/com/xzx/gc/shop/service/OrderService.java
index 7d6c439..862975d 100644
--- a/gc-shop/src/main/java/com/xzx/gc/shop/service/OrderService.java
+++ b/gc-shop/src/main/java/com/xzx/gc/shop/service/OrderService.java
@@ -15,10 +15,7 @@
 import com.xzx.gc.entity.*;
 import com.xzx.gc.shop.dto.*;
 import com.xzx.gc.shop.mapper.*;
-import com.xzx.gc.shop.vo.ExpressInfoVo;
-import com.xzx.gc.shop.vo.QueryOrderListVo;
-import com.xzx.gc.shop.vo.ViewOrderVo;
-import com.xzx.gc.shop.vo.XcxOrderListVo;
+import com.xzx.gc.shop.vo.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -64,7 +61,7 @@
     private IdUtils idUtils;
 
     public Map<String, Object> queryOrderList(QueryOrderListDto model) {
-        String name = model.getName();
+        String name = StrUtil.isEmpty(model.getName())?model.getName():StringUtils.encode(model.getName());
         String orderNo = model.getOrderNo();
         Integer status = model.getStatus() == null ? 0 : model.getStatus();
         Date createdTimeStart = model.getCreatedTimeStart();
@@ -179,6 +176,10 @@
             }
         }
 
+        if (addGoodsOrderDto.getCnt() > sku.getStock()) {
+            throw new RestException(-3, "库存不足");
+        }
+
         AccountInfo accountInfo = accountInfoMapper.selectAccountInfoByUserId(addGoodsOrderDto.getUserId());
 
         if (accountInfo.getCollectScore() == null) {
@@ -245,17 +246,61 @@
         scoreDetails.setOriginalScore(score);
         scoreDetails.setCurrentScore(remianScore);
         scoreDetails.setChangeScore(totalPrice);
+        scoreDetails.setCreatedTime(new Date());
         scoreDetailsMapper.insert(scoreDetails);
+
+        sku.setStock(sku.getStock() - addGoodsOrderDto.getCnt());
+        sku.setQuantity(sku.getQuantity() - addGoodsOrderDto.getCnt());
+        scoreGoodsSkuMapper.updateByPrimaryKey(sku);
     }
 
     public Long cancelOrder(CancelOrderDto model) {
         /**
-         * todo 只更新了订单状态,后续操作待增加
+         * 更新订单状态
+         * 增加一条取消订单积分退回的记录
+         * 查询所有的订单详情,依次库存增加,销量减少
          */
         ScoreOrder scoreOrder = new ScoreOrder();
         scoreOrder.setId(model.getId());
         scoreOrder.setStatus(ScoreOrder.STATUS_CANCEL);
         scoreOrderMapper.updateByPrimaryKeySelective(scoreOrder);
+
+        ScoreOrder order = scoreOrderMapper.selectByPrimaryKey(model.getId());
+        Example exampleDetails = new Example(ScoreDetails.class);
+        Example.Criteria criteriaDetails = exampleDetails.createCriteria();
+        criteriaDetails.andEqualTo("orderNo",order.getOrderNo());
+        criteriaDetails.andEqualTo("userId",order.getUserId());
+        criteriaDetails.andEqualTo("type",ScoreDetails.SCORE_TYPE_SHOPPING);
+        ScoreDetails scoreDetails = scoreDetailsMapper.selectOneByExample(exampleDetails);
+        if(ObjectUtil.isNotEmpty(scoreDetails)){
+            AccountInfo accountInfo = accountInfoMapper.selectAccountInfoByUserId(order.getUserId());
+            BigDecimal score = new BigDecimal(accountInfo.getCollectScore());
+            BigDecimal remianScore = score.add(scoreDetails.getChangeScore()).setScale(0, BigDecimal.ROUND_DOWN);
+            ScoreDetails scoreDetailsRet = new ScoreDetails();
+            scoreDetailsRet.setOrderNo(scoreDetails.getOrderNo());
+            scoreDetailsRet.setUserId(scoreDetails.getUserId());
+            scoreDetailsRet.setType(ScoreDetails.SCORE_TYPE_SHOPPING_RETURN);
+            scoreDetailsRet.setOriginalScore(score);
+            scoreDetailsRet.setCurrentScore(remianScore);
+            scoreDetailsRet.setChangeScore(scoreDetails.getChangeScore());
+            scoreDetailsRet.setCreatedTime(new Date());
+            scoreDetailsMapper.insert(scoreDetailsRet);
+        }
+
+        Example exampleScoreOrderDetails = new Example(ScoreOrderDetails.class);
+        Example.Criteria criteriaScoreOrderDetails = exampleScoreOrderDetails.createCriteria();
+        criteriaScoreOrderDetails.andEqualTo("orderId",order.getId());
+        List<ScoreOrderDetails> scoreOrderDetails = scoreOrderDetailsMapper.selectByExample(exampleScoreOrderDetails);
+        if(CollUtil.isNotEmpty(scoreOrderDetails)){
+            for(ScoreOrderDetails scoreOrderDetail : scoreOrderDetails){
+                ScoreGoodsSku scoreGoodsSku = scoreGoodsSkuMapper.selectByPrimaryKey(scoreOrderDetail.getSkuId());
+                if(ObjectUtil.isNotEmpty(scoreGoodsSku)){
+                    scoreGoodsSku.setStock(scoreGoodsSku.getStock() + scoreOrderDetail.getCnt());
+                    scoreGoodsSku.setQuantity(scoreGoodsSku.getQuantity() - scoreOrderDetail.getCnt());
+                    scoreGoodsSkuMapper.updateByPrimaryKey(scoreGoodsSku);
+                }
+            }
+        }
         return scoreOrder.getId();
     }
 
@@ -264,4 +309,96 @@
         List<XcxOrderListVo> data = scoreOrderMapper.selectXcxOrderList(xcxOrderListDto);
         return new PageInfo<>(data);
     }
+
+    public XcxOrderDetailsVo orderDetails(Long id) {
+        return scoreOrderMapper.selectXcxOrderDetails(id);
+    }
+
+    public void confirmOrder(Long id, String userId) {
+        ScoreOrder order = scoreOrderMapper.selectByPrimaryKey(id);
+        if (order == null) {
+            throw new RestException(-3, "订单不存在");
+        }
+
+        if (!order.getUserId().equals(userId)) {
+            throw new RestException(-3, "订单不存在");
+        }
+
+        if (!ScoreOrder.STATUS_ON.equals(order.getStatus())) {
+            throw new RestException(-3, "暂不能确认收货");
+        }
+
+        scoreOrderMapper.updateOrderStatus(id, ScoreOrder.STATUS_DOING, userId);
+    }
+
+    public void cancelOrder(Long id, String userId) {
+        ScoreOrder order = scoreOrderMapper.selectByPrimaryKey(id);
+        if (order == null) {
+            throw new RestException(-3, "订单不存在");
+        }
+
+        if (!order.getUserId().equals(userId)) {
+            throw new RestException(-3, "订单不存在");
+        }
+
+        if (!ScoreOrder.STATUS_READY.equals(order.getStatus())) {
+            throw new RestException(-3, "订单不能取消");
+        }
+
+        scoreOrderMapper.updateOrderStatus(id, ScoreOrder.STATUS_CANCEL, userId);
+
+
+        // 退积分
+        AccountInfo accountInfo = accountInfoMapper.selectAccountInfoByUserId(userId);
+        BigDecimal score = new BigDecimal(accountInfo.getCollectScore()).add(order.getTotalPrice()).setScale(0, BigDecimal.ROUND_DOWN);
+
+        ScoreDetails scoreDetails = new ScoreDetails();
+        scoreDetails.setOriginalScore(new BigDecimal(accountInfo.getCollectScore()));
+        scoreDetails.setCurrentScore(score);
+        scoreDetails.setChangeScore(order.getTotalPrice());
+        scoreDetails.setOrderNo(order.getOrderNo());
+        scoreDetails.setType(ScoreDetails.SCORE_TYPE_SHOPPING_RETURN);
+        scoreDetails.setCreatedTime(new Date());
+        scoreDetails.setUserId(userId);
+        scoreDetailsMapper.insert(scoreDetails);
+
+        accountInfo.setCollectScore(score.toString());
+        accountInfoMapper.updateByPrimaryKey(accountInfo);
+
+
+
+        // 减销量 加库存
+        List<ScoreOrderDetails> details = scoreOrderDetailsMapper.selectOrderDetailsByOrderId(order.getId());
+        if (CollUtil.isNotEmpty(details)) {
+            for (ScoreOrderDetails detail : details) {
+                ScoreGoodsSku sku = scoreGoodsSkuMapper.selectByPrimaryKey(detail.getSkuId());
+                if (sku == null) {
+                    continue;
+                }
+
+                sku.setQuantity(sku.getQuantity() - detail.getCnt());
+                sku.setStock(sku.getStock() + detail.getCnt());
+                scoreGoodsSkuMapper.updateByPrimaryKey(sku);
+            }
+        }
+    }
+
+    public StatisticsVo orderStatistics(String userId) {
+        BigDecimal score = scoreDetailsMapper.selectTotalScoreByUserId(userId, ScoreDetails.SCORE_TYPE_EXCHANGE);
+
+        Map<String, Object> userData = scoreOrderMapper.selectOrderStastics(userId);
+        Map<String, Object> allData = scoreOrderMapper.selectOrderStastics(null);
+
+        StatisticsVo statisticsVo = new StatisticsVo();
+        statisticsVo.setScore(score);
+        BigDecimal userPrice = (BigDecimal) userData.get("totalPrice");
+        statisticsVo.setReduceCarbon(userPrice.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_DOWN));
+        statisticsVo.setOrderCnt(Integer.parseInt(userData.get("totalOrder").toString()));
+
+        BigDecimal totalPrice = (BigDecimal) allData.get("totalPrice");
+        statisticsVo.setTotalReduceCarbon(totalPrice.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_DOWN));
+        statisticsVo.setTotalOrderCnt(Integer.parseInt(allData.get("totalOrder").toString()));
+        statisticsVo.setTotalBuyCnt(Integer.parseInt(allData.get("totalBuy").toString()));
+        return statisticsVo;
+    }
 }

--
Gitblit v1.9.1