Преглед изворни кода

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/main/resources/backup.sql
Aslee пре 4 година
родитељ
комит
aaef1d37aa

+ 3 - 0
src/main/java/com/caimei/controller/ProductApi.java

@@ -49,6 +49,9 @@ public class ProductApi {
     })
     @GetMapping("/clubList")
     public ResponseJson<PageInfo<ClubVo>> clubList(Integer organizeId, String name, Integer pageNum, Integer pageSize) {
+        if (organizeId == null) {
+            return ResponseJson.error("参数异常", null);
+        }
         return productService.clubList(organizeId, name, pageNum, pageSize);
     }
 

+ 68 - 0
src/main/java/com/caimei/controller/ShoppingCartApi.java

@@ -0,0 +1,68 @@
+package com.caimei.controller;
+
+import com.caimei.model.ResponseJson;
+import com.caimei.model.dto.CartDto;
+import com.caimei.service.ShoppingCartService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/3/23
+ */
+@Api(tags = "购物车")
+@RestController
+@RequestMapping("/shopping")
+public class ShoppingCartApi {
+    private ShoppingCartService shoppingCartService;
+
+    @Autowired
+    public void setShoppingCartService(ShoppingCartService shoppingCartService) {
+        this.shoppingCartService = shoppingCartService;
+    }
+
+    /**
+     * 加入购物车
+     */
+    @ApiOperation("加入购物车")
+    @PostMapping("/add/cart")
+    public synchronized ResponseJson<Integer> addShoppingCart(@RequestBody CartDto cart) {
+        if (cart.getUserId() == null || cart.getProductId() == null || cart.getProductCount() == null) {
+            return ResponseJson.error("参数异常", null);
+        }
+        return shoppingCartService.addShoppingCart(cart);
+    }
+
+    /**
+     * 统计购物车数量
+     */
+    @ApiOperation("统计购物车数量")
+    @ApiImplicitParam(name = "userId", value = "机构用户id", required = true)
+    @PostMapping("/quantity")
+    public ResponseJson<Integer> getCartQuantity(Integer userId) {
+        if (userId == null) {
+            return ResponseJson.error("参数异常", null);
+        }
+        return shoppingCartService.getCartQuantity(userId);
+    }
+
+    /**
+     * 购物车数据
+     */
+    @ApiOperation("购物车数据")
+    @ApiImplicitParam(name = "userId", value = "机构用户id", required = true)
+    @GetMapping("/info")
+    public ResponseJson<Map<String, Object>> shoppingInfo(Integer userId) {
+        if (userId == null) {
+            return ResponseJson.error("参数异常", null);
+        }
+        return shoppingCartService.shoppingInfo(userId);
+    }
+}

+ 56 - 0
src/main/java/com/caimei/mapper/ShoppingCartMapper.java

@@ -0,0 +1,56 @@
+package com.caimei.mapper;
+
+import com.caimei.model.dto.CartDto;
+import com.caimei.model.po.CmMallCartPo;
+import com.caimei.model.vo.ShopVo;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/3/23
+ */
+@Mapper
+public interface ShoppingCartMapper {
+    /**
+     * 查询购物车
+     *
+     * @param userId    机构id
+     * @param productId 组织商品id
+     * @return
+     */
+    CmMallCartPo findCartProduct(@Param("userId") Integer userId, @Param("productId") Integer productId);
+
+    /**
+     * 保存购物车
+     *
+     * @param cart
+     */
+    void insertCart(CartDto cart);
+
+    /**
+     * 修改购物车数量
+     *
+     * @param cartId
+     * @param productCount
+     */
+    void updateByProductCount(@Param("cartId") Integer cartId, @Param("productCount") int productCount);
+
+    /**
+     * 查询购物车数量
+     * @param userId
+     * @return
+     */
+    Integer getCartQuantity(Integer userId);
+
+    /**
+     * 查询购物车对应供应商
+     * @param userId
+     * @return
+     */
+    List<ShopVo> findCartShop(Integer userId);
+}

+ 34 - 0
src/main/java/com/caimei/model/dto/CartDto.java

@@ -0,0 +1,34 @@
+package com.caimei.model.dto;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/3/24
+ */
+@Data
+public class CartDto implements Serializable {
+
+    /**
+     * 关联cm_organize_product的ID
+     */
+    @ApiModelProperty("组织商品id")
+    private Integer productId;
+
+    /**
+     * 用户ID(目前小程序用户和采美用户都在user表中)
+     */
+    @ApiModelProperty("机构用户id")
+    private Integer userId;
+
+    /**
+     * 商品数量
+     */
+    @ApiModelProperty("商品数量")
+    private Integer productCount;
+}

+ 43 - 0
src/main/java/com/caimei/model/po/CmMallCartPo.java

@@ -0,0 +1,43 @@
+package com.caimei.model.po;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import lombok.Data;
+
+/**
+ * cm_mall_cart
+ *
+ * @author
+ */
+@Data
+public class CmMallCartPo implements Serializable {
+    private Integer id;
+
+    /**
+     * 关联cm_organize_product的ID
+     */
+    private Integer productID;
+
+    /**
+     * 用户ID(目前小程序用户和采美用户都在user表中)
+     */
+    private Integer userID;
+
+    /**
+     * 商品数量
+     */
+    private Integer productCount;
+
+    /**
+     * 添加时间
+     */
+    private Date addTime;
+
+    /**
+     * 0 有效  其它无效
+     */
+    private String delFlag;
+
+    private static final long serialVersionUID = 1L;
+}

+ 33 - 0
src/main/java/com/caimei/model/vo/ShopVo.java

@@ -0,0 +1,33 @@
+package com.caimei.model.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/3/24
+ */
+@Data
+public class ShopVo implements Serializable {
+    /**
+     * 供应商id
+     */
+    @ApiModelProperty("供应商id")
+    private Integer shopId;
+
+    /**
+     * 供应商名称
+     */
+    @ApiModelProperty("供应商名称")
+    private String name;
+
+    /**
+     * 供应商logo
+     */
+    @ApiModelProperty("供应商logo")
+    private String logo;
+}

+ 38 - 0
src/main/java/com/caimei/service/ShoppingCartService.java

@@ -0,0 +1,38 @@
+package com.caimei.service;
+
+import com.caimei.model.ResponseJson;
+import com.caimei.model.dto.CartDto;
+
+import java.util.Map;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/3/23
+ */
+public interface ShoppingCartService {
+    /**
+     * 加入购物车
+     *
+     * @param cart
+     * @return
+     */
+    ResponseJson<Integer> addShoppingCart(CartDto cart);
+
+    /**
+     * 统计购物车数量
+     *
+     * @param userId
+     * @return
+     */
+    ResponseJson<Integer> getCartQuantity(Integer userId);
+
+    /**
+     * 购物车数据
+     *
+     * @param userId
+     * @return
+     */
+    ResponseJson<Map<String, Object>> shoppingInfo(Integer userId);
+}

+ 55 - 0
src/main/java/com/caimei/service/impl/ShoppingCartServiceImpl.java

@@ -0,0 +1,55 @@
+package com.caimei.service.impl;
+
+import com.caimei.mapper.ShoppingCartMapper;
+import com.caimei.model.ResponseJson;
+import com.caimei.model.dto.CartDto;
+import com.caimei.model.po.CmMallCartPo;
+import com.caimei.model.vo.ShopVo;
+import com.caimei.service.ShoppingCartService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/3/23
+ */
+@Service
+public class ShoppingCartServiceImpl implements ShoppingCartService {
+    @Resource
+    private ShoppingCartMapper shoppingCartMapper;
+
+    @Override
+    public ResponseJson<Integer> addShoppingCart(CartDto cart) {
+        CmMallCartPo cartPo = shoppingCartMapper.findCartProduct(cart.getUserId(), cart.getProductId());
+        if (cartPo == null) {
+            shoppingCartMapper.insertCart(cart);
+        } else {
+            int productCount = cartPo.getProductCount() + cart.getProductCount();
+            shoppingCartMapper.updateByProductCount(cartPo.getId(), productCount);
+        }
+        Integer cartQuantity = shoppingCartMapper.getCartQuantity(cart.getUserId());
+        return ResponseJson.success(cartQuantity);
+    }
+
+    @Override
+    public ResponseJson<Integer> getCartQuantity(Integer userId) {
+        Integer cartQuantity = shoppingCartMapper.getCartQuantity(userId);
+        return ResponseJson.success(cartQuantity);
+    }
+
+    @Override
+    public ResponseJson<Map<String, Object>> shoppingInfo(Integer userId) {
+        Map<String, Object> map = new HashMap<>(3);
+        List<ShopVo> shopList = shoppingCartMapper.findCartShop(userId);
+        shopList.forEach(shop -> {
+
+        });
+        return null;
+    }
+}

+ 4 - 0
src/main/resources/backup.sql

@@ -113,3 +113,7 @@ create table `cm_organize_promotions_order` (
                                                 primary key (`id`)
 ) engine=innodb default charset=utf8mb4 comment='订单组织促销记录表';
 
+
+
+ALTER TABLE `cm_mall_cart`
+  CHANGE `productID` `productID` INT NOT NULL   COMMENT '关联cm_organize_product的ID';

+ 65 - 0
src/main/resources/mapper/ShoppingCartMapper.xml

@@ -0,0 +1,65 @@
+<?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.mapper.ShoppingCartMapper">
+    <select id="findCartProduct" resultType="com.caimei.model.po.CmMallCartPo">
+        SELECT
+          id,
+          productID,
+          userID,
+          productCount
+        FROM
+          cm_mall_cart
+        WHERE
+          userID = #{userId}
+          AND productID = #{productId}
+          AND delFlag = 0
+    </select>
+
+    <insert id="insertCart">
+        INSERT INTO cm_mall_cart (
+          productID, userID, productCount, addTime,
+          delFlag
+        )
+        VALUES
+          (
+            #{productId}, #{userId}, #{productCount}, NOW(),
+            0
+          )
+    </insert>
+
+    <update id="updateByProductCount">
+        UPDATE cm_mall_cart SET productCount = #{productCount} WHERE id = #{cartId}
+    </update>
+
+    <select id="getCartQuantity" resultType="integer">
+        SELECT
+          COUNT(cmc.productID)
+        FROM
+          cm_mall_cart cmc
+          LEFT JOIN cm_organize_product cop ON cmc.productID = cop.id
+        WHERE cmc.userID = #{userId}
+          AND cop.status = 1
+          AND cop.delFlag = 0
+    </select>
+
+    <select id="findCartShop" resultType="com.caimei.model.vo.ShopVo">
+        SELECT
+          s.shopID AS shopId,
+          s.name,
+          s.logo
+        FROM
+          cm_mall_cart cmc
+          LEFT JOIN cm_organize_product cop ON cmc.productID = cop.id
+          LEFT JOIN product p ON cop.productId = p.productID
+          LEFT JOIN shop s ON p.shopID = s.shopID
+        WHERE
+          cmc.userID = #{userId}
+          AND cmc.delFlag = 0
+          AND cop.status = 1
+          AND cop.delFlag = 0
+        GROUP BY
+          s.shopID
+    </select>
+</mapper>