当前位置: 首页 > news >正文

苍穹外卖--添加购物车

1.需求分析和设计

产品原型:

接口设计:

数据库设计:

①作用:暂时存放所选商品的地方

②选的什么商品

③每个商品都买了几个

④不同用户的购物车需要区分开

shopping_cart表:

2.代码开发

hoppingCartController.java代码:

package com.sky.controller.user;import com.sky.dto.ShoppingCartDTO;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端购物车相关接口")
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;@PostMapping("/add")@ApiOperation("添加购物车")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){log.info("添加购物车,商品信息为:{}",shoppingCartDTO);shoppingCartService.addShoppingCart(shoppingCartDTO);return Result.success();}
}

ShoppingCartService.java代码:

package com.sky.service;import com.sky.dto.ShoppingCartDTO;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;@Service
public interface ShoppingCartService {/*** 添加购物车* @param shoppingCartDTO*/void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
}

ShoppingCartServiceImpl.java代码:

package com.sky.service.impl;import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {@Autowiredprivate ShoppingCartMapper shoppingCartMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 添加购物车* @param shoppingCartDTO*/@Overridepublic void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {//判断当前加入到购物车中的商品是否已经存在了ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);Long userId = BaseContext.getCurrentId();shoppingCart.setUserId(userId);List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//这个时候查询出来的数据只有可能是1条或者0条//如果已经存在,只需要将数量加1if(list != null && list.size() >0){//这里判断他有数据,就代表商品已经加入购物车,存在了ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber() + 1);shoppingCartMapper.updateNumberById(cart);//修改商品数量}else {//如果不存在,需要插入一条购物车数据//判断本次添加到购物车的是菜品还是套餐Long dishId = shoppingCartDTO.getDishId();if(dishId != null){//从前端获取到的id只有可能是菜品和套餐的id,口味是附加在菜品里的,所以当判断获取处理的id中有菜品id就没有套餐id反之亦然//本次添加到购物车的是菜品Dish dish = dishMapper.getById(dishId);shoppingCart.setName(dish.getName());shoppingCart.setImage(dish.getImage());shoppingCart.setAmount(dish.getPrice());}else {//本次添加到购物车的是套餐Long setmealId = shoppingCart.getSetmealId();Setmeal setmeal = setmealMapper.getById(setmealId);shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());}shoppingCart.setNumber(1);//这两句话是固定的,可以提取出来shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}
}

ShoppingCartMapper.java代码:

package com.sky.mapper;import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;import java.util.List;@Mapper
public interface ShoppingCartMapper {/*** 动态条件查询* @param shoppingCart* @return*/List<ShoppingCart> list(ShoppingCart shoppingCart);/*** 根据id修改商品数量* @param shoppingCart*/@Update("UPDATE shopping_cart SET number = #{number} WHERE id = #{id}")void updateNumberById(ShoppingCart shoppingCart);/*** 插入购物车数据* @param shoppingCart*/@Insert("INSERT INTO shopping_cart(name, user_id, dish_id, setmeal_id, dish_flavor, number , amount , image , create_time)" +"values (#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{image},#{createTime})")void insert(ShoppingCart shoppingCart);
}

ShoppingCartMapper.xml代码:

<?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.sky.mapper.ShoppingCartMapper"><select id="list" resultType="com.sky.entity.ShoppingCart">SELECT * FROM shopping_cart<where><if test="userId != null">and user_id = #{userId}</if><if test="setmealId != null">and setmeal_id = #{setmealId}</if><if test="dishId != null">and dish_id = #{dishId}</if><if test="dishFlavor != null">and dish_flavor = #{dishFlavor}</if></where></select>
</mapper>

相关文章:

  • Websocket 数据实时更新(消息提醒功能)异步+事件发布
  • 我是如何使用Claude Code
  • 基于NAS与内网穿透技术的远程访问架构设计及安全实现
  • 浏览器实现跨系统交互
  • 【寻找Linux的奥秘】第十章:基础文件IO(上)
  • 华为流程体系拆解系列:L1-L6分层拆解逻辑
  • CentOS 7 环境下 Visual Studio Code 安装与部署
  • 分布式系统全链路监控之一:分布式全链路监控基础概念和OpenTelemetry
  • 5.安装IK分词器
  • 鸿蒙组件通用事件开发全攻略:从基础交互到工程实践
  • 华大TAU1114-1216A00四系统GNSS定位模块,车载/穿戴/IoT全适配!-165dBm高灵敏度,定位快人一步!“
  • 基于nacos和gateway搭建微服务管理平台详细教程
  • 安宝特案例丨突破传统手术室,Vuzix AR 眼镜圆满助力全膝关节置换术
  • 【力扣 中等 C】912. 排序数组
  • 高级网络中间人攻击与加密防护机制
  • 安宝特方案丨AR破解带电配网作业困局!全方位解决方案赋能电力运维新变革
  • 日志混乱与数据不一致问题实战排查:工具协同调试记录(含克魔使用点)
  • java 数组排序算法
  • 【Linux指南】文件内容查看与文本处理
  • OpenCV CUDA模块设备层------简介
  • 做的好的淘宝客网站/怎么开发网站
  • 商丘网站建设优化推广/seo 什么意思
  • 手表价格网站/表白网站制作
  • 天津武清做网站/创建网站平台
  • 个网站能申请贝宝支付接口/免费做网站怎么做网站吗
  • 企业官网网站优化公司/优化营商环境个人心得