diff --git a/README.md b/README.md index 491d28c..d8f0a27 100644 --- a/README.md +++ b/README.md @@ -264,7 +264,7 @@ RestTemplate服务间调用 | - 购物车商品列表(商品主图、商品名称、商品数量、商品规格) - 修改购物车中商品数量 - 购物车中商品重选规格 -- 商品选中功能及价格计算 +- 购物车中商品删除功能 > **生成确认单** diff --git a/mall-portal/src/main/java/com/macro/mall/portal/config/JacksonConfig.java b/mall-portal/src/main/java/com/macro/mall/portal/config/JacksonConfig.java new file mode 100644 index 0000000..c00d560 --- /dev/null +++ b/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() +// { +// @Override +// public void serialize(Object o, JsonGenerator jsonGenerator, +// SerializerProvider serializerProvider) +// throws IOException, JsonProcessingException +// { +// jsonGenerator.writeString(""); +// } +// }); + return objectMapper; + } +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/controller/OmsCartItemController.java b/mall-portal/src/main/java/com/macro/mall/portal/controller/OmsCartItemController.java new file mode 100644 index 0000000..a75747b --- /dev/null +++ b/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 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 ids) { + int count = cartItemService.delete(memberId,ids); + if (count > 0) { + return new CommonResult().success(count); + } + return new CommonResult().failed(); + } +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/dao/PortalProductDao.java b/mall-portal/src/main/java/com/macro/mall/portal/dao/PortalProductDao.java new file mode 100644 index 0000000..03f8ccd --- /dev/null +++ b/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); +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/domain/CartProduct.java b/mall-portal/src/main/java/com/macro/mall/portal/domain/CartProduct.java new file mode 100644 index 0000000..db014c0 --- /dev/null +++ b/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 productAttributeList; + private List skuStockList; + + public List getProductAttributeList() { + return productAttributeList; + } + + public void setProductAttributeList(List productAttributeList) { + this.productAttributeList = productAttributeList; + } + + public List getSkuStockList() { + return skuStockList; + } + + public void setSkuStockList(List skuStockList) { + this.skuStockList = skuStockList; + } +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/domain/CommonResult.java b/mall-portal/src/main/java/com/macro/mall/portal/domain/CommonResult.java new file mode 100644 index 0000000..4be915b --- /dev/null +++ b/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 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; + } +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/service/OmsCartItemService.java b/mall-portal/src/main/java/com/macro/mall/portal/service/OmsCartItemService.java new file mode 100644 index 0000000..e188ec5 --- /dev/null +++ b/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 list(Long memberId); + + /** + * 修改某个购物车商品的数量 + */ + int updateQuantity(Long id, Long memberId, Integer quantity); + + /** + * 批量删除购物车中的商品 + */ + int delete(Long memberId,List ids); + + /** + *获取购物车中用于选择商品规格的商品信息 + */ + CartProduct getCartProduct(Long productId); + + /** + * 修改购物车中商品的规格 + */ + @Transactional + int updateAttr(OmsCartItem cartItem); +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/service/impl/OmsCartItemServiceImpl.java b/mall-portal/src/main/java/com/macro/mall/portal/service/impl/OmsCartItemServiceImpl.java new file mode 100644 index 0000000..b4c92ec --- /dev/null +++ b/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 cartItemList = cartItemMapper.selectByExample(example); + if (!CollectionUtils.isEmpty(cartItemList)) { + return cartItemList.get(0); + } + return null; + } + + @Override + public List 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 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; + } +} diff --git a/mall-portal/src/main/resources/dao/PortalProductDao.xml b/mall-portal/src/main/resources/dao/PortalProductDao.xml new file mode 100644 index 0000000..5b0d1c3 --- /dev/null +++ b/mall-portal/src/main/resources/dao/PortalProductDao.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + \ No newline at end of file