From 89f8237e343d2b59dbf280c055751b69ee32f01d Mon Sep 17 00:00:00 2001
From: 陶杰 <1378534974@qq.com>
Date: 星期四, 12 九月 2024 18:04:52 +0800
Subject: [PATCH] 1.日历功能

---
 src/main/java/com/mzl/flower/service/payment/OrderService.java |  268 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 229 insertions(+), 39 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/payment/OrderService.java b/src/main/java/com/mzl/flower/service/payment/OrderService.java
index 95bc18a..54c79fb 100644
--- a/src/main/java/com/mzl/flower/service/payment/OrderService.java
+++ b/src/main/java/com/mzl/flower/service/payment/OrderService.java
@@ -12,6 +12,7 @@
 import com.mzl.flower.config.security.SecurityUtils;
 import com.mzl.flower.constant.Constants;
 import com.mzl.flower.dto.PriceDTO;
+import com.mzl.flower.dto.request.menber.MemberGrowthRecordDTO;
 import com.mzl.flower.dto.request.payment.*;
 import com.mzl.flower.dto.response.flower.FlowerCartListDTO;
 import com.mzl.flower.dto.response.flower.FlowerCartListWrapDTO;
@@ -39,7 +40,11 @@
 import com.mzl.flower.mapper.system.UserWechatMapper;
 import com.mzl.flower.mapper.warehouse.WarehouseLocationMapper;
 import com.mzl.flower.service.BaseService;
+import com.mzl.flower.service.coupon.CouponRecordService;
 import com.mzl.flower.service.flower.FlowerService;
+import com.mzl.flower.service.menber.MemberGrowthRecordService;
+import com.mzl.flower.service.point.CustomerPointService;
+import com.mzl.flower.service.point.PointGoodsService;
 import com.mzl.flower.service.system.CodeService;
 import com.mzl.flower.service.system.WeChatService;
 import com.mzl.flower.service.transport.TransportService;
@@ -50,6 +55,7 @@
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -133,47 +139,87 @@
     @Autowired
     private OrderItemCheckMapper orderItemCheckMapper;
 
+    @Autowired
+    private OrderPointGoodsMapper pointGoodsMapper;
+
+    @Autowired
+    @Lazy
+    private MemberGrowthRecordService memberGrowthRecordService;
+
+    @Autowired
+    private CustomerPointService customerPointService;
+
+    @Autowired
+    private PointGoodsService pointGoodsService;
+
+    @Autowired
+    private RedisLockService lockService;
+
+    @Autowired
+    private CouponRecordService couponRecordService;
+
     public void changeFlower2Cart(CartSaveDTO dto) {
         String userId = SecurityUtils.getUserId();
-        Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
-                .eq("create_by", userId).eq("flower_id", dto.getId()));
-        if (c == null) {
-            if (dto.getNum() <= 0) {
-                throw new ValidationException("数量不能小于等于0,请刷新页面重试");
-            }
-            c = new Cart();
-            c.setFlowerId(dto.getId());
-            c.setNum(dto.getNum());
-            c.create(userId);
+        String key = userId + "_" + dto.getId();
+        boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key);
+        if(!lock){
+            return;
+        }
 
-            cartMapper.insert(c);
-        } else {
-            c.setNum(c.getNum() + dto.getNum());
-            c.update(userId);
-            if (c.getNum() <= 0) {
-                cartMapper.deleteById(c.getId());
+        try {
+            Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
+                    .eq("create_by", userId).eq("flower_id", dto.getId()));
+            if (c == null) {
+                if (dto.getNum() <= 0) {
+                    throw new ValidationException("数量不能小于等于0,请刷新页面重试");
+                }
+                c = new Cart();
+                c.setFlowerId(dto.getId());
+                c.setNum(dto.getNum());
+                c.create(userId);
+
+                cartMapper.insert(c);
             } else {
-                cartMapper.updateById(c);
+                c.setNum((c.getNum()==null?0:c.getNum()) + dto.getNum());
+                c.update(userId);
+                if (c.getNum() <= 0) {
+                    cartMapper.deleteById(c.getId());
+                } else {
+                    cartMapper.updateById(c);
+                }
             }
+        } finally {
+            lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key);
         }
     }
 
     public void saveFlower2Cart(CartSaveDTO dto) {
         String userId = SecurityUtils.getUserId();
-        Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
-                .eq("create_by", userId).eq("flower_id", dto.getId()));
-        if (c == null) {
-            c = new Cart();
-            c.setFlowerId(dto.getId());
-            c.setNum(dto.getNum());
-            c.create(userId);
 
-            cartMapper.insert(c);
-        } else {
-            c.setNum(dto.getNum());
-            c.update(userId);
+        String key = userId + "_" + dto.getId();
+        boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key);
+        if(!lock){
+            return;
+        }
 
-            cartMapper.updateById(c);
+        try {
+            Cart c = cartMapper.selectOne(new QueryWrapper<Cart>()
+                    .eq("create_by", userId).eq("flower_id", dto.getId()));
+            if (c == null) {
+                c = new Cart();
+                c.setFlowerId(dto.getId());
+                c.setNum(dto.getNum());
+                c.create(userId);
+
+                cartMapper.insert(c);
+            } else {
+                c.setNum(dto.getNum());
+                c.update(userId);
+
+                cartMapper.updateById(c);
+            }
+        } finally {
+            lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key);
         }
     }
 
@@ -361,7 +407,8 @@
         }
 
         Address address = addressMapper.selectById(dto.getAddressId());
-        List<TransportOrderDTO> tLs = transportService.getPreOrderTransportList(address, p.getTotalWeight(), dto.getTransportId());
+        List<TransportOrderDTO> tLs = transportService.getPreOrderTransportList(address
+                , p.getTotalWeight(), dto.getTransportId());
         BigDecimal transportFee = new BigDecimal(0);
         String deliveryName = "";
         if (tLs != null && tLs.size() > 0) {
@@ -373,6 +420,10 @@
         String userId = SecurityUtils.getUserId();
         Order order = new Order();
         order.setId(UUIDGenerator.getUUID());
+
+        final List<Long> goodsRecordIdList = dto.getGoodsRecordIdList();
+        usePointGoods(order.getId(), goodsRecordIdList);//使用积分商品兑换券
+
         order.create(userId);
         order.setRemarks(dto.getRemarks());
         order.setSpecialNeeds(dto.getSpecialNeeds());
@@ -393,6 +444,7 @@
 
         BigDecimal totalAmount = order.getFlowerAmount().add(p.getPacking()).add(transportFee);//使用优惠券之前的总价
         CouponRecordDO coupon = useCouponRecord(order.getId(), totalAmount, dto.getCouponRecordId());
+        log.info("用户优惠券: " + coupon);
         if(coupon != null){
             order.setMemberCouponId(coupon.getId());
             order.setMemberCouponCode(coupon.getCouponCode());
@@ -421,7 +473,8 @@
         //商品列表处理
         List<FlowerCartListDTO> flowers = p.getFlowers();
         Map<Long, List<ParamItemDTO>> paramMap = new HashMap<>();
-        BigDecimal flowerAmount = order.getFlowerAmount();//订单商品总价
+        final BigDecimal flowerAmount = order.getFlowerAmount();//订单商品总价
+        final BigDecimal memberCouponAmount = order.getMemberCouponAmount();//使用优惠券面值
         for (FlowerCartListDTO f : flowers) {
             OrderItem t = new OrderItem();
             t.setId(UUIDGenerator.getUUID());
@@ -451,8 +504,7 @@
             t.setTotal(f.getTotalMember());//使用会员总价
 
             t.setOriginalPrice(pp.getPrice());//非会员售价
-            BigDecimal couponAmount = calculateCoupon(order.getMemberCouponAmount()
-                    , t.getTotal(), order.getFlowerAmount(), t.getNum());
+            BigDecimal couponAmount = calculateCoupon(memberCouponAmount, t.getTotal(), flowerAmount, t.getNum());
             t.setCouponAmount(couponAmount);//每扎平摊的优惠券面值
             t.setRealPrice(t.getPrice().subtract(couponAmount));//退款时使用的真实成交价
 
@@ -466,11 +518,30 @@
         order.setPayOpenid(openId);
         orderMapper.insert(order);
 
-        //TODO 处理积分商品兑换券
+        //处理积分商品兑换券
+        if(goodsRecordIdList != null && goodsRecordIdList.size() > 0){
+            for(Long goodsRecordId : goodsRecordIdList){
+                OrderPointGoods g = new OrderPointGoods();
+                g.setId(UUIDGenerator.getUUID());
+                g.setOrderId(order.getId());
+                g.setGoodsRecordId(goodsRecordId);
+                g.create(userId);
+                pointGoodsMapper.insert(g);
+            }
+        }
 
         Map map = paymentV3Service.wxPrepay(order);
         map.put("_testV2OrderId", order.getId());
         return map;
+    }
+
+    private void usePointGoods(String orderId, List<Long> goodsRecordIdList){
+        //使用积分商品兑换券
+        if(goodsRecordIdList != null && goodsRecordIdList.size() > 0){
+            for(Long grId : goodsRecordIdList){
+                pointGoodsService.useExchangeGoods(grId, orderId);
+            }
+        }
     }
 
     /**
@@ -493,8 +564,11 @@
     }
 
     private CouponRecordDO useCouponRecord(String orderId, BigDecimal amount, String couponRecordId){
+        log.info("使用用户优惠券id: " + couponRecordId + "; 订单id: " + orderId);
         if(StringUtils.isNotEmpty(couponRecordId)){
-            //TODO 优惠券使用和验证,如果不符合使用条件需要抛出异常。需调用优惠券使用方法
+            //优惠券使用和验证,如果不符合使用条件需要抛出异常。需调用优惠券使用方法
+            couponRecordService.useCoupon(couponRecordId, orderId, amount);
+            return couponRecordService.getCouponRecordById(couponRecordId);
         }
 
         return null;
@@ -806,6 +880,81 @@
         return ls;
     }
 
+    public List<OrderCheckLocationListDTO> selectOrderCheckLocationList(OrderQueryDTO dto) {
+        dto.setIdList(splitParam(dto.getIds()));
+        dto.setStartDate(parseLocalDateTime(dto.getStartDateStr(), true));
+        dto.setEndDate(parseLocalDateTime(dto.getEndDateStr(), false));
+
+        dto.setCreateStartDate(parseLocalDateTime(dto.getCreateStartDateStr(), 17, 0, 0, -1));
+        dto.setCreateEndDate(parseLocalDateTime(dto.getCreateEndDateStr(), 17, 0, 0, 0));
+
+        List<OrderCheckListDTO> ls = orderMapper.selectOrderCheckList(dto);
+
+        List<OrderCheckLocationListDTO> result = new ArrayList<>();
+
+        if (ls != null && ls.size() > 0) {
+            List<String> orderIds = new ArrayList<>();
+            Map<Long, OrderCheckLocationListDTO> rMap = new HashMap<>();
+            Map<String, OrderCheckListDTO> orderMap = new HashMap<>();
+            for (OrderCheckListDTO c : ls) {
+                orderIds.add(c.getId());
+
+                orderMap.put(c.getId(), c);
+
+                Long locationId = c.getWarehouseLocationId();
+                OrderCheckLocationListDTO llc = rMap.get(locationId);
+                if(llc == null){
+                    llc = new OrderCheckLocationListDTO();
+                    BeanUtils.copyProperties(c, llc);
+                    rMap.put(locationId, llc);
+
+                    result.add(llc);
+                }
+
+                BigDecimal t = llc.getTotalAmount();
+                if(t == null){
+                    t = new BigDecimal(0);
+                }
+                t = t.add(c.getTotalAmount());
+                llc.setTotalAmount(t);
+            }
+
+            Map<Long, String> stationMap = prepareStationMap();
+
+            List<OrderItem> itemList = orderItemMapper.selectList(new QueryWrapper<OrderItem>()
+                    .in("order_id", orderIds));
+            Map<String, List<OrderItemLocationListDTO>> map = new HashMap<>();
+
+            for (OrderItem oi : itemList) {
+                String orderId = oi.getOrderId();
+                List<OrderItemLocationListDTO> ll = map.computeIfAbsent(orderId, k -> new ArrayList<>());
+                OrderItemLocationListDTO d = new OrderItemLocationListDTO();
+                BeanUtils.copyProperties(oi, d);
+                d.setStationName(stationMap.get(oi.getStationId()));
+
+                OrderCheckListDTO c = orderMap.get(oi.getOrderId());
+                d.setOrderNo(c.getOrderNo());
+                d.setOrderId(c.getId());
+
+                ll.add(d);
+            }
+
+            for (OrderCheckListDTO c : ls) {
+                String orderId = c.getId();
+                Long locationId = c.getWarehouseLocationId();
+                OrderCheckLocationListDTO r = rMap.get(locationId);
+                List<OrderItemLocationListDTO> items = r.getItems();
+                if(items == null){
+                    items = new ArrayList<>();
+                    r.setItems(items);
+                }
+                items.addAll(map.get(orderId));
+            }
+        }
+
+        return result;
+    }
+
     public Page<OrderListDTO> selectPartnerOrderList(Page page, OrderQueryDTO dto) {
         Partner p = getCurrentPartner();
         dto.setPartnerId(p.getId());
@@ -840,6 +989,9 @@
         );
         boolean couldCheckRefund = StringUtils.isEmpty(transferId) && count == 0 && cc == 0;
         dto.setCouldCheckRefund(couldCheckRefund);
+
+        List<OrderPointGoodsListDTO> pointGoodsList = orderMapper.getPointGoodsList(id);
+        dto.setPointGoodsList(pointGoodsList);
 
         return dto;
     }
@@ -879,12 +1031,12 @@
         return result;
     }
 
-    public void autoReceive() {
+    public List<Order> autoReceive() {
         int days = -5; //5天前的订单自动收货
         LocalDateTime endTime = LocalDateTime.now().plusDays(days);
         List<Order> ls = orderMapper.getOrderForAutoReceive(endTime);
         if (ls == null || ls.size() == 0) {
-            return;
+            return ls;
         }
 
         for (Order o : ls) {
@@ -893,8 +1045,44 @@
             o.setReceiveTime(LocalDateTime.now());
             o.update("sys");
             orderMapper.updateById(o);
-
         }
+
+        return ls;
+    }
+
+    public void processAfterReceive(Order o){
+        String status = o.getStatus();
+        if(!Constants.ORDER_STATUS.EVALUATE.name().equals(status)){
+            log.warn("订单未确认收货,无法处理积分和成长值");
+            return;
+        }
+
+        BigDecimal totalAmount = o.getTotalAmount();
+        List<OrderItemCheck> checkList = orderItemCheckMapper.selectList(new QueryWrapper<OrderItemCheck>()
+                .eq("order_id", o.getId())
+                .eq("audit_status", Constants.CHECK_AUDIT_STATUS.AGREED.name())
+        );
+
+        if(checkList != null && checkList.size() > 0){
+            for(OrderItemCheck c : checkList){
+                if(Constants.CHECK_TYPE.replace.name().equals(c.getType())){
+                    continue;
+                }
+                totalAmount = totalAmount.subtract(getAmount(c.getDeductAmount()));
+            }
+        }
+
+        //成长值计算
+        MemberGrowthRecordDTO mDto = new MemberGrowthRecordDTO();
+        mDto.setTotalAmount(totalAmount);
+        mDto.setUserId(o.getCreateBy());
+        mDto.setSource(Constants.GROWTH_SOURCE.consume.name());
+        mDto.setType(Constants.GROWTH_TYPE.add.name());
+        mDto.setRemarks("订单: " + o.getOrderNo());
+        memberGrowthRecordService.saveMemberGrowthRecord(mDto);
+
+        //积分计算
+        customerPointService.consumptionPoint(totalAmount, o.getOrderNo(), o.getCreateBy());
     }
 
     public JSONObject sendWxDeliveryGood(Order o) {
@@ -936,7 +1124,7 @@
         }
     }
 
-    public void confirmOrderReceive(String id) {
+    public Order confirmOrderReceive(String id) {
         Order o = orderMapper.selectById(id);
         String userId = SecurityUtils.getUserId();
         if (!userId.equals(o.getCreateBy())) {
@@ -955,6 +1143,8 @@
         o.setReceiveTime(LocalDateTime.now());
         o.update(userId);
         orderMapper.updateById(o);
+
+        return o;
     }
 
     public Integer getMyOrderStatusCount(String status) {

--
Gitblit v1.9.3