网页网站制作公司,wordpress wp rewrite,wordpress调用分类的所有子目录,深圳百度推广seo公司购物车数据是关联用户的#xff0c;在表结构中#xff0c;我们需要记录#xff0c;每一个用户的购物车数据是哪些菜品列表展示出来的既有套餐#xff0c;又有菜品#xff0c;如果用户选择的是套餐#xff0c;就保存套餐ID(setmeal_id)#xff0c;如果用户选择的是菜品在表结构中我们需要记录每一个用户的购物车数据是哪些菜品列表展示出来的既有套餐又有菜品如果用户选择的是套餐就保存套餐ID(setmeal_id)如果用户选择的是菜品就保存菜品ID(dish_id)对同一个菜品/套餐如果选择多份不需要添加多条记录增加数量number即可
DTO设计
根据添加购物车接口的参数设计DTO 在sky-pojo模块ShoppingCartDTO.java已定义
package com.sky.dto;import lombok.Data;
import java.io.Serializable;Data
public class ShoppingCartDTO implements Serializable {private Long dishId;private Long setmealId;private String dishFlavor;}Controller层
根据添加购物车接口创建ShoppingCartController
package com.sky.controller.user;import com.sky.dto.ShoppingCartDTO;
import com.sky.result.Result;
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;/*** 添加购物车* param shoppingCartDTO* return*/PostMapping(/add)ApiOperation(添加购物车)public ResultString add(RequestBody ShoppingCartDTO shoppingCartDTO){log.info(添加购物车{}, shoppingCartDTO);shoppingCartService.addShoppingCart(shoppingCartDTO);//后绪步骤实现return Result.success();}
}Service层接口
创建ShoppingCartService接口
package com.sky.service;import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import java.util.List;public interface ShoppingCartService {/*** 添加购物车* param shoppingCartDTO*/void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
}Service层实现类
创建ShoppingCartServiceImpl实现类并实现add方法
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.service.ShoppingCartService;
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;Service
public class ShoppingCartServiceImpl implements ShoppingCartService {Autowiredprivate ShoppingCartMapper shoppingCartMapper;Autowiredprivate DishMapper dishMapper;Autowiredprivate SetmealMapper setmealMapper;/*** 添加购物车** param shoppingCartDTO*/public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {ShoppingCart shoppingCart new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//只能查询自己的购物车数据shoppingCart.setUserId(BaseContext.getCurrentId());//判断当前商品是否在购物车中ListShoppingCart shoppingCartList shoppingCartMapper.list(shoppingCart);if (shoppingCartList ! null shoppingCartList.size() 1) {//如果已经存在就更新数量数量加1shoppingCart shoppingCartList.get(0);shoppingCart.setNumber(shoppingCart.getNumber() 1);shoppingCartMapper.updateNumberById(shoppingCart);} else {//如果不存在插入数据数量就是1//判断当前添加到购物车的是菜品还是套餐Long dishId shoppingCartDTO.getDishId();if (dishId ! null) {//添加到购物车的是菜品Dish dish dishMapper.getById(dishId);shoppingCart.setName(dish.getName());shoppingCart.setImage(dish.getImage());shoppingCart.setAmount(dish.getPrice());} else {//添加到购物车的是套餐Setmeal setmeal setmealMapper.getById(shoppingCartDTO.getSetmealId());shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());}shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}
}Mapper层
创建ShoppingCartMapper接口:
package com.sky.mapper;import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Delete;
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*/ListShoppingCart list(ShoppingCart shoppingCart);/*** 更新商品数量** 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 version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.sky.mapper.ShoppingCartMapperselect idlist parameterTypeShoppingCart resultTypeShoppingCartselect * from shopping_cartwhereif testuserId ! nulland user_id #{userId}/ifif testdishId ! nulland dish_id #{dishId}/ifif testsetmealId ! nulland setmeal_id #{setmealId}/ifif testdishFlavor ! nulland dish_flavor #{dishFlavor}/if/whereorder by create_time desc/select
/mapper