当前位置:网站首页>Order submission
Order submission
2022-07-27 18:50:00 【Fantasia of the cat】
Order submission :
Process analysis :

Implementation of order addition interface :
Database operation :

Revised 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>Save order :(tkMapper)
Modify inventory :(tkMapper)
Save product snapshot :(tkMapper)
service Interface :
package com.qfedu.fmmall.service;
import com.qfedu.fmmall.entity.Orders;
import com.qfedu.fmmall.vo.R;
/**
* @Description: Order interface
* @Author : Jerry
* @create : 2022-07-01 17:45
*/
public interface OrderService {
public R addOrder(String cids, Orders order);
}
service Implementation class :
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;
/**
* Save order business
* @param cids Select the record of the shopping cart on the shopping cart list page ID
* @param order
* @return
*/
@Transactional
public R addOrder(String cids, Orders order) {
//1. Verify inventory : according to cids Query the details of the shopping cart records associated with the current order ( Including inventory )
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;
}
// Get all product names , With , Split splicing is called string
untitled = untitled+sc.getProductName()+",";
}
if(f){
//2. Save order
// Consignee information : full name , Telephone , Address , The total price of the goods , Method of payment , Order creation time
// Initial order status ( To be paid 1)
order.setUntitled(untitled);
order.setCreateTime(new Date());
order.setStatus("1");
// Generate order number
String orderId = UUID.randomUUID().toString().replace("-", " ");
order.setOrderId(orderId);
ordersMapper.insert(order);
//3. Generate product snapshot
for(ShoppingCartVO sc:list){
int cnum = Integer.parseInt(sc.getCartNum());
String itemId = System.currentTimeMillis()+""+(new Random().nextInt(89999)+10000);// Increase fault tolerance
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. Deducting the inventory : According to the package id Modify package inventory
for (ShoppingCartVO sc: list){
String skuId = sc.getSkuId();
int newStock = sc.getSkuStock() - Integer.parseInt(sc.getCartNum());
//updateByExampleSelective Add only modified , The rest is the same
ProductSku productSku = new ProductSku();
productSku.setSkuId(skuId);
productSku.setStock(newStock);
productSkuMapper.updateByPrimaryKeySelective(productSku);
}
//5. Delete shopping cart : When the shopping cart records the purchase success , Delete the shopping cart
for(int cid:cidList){
shoppingCartMapper.deleteByPrimaryKey(cid);
}
return new R(ResStatus.OK," checkout success !",orderId);
}else {
// It means that there is not enough stock
return new R(ResStatus.NO," Insufficient inventory , Order failure !",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;
}
}
边栏推荐
- Was not registered for synchronization because synchronization is not active[resolved]
- Complete set of machine learning classification task effect evaluation indicators (including ROC and AUC)
- 低噪负离子风扇触摸IC
- What should I do if MySQL master-slave replication data is inconsistent?
- 怎么会不喜欢呢,CI/CD中轻松发送邮件
- Pandas' to_ SQL function usage
- 全自动吸奶器芯片-DLTAP703SD
- 图文结合,完美解释MySQL逻辑备份的实现流程
- 2021.7.17 notes MySQL other commands
- pandas的to_sql函数使用
猜你喜欢

Uni app label jump

Commodity comment information and comment information classification

"MySQL things" explains the indexing principle in detail

JS to realize simple form verification and select all functions

What does the number of network request interface layers (2/3 layers) mean

Uni app for wechat login (to be finished)

个人中心--订单业务流程

20000 words + 30 pictures | what's the use of chatting about MySQL undo log, redo log and binlog?

Login page tablelayout

Alibaba architects spent 280 hours sorting out 1015 pages of distributed full stack pamphlets to easily start the distributed system
随机推荐
知识图谱 — jieba、pyhanlp、smoothnlp工具实现中文分词(词性表)
Vue uses keep alive to realize page caching
Jianmu continuous integration platform v2.5.2 release
Have you ever stumbled on MySQL's order by
Functions in JS and the use of DOM to obtain elements and event attributes
What does the number of network request interface layers (2/3 layers) mean
解决Jsp级联问题
Zhaoqi scientific and technological innovation introduces high-level talents at home and abroad and connects innovation and entrepreneurship projects
Preliminary introduction to C miscellaneous lecture linked list
The hero of the aircraft war comes out with bullets
百度地图技术概述,及基本API与WebApi的应用开发
全身多功能按摩仪芯片-DLTAP602SD
全自动吸奶器芯片-DLTAP703SD
10 SQL optimization schemes summarized by Alibaba P8 (very practical)
LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
Log4j 史诗级漏洞,京东这样的大厂都中招了
Wechat applet obtains openid, sessionkey, unionid
MySQL learning Day1 DDL, DML, DQL basic query
Generate PDM file from Navicat export table
家用静音驱蚊灯芯片-DLTAP703SD-杰力科创