文章目录
-
- 概要
- 整体架构流程
- 技术细节
- 小结
概要
我们通常指的是在电子商务或在线零售环境中,顾客通过互联网完成商品或服务购买的过程。随着互联网技术的发展和普及,越来越多的消费者选择在线购物,这不仅因为其便捷性,还因为它提供了更多的商品选择、价格比较和用户评价等信息。
电子商务平台,如亚马逊、阿里巴巴、京东、淘宝等,已经成为人们日常生活的一部分。这些平台不仅销售实物商品,还提供数字产品、服务预订、票务销售等多种业务。用户提交订单是这一过程中至关重要的一步,它标志着从浏览商品到实际购买的转变。
整体架构流程
技术细节
1.Controller层代码:
用户提交订单
@PostMapping("/submit")
@ApiOperation("用户提交订单")
public Result<OrderSubmitVO> OrderSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
log.info("用户提交订单,参数:{}", ordersSubmitDTO);
OrderSubmitVO orderSubmitVO = orderService.orderSubmit(ordersSubmitDTO);
return Result.success(orderSubmitVO);
}
2.Service层代码:
- 首先我们需要排除异常情况(用户购物车为空,收货地址为空)
//1.排除异常(购物车为空,地址为空) //向购物车表查询该用户的购物车,并判空 Long userId = BaseContext.getCurrentId(); ShoppingCart shoppingCart = new ShoppingCart(); shoppingCart.setUserId(userId); List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByUserId(shoppingCart); if(shoppingCarts == null || shoppingCarts.size() == 0){ throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL); } //向地址表中查询该用户的地址信息,并判空 Long addressBookId = ordersSubmitDTO.getAddressBookId(); AddressBook addressBook = addressBookMapper.getById(addressBookId); if(addressBook == null){ throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL); }
- 第二步向订单表插入一条订单数据:我们在其中的orders表中设置了几个冗余字段(image,address等),这是因为我们可以直接从dto中填充到表中,后续想要查询此类数据不需要另外查询别的表(如地址表等等).
//2.向订单表插入1条数据 Orders orders = new Orders(); BeanUtils.copyProperties(ordersSubmitDTO,orders); String adress = addressBook.getProvinceName() + addressBook.getCityName() + addressBook.getDistrictName() + addressBook.getDetail(); orders.setAddress(adress); orders.setOrderTime(LocalDateTime.now()); orders.setConsignee(addressBook.getConsignee()); orders.setUserId(userId); orders.setPayStatus(Orders.UN_PAID); orders.setStatus(Orders.PENDING_PAYMENT); orders.setNumber(String.valueOf(System.currentTimeMillis())); orderMapper.insert(orders);
- 第三步向订单明细表插入一条/多条数据
//3.向订单明细表插入n条数据 List<OrderDetail> orderDetails = new ArrayList<>(); shoppingCarts.forEach(cart -> { OrderDetail orderDetail = new OrderDetail(); BeanUtils.copyProperties(cart,orderDetail); orderDetail.setOrderId(orders.getId()); orderDetails.add(orderDetail); }); orderDetailMapper.insertBatch(orderDetails);
- 第四步清空购物车
shoppingCartMapper.deleteByUserId(userId);
- 最后返回VO对象(订单id,价格,订单号,下单时间)
//5.返回VO对象 OrderSubmitVO orderSubmitVO = new OrderSubmitVO(); orderSubmitVO.setOrderNumber(orders.getNumber()); orderSubmitVO.setOrderAmount(orders.getAmount()); orderSubmitVO.setOrderTime(orders.getOrderTime()); orderSubmitVO.setId(orders.getId()); return orderSubmitVO;
3.Mapper持久层代码:
void insert(Orders orders);
/**
* 根据订单号查询订单
* @param orderNumber
*/
@Select("select * from `sky-take-out`.orders where number = #{orderNumber}")
Orders getByNumber(String orderNumber);
/**
* 修改订单信息
* @param orders
*/
void update(Orders orders);
Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);
@Select("select * from `sky-take-out`.orders where id = #{id}")
Orders getById(Long id);
<?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.OrderMapper">
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into `sky-take-out`.orders (id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) VALUES
(#{id},#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})
</insert>
<update id="update" parameterType="com.sky.entity.Orders">
update orders
<set>
<if test="cancelReason != null and cancelReason!='' ">
cancel_reason=#{cancelReason},
</if>
<if test="rejectionReason != null and rejectionReason!='' ">
rejection_reason=#{rejectionReason},
</if>
<if test="cancelTime != null">
cancel_time=#{cancelTime},
</if>
<if test="payStatus != null">
pay_status=#{payStatus},
</if>
<if test="payMethod != null">
pay_method=#{payMethod},
</if>
<if test="checkoutTime != null">
checkout_time=#{checkoutTime},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="deliveryTime != null">
delivery_time = #{deliveryTime}
</if>
</set>
where id = #{id}
</update>
<select id="pageQuery" resultType="com.sky.entity.Orders">
select * from `sky-take-out`.orders
<where>
<if test="number != null and number!=''">
and number like concat('%',#{number},'%')
</if>
<if test="phone != null and phone!=''">
and phone like concat('%',#{phone},'%')
</if>
<if test="userId != null">
and user_id = #{userId}
</if>
<if test="status != null">
and status = #{status}
</if>
<if test="beginTime != null">
and order_time >= #{beginTime}
</if>
<if test="endTime != null">
and order_time <= #{endTime}
</if>
</where>
order by order_time desc
</select>
</mapper>
效果演示
订单表就有订单数据了