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的更多相关文章

  1. springboot秒杀课程学习整理1-6

    1)活动模型设计 配饰秒杀的模型(promoModel)id promoName startDate(建议使用joda-time) endDate itemId promoItemPrice 数据库( ...

  2. springboot秒杀课程学习整理1-1

    1)新建一个maven工程quickStart,然后在pom文件里添加依赖 <parent> <groupId>org.springframework.boot</gro ...

  3. springboot秒杀课程学习整理1-3

    1)实现手机验证码功能,用户注册功能,用户登入功能(这里讲开发流程,及本人遇到的问题,具体实现请看代码) 1.拦截请求,获取请求参数(这里的consumes是个常量,可以定义在baseControll ...

  4. springboot秒杀课程学习整理1-4

    1)商品模型设计 (应该是先设计商品的model,然后才是数据库表) 模型字段(id,title,price(double),stock(库存),description,sales,imgUrl) 创 ...

  5. springboot秒杀课程学习整理1-2

    1)从数据库到前端,做了三层转换,最后统一返回给前端的格式 DO-> model: 放在service中,目的是为了组装来自于数据库的数据,有些字段来自于不同的表的取,这一层相当于真正的业务模型 ...

  6. SpringBoot源码学习系列之异常处理自动配置

    SpringBoot源码学习系列之异常处理自动配置 1.源码学习 先给个SpringBoot中的异常例子,假如访问一个错误链接,让其返回404页面 在浏览器访问: 而在其它的客户端软件,比如postm ...

  7. 201671010450-姚玉婷-实验十四 团队项目评审&课程学习总结

    项目 内容 所属科目 软件工程http://www.cnblogs.com/nwnu-daizh 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11093584. ...

  8. 金生芳-实验十四 团队项目评审&课程学习总结

    实验十四 团队项目评审&课程学习总结 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 作业学习目标 (1)掌握软件项目评审会流程(2)反思总结课 ...

  9. 201671030117 孙欢灵 实验十四 团队项目评审&课程学习总结

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 课程学习目标 (1)掌握软件项目评审会流程:(2)反思总结课程学习内容 任务一:团队项目审核已完成.项目验收过程意见表已上交. 任务二:课程学习 ...

随机推荐

  1. 利用composer安装laraval

    首先,毋庸置疑我们需要安装composer.这个在我上一篇文章中有提到,这里不做过多赘述. 其次,配置composer国内镜像.(如果不配置国内镜像,你们懂得) 打开cmd输入以下命令即可 compo ...

  2. 【Alpha】Scrum Meeting 9

    目录 前言 任务分配 燃尽图 会议照片 签入记录 困难 前言 第9次会议于4月13日20:00在一公寓3楼召开. 交流确认了任务进度,对下一阶段任务进行分配.时长20min. 任务分配 姓名 当前阶段 ...

  3. uirecorder 启动webdriver服务报错

    在安装好uirecorder后,执行起来是各种错误. 不是少这个就是缺那个,也是因为自己对自动化测试知识太匮乏. 导致刚开始走自动化测试绕了很多弯路,报个错都不知所措.后来才知道要多看ERROR后面的 ...

  4. Spring boot Spring cloud 框架搭建

    随笔记载几个框架搭建时的坑: 这个是server提供者模块,需要注意的是spring:application:name 接下来是fegin模块,需要主要注意信息已说明,需要特别说明的是RequestM ...

  5. tcp config

    $ sudo sysctl net.ipv4.tcp_reordering=1 $ sudo sysctl net.ipv4.tcp_thin_linear_timeouts=1 $ sudo sys ...

  6. 使用ffmpeg进行视频封面截取

    项目需求:用户上传视频格式的文件,需要转为指定编码的MP4格式(为了适应在线播放),并且截取视频的第一帧作为封面图片(用于展示) 实现: 1.下载ffmpeg.exe 地址:http://ffmpeg ...

  7. sql server把一个库表的某个字段更新到另一张表的相同字段

    缘由:进行update时忘了加where条件,导致所有数据全部update.. sql: 1 update Activity set endTime=b.endTime from idj_tl_bf. ...

  8. 如何查找redis使用的是哪个配置文件

    ps -ef|grep redis 得到了进程号 xxxx 然后 ls -l /proc/xxxx/cwd ps:可以推广到其他进程,只要有pid,就能找到配置文件

  9. glup简单应用---gulpfile.js

    //npm install gulp -g (global环境) //npm install gulp --save-dev (项目环境) //在项目中install需要的gulp插件,一般只压缩的话 ...

  10. stylus入门学习笔记

    title: stylus入门学习笔记 date: 2018-09-06 17:35:28 tags: [stylus] description: 学习到 vue, 有人推荐使用 stylus 这个 ...