订单的提交:
流程分析:
订单添加接口的实现:
数据库操作:
修改了ShoppingCartMapper:
<?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.qfedu.fmmall.dao.ShoppingCartMapper">
<resultMap id="BaseResultMap" type="com.qfedu.fmmall.entity.ShoppingCart">
<result column="cart_id" jdbcType="INTEGER" property="cartId" />
<result column="product_id" jdbcType="VARCHAR" property="productId" />
<result column="sku_id" jdbcType="VARCHAR" property="skuId" />
<result column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="cart_num" jdbcType="VARCHAR" property="cartNum" />
<result column="cart_time" jdbcType="VARCHAR" property="cartTime" />
<result column="product_price" jdbcType="DECIMAL" property="productPrice" />
<result column="sku_props" jdbcType="VARCHAR" property="skuProps" />
</resultMap>
<resultMap id="ShoppingCartVOMap" type="com.qfedu.fmmall.entity.ShoppingCartVO">
<result column="cart_id" jdbcType="INTEGER" property="cartId" />
<result column="product_id" jdbcType="VARCHAR" property="productId" />
<result column="sku_id" jdbcType="VARCHAR" property="skuId" />
<result column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="cart_num" jdbcType="VARCHAR" property="cartNum" />
<result column="cart_time" jdbcType="VARCHAR" property="cartTime" />
<result column="product_price" jdbcType="DECIMAL" property="productPrice" />
<result column="sku_props" jdbcType="VARCHAR" property="skuProps" />
<result column="product_name" jdbcType="VARCHAR" property="productName" />
<result column="url" jdbcType="VARCHAR" property="productImg" />
<result column="original_price" jdbcType="VARCHAR" property="originalPrice" />
<result column="sell_price" jdbcType="VARCHAR" property="sellPrice" />
<result column="sku_name" jdbcType="VARCHAR" property="skuName" />
</resultMap>
<resultMap id="ShoppingCartVOMap2" type="com.qfedu.fmmall.entity.ShoppingCartVO">
<result column="cart_id" jdbcType="INTEGER" property="cartId" />
<result column="product_id" jdbcType="VARCHAR" property="productId" />
<result column="sku_id" jdbcType="VARCHAR" property="skuId" />
<result column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="cart_num" jdbcType="VARCHAR" property="cartNum" />
<result column="cart_time" jdbcType="VARCHAR" property="cartTime" />
<result column="product_price" jdbcType="DECIMAL" property="productPrice" />
<result column="sku_props" jdbcType="VARCHAR" property="skuProps" />
<result column="product_name" jdbcType="VARCHAR" property="productName" />
<result column="url" jdbcType="VARCHAR" property="productImg" />
<result column="original_price" jdbcType="VARCHAR" property="originalPrice" />
<result column="sell_price" jdbcType="VARCHAR" property="sellPrice" />
<result column="sku_name" jdbcType="VARCHAR" property="skuName" />
<result column="stock" property="skuStock" />
</resultMap>
<select id="selectShopCartByUserId" resultMap="ShoppingCartVOMap">
select c.cart_id,
c.product_id,
c.sku_id,
c.user_id,
c.cart_num,
c.cart_time,
c.product_price,
c.sku_props,
p.product_name, i.url,
s.original_price,s.sell_price,s.sku_name
from shopping_cart c
INNER JOIN product p
INNER JOIN product_img i
INNER JOIN product_sku s
ON c.product_id = p.product_id
and i.item_id = p.product_id
and c.sku_id = s.sku_id
where user_id = #{userId}
and i.is_main=1
</select>
<update id="updateCartNumByCartId">
update shopping_cart set cart_num=#{cartNum} where cart_id=#{cartId}
</update>
<select id="selectShopCartByCids" resultMap="ShoppingCartVOMap2">
select c.cart_id,
c.product_id,
c.sku_id,
c.user_id,
c.cart_num,
c.cart_time,
c.product_price,
c.sku_props,
p.product_name, i.url,
s.original_price,s.sell_price,s.sku_name,s.stock
from shopping_cart c
INNER JOIN product p
INNER JOIN product_img i
INNER JOIN product_sku s
ON c.product_id = p.product_id
and i.item_id = p.product_id
and c.sku_id = s.sku_id
where i.is_main = 1 and c.cart_id in
<foreach collection="list" item="cid" separator="," open="(" close=")">
#{cid}
</foreach>
</select>
</mapper>
保存订单:(tkMapper)
修改库存:(tkMapper)
保存商品快照:(tkMapper)
service接口:
package com.qfedu.fmmall.service;
import com.qfedu.fmmall.entity.Orders;
import com.qfedu.fmmall.vo.R;
/**
* @Description: 订单接口
* @Author : Jerry
* @create : 2022-07-01 17:45
*/
public interface OrderService {
public R addOrder(String cids, Orders order);
}
service实现类:
package com.qfedu.fmmall.service.impl;
import com.qfedu.fmmall.dao.OrderItemMapper;
import com.qfedu.fmmall.dao.OrdersMapper;
import com.qfedu.fmmall.dao.ProductSkuMapper;
import com.qfedu.fmmall.dao.ShoppingCartMapper;
import com.qfedu.fmmall.entity.OrderItem;
import com.qfedu.fmmall.entity.Orders;
import com.qfedu.fmmall.entity.ProductSku;
import com.qfedu.fmmall.entity.ShoppingCartVO;
import com.qfedu.fmmall.service.OrderService;
import com.qfedu.fmmall.vo.R;
import com.qfedu.fmmall.vo.ResStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.*;
/**
* @Description:
* @Author : Jerry
* @create : 2022-07-01 17:46
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ShoppingCartMapper shoppingCartMapper;
@Autowired
private OrdersMapper ordersMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private ProductSkuMapper productSkuMapper;
/**
* 保存订单业务
* @param cids 在购物车列表页面选择得购物车记录的ID
* @param order
* @return
*/
@Transactional
public R addOrder(String cids, Orders order) {
//1.校验库存:根据cids查询当前订单中关联的购物车记录详情(包括库存)
String[] arr = cids.split(",");
List<Integer> cidList = new ArrayList<>();
for (int i = 0; i<arr.length; i++){
cidList.add(Integer.parseInt(arr[i]));
}
List<ShoppingCartVO> list = shoppingCartMapper.selectShopCartByCids(cidList);
boolean f = true;
String untitled = "";
for (ShoppingCartVO sc: list){
if(Integer.parseInt(sc.getCartNum())>sc.getSkuStock()){
f = false;
}
//获取所有商品名称,以,分割拼接称字符串
untitled = untitled+sc.getProductName()+",";
}
if(f){
//2. 保存订单
//收货人信息:姓名,电话,地址,商品总价格,支付方式,订单创建时间
//订单初始状态(待支付 1)
order.setUntitled(untitled);
order.setCreateTime(new Date());
order.setStatus("1");
//生成订单编号
String orderId = UUID.randomUUID().toString().replace("-", " ");
order.setOrderId(orderId);
ordersMapper.insert(order);
//3.生成商品快照
for(ShoppingCartVO sc:list){
int cnum = Integer.parseInt(sc.getCartNum());
String itemId = System.currentTimeMillis()+""+(new Random().nextInt(89999)+10000);//增大容错率
OrderItem orderItem = new OrderItem(itemId, orderId, sc.getProductId(), sc.getProductName(), sc.getProductImg(), sc.getSkuId(), sc.getSkuName(), new BigDecimal(sc.getSellPrice())
, cnum, new BigDecimal(sc.getSellPrice()*cnum), new Date(), new Date(), 0);
orderItemMapper.insert(orderItem);
}
//4.扣减库存:根据套餐id修改套餐库存量
for (ShoppingCartVO sc: list){
String skuId = sc.getSkuId();
int newStock = sc.getSkuStock() - Integer.parseInt(sc.getCartNum());
//updateByExampleSelective只添加修改的,其他的不变
ProductSku productSku = new ProductSku();
productSku.setSkuId(skuId);
productSku.setStock(newStock);
productSkuMapper.updateByPrimaryKeySelective(productSku);
}
//5.删除购物车:当购物车中得记录购买成功之后,购物车中对应做删除操作
for(int cid:cidList){
shoppingCartMapper.deleteByPrimaryKey(cid);
}
return new R(ResStatus.OK,"下单成功!",orderId);
}else {
//表示库存不足
return new R(ResStatus.NO,"库存不足,下单失败!",null);
}
}
}
controller:
package com.qfedu.controller;
import com.qfedu.fmmall.entity.Orders;
import com.qfedu.fmmall.service.OrderService;
import com.qfedu.fmmall.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @Description:
* @Author : Jerry
* @create : 2022-07-01 19:40
*/
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping("/add")
public R add(String cids, @RequestBody Orders order){
R r = orderService.addOrder(cids, order);
return r;
}
}