浏览代码

添加购物车功能接口

zhh 6 年之前
父节点
当前提交
cfe5c0fe80

+ 1 - 1
README.md

@@ -264,7 +264,7 @@ RestTemplate服务间调用 |
 - 购物车商品列表(商品主图、商品名称、商品数量、商品规格)
 - 修改购物车中商品数量
 - 购物车中商品重选规格
-- 商品选中功能及价格计算
+- 购物车中商品删除功能
 
 > **生成确认单**
 

+ 45 - 0
mall-portal/src/main/java/com/macro/mall/portal/config/JacksonConfig.java

@@ -0,0 +1,45 @@
+package com.macro.mall.portal.config;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
+
+
+/**
+ * Jackson配置类
+ * json不返回null的字段
+ * Created by macro on 2018/8/2.
+ */
+@Configuration
+public class JacksonConfig {
+    @Bean
+    @Primary
+    @ConditionalOnMissingBean(ObjectMapper.class)
+    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
+        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
+
+        // 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
+        // Include.Include.ALWAYS 默认
+        // Include.NON_DEFAULT 属性为默认值不序列化
+        // Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
+        // Include.NON_NULL 属性为NULL 不序列化,就是为null的字段不参加序列化
+        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+
+        // 字段保留,将null值转为""
+//        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
+//        {
+//            @Override
+//            public void serialize(Object o, JsonGenerator jsonGenerator,
+//                                  SerializerProvider serializerProvider)
+//                    throws IOException, JsonProcessingException
+//            {
+//                jsonGenerator.writeString("");
+//            }
+//        });
+        return objectMapper;
+    }
+}

+ 87 - 0
mall-portal/src/main/java/com/macro/mall/portal/controller/OmsCartItemController.java

@@ -0,0 +1,87 @@
+package com.macro.mall.portal.controller;
+
+import com.macro.mall.model.OmsCartItem;
+import com.macro.mall.portal.domain.CartProduct;
+import com.macro.mall.portal.domain.CommonResult;
+import com.macro.mall.portal.service.OmsCartItemService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * 购物车管理Controller
+ * Created by macro on 2018/8/2.
+ */
+@Controller
+@Api(tags = "OmsCartItemController", description = "购物车管理")
+@RequestMapping("/cart")
+public class OmsCartItemController {
+    @Autowired
+    private OmsCartItemService cartItemService;
+
+    @ApiOperation("添加商品到购物车")
+    @RequestMapping(value = "/add", method = RequestMethod.POST)
+    @ResponseBody
+    public Object add(@RequestBody OmsCartItem cartItem) {
+        int count = cartItemService.add(cartItem);
+        if (count > 0) {
+            return new CommonResult().success(count);
+        }
+        return new CommonResult().failed();
+    }
+
+    @ApiOperation("获取某个会员的购物车列表")
+    @RequestMapping(value = "/list/{memberId}", method = RequestMethod.GET)
+    @ResponseBody
+    public Object list(@PathVariable Long memberId) {
+        List<OmsCartItem> cartItemList = cartItemService.list(memberId);
+        return new CommonResult().success(cartItemList);
+    }
+
+    @ApiOperation("修改购物车中某个商品的数量")
+    @RequestMapping(value = "/update/quantity", method = RequestMethod.GET)
+    @ResponseBody
+    public Object updateQuantity(@RequestParam Long id,
+                                 @RequestParam Long memberId,
+                                 @RequestParam Integer quantity) {
+        int count = cartItemService.updateQuantity(id,memberId,quantity);
+        if (count > 0) {
+            return new CommonResult().success(count);
+        }
+        return new CommonResult().failed();
+    }
+
+    @ApiOperation("获取购物车中某个商品的规格,用于重选规格")
+    @RequestMapping(value = "/getProduct/{productId}", method = RequestMethod.GET)
+    @ResponseBody
+    public Object getCartProduct(@PathVariable Long productId) {
+        CartProduct cartProduct = cartItemService.getCartProduct(productId);
+        return new CommonResult().success(cartProduct);
+    }
+
+    @ApiOperation("修改购物车中商品的规格")
+    @RequestMapping(value = "/update/attr", method = RequestMethod.POST)
+    @ResponseBody
+    public Object updateAttr(@RequestBody OmsCartItem cartItem) {
+        int count = cartItemService.updateAttr(cartItem);
+        if (count > 0) {
+            return new CommonResult().success(count);
+        }
+        return new CommonResult().failed();
+    }
+
+    @ApiOperation("删除购物车中的某个商品")
+    @RequestMapping(value = "/delete", method = RequestMethod.POST)
+    @ResponseBody
+    public Object delete(@RequestParam Long memberId,@RequestParam("ids") List<Long> ids) {
+        int count = cartItemService.delete(memberId,ids);
+        if (count > 0) {
+            return new CommonResult().success(count);
+        }
+        return new CommonResult().failed();
+    }
+}

+ 12 - 0
mall-portal/src/main/java/com/macro/mall/portal/dao/PortalProductDao.java

@@ -0,0 +1,12 @@
+package com.macro.mall.portal.dao;
+
+import com.macro.mall.portal.domain.CartProduct;
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * 前台系统自定义商品Dao
+ * Created by macro on 2018/8/2.
+ */
+public interface PortalProductDao {
+    CartProduct getCartProduct(@Param("id") Long id);
+}

+ 32 - 0
mall-portal/src/main/java/com/macro/mall/portal/domain/CartProduct.java

@@ -0,0 +1,32 @@
+package com.macro.mall.portal.domain;
+
+import com.macro.mall.model.PmsProduct;
+import com.macro.mall.model.PmsProductAttribute;
+import com.macro.mall.model.PmsSkuStock;
+
+import java.util.List;
+
+/**
+ * 购物车中选择规格的商品信息
+ * Created by macro on 2018/8/2.
+ */
+public class CartProduct extends PmsProduct {
+    private List<PmsProductAttribute> productAttributeList;
+    private List<PmsSkuStock> skuStockList;
+
+    public List<PmsProductAttribute> getProductAttributeList() {
+        return productAttributeList;
+    }
+
+    public void setProductAttributeList(List<PmsProductAttribute> productAttributeList) {
+        this.productAttributeList = productAttributeList;
+    }
+
+    public List<PmsSkuStock> getSkuStockList() {
+        return skuStockList;
+    }
+
+    public void setSkuStockList(List<PmsSkuStock> skuStockList) {
+        this.skuStockList = skuStockList;
+    }
+}

+ 81 - 0
mall-portal/src/main/java/com/macro/mall/portal/domain/CommonResult.java

@@ -0,0 +1,81 @@
+package com.macro.mall.portal.domain;
+
+import org.springframework.data.domain.Page;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 通用返回对象
+ * Created by macro on 2018/4/26.
+ */
+public class CommonResult {
+    //操作成功
+    public static final int SUCCESS = 200;
+    //操作失败
+    public static final int FAILED = 500;
+    private int code;
+    private String message;
+    private Object data;
+
+    /**
+     * 普通成功返回
+     *
+     * @param data 获取的数据
+     */
+    public CommonResult success(Object data) {
+        this.code = SUCCESS;
+        this.message = "操作成功";
+        this.data = data;
+        return this;
+    }
+
+    /**
+     * 返回分页成功数据
+     */
+    public CommonResult pageSuccess(Page pageInfo) {
+        Map<String, Object> result = new HashMap<>();
+        result.put("pageSize", pageInfo.getSize());
+        result.put("totalPage", pageInfo.getTotalPages());
+        result.put("total", pageInfo.getTotalElements());
+        result.put("pageNum", pageInfo.getNumber());
+        result.put("list", pageInfo.getContent());
+        this.code = SUCCESS;
+        this.message = "操作成功";
+        this.data = result;
+        return this;
+    }
+
+    /**
+     * 普通失败提示信息
+     */
+    public CommonResult failed() {
+        this.code = FAILED;
+        this.message = "操作失败";
+        return this;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public Object getData() {
+        return data;
+    }
+
+    public void setData(Object data) {
+        this.data = data;
+    }
+}

+ 45 - 0
mall-portal/src/main/java/com/macro/mall/portal/service/OmsCartItemService.java

@@ -0,0 +1,45 @@
+package com.macro.mall.portal.service;
+
+import com.macro.mall.model.OmsCartItem;
+import com.macro.mall.portal.domain.CartProduct;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+
+/**
+ * 购物车管理Service
+ * Created by macro on 2018/8/2.
+ */
+public interface OmsCartItemService {
+    /**
+     * 查询购物车中是否包含该商品,有增加数量,无添加到购物车
+     */
+    @Transactional
+    int add(OmsCartItem cartItem);
+
+    /**
+     * 根据会员编号获取购物车列表
+     */
+    List<OmsCartItem> list(Long memberId);
+
+    /**
+     * 修改某个购物车商品的数量
+     */
+    int updateQuantity(Long id, Long memberId, Integer quantity);
+
+    /**
+     * 批量删除购物车中的商品
+     */
+    int delete(Long memberId,List<Long> ids);
+
+    /**
+     *获取购物车中用于选择商品规格的商品信息
+     */
+    CartProduct getCartProduct(Long productId);
+
+    /**
+     * 修改购物车中商品的规格
+     */
+    @Transactional
+    int updateAttr(OmsCartItem cartItem);
+}

+ 104 - 0
mall-portal/src/main/java/com/macro/mall/portal/service/impl/OmsCartItemServiceImpl.java

@@ -0,0 +1,104 @@
+package com.macro.mall.portal.service.impl;
+
+import com.macro.mall.mapper.OmsCartItemMapper;
+import com.macro.mall.model.OmsCartItem;
+import com.macro.mall.model.OmsCartItemExample;
+import com.macro.mall.portal.dao.PortalProductDao;
+import com.macro.mall.portal.domain.CartProduct;
+import com.macro.mall.portal.service.OmsCartItemService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+
+import java.util.List;
+
+/**
+ * 购物车管理Service实现类
+ * Created by macro on 2018/8/2.
+ */
+@Service
+public class OmsCartItemServiceImpl implements OmsCartItemService {
+    @Autowired
+    private OmsCartItemMapper cartItemMapper;
+    @Autowired
+    private PortalProductDao productDao;
+
+    @Override
+    public int add(OmsCartItem cartItem) {
+        OmsCartItem existCartItem = getCartItem(cartItem);
+        if (existCartItem == null) {
+            cartItemMapper.insert(cartItem);
+        } else {
+            existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
+            cartItemMapper.updateByPrimaryKey(existCartItem);
+        }
+        return 1;
+    }
+
+    /**
+     * 根据会员id,商品id和规格获取购物车中商品
+     */
+    private OmsCartItem getCartItem(OmsCartItem cartItem) {
+        OmsCartItemExample example = new OmsCartItemExample();
+        OmsCartItemExample.Criteria criteria = example.createCriteria().andMemberIdEqualTo(cartItem.getMemberId())
+                .andProductIdEqualTo(cartItem.getProductId()).andDeleteStatusEqualTo(0);
+        if (!StringUtils.isEmpty(cartItem.getSp1())) {
+            criteria.andSp1EqualTo(cartItem.getSp1());
+        }
+        if (!StringUtils.isEmpty(cartItem.getSp2())) {
+            criteria.andSp2EqualTo(cartItem.getSp2());
+        }
+        if (!StringUtils.isEmpty(cartItem.getSp3())) {
+            criteria.andSp3EqualTo(cartItem.getSp3());
+        }
+        List<OmsCartItem> cartItemList = cartItemMapper.selectByExample(example);
+        if (!CollectionUtils.isEmpty(cartItemList)) {
+            return cartItemList.get(0);
+        }
+        return null;
+    }
+
+    @Override
+    public List<OmsCartItem> list(Long memberId) {
+        OmsCartItemExample example = new OmsCartItemExample();
+        example.createCriteria().andDeleteStatusEqualTo(0).andMemberIdEqualTo(memberId);
+        return cartItemMapper.selectByExample(example);
+    }
+
+    @Override
+    public int updateQuantity(Long id, Long memberId, Integer quantity) {
+        OmsCartItem cartItem = new OmsCartItem();
+        cartItem.setQuantity(quantity);
+        OmsCartItemExample example = new OmsCartItemExample();
+        example.createCriteria().andDeleteStatusEqualTo(0)
+                .andIdEqualTo(id).andMemberIdEqualTo(memberId);
+        return cartItemMapper.updateByExampleSelective(cartItem, example);
+    }
+
+    @Override
+    public int delete(Long memberId, List<Long> ids) {
+        OmsCartItem record = new OmsCartItem();
+        record.setDeleteStatus(1);
+        OmsCartItemExample example = new OmsCartItemExample();
+        example.createCriteria().andIdIn(ids).andMemberIdEqualTo(memberId);
+        return cartItemMapper.updateByExampleSelective(record, example);
+    }
+
+    @Override
+    public CartProduct getCartProduct(Long productId) {
+        return productDao.getCartProduct(productId);
+    }
+
+    @Override
+    public int updateAttr(OmsCartItem cartItem) {
+        //删除原购物车信息
+        OmsCartItem updateCart = new OmsCartItem();
+        updateCart.setId(cartItem.getId());
+        updateCart.setDeleteStatus(1);
+        cartItemMapper.updateByPrimaryKeySelective(updateCart);
+        cartItem.setId(null);
+        add(cartItem);
+        return 1;
+    }
+}

+ 39 - 0
mall-portal/src/main/resources/dao/PortalProductDao.xml

@@ -0,0 +1,39 @@
+<?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.macro.mall.portal.dao.PortalProductDao">
+    <resultMap id="cartProductMap" type="com.macro.mall.portal.domain.CartProduct" autoMapping="true">
+        <id column="id" jdbcType="BIGINT" property="id" />
+        <collection property="productAttributeList" columnPrefix="attr_" resultMap="com.macro.mall.mapper.PmsProductAttributeMapper.BaseResultMap">
+        </collection>
+        <collection property="skuStockList" columnPrefix="sku_" resultMap="com.macro.mall.mapper.PmsSkuStockMapper.BaseResultMap">
+        </collection>
+    </resultMap>
+    <select id="getCartProduct" resultMap="cartProductMap">
+        SELECT
+            p.id id,
+            p.`name` name,
+            p.sub_title subTitle,
+            p.price price,
+            p.pic pic,
+            p.product_attribute_category_id productAttributeCategoryId,
+            p.stock stock,
+            pa.id attr_id,
+            pa.`name` attr_name,
+            ps.id sku_id,
+            ps.sku_code sku_code,
+            ps.price sku_price,
+            ps.sp1 sku_sp1,
+            ps.sp2 sku_sp2,
+            ps.sp3 sku_sp3,
+            ps.stock sku_stock,
+            ps.pic sku_pic
+        FROM
+            pms_product p
+            LEFT JOIN pms_product_attribute pa ON p.product_attribute_category_id = pa.product_attribute_category_id
+            LEFT JOIN pms_sku_stock ps ON p.id=ps.product_id
+        WHERE
+            p.id = #{id}
+            AND pa.type = 0
+        ORDER BY pa.sort desc
+    </select>
+</mapper>