首页 > 其他分享 >瑞吉外卖(六)

瑞吉外卖(六)

时间:2023-03-18 11:12:14浏览次数:41  
标签:queryWrapper LambdaQueryWrapper addressBook 瑞吉 shoppingCart Result 外卖 new

用户地址簿相关功能

代码开发

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    public Result<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return Result.success(addressBook);
        } else {
            return Result.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public Result<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return Result.error("没有找到该对象");
        } else {
            return Result.success(addressBook);
        }
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return Result.success(addressBookService.list(queryWrapper));
    }
}

菜品展示

代码开发

代码开发-梳理交互过程

在开发代码之前,需要梳理一下前端页面和服务端的交互过程:

1、页面(front/index.html)发送ajax请求,获取分类数据(菜品分类和套餐分类)

2、页面发送ajax请求,获取第一个分类下的菜品或者套餐

开发菜品展示功能,其实就是在服务端编写代码去处理前端页面发送的这2次请求即可。

改造DishController中的list方法:

@GetMapping("/list")
public Result<List<DishDto>> getDishByCategory(Dish dish){
    LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
    dishLambdaQueryWrapper.eq(Dish::getStatus,1);
    dishLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
    dishLambdaQueryWrapper.like(StringUtils.hasLength(dish.getName()),Dish::getName,dish.getName());
    dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
    List<Dish> dishes = dishService.list(dishLambdaQueryWrapper);

    List<DishDto> dishDtos = new LinkedList<>();
    dishes.forEach(dishItem->{
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dishItem,dishDto);
        Long dishId = dishDto.getId();

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
        List<DishFlavor> flavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(flavors);

        dishDtos.add(dishDto);

    });

    return Result.success(dishDtos);
}

在SetmealController里添加list方法显示套餐信息:

@GetMapping("/list")
public Result<List<Setmeal>> list( Setmeal setmeal){
    log.info(setmeal.toString());

    LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
    setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
    setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
    setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

    List<Setmeal> setmeals = setmealService.list(setmealLambdaQueryWrapper);

    return Result.success(setmeals);

}

购物车

代码开发

在开发代码之前,需要梳理一下购物车操作时前端页面和服务端的交互过程:

1、点击 [加入购物车] 或者 [+] 按钮,页面发送ajax请求,请求服务端,将菜品或者套餐添加到购物车

添加菜品到购物车
@PostMapping("/add")
public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
    log.info(shoppingCart.toString());
    Long userId = BaseContext.getCurrentId();
    shoppingCart.setUserId(userId);

    LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
    shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
    shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
    shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
    ShoppingCart shoppingCartInfo = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
    if (shoppingCartInfo!=null){
        Integer number = shoppingCartInfo.getNumber();
        shoppingCartInfo.setNumber(number+1);
        shoppingCartService.updateById(shoppingCartInfo);
    }else {
        shoppingCart.setNumber(1);
        shoppingCartService.save(shoppingCart);
    }

    return Result.success(shoppingCart);
}

2、点击购物车图标,页面发送ajax请求,请求服务端查询购物车中的菜品和套餐

查询购物车
@GetMapping("list")
public Result<List<ShoppingCart>> list(){
    Long userId = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
    shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
    List<ShoppingCart> shoppingCarts = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
    return Result.success(shoppingCarts);
}

3、点击清空购物车按钮,页面发送ajax请求,请求服务端来执行清空购物车操作

清空购物车
@DeleteMapping("/clean")
public Result<String> clean(){
    Long userId = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId,userId);
    shoppingCartService.remove(queryWrapper);
    return Result.success("操作成功");
}

4、点击 [-] 按钮,页面发送ajax请求,请求服务端,将菜品或者套餐从购物车减少

减少商品数量
@PostMapping("/sub")
public Result<String> sub(@RequestBody ShoppingCart shoppingCart){
    log.info(shoppingCart.toString());
    Long userId = BaseContext.getCurrentId();

    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
    queryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
    queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
    shoppingCart = shoppingCartService.getOne(queryWrapper);

    if (shoppingCart!=null&&shoppingCart.getNumber()-1>1){
        shoppingCart.setNumber(shoppingCart.getNumber()-1);
        shoppingCartService.updateById(shoppingCart);
    }else {
        shoppingCartService.remove(queryWrapper);
    }

    return Result.success("操作成功");
}

开发购物车功能,其实就是在服务端编写代码去处理前端页面发送的这3次请求即可。

下单

代码开发

代码开发-梳理交互过程

在开发代码之前,需要梳理一下用户下单操作时前端页面和服务端的交互过程:

1、在购物车中点击 【去结算】 按钮,页面跳转到订单确认页面

2、在订单确认页面,发送ajax请求,请求服务端获取当前登录用户的默认地址

3、在订单确认页面,发送ajax请求,请求服务端获取当前登录用户的购物车数据

4、在订单确认页面点击 【去支付】 按钮,发送ajax请求,请求服务端完成下单操作

开发用户下单功能,其实就是在服务端编写代码去处理前端页面发送的请求即可。

在OrderService添加submit方法用于用户下单

@Override
@Transactional
public void submit(Orders orders) {
    //获取当前用户id
    Long currentId = BaseContext.getCurrentId();
    //查询当前用户的购物车数据
    LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId,currentId);
    List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

    if (list==null||list.size()==0){
        throw new CustomException("购物车为空,不能下单");
    }
    //查询用户数据
    User user = userService.getById(currentId);
    //查询地址数据
    Long addressBookId = orders.getAddressBookId();
    AddressBook addressBook = addressBookService.getById(addressBookId);
    if(addressBook==null){
        throw new CustomException("地址有误,不能下单");
    }

    long orderId = IdWorker.getId();//订单号

    AtomicInteger amount=new AtomicInteger(0);

    List<OrderDetail> orderDetails=list.stream().map((item)->{
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(orderId);
        orderDetail.setNumber(item.getNumber());
        orderDetail.setDishFlavor(item.getDishFlavor());
        orderDetail.setDishId(item.getDishId());
        orderDetail.setSetmealId(item.getSetmealId());
        orderDetail.setName(item.getName());
        orderDetail.setImage(item.getImage());
        orderDetail.setAmount(item.getAmount());
        amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
        return orderDetail;
    }).collect(Collectors.toList());

    //向订单表中插入一条数据
    orders.setNumber(String.valueOf(orderId));
    orders.setId(orderId);
    orders.setOrderTime(new Date());
    orders.setCheckoutTime(new Date());
    orders.setStatus(2);
    orders.setAmount(new BigDecimal(amount.get()));//计算总金额
    orders.setUserId(currentId);
    orders.setUserName(user.getName());
    orders.setConsignee(addressBook.getConsignee());
    orders.setPhone(addressBook.getPhone());
    orders.setAddress((addressBook.getProvinceName()==null?"":addressBook.getProvinceName())
            +(addressBook.getCityName()==null?"":addressBook.getCityName())
            +(addressBook.getDistrictName()==null?"":addressBook.getDistrictName())
            +(addressBook.getDetail()==null?"":addressBook.getDetail()));
    this.save(orders);

    //向订单明细表中插入多条数据
    orderDetailService.saveBatch(orderDetails);
    //清空购物车数据
    shoppingCartService.remove(queryWrapper);
}

标签:queryWrapper,LambdaQueryWrapper,addressBook,瑞吉,shoppingCart,Result,外卖,new
From: https://www.cnblogs.com/godofball/p/17229579.html

相关文章

  • 瑞吉外卖(三)
    1、公共字段自动填充问题分析我们已经完成了后台系统的员工管理功能开发,在新增员工时需要设置创建时间、创建人、修改时间、修改人等字段,在编辑员工时需要设置修改时间和......
  • 对校园外卖系统的分析和改进记录
    最近在网上找到一个校园外卖系统,我对这个项目进行了研究,但我认为这个项目可以在业务流程方面进一步优化。这是此项目的大致流程图 我认为该系统虽然比较完善,但如果上......
  • 外卖项目笔记
    1.SpringBoot启动报错错误信息:org.springframework.core.NestedIOException:ASMClassReaderfailedtoparseclassfile-probablyduetoanewJavaclassfile......
  • 如何快速开发一款外卖app?
    随着线上新零售app开发越来越成熟,外卖app开发的确是一个现有比较不错的选择。许多人不了解外卖app的开发,下面我们就来看看如何快速开发一款外卖app。一、外卖app开发......
  • 瑞吉外卖项目介绍
    一、项目介绍本项目(瑞吉外卖)是专门为餐饮企业(餐厅、饭店)定制的一款软件产品,包括系统管理后台和移动端应用两部分。其中系统管理后台主要提供给餐饮企业内部员工使用,可......
  • 瑞吉外卖
     初始化项目---创建数据库  导入脚本文件            创建maven项目导入pom文件   导入yml配置文件  创建启动类 ......
  • SpringBoot 项目实战 | 瑞吉外卖 优化篇 Day01
    该系列将记录一份完整的实战项目的完成过程,该篇属于优化篇第一天,主要负责完成缓存优化问题案例来自B站黑马程序员Java项目实战《瑞吉外卖》,请结合课程资料阅读以下内容该......
  • 盯着美团做外卖?不,抖音的最终目标,是干掉央视【快评】
    文|螳螂观察作者|张璐抖音做外卖引发广泛争议,虽然抖音辟谣自己的“亲自下场”,但与饿了么的合作也几乎板上钉钉。看样子,这是一个流量大户与外卖二哥合谋,要干掉外卖一哥的......
  • SpringBoot 项目实战 | 瑞吉外卖 Day06
    该系列将记录一份完整的实战项目的完成过程,该篇属于第六天案例来自B站黑马程序员Java项目实战《瑞吉外卖》,请结合课程资料阅读以下内容该篇我们将完成以下内容:用户地址......
  • SpringBoot 项目实战 | 瑞吉外卖 Day05
    该系列将记录一份完整的实战项目的完成过程,该篇属于第五天案例来自B站黑马程序员Java项目实战《瑞吉外卖》,请结合课程资料阅读以下内容该篇我们将完成以下内容:新增套餐......