springboot项目中外卖用户下单业务功能之需求分析+数据模型+功能开发(详细步骤)

Posted hhzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot项目中外卖用户下单业务功能之需求分析+数据模型+功能开发(详细步骤)相关的知识,希望对你有一定的参考价值。

一、需求分析

移动端用户将菜品或者套餐加入到购物车后,可以点击购物车种的 去结算 按钮,页面跳转到订单确认页面,点击 去支付 按钮则完成下单操作。

二、数据模型

用户下单业务对应的数据表为orders表和order_detail表:

  1. orders:订单表
CREATE TABLE `orders` (
  `id` bigint NOT NULL COMMENT '主键',
  `number` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '订单号',
  `status` int NOT NULL DEFAULT '1' COMMENT '订单状态 1待付款,2待派送,3已派送,4已完成,5已取消',
  `user_id` bigint NOT NULL COMMENT '下单用户',
  `address_book_id` bigint NOT NULL COMMENT '地址id',
  `order_time` datetime NOT NULL COMMENT '下单时间',
  `checkout_time` datetime NOT NULL COMMENT '结账时间',
  `pay_method` int NOT NULL DEFAULT '1' COMMENT '支付方式 1微信,2支付宝',
  `amount` decimal(10,2) NOT NULL COMMENT '实收金额',
  `remark` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '备注',
  `phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `consignee` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='订单表';
  1. order_detail:订单明细表
CREATE TABLE `order_detail` (
  `id` bigint NOT NULL COMMENT '主键',
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '名字',
  `image` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '图片',
  `order_id` bigint NOT NULL COMMENT '订单id',
  `dish_id` bigint DEFAULT NULL COMMENT '菜品id',
  `setmeal_id` bigint DEFAULT NULL COMMENT '套餐id',
  `dish_flavor` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '口味',
  `number` int NOT NULL DEFAULT '1' COMMENT '数量',
  `amount` decimal(10,2) NOT NULL COMMENT '金额',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='订单明细表';


三、用户下单

1. 代码开发-梳理交互过程

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

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

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

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

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

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

2. 代码开发

在开发业务功能之前,先将需要用到的类和接口基本结构创建好:

实体类 Orders:

package com.tigerhhzz.wuaimai.entity;

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

/**
 * 订单
 */
@Data
public class Orders implements Serializable 

    private static final long serialVersionUID = 1L;

    private Long id;

    //订单号
    private String number;

    //订单状态 1待付款,2待派送,3已派送,4已完成,5已取消
    private Integer status;


    //下单用户id
    private Long userId;

    //地址id
    private Long addressBookId;


    //下单时间
    private LocalDateTime orderTime;


    //结账时间
    private LocalDateTime checkoutTime;


    //支付方式 1微信,2支付宝
    private Integer payMethod;


    //实收金额
    private BigDecimal amount;

    //备注
    private String remark;

    //用户名
    private String userName;

    //手机号
    private String phone;

    //地址
    private String address;

    //收货人
    private String consignee;


实体类 OrderDetail:

package com.tigerhhzz.wuaimai.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 订单明细
 */
@Data
public class OrderDetail implements Serializable 

    private static final long serialVersionUID = 1L;

    private Long id;

    //名称
    private String name;

    //订单id
    private Long orderId;


    //菜品id
    private Long dishId;


    //套餐id
    private Long setmealId;


    //口味
    private String dishFlavor;


    //数量
    private Integer number;

    //金额
    private BigDecimal amount;

    //图片
    private String image;


Mapper接口 OrderMapper:

package com.tigerhhzz.wuaimai.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.tigerhhzz.wuaimai.entity.Orders;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface OrderMapper extends BaseMapper<Orders> 


Mapper接口 OrderDetailMapper:

package com.tigerhhzz.wuaimai.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.tigerhhzz.wuaimai.entity.OrderDetail;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface OrderDetailMapper extends BaseMapper<OrderDetail> 


业务接口OrderService:

package com.tigerhhzz.wuaimai.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tigerhhzz.wuaimai.entity.Orders;


public interface OrderService extends IService<Orders> 

    /**
     * 用户下单
     * @param orders
     */
    public void submit(Orders orders);


业务接口OrderDetailService

package com.tigerhhzz.wuaimai.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tigerhhzz.wuaimai.entity.OrderDetail;


public interface OrderDetailService extends IService<OrderDetail> 



业务层实现类 OrderServiceImpl:

package com.tigerhhzz.wuaimai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tigerhhzz.wuaimai.common.BaseContext;
import com.tigerhhzz.wuaimai.common.CustomException;
import com.tigerhhzz.wuaimai.entity.*;
import com.tigerhhzz.wuaimai.mapper.OrderMapper;
import com.tigerhhzz.wuaimai.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService 

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     */
    @Override
    @Transactional
    public void submit(Orders orders) 
    	log.info("订单数据:", orders);
        //获得当前用户id
        Long userId = BaseContext.getCurrentId();

        //查询当前用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(wrapper);

        if(shoppingCarts == null || shoppingCarts.size() == 0)
            throw new CustomException("购物车为空,不能下单");
        

        //查询用户数据
        User user = userService.getById(userId);

        //查询地址数据
        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 = shoppingCarts.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.setId(orderId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(new BigDecimal(amount.get()));//总金额
        orders.setUserId(userId);
        orders.setNumber(String.valueOf(orderId));
        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(wrapper);
    

业务层实现类OrderDetailServiceImpl:

package com.tigerhhzz.wuaimai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tigerhhzz.wuaimai.entity.OrderDetail;
import com.tigerhhzz.wuaimai.mapper.OrderDetailMapper;
import com.tigerhhzz.wuaimai.service.OrderDetailService;
import org.springframework.stereotype.Service;

@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService 


控制层OrderController:

package com.tigerhhzz.wuaimai.controller;


import com.tigerhhzz.wuaimai.common.R;
import com.tigerhhzz.wuaimai.entity.Orders;
import com.tigerhhzz.wuaimai.service.OrderService;
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;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController 

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders)
        log.info("订单数据:",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    

控制层OrderDetailController:

package com.tigerhhzz.wuaimai.controller;


import com.tigerhhzz.wuaimai.service.OrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单明细
 */
@Slf4j
@RestController
@RequestMapping("/orderDetail")
public class OrderDetailController 

    @Autowired
    private OrderDetailService orderDetailService;


3. 移动端查询订单

请求地址:http://localhost:8080/order/userPage?page=页数&pageSize=每页数量
请求类型:GET
请求参数:page,默认1,pageSize,默认5

OrdersService:

/**
 * 分页查询订单
 * @param page
 * @param pageSize
 * @return
 */
Page<OrdersDto> userPage(Integer page, Integer pageSize);

OrdersServiceImpl:

/**
 * 分页查询订单
 * @param page
 * @param pageSize
 * @return
 */
@Override
public Page<OrdersDto> userPage(Integer page, Integer pageSize) 
    // 分页构造器
    Page<Orders> ordersPage = new Page<>(page, pageSize);

    // 条件构造器
    LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
    ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);

    this.page(ordersPage,ordersLambdaQueryWrapper);

    Page<OrdersDto> ordersDtoPage = new Page<>();

    // 分页的ordersDtoPage,没有records
    BeanUtils.copyPropert

以上是关于springboot项目中外卖用户下单业务功能之需求分析+数据模型+功能开发(详细步骤)的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot+MP实现简单购物车并集成用户下单功能

基于Springboot和mybatis的外卖项目瑞吉外卖Day5

SpringBoot实现静态资源映射,登录功能以及访问拦截验证——以黑马瑞吉外卖为例

SpringBoot项目SpringBoot项目-瑞吉外卖day03分类管理

基于SpringBoot的外卖项目(详细开发过程)

关于项目搭建