瀏覽代碼

呵呵商城改版part2

Aslee 3 年之前
父節點
當前提交
015265cdfa

+ 3 - 3
src/main/java/com/caimei/controller/OrderSubmitApi.java

@@ -40,12 +40,12 @@ public class OrderSubmitApi {
             @ApiImplicitParam(name = "productId", value = "立即购买商品ID", required = false),
             @ApiImplicitParam(name = "productCount", value = "立即购买商品数量", required = false),
             @ApiImplicitParam(name = "heUserId", value = "分销者用户id", required = false),
-            @ApiImplicitParam(name = "collageFlag", value = "是否拼团购买:0不拼团,1拼团", required = false),
+            @ApiImplicitParam(name = "couponId", value = "领券购买的优惠券id", required = false),
             @ApiImplicitParam(name = "collageFlag", value = "是否拼团购买:0不拼团,1拼团", required = false)
     })
     @GetMapping("/confirm")
-    public ResponseJson<Map<String, Object>> orderConfirm(Integer userId, String cartIds, Integer productId, Integer productCount, Integer heUserId, Integer collageFlag) {
-        return orderSubmitService.orderConfirm(userId, cartIds, productId, productCount, heUserId, collageFlag);
+    public ResponseJson<Map<String, Object>> orderConfirm(Integer userId, String cartIds, Integer productId, Integer productCount, Integer heUserId, Integer couponId, Integer collageFlag) {
+        return orderSubmitService.orderConfirm(userId, cartIds, productId, productCount, heUserId, couponId, collageFlag);
     }
 
     /**

+ 10 - 17
src/main/java/com/caimei/controller/ProductApi.java

@@ -54,22 +54,6 @@ public class ProductApi {
         return getResponseJson(parameters);
     }
 
-    /**
-     * 废弃
-     */
-    @ApiOperation("首页楼层详情")
-    @ApiImplicitParams({
-            @ApiImplicitParam(name = "floorId", value = "楼层Id", required = true),
-            @ApiImplicitParam(name = "userId", value = "机构用户Id", required = false),
-            @ApiImplicitParam(name = "productName", value = "商品名称", required = false),
-            @ApiImplicitParam(name = "pageNum", value = "第几页", required = false),
-            @ApiImplicitParam(name = "pageSize", value = "一页多少条", required = false)
-    })
-    @GetMapping("/floor/detail")
-    public ResponseJson<PageInfo<ProductVo>> floorDetail(Integer floorId, Integer userId, String productName, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
-                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
-        return productService.floorDetail(floorId, userId, productName, pageNum, pageSize);
-    }
 
     @ApiOperation("商品列表")
     @ApiImplicitParams({
@@ -154,7 +138,8 @@ public class ProductApi {
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
         String url = cloudApi + "/commodity/hehe/activity/details?userId=" + userId + "&activityId=" + activityId+ "&pageNum=" + pageNum + "&pageSize=" + pageSize;
         String parameters = HttpRequest.sendGet(url);
-        return getResponseJson(parameters);}
+        return getResponseJson(parameters);
+    }
 
     @ApiOperation("首页分类列表")
     @GetMapping("/home/type")
@@ -164,6 +149,14 @@ public class ProductApi {
         return getResponseJson(parameters);
     }
 
+    @ApiOperation("一级分类列表")
+    @GetMapping("/type/first")
+    public ResponseJson<String> getBigTypeSelect() throws Exception {
+        String url = cloudApi + "/commodity/type/first?mallType=2";
+        String parameters = HttpRequest.sendGet(url);
+        return getResponseJson(parameters);
+    }
+
     private ResponseJson<String> getResponseJson(String parameters) {
         JSONObject object = JSONObject.parseObject(parameters);
         Integer code = object.getInteger("code");

+ 12 - 0
src/main/java/com/caimei/mapper/CouponMapper.java

@@ -156,4 +156,16 @@ public interface CouponMapper {
     String getUserMobile(Integer shareUserId);
 
     void updateSmsSendCount(@Param("markId") int markId, @Param("count") int count);
+
+    /**
+     * 删除用户已删除的优惠券
+     */
+    void deleteReceiveCoupon(Integer receiveCouponId);
+
+    /**
+     * 根据id查询已领取的优惠券信息
+     * @param receiveCouponId
+     * @return
+     */
+    ReceiveCouponPo getReceiveCouponById(Integer receiveCouponId);
 }

+ 5 - 2
src/main/java/com/caimei/mapper/ProductMapper.java

@@ -7,6 +7,7 @@ import com.caimei.model.vo.*;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 /**
@@ -47,7 +48,7 @@ public interface ProductMapper {
      * @param productId
      * @return
      */
-    Integer findActivityByProductId(Integer productId);
+    Integer getActivityIdByProductId(Integer productId);
 
     /**
      * 活动阶梯价格
@@ -56,7 +57,7 @@ public interface ProductMapper {
      * @param productId  商品id
      * @return
      */
-    List<ActivityLadderVo> findActivityLadder(@Param("activityId") Integer activityId, @Param("productId") Integer productId);
+    List<ActivityLadderVo> getActivityLadderList(@Param("activityId") Integer activityId, @Param("productId") Integer productId);
 
     /**
      * 查询商品详情
@@ -217,4 +218,6 @@ public interface ProductMapper {
     CmHeheCollageProductPo findCollageProduct(Integer productID);
 
     Integer findProductBuyNum(@Param("productId") Integer productId, @Param("userId") Integer userId);
+
+    BigDecimal getDiscountPrice(Integer productId);
 }

+ 2 - 2
src/main/java/com/caimei/mapper/ShoppingCartMapper.java

@@ -65,7 +65,7 @@ public interface ShoppingCartMapper {
      * @param userId
      * @return
      */
-    List<CartProductVo> findByShopCartProduct(@Param("shopId") Integer shopId, @Param("userId") Integer userId);
+    List<CartProductVo> getCartProductsByShopId(@Param("shopId") Integer shopId, @Param("userId") Integer userId);
 
     /**
      * 查询失效购物车商品
@@ -73,7 +73,7 @@ public interface ShoppingCartMapper {
      * @param userId
      * @return
      */
-    List<CartProductVo> findExpiredGoods(Integer userId);
+    List<CartProductVo> getInvalidProducts(Integer userId);
 
     /**
      * 删除购物车

+ 3 - 1
src/main/java/com/caimei/model/po/ReceiveCouponPo.java

@@ -15,6 +15,8 @@ import java.util.Date;
  */
 @Data
 public class ReceiveCouponPo implements Serializable {
+    private Integer receiveCouponId;
+
     /**
      * 优惠券id
      */
@@ -31,7 +33,7 @@ public class ReceiveCouponPo implements Serializable {
     private Integer userId;
 
     /**
-     * 领取渠道:1小程序,2订单退回
+     * 领取渠道:1小程序,2订单退回,3领券购买(取消订单时取消领取)
      */
     private Integer source;
 

+ 35 - 0
src/main/java/com/caimei/model/vo/CartProductVo.java

@@ -40,6 +40,11 @@ public class CartProductVo implements Serializable {
      */
     private String mainImage;
 
+    /**
+     * 市场价
+     */
+    private BigDecimal normalPrice;
+
     /**
      * 机构价
      */
@@ -99,5 +104,35 @@ public class CartProductVo implements Serializable {
      * 拼团状态:0没有拼团价,1拼团价价
      */
     private Integer collageStatus = 0;
+
+    /**
+     * 限时特价状态:0没有限时特价,1限时特价
+     */
+    private Integer discountStatus = 0;
+
+    /**
+     * 优惠券标签状态:0无可用优惠券,1券后价,2优惠券信息
+     */
+    private Integer couponStatus = 0;
+
+    /**
+     * 券后价
+     */
+    private BigDecimal couponPrice = BigDecimal.ZERO;
+
+    /**
+     * 券后价对应优惠券id
+     */
+    private Integer couponId;
+
+    /**
+     * 原价券后价
+     */
+    private BigDecimal normalCouponPrice = BigDecimal.ZERO;
+
+    /**
+     * 单价未满足优惠条件,显示最小优惠条件的优惠券信息
+     */
+    private String couponInfo;
 }
 

+ 2 - 3
src/main/java/com/caimei/service/OrderSubmitService.java

@@ -15,16 +15,15 @@ public interface OrderSubmitService {
     /**
      * 商品结算
      *
-     *
      * @param userId
      * @param cartIds      购物车ID串用逗号隔开。
      * @param productId    立即购买商品ID
      * @param productCount 立即购买商品数量
-     * @param heUserId       用户id。
+     * @param heUserId     用户id。
      * @param collageFlag
      * @return
      */
-    ResponseJson<Map<String, Object>> orderConfirm(Integer userId, String cartIds, Integer productId, Integer productCount, Integer heUserId, Integer collageFlag);
+    ResponseJson<Map<String, Object>> orderConfirm(Integer userId, String cartIds, Integer productId, Integer productCount, Integer heUserId, Integer couponId, Integer collageFlag);
 
     /**
      * 提交订单

+ 16 - 16
src/main/java/com/caimei/service/ProductService.java

@@ -24,7 +24,7 @@ public interface ProductService {
      */
     ResponseJson<List<CmHeHeImageVo>> carouselFigure();
 
-    /**
+/*    *//**
      * 商品列表
      *
      * @param name     商品名称
@@ -32,44 +32,44 @@ public interface ProductService {
      * @param pageNum
      * @param pageSize
      * @return
-     */
+     *//*
     ResponseJson<PageInfo<ProductVo>> productList(String name, Integer userId, Integer pageNum, Integer pageSize);
 
-    /**
+    *//**
      * 商品详情
      *
      * @param productId 商品id
      * @return
-     */
+     *//*
     ResponseJson<ProductVo> productDetails(Integer productId, Integer userId);
 
-    /**
+    *//**
      * 商品搜索历史记录
      *
      * @param userId 用户id
      * @return
-     */
+     *//*
     ResponseJson<List<String>> searchHistory(Integer userId);
 
-    /**
+    *//**
      * 删除搜索历史记录
      *
      * @param userId 用户id
      * @return
-     */
+     *//*
     ResponseJson<String> deleteHistory(Integer userId);
 
-    /**
+    *//**
      * 活动专区
      *
      * @param userId   分销者id
      * @param pageNum
      * @param pageSize
      * @return
-     */
+     *//*
     ResponseJson<PageInfo<HeHeActivityVo>> activityArea(Integer userId, Integer pageNum, Integer pageSize);
 
-    /**
+    *//**
      * 活动详情
      *
      * @param userId     分销者id
@@ -77,15 +77,15 @@ public interface ProductService {
      * @param pageNum
      * @param pageSize
      * @return
-     */
+     *//*
     ResponseJson<Map<String, Object>> activityDetails(Integer userId, Integer activityId, Integer pageNum, Integer pageSize);
 
 
-    /**
+    *//**
      * 商品楼层
      * @return
-     */
-    ResponseJson<List<FloorVo>> productFloor(Integer userId);
+     *//*
+    ResponseJson<List<FloorVo>> productFloor(Integer userId);*/
 
     /**
      * 楼层详情
@@ -94,7 +94,7 @@ public interface ProductService {
      * @param productName
      * @return
      */
-    ResponseJson<PageInfo<ProductVo>> floorDetail(Integer floorId, Integer userId, String productName, Integer pageNum, Integer pageSize);
+//    ResponseJson<PageInfo<ProductVo>> floorDetail(Integer floorId, Integer userId, String productName, Integer pageNum, Integer pageSize);
 
     /**
      * 设置商品详情

+ 3 - 4
src/main/java/com/caimei/service/impl/CouponServiceImpl.java

@@ -158,13 +158,12 @@ public class CouponServiceImpl implements CouponService {
     public ResponseJson<List<CouponVo>> productDetailCoupons(Integer productId, Integer userId) {
         Date registerTime = couponMapper.getUserRegisterTime(userId);
         // 所有优惠券列表
-        List<CouponVo> totalCouponList = new ArrayList<>();
-        // 可领取优惠券列表
         String[] productArr = new String[1];
         productArr[0] = productId + "";
+        // 可领取优惠券列表
         List<CouponVo> couponList = couponMapper.findCouponList(userId, productArr, registerTime,null);
-        totalCouponList.addAll(couponList);
-        // 已领取优惠券列表
+        List<CouponVo> totalCouponList = new ArrayList<>(couponList);
+        // 已领取未使用优惠券列表
         List<CouponVo> receiveCouponList;
         if (null != userId && userId > 0) {
             receiveCouponList = couponMapper.findReceiveCouponList(userId, productArr, null, 1);

+ 15 - 2
src/main/java/com/caimei/service/impl/OrderServiceImpl.java

@@ -2,10 +2,12 @@ package com.caimei.service.impl;
 
 import com.alibaba.fastjson.JSONArray;
 import com.caimei.mapper.CollageMapper;
+import com.caimei.mapper.CouponMapper;
 import com.caimei.mapper.OrderMapper;
 import com.caimei.mapper.ProductMapper;
 import com.caimei.model.ResponseJson;
 import com.caimei.model.po.CmPayShopRecordPo;
+import com.caimei.model.po.ReceiveCouponPo;
 import com.caimei.model.po.UserSearchHistoryPo;
 import com.caimei.model.vo.*;
 import com.caimei.service.OrderService;
@@ -42,6 +44,8 @@ public class OrderServiceImpl implements OrderService {
     @Resource
     private ProductMapper productMapper;
     @Resource
+    private CouponMapper couponMapper;
+    @Resource
     private ProductService productService;
     @Value("${caimei.oldapi}")
     private String domain;
@@ -297,8 +301,17 @@ public class OrderServiceImpl implements OrderService {
         order.setStatus("6");
         orderMapper.cancelOrder(orderId,"用户主动取消订单");
         Integer receiveCouponId = orderMapper.findOrderCoupon(orderId);
-        //取消订单回退优惠券
-        orderMapper.revertCoupon(receiveCouponId);
+        ReceiveCouponPo receiveCoupon = couponMapper.getReceiveCouponById(receiveCouponId);
+        if (null != receiveCoupon) {
+            if (2 == receiveCoupon.getSource()) {
+                // 取消订单回退优惠券
+                orderMapper.revertCoupon(receiveCouponId);
+            }
+            if (3 == receiveCoupon.getSource()) {
+                // 领券购买取消订单撤销领取
+                couponMapper.deleteReceiveCoupon(receiveCouponId);
+            }
+        }
         return ResponseJson.success(null);
     }
 

+ 45 - 8
src/main/java/com/caimei/service/impl/OrderSubmitServiceImpl.java

@@ -14,6 +14,7 @@ import com.caimei.util.OrderNoUtils;
 import com.caimei.util.ProductUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -24,6 +25,7 @@ import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -49,7 +51,7 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
     private String domain;
 
     @Override
-    public ResponseJson<Map<String, Object>> orderConfirm(Integer userId, String cartIds, Integer productId, Integer productCount, Integer heUserId, Integer collageFlag) {
+    public ResponseJson<Map<String, Object>> orderConfirm(Integer userId, String cartIds, Integer productId, Integer productCount, Integer heUserId, Integer couponId, Integer collageFlag) {
         Map<String, Object> confirmData = new HashMap<>(5);
         log.info("<<<<< 结算订单 >>>>>");
         //商品总金额
@@ -86,7 +88,31 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
             totalPrice.set(MathUtil.add(totalPrice.get(), shopTotalPrice));
         });
         // 用户所有可用优惠券
-        List<CouponVo> receiveCouponList = couponMapper.findReceiveCouponList(userId, null,null, 1);
+        List<CouponVo> receiveCouponList = couponMapper.findReceiveCouponList(userId, null, null, 1);
+        if (null != couponId) {
+            // 领券购买对应优惠券领取标识
+            AtomicBoolean couponReceiveFlag = new AtomicBoolean(false);
+            receiveCouponList.forEach(coupon->{
+                if (couponId.equals(coupon.getCouponId())) {
+                    couponReceiveFlag.set(true);
+                }
+            });
+            if (!couponReceiveFlag.get()) {
+                // 未领取,自动领取
+                ReceiveCouponPo receiveCoupon = new ReceiveCouponPo();
+                receiveCoupon.setCouponId(couponId);
+                receiveCoupon.setUserId(userId);
+                receiveCoupon.setSource(3);
+                receiveCoupon.setStatus(1);
+                receiveCoupon.setReceiveTime(new Date());
+                receiveCoupon.setDelFlag(0);
+                couponMapper.insertReceiveCoupon(receiveCoupon);
+                // 重新查询已领取优惠券列表
+                receiveCouponList = couponMapper.findReceiveCouponList(userId, null, null, 1);
+                // 再将该领取的优惠券删除,未提交订单不算真正领取
+                couponMapper.deleteReceiveCoupon(receiveCoupon.getReceiveCouponId());
+            }
+        }
         receiveCouponList.forEach(coupon -> {
             // 能参与该优惠券的总金额
             BigDecimal touchCouponAmount = BigDecimal.ZERO;
@@ -120,10 +146,10 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
         });
         receiveCouponList.sort((o1, o2) -> {
             // 按满足优惠条件/优惠金额进行排序
-            if (!o1.getTouchFlag().equals(o2.getTouchFlag()) ) {
+            if (!o1.getTouchFlag().equals(o2.getTouchFlag())) {
                 return o1.getTouchFlag() - o2.getTouchFlag();
             } else {
-                return o1.getCouponAmount().compareTo(o2.getCouponAmount());
+                return o2.getCouponAmount().compareTo(o1.getCouponAmount());
             }
         });
         // 可参与的分享减免活动
@@ -251,10 +277,10 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
                     BigDecimal addedValueTax = new BigDecimal(0);
                     int priceType = 0;
                     //不含税可开票商品计算税费
-                    Integer activityId = productMapper.findActivityByProductId(product.getProductId());
+                    Integer activityId = productMapper.getActivityIdByProductId(product.getProductId());
                     if (activityId != null && activityId > 0) {
                         //活动阶梯
-                        List<ActivityLadderVo> ladderList = productMapper.findActivityLadder(activityId, product.getProductId());
+                        List<ActivityLadderVo> ladderList = productMapper.getActivityLadderList(activityId, product.getProductId());
                         if (ladderList != null && ladderList.size() > 0) {
                             priceType = 1;
                             for (ActivityLadderVo ladder : ladderList) {
@@ -329,6 +355,17 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
             List<CouponVo> receiveCouponList = null;
             if (null != receiveCouponId) {
                 receiveCouponList = couponMapper.findReceiveCouponList(userId, null, receiveCouponId, 1);
+            }else {
+                // 商品详情领券购买,提交订单时自动领券并使用
+                ReceiveCouponPo receiveCoupon = new ReceiveCouponPo();
+                receiveCoupon.setCouponId(couponId);
+                receiveCoupon.setUserId(userId);
+                receiveCoupon.setSource(3);
+                receiveCoupon.setStatus(1);
+                receiveCoupon.setReceiveTime(new Date());
+                receiveCoupon.setDelFlag(0);
+                couponMapper.insertReceiveCoupon(receiveCoupon);
+                receiveCouponList = couponMapper.findReceiveCouponList(userId, null, receiveCoupon.getReceiveCouponId(), 1);
             }
             if (receiveCouponList == null || receiveCouponList.size() <= 0) {
                 // 设置手动回滚事务
@@ -458,9 +495,9 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
             log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>保存订单商品(insert[cm_order_product])OrderProductID:" + orderProduct.getOrderProductID());
             if ("1".equals(orderProduct.getLadderPriceFlag())) {
                 //使用阶梯价格的订单商品保存下单时的阶梯价格列表
-                Integer activityId = productMapper.findActivityByProductId(orderProduct.getProductID());
+                Integer activityId = productMapper.getActivityIdByProductId(orderProduct.getProductID());
                 if (activityId != null && activityId > 0) {
-                    List<ActivityLadderVo> ladderPriceList = productMapper.findActivityLadder(activityId, orderProduct.getProductID());
+                    List<ActivityLadderVo> ladderPriceList = productMapper.getActivityLadderList(activityId, orderProduct.getProductID());
                     int ladderNum = 0;
                     for (ActivityLadderVo ladder : ladderPriceList) {
                         ladderNum++;

+ 6 - 11
src/main/java/com/caimei/service/impl/ProductServiceImpl.java

@@ -3,15 +3,10 @@ package com.caimei.service.impl;
 import com.caimei.mapper.ProductMapper;
 import com.caimei.model.ResponseJson;
 import com.caimei.model.po.CmHeheCollageProductPo;
-import com.caimei.model.po.ProductDetailInfoPo;
-import com.caimei.model.po.UserSearchHistoryPo;
 import com.caimei.model.vo.*;
 import com.caimei.service.ProductService;
 import com.caimei.util.MathUtil;
 import com.caimei.util.ProductUtils;
-import com.github.pagehelper.PageHelper;
-import com.github.pagehelper.PageInfo;
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -38,7 +33,7 @@ public class ProductServiceImpl implements ProductService {
         return ResponseJson.success(images);
     }
 
-    @Override
+    /*@Override
     public ResponseJson<PageInfo<ProductVo>> productList(String name, Integer userId, Integer pageNum, Integer pageSize) {
         PageHelper.startPage(pageNum, pageSize);
         List<ProductVo> productList = productMapper.findProductList(name);
@@ -60,18 +55,18 @@ public class ProductServiceImpl implements ProductService {
             }
         }
         return ResponseJson.success(pageInfo);
-    }
+    }*/
 
     @Override
     public void setProductDetails(ProductVo product, Integer userId) {
         product.setMainImage(ProductUtils.getImageURL("product", product.getMainImage(), 0, domain));
-        Integer activityId = productMapper.findActivityByProductId(product.getProductId());
+        Integer activityId = productMapper.getActivityIdByProductId(product.getProductId());
         // 拼团商品
         CmHeheCollageProductPo collageProduct = productMapper.findCollageProduct(product.getProductId());
         if (activityId != null && activityId > 0) {
             product.setActiveStatus(1);
             //活动阶梯
-            List<ActivityLadderVo> ladderList = productMapper.findActivityLadder(activityId, product.getProductId());
+            List<ActivityLadderVo> ladderList = productMapper.getActivityLadderList(activityId, product.getProductId());
             if (ladderList != null && ladderList.size() > 0) {
                 ActivityLadderVo ladder = ladderList.get(0);
                 // 没有起订量概念,默认显示购买1个商品的价格,若第一阶梯为一个商品,则取第一阶梯时间
@@ -115,7 +110,7 @@ public class ProductServiceImpl implements ProductService {
         }
     }
 
-    @Override
+    /*@Override
     public ResponseJson<ProductVo> productDetails(Integer productId, Integer userId) {
         ProductDetailsVo product = productMapper.findProductByProductId(productId);
         if (product == null) {
@@ -239,5 +234,5 @@ public class ProductServiceImpl implements ProductService {
         productList.forEach(product -> setProductDetails(product, userId));
         PageInfo<ProductVo> pageInfo = new PageInfo<>(productList);
         return ResponseJson.success(pageInfo);
-    }
+    }*/
 }

+ 77 - 19
src/main/java/com/caimei/service/impl/ShoppingCartServiceImpl.java

@@ -1,5 +1,6 @@
 package com.caimei.service.impl;
 
+import com.caimei.mapper.CouponMapper;
 import com.caimei.mapper.OrderMapper;
 import com.caimei.mapper.ProductMapper;
 import com.caimei.mapper.ShoppingCartMapper;
@@ -17,9 +18,7 @@ import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * Description
@@ -35,6 +34,8 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
     private ProductMapper productMapper;
     @Resource
     private OrderMapper orderMapper;
+    @Resource
+    private CouponMapper couponMapper;
     @Value("${caimei.oldapi}")
     private String domain;
 
@@ -66,7 +67,7 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
         shopList.forEach(shop -> {
             shop.setLogo(ProductUtils.getImageURL("shopLogo", shop.getLogo(), 0, domain));
             BigDecimal shopTotalPrice = BigDecimal.ZERO;
-            List<CartProductVo> productList = shoppingCartMapper.findByShopCartProduct(shop.getShopId(), userId);
+            List<CartProductVo> productList = shoppingCartMapper.getCartProductsByShopId(shop.getShopId(), userId);
             for (CartProductVo product : productList) {
                 setPrice(product, userId, null);
                 shopTotalPrice = MathUtil.add(shopTotalPrice, MathUtil.mul(product.getProductCount(), product.getPrice()));
@@ -76,7 +77,7 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
         });
         map.put("shopList", shopList);
         //失效商品
-        List<CartProductVo> products = shoppingCartMapper.findExpiredGoods(userId);
+        List<CartProductVo> products = shoppingCartMapper.getInvalidProducts(userId);
         map.put("products", products);
         //商品数量
         Integer cartQuantity = shoppingCartMapper.getCartQuantity(userId);
@@ -88,20 +89,20 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
      * 设置活动价格阶梯
      */
     @Override
-    public void setPrice(CartProductVo product, Integer userId, Integer collageFlag) {
-        //税费
-        boolean addTaxFlag = ("0".equals(product.getIncludedTax()) && ("1".equals(product.getInvoiceType()) || "2".equals(product.getInvoiceType())));
-        // 活动id
-        Integer activityId = productMapper.findActivityByProductId(product.getProductId());
+    public void setPrice(CartProductVo product, Integer userId, Integer collageFlag) {// 活动id
+        Integer activityId = productMapper.getActivityIdByProductId(product.getProductId());
         // 拼团商品
         CmHeheCollageProductPo collageProduct = productMapper.findCollageProduct(product.getProductId());
+        // 限时特价
+        BigDecimal discountPrice = productMapper.getDiscountPrice(product.getProductId());
         if (activityId != null && activityId > 0) {
             product.setActiveStatus(1);
             //活动阶梯
-            List<ActivityLadderVo> ladderList = productMapper.findActivityLadder(activityId, product.getProductId());
+            List<ActivityLadderVo> ladderList = productMapper.getActivityLadderList(activityId, product.getProductId());
             if (ladderList != null && ladderList.size() > 0) {
                 for (ActivityLadderVo ladder : ladderList) {
                     if (product.getProductCount() >= ladder.getBuyNum()) {
+                        // 根据商品数量设置价格
                         product.setPrice(ladder.getBuyPrice());
                     }
                 }
@@ -110,29 +111,86 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
         } else if (null != collageProduct && null != collageFlag && 1 == collageFlag) {
             product.setCollageStatus(1);
             product.setPrice(collageProduct.getPrice());
+        } else if (discountPrice != null) {
+            // 限时特价
+            product.setPrice(discountPrice);
+            product.setDiscountStatus(1);
         }
         List<ActivityLadderVo> ladderList = product.getLadderList();
-        // 查询商品折扣,设置折扣价
+        // 内部优惠折扣
         Integer discount = productMapper.findProductDiscount(product.getProductId(), userId);
         if (null != discount && discount > 0) {
-            product.setPrice(MathUtil.div(MathUtil.mul(product.getPrice(), discount), 100));
+            product.setPrice(MathUtil.div(MathUtil.mul(product.getPrice(), discount), 100, 2));
+            if (null != product.getNormalPrice()) {
+                product.setNormalPrice(MathUtil.div(MathUtil.mul(product.getNormalPrice(), discount), 100, 2));
+            }
             if (null != ladderList) {
-                ladderList.forEach(ladder->{
-                    ladder.setBuyPrice((MathUtil.div(MathUtil.mul(ladder.getBuyPrice(), discount), 100)));
-                });
+                ladderList.forEach(ladder-> ladder.setBuyPrice((MathUtil.div(MathUtil.mul(ladder.getBuyPrice(), discount), 100, 2))));
             }
         }
+        //税费
+        boolean addTaxFlag = ("0".equals(product.getIncludedTax()) && ("1".equals(product.getInvoiceType()) || "2".equals(product.getInvoiceType())));
         // 为商品价格和阶梯列表添加税费
         if (addTaxFlag) {
             BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(product.getPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
-            product.setPrice(MathUtil.add(addedValueTax, product.getPrice()));
+            BigDecimal price = MathUtil.add(product.getPrice(), addedValueTax);
+            product.setPrice(price);
+            if (null != product.getNormalPrice()) {
+                addedValueTax = MathUtil.div(MathUtil.mul(product.getNormalPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
+                price = MathUtil.add(product.getNormalPrice(), addedValueTax);
+                product.setNormalPrice(price);
+            }
             if (null != ladderList) {
                 ladderList.forEach(ladder->{
                     BigDecimal ladderTax = MathUtil.div(MathUtil.mul(ladder.getBuyPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
-                    ladder.setBuyPrice(MathUtil.add(ladderTax, ladder.getBuyPrice()));
+                    ladder.setBuyPrice(MathUtil.add(ladder.getBuyPrice(), ladderTax));
                 });
             }
         }
+        // 查询用户的注册时间
+        Date registerTime = couponMapper.getUserRegisterTime(userId);
+        String[] productIdArr = {product.getProductId().toString()};
+        // 用户在该商品上可领取的优惠券列表
+        List<CouponVo> couponList = couponMapper.findCouponList(userId, productIdArr, registerTime, null);
+        if (null != userId) {
+            // 用户在该商品上已领取未使用的优惠券列表
+            List<CouponVo> receiveCouponList = couponMapper.findReceiveCouponList(userId, productIdArr, null, 1);
+            couponList.addAll(receiveCouponList);
+        }
+        // 单价满足优惠条件的优惠券列表
+        List<CouponVo> ableCouponList = new ArrayList<>();
+        // 单价不满足优惠条件的优惠券列表
+        List<CouponVo> unableCouponList = new ArrayList<>();
+        if (couponList.size() > 0) {
+            // 有可用优惠券
+            couponList.forEach(coupon->{
+                if (1 == coupon.getNoThresholdFlag() || MathUtil.compare(product.getPrice(), coupon.getTouchPrice()) > 0) {
+                    ableCouponList.add(coupon);
+                } else {
+                    unableCouponList.add(coupon);
+                }
+            });
+            if (ableCouponList.size() > 0) {
+                // 根据优惠金额排序,优惠金额大的排前面
+                ableCouponList.sort((o1, o2) -> o2.getCouponAmount().compareTo(o1.getCouponAmount()));
+                CouponVo biggestCoupon = ableCouponList.get(0);
+                // 现价-最大优惠金额=券后价
+                product.setCouponPrice(MathUtil.sub(product.getPrice(), biggestCoupon.getCouponAmount()));
+                // 原价-最大优惠金额=原价券后价
+                product.setNormalCouponPrice(MathUtil.sub(product.getNormalPrice(), biggestCoupon.getCouponAmount()));
+                // 优惠券id
+                product.setCouponId(biggestCoupon.getCouponId());
+                // 券后价标签
+                product.setCouponStatus(1);
+            } else {
+                // 根据优惠条件排序,条件小的排前面
+                unableCouponList.sort(Comparator.comparing(CouponVo::getTouchPrice));
+                // 优惠券信息
+                product.setCouponStatus(2);
+                CouponVo smallestCoupon = unableCouponList.get(0);
+                product.setCouponInfo("券|满" + smallestCoupon.getTouchPrice() + "元减" + smallestCoupon.getCouponAmount());
+            }
+        }
     }
 
     @Override
@@ -149,7 +207,7 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
                 cart.setProductCount(orderProduct.getNum());
                 Integer heUserId = orderProduct.getHeUserId();
                 if (heUserId != null && heUserId > 0) {
-                    Integer activityId = productMapper.findActivityByProductId(orderProduct.getProductId());
+                    Integer activityId = productMapper.getActivityIdByProductId(orderProduct.getProductId());
                     if (activityId != null && activityId > 0) {
                         cart.setHeUserId(heUserId);
                     } else {

+ 16 - 1
src/main/resources/mapper/CouponMapper.xml

@@ -7,7 +7,7 @@
         insert into cm_hehe_coupon_display_record (userId, couponId, addTime)
         values (#{userId}, #{couponId}, NOW())
     </insert>
-    <insert id="insertReceiveCoupon">
+    <insert id="insertReceiveCoupon" useGeneratedKeys="true" keyColumn="id" keyProperty="receiveCouponId">
         insert into cm_hehe_receive_coupon (userId, couponId, couponShareId, source, status, receiveTime, delFlag)
         values (#{userId}, #{couponId}, #{couponShareId}, #{source}, #{status}, #{receiveTime}, #{delFlag})
     </insert>
@@ -18,6 +18,9 @@
     <update id="updateSmsSendCount">
         UPDATE cm_sms_statistics SET sendNum = (sendNum + #{count}) WHERE markId = #{markId}
     </update>
+    <delete id="deleteReceiveCoupon">
+        delete from cm_hehe_receive_coupon where id = #{receiveCouponId}
+    </delete>
     <select id="getCouponCount" resultType="java.lang.Integer">
         select count(*) from cm_hehe_coupon
         where couponType = #{couponType}
@@ -226,5 +229,17 @@
     <select id="getUserMobile" resultType="java.lang.String">
         select mobile from cm_hehe_user where userId = #{shareUserId}
     </select>
+    <select id="getReceiveCouponById" resultType="com.caimei.model.po.ReceiveCouponPo">
+        select id as receiveCouponId,
+               couponId,
+               couponShareId,
+               userId,
+               source,
+               status,
+               receiveTime,
+               useTime
+        from cm_hehe_receive_coupon
+        where id = #{receiveCouponId} and delFlag = 0
+    </select>
 
 </mapper>

+ 10 - 2
src/main/resources/mapper/ProductMapper.xml

@@ -73,7 +73,7 @@
     </select>
 
 
-    <select id="findActivityByProductId" resultType="integer">
+    <select id="getActivityIdByProductId" resultType="integer">
         SELECT
             activityId
         FROM
@@ -87,7 +87,7 @@
           AND a.productId = #{productId}
     </select>
 
-    <select id="findActivityLadder" resultType="com.caimei.model.vo.ActivityLadderVo">
+    <select id="getActivityLadderList" resultType="com.caimei.model.vo.ActivityLadderVo">
         SELECT
             buyNum,
             buyPrice
@@ -323,5 +323,13 @@
             left join cm_order_product cop on chcm.orderId = cop.orderID
         where chc.productId = #{productId} and chcm.userId = #{userId}
     </select>
+    <select id="getDiscountPrice" resultType="java.math.BigDecimal">
+        select a.discountPrice
+        from hehe_discount_activity_product a
+                 left join cm_hehe_discount_activity chda on a.activityId = chda.id
+        where a.productId = #{productId}
+          and a.status = 1
+          and NOW() between chda.onlineTime and chda.offlineTime
+    </select>
 
 </mapper>

+ 2 - 2
src/main/resources/mapper/ShoppingCartMapper.xml

@@ -72,7 +72,7 @@
 
     </select>
 
-    <select id="findByShopCartProduct" resultType="com.caimei.model.vo.CartProductVo">
+    <select id="getCartProductsByShopId" resultType="com.caimei.model.vo.CartProductVo">
         SELECT
           cc.cm_cartID AS cartId,
           cc.productID AS productId,
@@ -96,7 +96,7 @@
           AND p.shopID = #{shopId}
     </select>
 
-    <select id="findExpiredGoods" resultType="com.caimei.model.vo.CartProductVo">
+    <select id="getInvalidProducts" resultType="com.caimei.model.vo.CartProductVo">
         SELECT
           cc.cm_cartID AS cartId,
           cc.productID AS productId,