前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >瑞吉外卖(八)购物车业务开发

瑞吉外卖(八)购物车业务开发

作者头像
小沐沐吖
发布2022-09-22 15:26:36
6200
发布2022-09-22 15:26:36
举报
文章被收录于专栏:小沐沐吖小沐沐吖

01、菜品展示

1、需求分析

根据分类id查询套餐信息

  • 请求地址:http://localhost:8080/setmeal/list?categoryId=分类id&status=1
  • 请求类型:GET
  • 请求参数:categoryId

2、代码实现

02.改造根据条件查询菜品数据 位置:DishController

代码语言:javascript
复制
/**
 * 根据条件查询菜品数据
 * @param dish
 * @return
 */
@GetMapping("/list")
public R<List<DishDto>> list(Dish dish){

    // 构造查询条件
    LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
    // 查询状态为 1(起售)
    queryWrapper.eq(Dish::getStatus, 1);

    // 添加排序条件
    queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

    List<Dish> list = dishService.list(queryWrapper);

    List<DishDto> dishDtoList = list.stream().map((item) -> {
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(item, dishDto);

        // 分类id
        Long categoryId = item.getCategoryId();
        // 根据id查询分类对象
        Category category = categoryService.getById(categoryId);

        if (category != null){
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
        }
        // 当前菜品id
        Long dishId = item.getId();
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dishId);

        List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);

        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }).collect(Collectors.toList());

    return R.success(dishDtoList);
}

02.根据分类id查询套餐信息 位置:SetmealController

代码语言:javascript
复制
/**
 * 根据条件查询套餐数据
 * @param setmeal
 * @return
 */
@GetMapping("/list")
public R<List<Setmeal>> list(Setmeal setmeal){
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
    queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
    queryWrapper.orderByDesc(Setmeal::getUpdateTime);
    List<Setmeal> list = setmealService.list(queryWrapper);
    return R.success(list);
}

3、测试功能

01.临时添加静态数据cartData.json 目录:src/main/resources/front/cartData.json

代码语言:javascript
复制
{"code":1,"msg":null,"data":[],"map":{}}

02.临时修改cartListApi 目录:src/main/resources/front/api/main.js

代码语言:javascript
复制
//获取购物车内商品的集合
function cartListApi(data) {
    return $axios({
        // 'url': '/shoppingCart/list',
        'url': '/front/cartData.json',
        'method': 'get',
        params:{...data}
    })
}

02、购物车添加

1、需求分析

  • 请求地址:http://localhost:8080/shoppingCart/add
  • 请求类型:POST
购物车添加
购物车添加

2、代码实现

01.ShoppingCart实体类

代码语言:javascript
复制
package cn.mu00.reggie.entity;

import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 购物车
 */
@Data
public class ShoppingCart implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    //名称
    private String name;

    //用户id
    private Long userId;

    //菜品id
    private Long dishId;

    //套餐id
    private Long setmealId;

    //口味
    private String dishFlavor;

    //数量
    private Integer number;

    //金额
    private BigDecimal amount;

    //图片
    private String image;

    private LocalDateTime createTime;
}

02.ShoppingCartMapper

代码语言:javascript
复制
package cn.mu00.reggie.mapper;

import cn.mu00.reggie.entity.ShoppingCart;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {
}

03.ShoppingCartService

代码语言:javascript
复制
package cn.mu00.reggie.service;

import cn.mu00.reggie.entity.ShoppingCart;
import com.baomidou.mybatisplus.extension.service.IService;

public interface ShoppingCartService extends IService<ShoppingCart> {
}

04.ShoppingCartServiceImpl

代码语言:javascript
复制
package cn.mu00.reggie.service.impl;

import cn.mu00.reggie.entity.ShoppingCart;
import cn.mu00.reggie.mapper.ShoppingCartMapper;
import cn.mu00.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
}

05.ShoppingCartController

代码语言:javascript
复制
package cn.mu00.reggie.controller;

import cn.mu00.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
}

06.购物车添加 位置:ShoppingCartController

代码语言:javascript
复制
/**
 * 购物车添加
 * @param shoppingCart
 * @return
 */
@PostMapping("/add")
public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
    log.info("购物车数据:{}", shoppingCart);

    // 设置用户id
    Long currentId = BaseContext.getCurrentId();
    shoppingCart.setUserId(currentId);

    // 查询当前菜品或者套餐是否在购物车中
    Long dishId = shoppingCart.getDishId();


    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId, currentId);

    if (dishId != null){
        // 存在是菜品
        queryWrapper.eq(ShoppingCart::getDishId, dishId);
    }else {
        // 不存在是套餐
        queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
    }

    ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

    if (cartServiceOne != null){
        // 存在,则原数量+1
        Integer number = cartServiceOne.getNumber();
        cartServiceOne.setNumber(number + 1);
        shoppingCartService.updateById(cartServiceOne);
    }else {
        // 不存在,则添加数据
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartService.save(shoppingCart);
        cartServiceOne = shoppingCart;
    }
    return R.success(cartServiceOne);
}

03、购物车查询

恢复菜品展示的修改

1、需求分析

  • 请求地址:http://localhost:8080/shoppingCart/list
  • 请求类型:GET

2、代码实现

代码语言:javascript
复制
/**
 * 根据用户id查询购物车
 * @return
 */
@GetMapping("/list")
public R<List<ShoppingCart>> list(){
    // 用户id
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId, currentId);
    queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
    List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
    return R.success(list);
}

03、购物车修改

1、需求分析

  • 请求地址:http://localhost:8080/shoppingCart/sub
  • 请求类型:POST
  • 请求参数:dishIdsetmealId

2、代码实现

代码语言:javascript
复制
/**
 * 购物车更新
 * @param shoppingCart
 * @return
 */
@PostMapping("/sub")
public R<String> sub(@RequestBody ShoppingCart shoppingCart){
    Long currentId = BaseContext.getCurrentId();
    // 查询当前菜品或者套餐是否在购物车中
    Long dishId = shoppingCart.getDishId();


    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId, currentId);
    if (dishId != null){
        // 存在是菜品
        queryWrapper.eq(ShoppingCart::getDishId, dishId);
    }else {
        // 不存在是套餐
        queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
    }
    ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

    Integer number = cartServiceOne.getNumber();

    // 判断数量是否为1
    if (number == 1){
        // 删除
        shoppingCartService.remove(queryWrapper);
    }else {
        // 减少
        cartServiceOne.setNumber(number - 1);
        shoppingCartService.updateById(cartServiceOne);
    }
    return R.success("更新成功");
}

03、购物车清空

1、需求分析

  • 请求地址:http://localhost:8080/shoppingCart/clean
  • 请求类型:DELETE

2、代码实现

代码语言:javascript
复制
/**
 * 清空购物车
 * @return
 */
@DeleteMapping("/clean")
public R<String> clean(){
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId, currentId);
    shoppingCartService.remove(queryWrapper);
    return R.success("清空购物车成功");
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022 年 08 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 01、菜品展示
    • 1、需求分析
      • 2、代码实现
        • 3、测试功能
        • 02、购物车添加
          • 1、需求分析
            • 2、代码实现
            • 03、购物车查询
              • 1、需求分析
                • 2、代码实现
                • 03、购物车修改
                  • 1、需求分析
                    • 2、代码实现
                    • 03、购物车清空
                      • 1、需求分析
                        • 2、代码实现
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档