Skip to content

Commit

Permalink
添加购物车功能接口
Browse files Browse the repository at this point in the history
  • Loading branch information
zhh committed Aug 2, 2018
1 parent 96fc2c6 commit cfe5c0f
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ RestTemplate服务间调用 |
- 购物车商品列表(商品主图、商品名称、商品数量、商品规格)
- 修改购物车中商品数量
- 购物车中商品重选规格
- 商品选中功能及价格计算
- 购物车中商品删除功能

> **生成确认单**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit cfe5c0f

Please sign in to comment.