springboot秒杀课程学习整理1-5
1)交易模型设计
交易模型(用户下单的交易模型)OrderModel
id(String 交易单号使用String), userId,itemId,amount(数量),orderAmount(总金额), itemPrice(购买时的价格)
创建表order_info
id,userId,item_id,item_price,amount,order_amount
2)使用mybatis-generator生成对应的文件
3)生成订单的流程
1)校验商品是否存在,用户是否合法,购买数量是否正确
2)落单减库存(需要在ItemServiceImpl里添加方法用户更新库存数量,这里需要添加sql语句更新库存)
3) 生成订单(订单有一定的规则:这里是16位,由时间,自增序列,分库分表组成)
4)订单入库
5)添加销量(需要在itemServiceImpl里添加方法更新销量,并且需要新增sql语句)
OrderModel
package com.miaoshaproject.service.model; import java.math.BigDecimal; public class OrderModel {
private String id;
private Integer userId;
private Integer itemId;
private Integer amount;
private BigDecimal orderAmount;
private BigDecimal itemPrice; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public Integer getUserId() {
return userId;
} public void setUserId(Integer userId) {
this.userId = userId;
} public Integer getItemId() {
return itemId;
} public void setItemId(Integer itemId) {
this.itemId = itemId;
} public Integer getAmount() {
return amount;
} public void setAmount(Integer amount) {
this.amount = amount;
} public BigDecimal getOrderAmount() {
return orderAmount;
} public void setOrderAmount(BigDecimal orderAmount) {
this.orderAmount = orderAmount;
} public BigDecimal getItemPrice() {
return itemPrice;
} public void setItemPrice(BigDecimal itemPrice) {
this.itemPrice = itemPrice;
}
}
OrderService
package com.miaoshaproject.service; import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.service.model.OrderModel; public interface OrderService {
OrderModel createModel(Integer userId, Integer itemId, Integer amount) throws BusinessException;
}
OrderServiceIpml
package com.miaoshaproject.service.impl; import com.miaoshaproject.dao.ItemDOMapper;
import com.miaoshaproject.dao.OrderDOMapper;
import com.miaoshaproject.dao.SequenceDOMapper;
import com.miaoshaproject.dao.UserDOMapper;
import com.miaoshaproject.dataobject.ItemDO;
import com.miaoshaproject.dataobject.OrderDO;
import com.miaoshaproject.dataobject.SequenceDO;
import com.miaoshaproject.dataobject.UserDO; import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.service.OrderService;
import com.miaoshaproject.service.model.ItemModel;
import com.miaoshaproject.service.model.OrderModel;
import com.miaoshaproject.service.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date; @Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ItemServiceImpl itemService; @Autowired
private UserServiceImpl userService; @Autowired
private ItemDOMapper itemDOMapper; @Autowired
private UserDOMapper userDOMapper; @Autowired
private OrderDOMapper orderDOMapper; @Autowired
private SequenceDOMapper sequenceDOMapper; @Override
@Transactional //保证订单是在同一事务当中?
public OrderModel createModel(Integer userId, Integer itemId, Integer amount) throws BusinessException {
//1校验下单状态,下单的商品是否存在,用户是否合法,购买数量是否正确
ItemModel itemModel = itemService.getItemById(itemId);
if(itemModel == null){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,"商品信息不存在");
}
UserModel userModel =userService.getUserById(userId);
if(userModel == null){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,"用户不存在");
}
if(amount<=0 || amount>99){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,"商品数量不正确");
}
//2.落单减库存
boolean result =itemService.decreaseStock(itemId,amount);
if(!result){
throw new BusinessException(EmBusinessError.STOCK_NOT_ENOUGH);
} //3.订单入库
OrderModel orderModel = new OrderModel();
orderModel.setUserId(userId);
orderModel.setItemId(itemId);
orderModel.setAmount(amount);
orderModel.setItemPrice(itemModel.getPrice());
orderModel.setOrderAmount(itemModel.getPrice().multiply(new BigDecimal(amount)));
//生成订单号
orderModel.setId(generatorOrderNo());
OrderDO orderDO=this.convertOrderDOFromOrderModel(orderModel);
orderDOMapper.insertSelective(orderDO); //加上销售额
itemService.increaseSales(itemId,amount);
//返回前端
return orderModel;
} public OrderDO convertOrderDOFromOrderModel(OrderModel orderModel){
OrderDO orderDO = new OrderDO();
if(orderModel == null){
return null;
}
BeanUtils.copyProperties(orderModel,orderDO);
orderDO.setItemPrice(orderModel.getItemPrice().doubleValue());
orderDO.setOrderAmount(orderModel.getOrderAmount().doubleValue());
return orderDO;
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public String generatorOrderNo(){
//订单号有16
StringBuilder stringBuilder = new StringBuilder();
// 前8位是时间年月日
LocalDateTime now = LocalDateTime.now();
String nowDate = now.format(DateTimeFormatter.ISO_DATE).replace("-","");
stringBuilder.append(nowDate);
//中间6位是自增序列,
// 创建sequence_info表有字段name current_value step
//每次加step,然后更新表,不够6位用0补上
int sequence = 0;
SequenceDO sequenceDO = sequenceDOMapper.getSequenceByName("order_info");
sequence = sequenceDO.getCurrentValue();
sequenceDO.setCurrentValue(sequenceDO.getCurrentValue()+sequenceDO.getStep());
sequenceDOMapper.updateByPrimaryKey(sequenceDO);
String sequenceStr = String.valueOf(sequence);
for(int i=0;i<6 - sequenceStr.length(); i++){
//这里需要考虑的一点是如果大于6的
// 时候怎么处理
stringBuilder.append(0);
}
stringBuilder.append(sequenceStr);
//最后两位是分库分表位,这里而写死
stringBuilder.append("00"); return stringBuilder.toString();
} }
OrderController
package com.miaoshaproject.controller; import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.response.CommonReturnType;
import com.miaoshaproject.service.impl.OrderServiceImpl;
import com.miaoshaproject.service.model.OrderModel;
import com.miaoshaproject.service.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; @RestController
@RequestMapping("/order")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class OrderController extends BaseController{
@Autowired
private HttpServletRequest httpServletRequest; @Autowired
private OrderServiceImpl orderService; @RequestMapping(value="create",method = RequestMethod.POST,consumes = {CONTENT_TYPE_FORMED})
public CommonReturnType createOrder (@RequestParam(name="itemId")Integer itemId,
@RequestParam(name="amount")Integer amount
) throws BusinessException {
Boolean isLogin = (Boolean)httpServletRequest.getSession().getAttribute("IS_LOGIN");
if(isLogin == null||!isLogin.booleanValue()){
throw new BusinessException(EmBusinessError.USER_NOT_LOGIN,"用户还未登入不能下单");
}
UserModel userModel=(UserModel) httpServletRequest.getSession().getAttribute("LOGIN_USER");
//进行下单操作
OrderModel orderModel=orderService.createModel(userModel.getId(),itemId,amount); return CommonReturnType.create(null);
}
}
springboot秒杀课程学习整理1-5的更多相关文章
- springboot秒杀课程学习整理1-6
1)活动模型设计 配饰秒杀的模型(promoModel)id promoName startDate(建议使用joda-time) endDate itemId promoItemPrice 数据库( ...
- springboot秒杀课程学习整理1-1
1)新建一个maven工程quickStart,然后在pom文件里添加依赖 <parent> <groupId>org.springframework.boot</gro ...
- springboot秒杀课程学习整理1-3
1)实现手机验证码功能,用户注册功能,用户登入功能(这里讲开发流程,及本人遇到的问题,具体实现请看代码) 1.拦截请求,获取请求参数(这里的consumes是个常量,可以定义在baseControll ...
- springboot秒杀课程学习整理1-4
1)商品模型设计 (应该是先设计商品的model,然后才是数据库表) 模型字段(id,title,price(double),stock(库存),description,sales,imgUrl) 创 ...
- springboot秒杀课程学习整理1-2
1)从数据库到前端,做了三层转换,最后统一返回给前端的格式 DO-> model: 放在service中,目的是为了组装来自于数据库的数据,有些字段来自于不同的表的取,这一层相当于真正的业务模型 ...
- SpringBoot源码学习系列之异常处理自动配置
SpringBoot源码学习系列之异常处理自动配置 1.源码学习 先给个SpringBoot中的异常例子,假如访问一个错误链接,让其返回404页面 在浏览器访问: 而在其它的客户端软件,比如postm ...
- 201671010450-姚玉婷-实验十四 团队项目评审&课程学习总结
项目 内容 所属科目 软件工程http://www.cnblogs.com/nwnu-daizh 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11093584. ...
- 金生芳-实验十四 团队项目评审&课程学习总结
实验十四 团队项目评审&课程学习总结 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 作业学习目标 (1)掌握软件项目评审会流程(2)反思总结课 ...
- 201671030117 孙欢灵 实验十四 团队项目评审&课程学习总结
项目 内容 作业所属课程 所属课程 作业要求 作业要求 课程学习目标 (1)掌握软件项目评审会流程:(2)反思总结课程学习内容 任务一:团队项目审核已完成.项目验收过程意见表已上交. 任务二:课程学习 ...
随机推荐
- hiho一下 第206周
题目1 : Guess Number with Lower or Higher Hints 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 There is a game ...
- ColumnStore完整验证指南之安装与启动停止
官方文档https://mariadb.com/kb/en/library/mariadb-columnstore/ 推荐使用最新1.2.x最新版本. 先决条件 yum -y install boos ...
- range 小数据池介绍
1.range 2.小数据池 1. range 范围 [起始位置:终止位置:步长]range(起始位置,终止位置,步长) #顾头不顾尾 3.小数据池 小数据池,也称为小整数缓存机制,或者称为驻留机制等 ...
- The dependency `XXX` is not used in any concrete target.
1.在新建项目,引入CocoaPod时,当创建了podfile文件后,执行pod install时报一下错误 2.这是因为 这个第三方不知道用于哪个target,所以必须指定target 解决方案 ...
- unity 根据平板 或者 手机 确立横竖屏
/* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ####### ...
- 20175312 2018-2019-2 《Java程序设计》第6周课下选做——类定义
20175312 2018-2019-2 <Java程序设计>第6周课下选做--类定义 设计思路 1.我觉得Book其实就是一个中转的作用,由测试类Bookself通过Book输入数据,然 ...
- Python中不尽如人意的断言Assertion
Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛出异常. >>> assert 1 + 1 ...
- 使用bootstrap-select有时显示“Nothing selected”
.html()后加 $('#courseList').selectpicker('refresh'); $('#courseList').selectpicker('render'); 来源
- 换行符java去除字符串中的空格、回车、换行符、制表符
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author lei * 2011-9-2 */ publ ...
- Back To Top
//scroll to top (function ($) { $.fn.backTop = function () { var backBtn = this; var position = 1000 ...