From 5841e307ddc1f1814f77569f99ab49cd2a1237ec Mon Sep 17 00:00:00 2001 From: lengyuetian <1750487841@qq.com> Date: Fri, 26 Feb 2021 15:42:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=A0=E8=BE=89=E7=AC=ACn=E6=AD=A4=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaomi/controller/OrderController.java | 62 +++++++++ .../controller/ShoppingCartController.java | 84 ++++++++++++ .../com/mall/xiaomi/mapper/OrderMapper.java | 16 ++- .../xiaomi/mapper/ShoppingCartMapper.java | 4 +- .../com/mall/xiaomi/service/OrderService.java | 43 ++++++- .../xiaomi/service/ShoppingCartService.java | 120 ++++++++++++++++++ src/main/resources/application.yml | 2 +- 7 files changed, 324 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/mall/xiaomi/controller/OrderController.java create mode 100644 src/main/java/com/mall/xiaomi/controller/ShoppingCartController.java create mode 100644 src/main/java/com/mall/xiaomi/service/ShoppingCartService.java diff --git a/src/main/java/com/mall/xiaomi/controller/OrderController.java b/src/main/java/com/mall/xiaomi/controller/OrderController.java new file mode 100644 index 0000000..97fcc50 --- /dev/null +++ b/src/main/java/com/mall/xiaomi/controller/OrderController.java @@ -0,0 +1,62 @@ +package com.mall.xiaomi.controller; + +import com.mall.xiaomi.service.OrderService; +import com.mall.xiaomi.util.ResultMessage; +import com.mall.xiaomi.vo.CartVo; +import com.mall.xiaomi.vo.OrderVo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @author zhanghui + * @ClassName OrderController + * @Description TODO + * @Date 2021/2/26 14:21 + * @Version 1.0 + **/ +@RestController +@RequestMapping("/order") +public class OrderController { + + @Autowired + private ResultMessage resultMessage; + @Autowired + private RedisTemplate redisTemplate; + @Autowired + private OrderService orderService; + /** + * @Author zhanghui + * @Description 通过判断用户是否登录(cookie)是否存在,以及redis里面的userId比对,在进行添加订单操作 + * @Date 2021/2/26 10:27 + * @Param [cartVoList, cookie] + * @return com.mall.xiaomi.util.ResultMessage + **/ + @PostMapping("") + public ResultMessage addOrder(@RequestBody List cartVoList, @CookieValue("XM_TOKEN") String cookie) { + // 先判断cookie是否存在,和redis校验 + Integer userId = (Integer) redisTemplate.opsForHash().get(cookie, "userId"); + orderService.addOrder(cartVoList, userId); + resultMessage.success("001", "下单成功"); + return resultMessage; + } + /** + * @Author zhanghui + * @Description 判断cookie和redis来进行订单页面的显示 + * @Date 2021/2/26 10:28 + * @Param [cookie] + * @return com.mall.xiaomi.util.ResultMessage + **/ + + @GetMapping("") + public ResultMessage getOrder(@CookieValue("XM_TOKEN") String cookie) { + // 先判断cookie是否存在,和redis校验 + Integer userId = (Integer) redisTemplate.opsForHash().get(cookie, "userId"); + List> orders = orderService.getOrder(userId); + resultMessage.success("001", orders); + return resultMessage; + } + +} diff --git a/src/main/java/com/mall/xiaomi/controller/ShoppingCartController.java b/src/main/java/com/mall/xiaomi/controller/ShoppingCartController.java new file mode 100644 index 0000000..21c9183 --- /dev/null +++ b/src/main/java/com/mall/xiaomi/controller/ShoppingCartController.java @@ -0,0 +1,84 @@ +package com.mall.xiaomi.controller; + +import com.mall.xiaomi.service.ShoppingCartService; +import com.mall.xiaomi.util.ResultMessage; +import com.mall.xiaomi.vo.CartVo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @author zhanghui + * @ClassName ShoppingCartController + * @Description TODO + * @Date 2021/2/26 14:33 + * @Version 1.0 + **/ +@RestController +@RequestMapping("/cart") +public class ShoppingCartController { + @Autowired + private ResultMessage resultMessage; + @Autowired + private ShoppingCartService cartService; + + /** + * @Author zhanghui + * @Description 根据用户id查询购物车信息接口 + * @Date 2021/2/26 14:42 + * @Param [userId] + * @return com.mall.xiaomi.util.ResultMessage + **/ + @GetMapping("/user/{userId}") + public ResultMessage cart(@PathVariable String userId) { + List carts = cartService.getCartByUserId(userId); + resultMessage.success("001",carts); + return resultMessage; + } + /** + * @Author zhanghui + * @Description 添加购物车 + * @Date 2021/2/26 15:21 + * @Param [productId, userId] + * @return com.mall.xiaomi.util.ResultMessage + **/ + @PostMapping("//product/user/{productId}/{userId}") + public ResultMessage cart(@PathVariable String productId,@PathVariable String userId){ + CartVo cartVo = cartService.addCart(productId,userId); + if(cartVo != null){ + resultMessage.success("001","添加购物车成功",cartVo); + }else { + resultMessage.success("002","该商品已在购物车上,数目+1"); + } + return resultMessage; + } + + /** + * @Author zhanghui + * @Description 修改商品数目,根据userId和productId的双向绑定 + * @Date 2021/2/26 15:27 + * @Param [cartId, userId, num] + * @return com.mall.xiaomi.util.ResultMessage + **/ + @PutMapping("/user/num/{cartId}/{userId}/{num}") + public ResultMessage cart(@PathVariable String cartId,@PathVariable String userId,@PathVariable String num){ + cartService.updateCartNum(cartId,userId,num); + resultMessage.success("001","更新成功"); + return resultMessage; + } + /** + * @Author zhanghui + * @Description 删除对应用户的购物车 + * @Date 2021/2/26 15:31 + * @Param [cartId, userId] + * @return com.mall.xiaomi.util.ResultMessage + **/ + @DeleteMapping("/user/{cartId}/{userId}") + public ResultMessage deleteCart(@PathVariable String cartId,@PathVariable String userId){ + cartService.deleteCart(cartId,userId); + resultMessage.success("001","删除成功"); + return resultMessage; + } + +} diff --git a/src/main/java/com/mall/xiaomi/mapper/OrderMapper.java b/src/main/java/com/mall/xiaomi/mapper/OrderMapper.java index c190935..92878d9 100644 --- a/src/main/java/com/mall/xiaomi/mapper/OrderMapper.java +++ b/src/main/java/com/mall/xiaomi/mapper/OrderMapper.java @@ -2,12 +2,13 @@ package com.mall.xiaomi.mapper; import com.mall.xiaomi.pojo.Order; import com.mall.xiaomi.vo.OrderVo; +import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import tk.mybatis.mapper.common.Mapper; import java.util.List; -public interface OrderMapper extends Mapper { +public interface OrderMapper extends Mapper{ /** * @Author zhanghui * @Description 全查订单信息以及产品名称和图片 @@ -17,5 +18,16 @@ public interface OrderMapper extends Mapper { **/ @Select("select `order`.*, product.product_name as productName, product.product_picture as productPicture " + "from `order`, product where `order`.product_id = product.product_id and `order`.user_id = #{userId}") - public List findOrders(); + public List findOrders(Integer userId); +// /** +// * @Author zhanghui +// * @Description 添加订单各个信息 +// * @Date 2021/2/26 10:13 +// * @Param [order] +// * @return int +// **/ +// @Insert("insert into order values (order_id = #{orderId},user_id = #{userId},product_id = #{productId}" + +// ",product_num = #{productNum},product_price = #{productPrice} )") +// public int insert(Order order); + } diff --git a/src/main/java/com/mall/xiaomi/mapper/ShoppingCartMapper.java b/src/main/java/com/mall/xiaomi/mapper/ShoppingCartMapper.java index 03c627c..8c0a9ce 100644 --- a/src/main/java/com/mall/xiaomi/mapper/ShoppingCartMapper.java +++ b/src/main/java/com/mall/xiaomi/mapper/ShoppingCartMapper.java @@ -1,6 +1,7 @@ package com.mall.xiaomi.mapper; import com.mall.xiaomi.pojo.ShoppingCart; +import org.apache.ibatis.annotations.Delete; import tk.mybatis.mapper.common.Mapper; /** @@ -11,5 +12,6 @@ import tk.mybatis.mapper.common.Mapper; * @Version 1.0 **/ public interface ShoppingCartMapper extends Mapper { - + @Delete("delete from shopping_cart where user_id = #{userId}") + public int deleteCart(Integer userId); } diff --git a/src/main/java/com/mall/xiaomi/service/OrderService.java b/src/main/java/com/mall/xiaomi/service/OrderService.java index f8700f1..d342c77 100644 --- a/src/main/java/com/mall/xiaomi/service/OrderService.java +++ b/src/main/java/com/mall/xiaomi/service/OrderService.java @@ -29,20 +29,31 @@ import java.util.stream.Collectors; */ @Service public class OrderService { - + //自动生成订单编号的类 @Autowired private IdWorker idWorker; + //操作redis的一个类 @Autowired private RedisTemplate redisTemplate; + //OrderMapper注入 @Autowired private OrderMapper orderMapper; + //购物车注入 @Autowired private ShoppingCartMapper cartMapper; + //商品注入 @Autowired private ProductMapper productMapper; - private final static String SECKILL_PRODUCT_USER_LIST = "seckill:product:user:list"; - +// private final static String SECKILL_PRODUCT_USER_LIST = "seckill:product:user:list"; + //事务处理 + /** + * @Author zhanghui + * @Description 添加订单的操作, + * @Date 2021/2/26 10:14 + * @Param [cartVoList, userId] + * @return void + **/ @Transactional public void addOrder(List cartVoList, Integer userId) { // 先添加订单 @@ -83,6 +94,32 @@ public class OrderService { } } + /** + * @Author zhanghui + * @Description 将订单列表(集合)放进一个集合,显示整个订单信息 + * @Date 2021/2/26 10:25 + * @Param [userId] + * @return java.util.List> + **/ + + public List> getOrder(Integer userId) { + List list = null; + ArrayList> ret = new ArrayList<>(); + try { + list = orderMapper.findOrders(userId); + if (ArrayUtils.isEmpty(list.toArray())) { + throw new XmException(ExceptionEnum.GET_ORDER_NOT_FOUND); + } + // 将同一个订单放在一组 + Map> collect = list.stream().collect(Collectors.groupingBy(Order::getOrderId)); + Collection> values = collect.values(); + ret.addAll(values); + } catch (XmException e) { + e.printStackTrace(); + throw new XmException(ExceptionEnum.GET_ORDER_ERROR); + } + return ret; + } } diff --git a/src/main/java/com/mall/xiaomi/service/ShoppingCartService.java b/src/main/java/com/mall/xiaomi/service/ShoppingCartService.java new file mode 100644 index 0000000..eab8a1f --- /dev/null +++ b/src/main/java/com/mall/xiaomi/service/ShoppingCartService.java @@ -0,0 +1,120 @@ +package com.mall.xiaomi.service; + +import com.mall.xiaomi.exception.ExceptionEnum; +import com.mall.xiaomi.exception.XmException; +import com.mall.xiaomi.mapper.ProductMapper; +import com.mall.xiaomi.mapper.ShoppingCartMapper; +import com.mall.xiaomi.pojo.Product; +import com.mall.xiaomi.pojo.ShoppingCart; +import com.mall.xiaomi.vo.CartVo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author zhanghui + * @ClassName ShoppingCartService + * @Description TODO + * @Date 2021/2/26 14:27 + * @Version 1.0 + **/ +@Service +public class ShoppingCartService { + + @Autowired + private ShoppingCartMapper cartMapper; + @Autowired + private ProductMapper productMapper; + + public List getCartByUserId(String userId) { + ShoppingCart cart = new ShoppingCart(); + cart.setUserId(Integer.parseInt(userId)); + List list = null; + List cartVoList = new ArrayList<>(); + try { + list = cartMapper.select(cart); + for (ShoppingCart c : list) { + cartVoList.add(getCartVo(c)); + } + } catch (Exception e) { + e.printStackTrace(); + throw new XmException(ExceptionEnum.GET_CART_ERROR); + } + return cartVoList; + } + + @Transactional + public CartVo addCart(String productId, String userId) { + ShoppingCart cart = new ShoppingCart(); + cart.setUserId(Integer.parseInt(userId)); + cart.setProductId(Integer.parseInt(productId)); + // 查看数据库是否已存在,存在数量直接加1 + ShoppingCart one = cartMapper.selectOne(cart); + if (one != null) { + // 还要判断是否达到该商品规定上限 + if (one.getNum() >= 5) { // TODO 这里默认设为5 后期再动态修改 + throw new XmException(ExceptionEnum.ADD_CART_NUM_UPPER); + } + one.setNum(one.getNum() + 1); + cartMapper.updateByPrimaryKey(one); + return null; + }else { + // 不存在 + cart.setNum(1); + cartMapper.insert(cart); + return getCartVo(cart); + } + } + + /** + * 封装类 + * @param cart + * @return + */ + private CartVo getCartVo(ShoppingCart cart) { + // 获取商品,用于封装下面的类 + Product product = productMapper.selectByPrimaryKey(cart.getProductId()); + // 返回购物车详情 + CartVo cartVo = new CartVo(); + cartVo.setId(cart.getId()); + cartVo.setProductId(cart.getProductId()); + cartVo.setProductName(product.getProductName()); + cartVo.setProductImg(product.getProductPicture()); + cartVo.setPrice(product.getProductSellingPrice()); + cartVo.setNum(cart.getNum()); + cartVo.setMaxNum(5); // TODO 这里默认设为5 后期再动态修改 + cartVo.setCheck(false); + return cartVo; + } + + public void updateCartNum(String cartId, String userId, String num) { + ShoppingCart cart = new ShoppingCart(); + cart.setId(Integer.parseInt(cartId)); + cart.setUserId(Integer.parseInt(userId)); + cart.setNum(Integer.parseInt(num)); + try { + int count = cartMapper.updateByPrimaryKeySelective(cart); + if (count != 1) { + throw new XmException(ExceptionEnum.UPDATE_CART_ERROR); + } + } catch (Exception e) { + e.printStackTrace(); + throw new XmException(ExceptionEnum.UPDATE_CART_ERROR); + } + } + + public void deleteCart(String cartId, String userId) { + ShoppingCart cart = new ShoppingCart(); + cart.setId(Integer.parseInt(cartId)); + cart.setUserId(Integer.parseInt(userId)); + try { + cartMapper.delete(cart); + } catch (XmException e) { + e.printStackTrace(); + throw new XmException(ExceptionEnum.DELETE_CART_ERROR); + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d97a16e..e85b96d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,7 +4,7 @@ spring: application: name: emp-service datasource: - url: jdbc:mysql://10.12.151.254:3306/xiaomi?serverTimezone=UTC + url: jdbc:mysql://10.12.151.232:3306/xiaomi?serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver -- Gitee