浏览代码

购物车Part3

chao 5 年之前
父节点
当前提交
a906e11d99

+ 10 - 1
src/main/java/com/caimei/www/controller/unlimited/ProductController.java

@@ -2,6 +2,7 @@ package com.caimei.www.controller.unlimited;
 
 import com.caimei.www.controller.BaseController;
 import com.caimei.www.pojo.JsonModel;
+import com.caimei.www.pojo.order.CartItem;
 import com.caimei.www.pojo.page.ProductDetail;
 import com.caimei.www.pojo.page.ProductList;
 import com.caimei.www.pojo.page.Parameter;
@@ -82,7 +83,15 @@ public class ProductController extends BaseController {
         return productService.getProductParameters(productId);
     }
 
-
+    /**
+     * 详情-价格
+     * @return
+     */
+    @GetMapping("/product/price")
+    @ResponseBody
+    public JsonModel<CartItem> getProductPrice(Integer productId, Integer userId) {
+        return productService.getProductPrice(productId, userId);
+    }
 
 
 

+ 1 - 1
src/main/java/com/caimei/www/mapper/BaseDao.java

@@ -17,7 +17,7 @@ public interface BaseDao {
     /**
      * 获取搜索热门关键字
      */
-    public List<String> getSearchKeyword();
+    List<String> getSearchKeyword();
     /**
      * 获取头部菜单
      */

+ 35 - 0
src/main/java/com/caimei/www/mapper/CommonDao.java

@@ -0,0 +1,35 @@
+package com.caimei.www.mapper;
+
+import com.caimei.www.pojo.order.ActivityPrice;
+import com.caimei.www.pojo.order.LadderPrice;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/28
+ */
+@Mapper
+public interface CommonDao {
+
+    /**
+     * 根据商品ID查询活动价
+     * @return
+     */
+    ActivityPrice getActivityPriceByProductId(Integer productId);
+
+    /**
+     * 根据商品ID查询阶梯价
+     * @return
+     */
+    List<LadderPrice> getladderPricesByProductId(Integer productId);
+    /**
+     * 根据商品ID和用户ID 查询复购价
+     * @return
+     */
+    Double getRepurchasePrice(@Param("productId") Integer productId, @Param("userId") Integer userId);
+}

+ 8 - 0
src/main/java/com/caimei/www/mapper/ProductDao.java

@@ -1,5 +1,6 @@
 package com.caimei.www.mapper;
 
+import com.caimei.www.pojo.order.CartItem;
 import com.caimei.www.pojo.page.ProductDetail;
 import com.caimei.www.pojo.page.ProductList;
 import com.caimei.www.pojo.page.Parameter;
@@ -43,4 +44,11 @@ public interface ProductDao {
      * @return
      */
     List<String> getProductDetailImages(Integer productId);
+
+    /**
+     * 详情-价格
+     * @param productId
+     * @return
+     */
+    CartItem getPriceItemById(Integer productId);
 }

+ 0 - 16
src/main/java/com/caimei/www/mapper/ShoppingDao.java

@@ -36,20 +36,4 @@ public interface ShoppingDao {
      */
     List<CartItem> getShoppingCartBySupplierId(@Param("supplierId") Integer supplierId, @Param("userId") Integer userId);
 
-    /**
-     * 根据商品ID查询活动价
-     * @return
-     */
-    ActivityPrice getActivityPriceByProductId(Integer productId);
-
-    /**
-     * 根据商品ID查询阶梯价
-     * @return
-     */
-    List<LadderPrice> getladderPricesByProductId(Integer productId);
-    /**
-     * 根据商品ID和用户ID 查询复购价
-     * @return
-     */
-    Double getRepurchasePrice(@Param("productId") Integer productId, @Param("userId") Integer userId);
 }

+ 1 - 1
src/main/java/com/caimei/www/mapper/UserDao.java

@@ -1,6 +1,6 @@
 package com.caimei.www.mapper;
 
-import com.caimei.www.pojo.user.Account;
+import com.caimei.www.pojo.account.Account;
 import org.apache.ibatis.annotations.Mapper;
 
 /**

+ 1 - 1
src/main/java/com/caimei/www/pojo/user/Account.java → src/main/java/com/caimei/www/pojo/account/Account.java

@@ -1,4 +1,4 @@
-package com.caimei.www.pojo.user;
+package com.caimei.www.pojo.account;
 
 import lombok.Data;
 

+ 9 - 0
src/main/java/com/caimei/www/pojo/page/ProductDetail.java

@@ -1,8 +1,11 @@
 package com.caimei.www.pojo.page;
 
+import com.caimei.www.pojo.order.LadderPrice;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 
+import java.util.List;
+
 /**
  * Description
  *
@@ -40,4 +43,10 @@ public class ProductDetail extends ProductList {
 	private String shopTitle;
 	private String businessScope;
 	private String shopAddress;
+	/** 活动状态:1有效,0失效 */
+	private Integer activityFlag;
+	/** 启用阶梯价格标识:1是,0否  */
+	private Integer ladderFlag;
+	/** 阶梯价 */
+	List<LadderPrice> ladderPrices;
 }

+ 70 - 0
src/main/java/com/caimei/www/service/CommonServiceUtil.java

@@ -0,0 +1,70 @@
+package com.caimei.www.service;
+
+import com.caimei.www.mapper.CommonDao;
+import com.caimei.www.mapper.ShoppingDao;
+import com.caimei.www.pojo.order.ActivityPrice;
+import com.caimei.www.pojo.order.CartItem;
+import com.caimei.www.pojo.order.LadderPrice;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.stream.IntStream;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/28
+ */
+@Service
+public class CommonServiceUtil {
+    @Resource
+    private CommonDao commonDao;
+    /**
+     * 设置购物车商品价格
+     * @param cartItem
+     * @param userId
+     */
+    public void setCartItemPrice(CartItem cartItem, Integer userId) {
+        // 活动价
+        ActivityPrice activity = commonDao.getActivityPriceByProductId(cartItem.getProductId());
+        if (null != activity) {
+            cartItem.setPrice(activity.getPrice());
+            cartItem.setActivityFlag(1);
+            cartItem.setLadderFlag(0);
+        } else if (cartItem.getLadderFlag() == 1) {
+            // 阶梯价
+            List<LadderPrice> ladderPrices = commonDao.getladderPricesByProductId(cartItem.getProductId());
+            if (!CollectionUtils.isEmpty(ladderPrices)){
+                IntStream.range(0, ladderPrices.size()).forEach(i->{
+                    boolean isThisLadder;
+                    if (i == ladderPrices.size()-1) {
+                        ladderPrices.get(i).setMaxNum(0);
+                        isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum());
+                    } else {
+                        ladderPrices.get(i).setMaxNum(ladderPrices.get(i+1).getBuyNum());
+                        isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum() && cartItem.getNumber()<ladderPrices.get(i).getMaxNum());
+                    }
+                    if (isThisLadder){
+                        cartItem.setPrice(ladderPrices.get(i).getBuyPrice());
+                    }
+                });
+                cartItem.setMin(ladderPrices.get(0).getBuyNum());
+                cartItem.setLadderPrices(ladderPrices);
+            } else {
+                cartItem.setLadderFlag(0);
+            }
+        } else {
+            // 复购价
+            Double repurchase = commonDao.getRepurchasePrice(cartItem.getProductId(), userId);
+            if (null != repurchase && repurchase>0) {
+                cartItem.setPrice(repurchase);
+            }
+        }
+    }
+}
+
+

+ 10 - 0
src/main/java/com/caimei/www/service/ProductService.java

@@ -1,6 +1,7 @@
 package com.caimei.www.service;
 
 import com.caimei.www.pojo.JsonModel;
+import com.caimei.www.pojo.order.CartItem;
 import com.caimei.www.pojo.page.ProductDetail;
 import com.caimei.www.pojo.page.ProductList;
 import com.caimei.www.pojo.page.Parameter;
@@ -42,4 +43,13 @@ public interface ProductService {
      * @return
      */
     JsonModel<List<String>> getProductDetailImages(Integer productId);
+
+    /**
+     * 详情-价格
+     * @param productId
+     * @param userId
+     * @return
+     */
+    JsonModel<CartItem> getProductPrice(Integer productId, Integer userId);
+
 }

+ 27 - 2
src/main/java/com/caimei/www/service/impl/ProductServiceImpl.java

@@ -2,12 +2,15 @@ package com.caimei.www.service.impl;
 
 import com.caimei.www.pojo.JsonModel;
 import com.caimei.www.mapper.ProductDao;
+import com.caimei.www.pojo.order.CartItem;
 import com.caimei.www.pojo.page.ProductDetail;
 import com.caimei.www.pojo.page.ProductList;
 import com.caimei.www.pojo.page.Parameter;
+import com.caimei.www.service.CommonServiceUtil;
 import com.caimei.www.service.ProductService;
 import com.caimei.www.utils.ImageUtil;
 import io.netty.util.internal.StringUtil;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -22,10 +25,16 @@ import java.util.List;
  */
 @Service
 public class ProductServiceImpl implements ProductService {
-    @Resource
-    private ProductDao productDao;
     @Value("${caimei.wwwDomain}")
     private String domain;
+    @Resource
+    private ProductDao productDao;
+    @Resource
+    private CommonServiceUtil commonServiceUtil;
+    /*@Autowired
+    public void setCommonServiceUtil(CommonServiceUtil commonServiceUtil) {
+        this.commonServiceUtil = commonServiceUtil;
+    }*/
     /**
      * 根据商品Id获取详情
      *
@@ -101,4 +110,20 @@ public class ProductServiceImpl implements ProductService {
         }
 		return JsonModel.success(list);
     }
+
+    /**
+     * 详情-价格
+     *
+     * @param productId
+     * @param userId
+     * @return
+     */
+    @Override
+    public JsonModel<CartItem> getProductPrice(Integer productId, Integer userId) {
+        if (productId == null || userId == null || userId == 0) { return JsonModel.error("参数异常", null);}
+        CartItem priceItem = productDao.getPriceItemById(productId);
+        // 设置商品价格
+        commonServiceUtil.setCartItemPrice(priceItem, userId);
+		return JsonModel.success(priceItem);
+    }
 }

+ 9 - 49
src/main/java/com/caimei/www/service/impl/ShoppingServiceImpl.java

@@ -3,23 +3,20 @@ package com.caimei.www.service.impl;
 import com.caimei.www.mapper.ShoppingDao;
 import com.caimei.www.mapper.UserDao;
 import com.caimei.www.pojo.JsonModel;
-import com.caimei.www.pojo.order.ActivityPrice;
 import com.caimei.www.pojo.order.CartItem;
 import com.caimei.www.pojo.order.CartSupplier;
-import com.caimei.www.pojo.order.LadderPrice;
-import com.caimei.www.pojo.user.Account;
+import com.caimei.www.pojo.account.Account;
+import com.caimei.www.service.CommonServiceUtil;
 import com.caimei.www.service.ShoppingService;
 import com.caimei.www.utils.ImageUtil;
 import com.caimei.www.utils.PriceUtil;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.util.CollectionUtils;
 
 import javax.annotation.Resource;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
-import java.util.stream.IntStream;
 
 /**
  * Description
@@ -35,7 +32,12 @@ public class ShoppingServiceImpl implements ShoppingService {
     private ShoppingDao shoppingDao;
     @Resource
     private UserDao userDao;
-
+    @Resource
+    private CommonServiceUtil commonServiceUtil;
+    /*@Autowired
+    public void setCommonServiceUtil(CommonServiceUtil commonServiceUtil) {
+        this.commonServiceUtil = commonServiceUtil;
+    }*/
     /**
      * 顶部购物车数据
      *
@@ -76,7 +78,7 @@ public class ShoppingServiceImpl implements ShoppingService {
                     boolean priceVisible = (cartItem.getPriceFlag() == 0 || (cartItem.getPriceFlag() == 2 && account.getIdentity() ==2));
                     if (priceVisible) {
                         // 设置商品价格
-                        setCartItemPrice(cartItem, userId);
+                        commonServiceUtil.setCartItemPrice(cartItem, userId);
                         // 该供应商下价格累加
                         supplierPrice.updateAndGet(v -> v + PriceUtil.add(supplierPrice, cartItem.getPrice()).doubleValue());
                         itemCount.incrementAndGet();
@@ -124,46 +126,4 @@ public class ShoppingServiceImpl implements ShoppingService {
         return JsonModel.success(resultMap);
     }
 
-    /**
-     * 设置购物车商品价格
-     * @param cartItem
-     * @param userId
-     */
-    private void setCartItemPrice(CartItem cartItem, Integer userId) {
-        // 活动价
-        ActivityPrice activity = shoppingDao.getActivityPriceByProductId(cartItem.getProductId());
-        if (null != activity) {
-            cartItem.setPrice(activity.getPrice());
-            cartItem.setActivityFlag(1);
-            cartItem.setLadderFlag(0);
-        } else if (cartItem.getLadderFlag() == 1) {
-            // 阶梯价
-            List<LadderPrice> ladderPrices = shoppingDao.getladderPricesByProductId(cartItem.getProductId());
-            if (!CollectionUtils.isEmpty(ladderPrices)){
-                IntStream.range(0, ladderPrices.size()).forEach(i->{
-                    boolean isThisLadder;
-                    if (i == ladderPrices.size()-1) {
-                        ladderPrices.get(i).setMaxNum(0);
-                        isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum());
-                    } else {
-                        ladderPrices.get(i).setMaxNum(ladderPrices.get(i+1).getBuyNum());
-                        isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum() && cartItem.getNumber()<ladderPrices.get(i).getMaxNum());
-                    }
-                    if (isThisLadder){
-                        cartItem.setPrice(ladderPrices.get(i).getBuyPrice());
-                    }
-                });
-                cartItem.setMin(ladderPrices.get(0).getBuyNum());
-                cartItem.setLadderPrices(ladderPrices);
-            } else {
-                cartItem.setLadderFlag(0);
-            }
-        } else {
-            // 复购价
-            Double repurchase = shoppingDao.getRepurchasePrice(cartItem.getProductId(), userId);
-            if (null != repurchase && repurchase>0) {
-                cartItem.setPrice(repurchase);
-            }
-        }
-    }
 }

+ 34 - 0
src/main/resources/mapper/CommonMapper.xml

@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.caimei.www.mapper.CommonDao">
+	<select id="getActivityPriceByProductId" resultType="com.caimei.www.pojo.order.ActivityPrice">
+        select	ca.id,
+        		cpa.productId,
+        		cpa.actPrice as price,
+        		ca.startTime as beginTime,
+        		ca.endTime as endTime
+        from cm_product_activity cpa
+        left join cm_activity ca on cpa.activityId = ca.id
+        where cpa.productId = #{productId}
+        and ca.startTime <![CDATA[ <= ]]> NOW()
+        and NOW() <![CDATA[ <= ]]> ca.endTime
+        and cpa.delFlag = 0
+	</select>
+	<select id="getladderPricesByProductId" resultType="com.caimei.www.pojo.order.LadderPrice">
+        select
+        	id, productId, ladderNum, buyNum, buyPrice
+        from product_ladder_price
+        where productId = #{productId} and userType = 3 and delFlag = 0
+        order by ladderNum asc
+	</select>
+	<select id="getRepurchasePrice" resultType="java.lang.Double">
+        select
+          r.currentPrice
+        from repeat_purchase_price r
+        left join product p on p.productID = r.productId
+        where r.productId = #{productId} and userId = #{userId}
+        and ((p.costCheckFlag=1 and r.currentPrice <![CDATA[ >= ]]> p.costPrice) or p.costCheckFlag=0)
+        and r.delFlag = 0
+	</select>
+
+</mapper>

+ 14 - 0
src/main/resources/mapper/ProductMapper.xml

@@ -91,6 +91,20 @@
 		where productID = #{productId}
 		order by mainFlag desc ,sortIndex is null,sortIndex
 	</select>
+    <select id="getPriceItemById" resultType="com.caimei.www.pojo.order.CartItem">
+		select
+			p.productID as productId,
+			p.price1 as price,
+			p.unit as unit,
+			p.step as step,
+			p.minBuyNumber as min,
+			p.price1TextFlag as priceFlag,
+			p.ladderPriceFlag as ladderFlag,
+			p.validFlag as validFlag,
+			p.stock as stock
+		from product p
+		where p.productID = #{productId}
+	</select>
 
 
 </mapper>

+ 0 - 29
src/main/resources/mapper/ShoppingDao.xml → src/main/resources/mapper/ShoppingMapper.xml

@@ -49,34 +49,5 @@
 		where p.shopID = #{supplierId} and c.userID = #{userId}
 		order by c.addTime desc
 	</select>
-	<select id="getActivityPriceByProductId" resultType="com.caimei.www.pojo.order.ActivityPrice">
-        select	ca.id,
-        		cpa.productId,
-        		cpa.actPrice as price,
-        		ca.startTime as beginTime,
-        		ca.endTime as endTime
-        from cm_product_activity cpa
-        left join cm_activity ca on cpa.activityId = ca.id
-        where cpa.productId = #{productId}
-        and ca.startTime <![CDATA[ <= ]]> NOW()
-        and NOW() <![CDATA[ <= ]]> ca.endTime
-        and cpa.delFlag = 0
-	</select>
-	<select id="getladderPricesByProductId" resultType="com.caimei.www.pojo.order.LadderPrice">
-        select
-        	id, productId, ladderNum, buyNum, buyPrice
-        from product_ladder_price
-        where productId = #{productId} and userType = 3 and delFlag = 0
-        order by ladderNum asc
-	</select>
-	<select id="getRepurchasePrice" resultType="java.lang.Double">
-        select
-          r.currentPrice
-        from repeat_purchase_price r
-        left join product p on p.productID = r.productId
-        where r.productId = #{productId} and userId = #{userId}
-        and ((p.costCheckFlag=1 and r.currentPrice <![CDATA[ >= ]]> p.costPrice) or p.costCheckFlag=0)
-        and r.delFlag = 0
-	</select>
 
 </mapper>

+ 1 - 1
src/main/resources/mapper/UserMapper.xml

@@ -7,7 +7,7 @@
         		email as email,
         		password as password,
         		userIdentity as identity
-        from account
+        from user
         where userID = #{userId}
 	</select>
 </mapper>