Forráskód Böngészése

购物车数据init

chao 4 éve
szülő
commit
5b01c09369

+ 3 - 11
pom.xml

@@ -80,26 +80,18 @@
             <scope>test</scope>
         </dependency>
 
-        <!--引入商品模块-->
         <dependency>
-            <groupId>com.caimei.module</groupId>
-            <artifactId>product</artifactId>
-            <version>0.0.1-SNAPSHOT</version>
+            <groupId>com.github.pagehelper</groupId>
+            <artifactId>pagehelper-spring-boot-starter</artifactId>
+            <version>1.2.13</version>
         </dependency>
 
-
-
-
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.6</version>
         </dependency>
 
-
-
-
-
     </dependencies>
 
 

+ 24 - 0
src/main/java/com/caimei/www/controller/authorized/UserController.java

@@ -0,0 +1,24 @@
+package com.caimei.www.controller.authorized;
+
+import com.caimei.www.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/24
+ */
+@Controller
+public class UserController {
+
+    private UserService userService;
+    @Autowired
+    public void setUserService(UserService userService) {
+        this.userService = userService;
+    }
+
+
+
+}

+ 19 - 0
src/main/java/com/caimei/www/mapper/ShoppingCartDao.java

@@ -1,7 +1,9 @@
 package com.caimei.www.mapper;
 
+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 org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -33,4 +35,21 @@ public interface ShoppingCartDao {
      * @return
      */
     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);
 }

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

@@ -0,0 +1,20 @@
+package com.caimei.www.mapper;
+
+import com.caimei.www.pojo.user.User;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/24
+ */
+@Mapper
+public interface UserDao {
+    /**
+     * 根据Id查找用户
+     * @param userId
+     * @return
+     */
+    User getUserById(Integer userId);
+}

+ 28 - 0
src/main/java/com/caimei/www/pojo/order/ActivityPrice.java

@@ -0,0 +1,28 @@
+package com.caimei.www.pojo.order;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/24
+ */
+@Data
+public class ActivityPrice implements Serializable {
+    /** 活动id */
+    private Integer id;
+    /** 商品id */
+    private Integer productId;
+    /** 活动价格 */
+    private Double price;
+	/** 活动开始时间 */
+	private Date beginTime;
+	/** 活动开始时间 */
+	private Date endTime;
+
+    private static final long serialVersionUID = 1L;
+}

+ 20 - 0
src/main/java/com/caimei/www/pojo/order/CartItem.java

@@ -3,6 +3,7 @@ package com.caimei.www.pojo.order;
 import lombok.Data;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * 顶部购物车数据(简洁数据)
@@ -13,12 +14,31 @@ import java.io.Serializable;
 @Data
 public class CartItem implements Serializable {
 	private Integer id;
+	private Integer productId;
 	private String name;
 	private String image;
 	private Double price;
 	private String unit;
+	/** 增量 */
 	private Integer step;
+	/** 起订量 */
+	private Integer min;
+	/** 商品上架状态:0逻辑删除 1待审核 2已上架 3已下架 8审核未通过 9已冻结 */
+	private Integer validFlag;
+	/** 价格可见度:0公开价格 1不公开价格 2仅对会员机构公开   */
+	private Integer priceFlag;
+	/** 活动状态:1有效,0失效 */
+	private Integer activityFlag;
+	/** 启用阶梯价格标识:1是,0否  */
+	private Integer ladderFlag;
+	/** 库存 */
+	private Integer stock;
+	/** 购买数量 */
 	private Integer number;
+	/** 购物车失效状态:0有效,1后台删除的,2冻结的,3下架,4售罄 >7库存不足,5价格仅会员可见,6未公开价格 */
+	private Integer status;
+	/** 阶梯价 */
+	List<LadderPrice> ladderPrices;
 
     private static final long serialVersionUID = 1L;
 }

+ 2 - 1
src/main/java/com/caimei/www/pojo/order/CartSupplier.java

@@ -1,6 +1,5 @@
 package com.caimei.www.pojo.order;
 
-import com.caimei.www.pojo.page.BaseLink;
 import lombok.Data;
 
 import java.io.Serializable;
@@ -17,6 +16,8 @@ public class CartSupplier implements Serializable {
 	private Integer id;
 	private String name;
 	private String logo;
+	private Integer count;
+	private Double totalPrice;
 	private List<CartItem> cartList;
 
     private static final long serialVersionUID = 1L;

+ 28 - 0
src/main/java/com/caimei/www/pojo/order/LadderPrice.java

@@ -0,0 +1,28 @@
+package com.caimei.www.pojo.order;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/24
+ */
+@Data
+public class LadderPrice implements Serializable {
+    private Integer id;
+    /** 商品id */
+    private Integer productId;
+    /** 第几阶梯 */
+    private Integer ladderNum;
+    /** 购买数量 */
+    private Integer buyNum;
+    /** 购买价格 */
+    private Double buyPrice;
+    /** 下一阶数量 */
+    private Integer maxNum;
+
+    private static final long serialVersionUID = 1L;
+}

+ 23 - 0
src/main/java/com/caimei/www/pojo/user/User.java

@@ -0,0 +1,23 @@
+package com.caimei.www.pojo.user;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/24
+ */
+@Data
+public class User implements Serializable {
+    private Integer id;
+    private Integer mobile;
+    private String email;
+    private String password;
+    /** 用户身份: 0个人,1协销,2会员机构,3供应商,4普通机构 */
+    private Integer identity;
+
+    private static final long serialVersionUID = 1L;
+}

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

@@ -0,0 +1,10 @@
+package com.caimei.www.service;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/24
+ */
+public interface UserService {
+}

+ 101 - 1
src/main/java/com/caimei/www/service/impl/ShoppingCartServiceImpl.java

@@ -1,17 +1,25 @@
 package com.caimei.www.service.impl;
 
 import com.caimei.www.mapper.ShoppingCartDao;
+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.User;
 import com.caimei.www.service.ShoppingCartService;
 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.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.IntStream;
 
 /**
  * Description
@@ -25,6 +33,8 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
     private String domain;
     @Resource
     private ShoppingCartDao shoppingCartDao;
+    @Resource
+    private UserDao userDao;
 
     /**
      * 顶部购物车数据
@@ -49,10 +59,100 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
     public JsonModel<List<CartSupplier>> getShoppingCarts(Integer userId) {
         if (userId == null || userId == 0) { return JsonModel.error("参数异常", null);}
         List<CartSupplier> cartSuppliers = shoppingCartDao.getCartSuppliers(userId);
+        User user = userDao.getUserById(userId);
         cartSuppliers.forEach(supplier -> {
             List<CartItem> cartItems = shoppingCartDao.getShoppingCartBySupplierId(supplier.getId(), userId);
-            supplier.setCartList(null);
+            AtomicReference<Double> supplierPrice = new AtomicReference<>(0d);
+            AtomicInteger itemCount = new AtomicInteger();
+            cartItems.forEach(cartItem -> {
+                cartItem.setImage(ImageUtil.getImageURL("product", cartItem.getImage(), 0, domain));
+                if (cartItem.getValidFlag() == 2) {
+                    // 已上架
+                    cartItem.setStatus(0);
+                    // 价格是否可见
+                    boolean priceVisible = (cartItem.getPriceFlag() == 0 || (cartItem.getPriceFlag() == 2 && user.getIdentity() ==2));
+                    if (priceVisible) {
+                        // 设置商品价格
+                        setCartItemPrice(cartItem, userId);
+                        // 该供应商下价格累加
+                        supplierPrice.updateAndGet(v -> v + PriceUtil.add(supplierPrice, cartItem.getPrice()).doubleValue());
+                        itemCount.incrementAndGet();
+                    } else {
+                        // 失效商品
+                        if (cartItem.getPriceFlag() == 1){
+                            // 未公开价格
+                            cartItem.setStatus(6);
+                        } else if (cartItem.getPriceFlag() == 2) {
+                            // 价格仅会员可见
+                            cartItem.setStatus(5);
+                        }
+                    }
+                } else {
+                    // 失效商品
+                    if (cartItem.getValidFlag() == 0) {
+                        // 后台逻辑删除
+                        cartItem.setStatus(1);
+                    } else if (cartItem.getValidFlag() == 9) {
+                        // 已冻结
+                        cartItem.setStatus(2);
+                    } else if (cartItem.getValidFlag() == 3) {
+                        // 已下架
+                        cartItem.setStatus(3);
+                    } else if (cartItem.getStock() == null || cartItem.getStock() == 0) {
+                        // 售罄
+                        cartItem.setStatus(4);
+                    } else if (cartItem.getStock() != null && (cartItem.getStock() < cartItem.getMin() || cartItem.getStock() < cartItem.getNumber())) {
+                        // 库存不足
+                        cartItem.setStatus(7);
+                    }
+                }
+            });
+            supplier.setCartList(cartItems);
+            supplier.setTotalPrice(supplierPrice.get());
+            supplier.setCount(itemCount.get());
         });
         return JsonModel.success(cartSuppliers);
     }
+
+    /**
+     * 设置购物车商品价格
+     * @param cartItem
+     * @param userId
+     */
+    private void setCartItemPrice(CartItem cartItem, Integer userId) {
+        // 活动价
+        ActivityPrice activity = shoppingCartDao.getActivityPriceByProductId(cartItem.getProductId());
+        if (null != activity) {
+            cartItem.setPrice(activity.getPrice());
+            cartItem.setActivityFlag(1);
+        } else if (cartItem.getLadderFlag() == 1) {
+            // 阶梯价
+            List<LadderPrice> ladderPrices = shoppingCartDao.getladderPricesByProductId(cartItem.getProductId());
+            if (!CollectionUtils.isEmpty(ladderPrices)){
+                IntStream.range(0, ladderPrices.size()).forEach(i->{
+                    boolean isThisLadder;
+                    if (null != ladderPrices.get(i+1)) {
+                        ladderPrices.get(i).setMaxNum(ladderPrices.get(i+1).getBuyNum());
+                        isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum() && cartItem.getNumber()<ladderPrices.get(i).getMaxNum());
+                    } else {
+                        ladderPrices.get(i).setMaxNum(0);
+                        isThisLadder = (cartItem.getNumber()>ladderPrices.get(i).getBuyNum());
+                    }
+                    if (isThisLadder){
+                        cartItem.setPrice(ladderPrices.get(i).getBuyPrice());
+                    }
+                });
+                cartItem.setMin(ladderPrices.get(0).getBuyNum());
+                cartItem.setLadderPrices(ladderPrices);
+            } else {
+                cartItem.setLadderFlag(0);
+            }
+        } else {
+            // 复购价
+            Double repurchase = shoppingCartDao.getRepurchasePrice(cartItem.getProductId(), userId);
+            if (null != repurchase && repurchase>0) {
+                cartItem.setPrice(repurchase);
+            }
+        }
+    }
 }

+ 19 - 0
src/main/java/com/caimei/www/service/impl/UserServiceImpl.java

@@ -0,0 +1,19 @@
+package com.caimei.www.service.impl;
+
+import com.caimei.www.mapper.UserDao;
+import com.caimei.www.service.UserService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2020/7/24
+ */
+@Service
+public class UserServiceImpl implements UserService {
+    @Resource
+    private UserDao userDao;
+}

+ 109 - 1
src/main/java/com/caimei/www/utils/PriceUtil.java

@@ -1,5 +1,9 @@
 package com.caimei.www.utils;
 
+import org.springframework.util.StringUtils;
+
+import java.math.BigDecimal;
+
 /**
  * Description
  *
@@ -7,6 +11,111 @@ package com.caimei.www.utils;
  * @date : 2020/6/22
  */
 public class PriceUtil {
+    /** 默认小数位 */
+	private static final int DEFAULT_SCALE = 4;
+
+	/**
+	 * 两个实数比较
+	 *
+	 * @param v1 BigDecimal
+	 * @param v2 BigDecimal
+	 * @return int [1:v1>v2, 0:v1=v2, 1:v1<v2]
+	 */
+	public static int compare(Object v1, Object v2) {
+		BigDecimal b1 = convert(v1);
+		BigDecimal b2 = convert(v2);
+		return b1.compareTo(b2);
+	}
+	/**
+	 * 两个实数相加
+	 *
+	 * @param v1 BigDecimal
+	 * @param v2 BigDecimal
+	 * @return BigDecimal
+	 */
+	public static BigDecimal add(Object v1, Object v2) {
+		BigDecimal b1 = convert(v1);
+		BigDecimal b2 = convert(v2);
+		return b1.add(b2);
+	}
+	/**
+	 * 两个实数相减
+	 *
+	 * @param v1 BigDecimal
+	 * @param v2 BigDecimal
+	 * @return BigDecimal
+	 */
+	public static BigDecimal sub(Object v1, Object v2) {
+		BigDecimal b1 = convert(v1);
+		BigDecimal b2 = convert(v2);
+		return b1.subtract(b2);
+	}
+	/**
+	 * 两个实数相乘
+	 *
+	 * @param v1 BigDecimal
+	 * @param v2 BigDecimal
+	 * @return BigDecimal
+	 */
+	public static BigDecimal mul(Object v1, Object v2) {
+		BigDecimal b1 = convert(v1);
+		BigDecimal b2 = convert(v2);
+		return b1.multiply(b2);
+
+	}
+	/**
+	 * 两个实数相除,四舍五入到默认位数
+	 *
+	 * @param v1 BigDecimal
+	 * @param v2 BigDecimal
+	 * @return BigDecimal
+	 */
+	public static BigDecimal div(Object v1, Object v2) {
+		return div(v1, v2, DEFAULT_SCALE);
+	}
+	/**
+	 * 两个实数相除,默认四舍五入到scale位
+	 *
+	 * @param v1 BigDecimal
+	 * @param v2 BigDecimal
+	 * @param scale 小数位
+	 * @return BigDecimal
+	 */
+	public static BigDecimal div(Object v1, Object v2, int scale) {
+		if (scale < 0) {
+			throw new IllegalArgumentException("四舍五入的位数不能为负数");
+		}
+		BigDecimal b1 = convert(v1);
+		BigDecimal b2 = convert(v2);
+		if (equal0(b2)) {
+			throw new IllegalArgumentException("除数不能为0");
+		}
+		return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
+	}
+	/**
+	 * 类型转换函数
+	 *
+	 * @param o <T>
+	 * @return BigDecimal
+	 */
+	private static BigDecimal convert(Object o) {
+		if (o == null) {
+			return BigDecimal.ZERO;
+		} else if (o instanceof BigDecimal) {
+			return (BigDecimal) o;
+		}
+		String str = o.toString();
+		if (!StringUtils.isEmpty(str)) {
+			return new BigDecimal(str);
+		} else {
+			return BigDecimal.ZERO;
+		}
+	}
+	private static boolean equal0(Object v1) {
+		BigDecimal b1 = convert(v1);
+		return b1.compareTo(BigDecimal.ZERO) == 0;
+	}
+
     /**
      * 根据价格返回价格等级
      * @param price 价格
@@ -25,5 +134,4 @@ public class PriceUtil {
         }
         return grade;
     }
-
 }

+ 38 - 4
src/main/resources/mapper/ShoppingCartMapper.xml

@@ -31,18 +31,52 @@
 	</select>
 	<select id="getShoppingCartBySupplierId" resultType="com.caimei.www.pojo.order.CartItem">
 		select
-			p.productID as id,
+			c.cm_cartID as id,
+			p.productID as productId,
 			p.`name` as `name`,
 			p.mainImage as image,
 			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,
 			c.productCount as number
-		from cm_cart c
-		left join product p on c.productID = p.productID
+		from product p
+		left join cm_cart c on c.productID = p.productID
 		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>

+ 13 - 0
src/main/resources/mapper/UserMapper.xml

@@ -0,0 +1,13 @@
+<?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.UserDao">
+	<select id="getUserById" resultType="com.caimei.www.pojo.user.User">
+        select  userID as id,
+        		bindMobile as mobile,
+        		email as email,
+        		password as password,
+        		userIdentity as identity
+        from user
+        where userID = #{userId}
+	</select>
+</mapper>

+ 1 - 1
src/main/resources/static/js/base.js

@@ -46,7 +46,7 @@ var headCart = new Vue({
     methods: {},
     created: function () {
         var _self = this;
-        $.getJSON("/header/cart?userId=1050").done(function (r) {
+        $.getJSON("/header/cart?userId=2892").done(function (r) {
             if (r.code === 0 && r.data) {
                 _self.cartList = r.data;
                 _self.cartCount = r.data.length;