Spring Boot 构建电商基础秒杀项目 (十一) 秒杀
SpringBoot构建电商基础秒杀项目 学习笔记
新建表
create table if not exists promo (
id int not null auto_increment,
promo_name varchar(64) not null default '',
start_date datetime not null default '0000-00-00 00:00:00',
end_date datetime not null default '0000-00-00 00:00:00',
item_id int not null default 0,
promo_item_price double(10,2) not null default 0,
primary key (id)
);
新增 PromoModel
public class PromoModel {
private Integer id;
// 1: 还未开始 2: 进行中 3: 已结束
private Integer status;
private String promoName;
private DateTime startDate;
private DateTime endDate;
private Integer itemId;
private BigDecimal promoItemPrice;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getPromoName() {
return promoName;
}
public void setPromoName(String promoName) {
this.promoName = promoName;
}
public DateTime getStartDate() {
return startDate;
}
public void setStartDate(DateTime startDate) {
this.startDate = startDate;
}
public DateTime getEndDate() {
return endDate;
}
public void setEndDate(DateTime endDate) {
this.endDate = endDate;
}
public Integer getItemId() {
return itemId;
}
public void setItemId(Integer itemId) {
this.itemId = itemId;
}
public BigDecimal getPromoItemPrice() {
return promoItemPrice;
}
public void setPromoItemPrice(BigDecimal promoItemPrice) {
this.promoItemPrice = promoItemPrice;
}
}
新增 PromoService
public interface PromoService {
PromoModel getPromoByItemId(Integer itemId);
}
新增 PromoServiceImpl
@Service
public class PromoServiceImpl implements PromoService {
@Autowired
private PromoDOMapper promoDOMapper;
@Override
public PromoModel getPromoByItemId(Integer itemId) {
PromoDO promoDO = promoDOMapper.selectByItemId(itemId);
PromoModel promoModel = convertFromDataObject(promoDO);
if(promoDOMapper == null){
return null;
}
if(promoModel.getStartDate().isAfterNow()){
promoModel.setStatus(1);
}else if(promoModel.getEndDate().isBeforeNow()){
promoModel.setStatus(3);
}else{
promoModel.setStatus(2);
}
return promoModel;
}
private PromoModel convertFromDataObject(PromoDO promoDO){
if(promoDO == null){
return null;
}
PromoModel promoModel = new PromoModel();
BeanUtils.copyProperties(promoDO, promoModel);
promoModel.setPromoItemPrice(new BigDecimal(promoDO.getPromoItemPrice()));
promoModel.setStartDate(new DateTime(promoDO.getStartDate()));
promoModel.setEndDate(new DateTime(promoDO.getEndDate()));
return promoModel;
}
}
ItemModel 添加
private PromoModel promoModel;
修改 ItemController, OrderController 对应的 service 及详情页
private ItemVO convertFromModel(ItemModel itemModel){
if(itemModel == null){
return null;
}
ItemVO itemVO = new ItemVO();
BeanUtils.copyProperties(itemModel, itemVO);
if(itemModel.getPromoModel() != null){
itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
itemVO.setPromoId(itemModel.getPromoModel().getId());
itemVO.setStartDate(itemModel.getPromoModel().getStartDate()
.toString(DateTimeFormat.forPattern("yyy-MM-dd HH:mm:ss")));
itemVO.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
}else{
itemVO.setPromoStatus(0);
}
return itemVO;
}
@RequestMapping(value = "/createorder", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType createOrder(@RequestParam(name="itemId") Integer itemId,
@RequestParam(name="amount") Integer amount,
@RequestParam(name="promoId", required = false) Integer promoId)
throws BusinessException {
Boolean isLogin = (Boolean)httpServletRequest.getSession().getAttribute("LOGIN");
if(isLogin == null || !isLogin.booleanValue()){
throw new BusinessException(EmBusinessError.USER_NOT_LOGIN);
}
UserModel userModel = (UserModel)httpServletRequest.getSession().getAttribute("LOGIN_USER");
OrderModel orderModel = orderService.createOrder(userModel.getId(), itemId, promoId, amount);
return CommonReturnType.create(null);
}
Spring Boot 构建电商基础秒杀项目 (十一) 秒杀的更多相关文章
- Spring Boot 构建电商基础秒杀项目 (一) 项目搭建
SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...
- Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)
SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...
- Spring Boot 构建电商基础秒杀项目 (十) 交易下单
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists order_info ( id varchar(32) not null defaul ...
- Spring Boot 构建电商基础秒杀项目 (九) 商品列表 & 详情
SpringBoot构建电商基础秒杀项目 学习笔记 ItemDOMapper.xml 添加 <select id="listItem" resultMap="Bas ...
- Spring Boot 构建电商基础秒杀项目 (八) 商品创建
SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...
- Spring Boot 构建电商基础秒杀项目 (七) 自动校验
SpringBoot构建电商基础秒杀项目 学习笔记 修改 UserModel 添加注解 public class UserModel { private Integer id; @NotBlank(m ...
- Spring Boot 构建电商基础秒杀项目 (六) 用户登陆
SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...
- Spring Boot 构建电商基础秒杀项目 (五) 用户注册
SpringBoot构建电商基础秒杀项目 学习笔记 UserService 添加 void register(UserModel userModel) throws BusinessException ...
- Spring Boot 构建电商基础秒杀项目 (四) getotp 页面
SpringBoot构建电商基础秒杀项目 学习笔记 BaseController 添加 public static final String CONTENT_TYPE_FORMED = "a ...
随机推荐
- 初学Python——面向对象(二)
一.抽象类.接口类和抽象接口 转自博客园魏恒https://www.cnblogs.com/weihengblog/p/8528967.html (一)接口类 什么是接口类?在继承中,我们可以声明某个 ...
- Java线程和多线程(十五)——线程的活性
当开发者在应用中使用了并发来提升性能的同时,开发者也需要注意线程之间有可能会相互阻塞.当整个应用执行的速度比预期要慢的时候,也就是应用没有按照预期的执行时间执行完毕.在本章中,我们来需要仔细分析可能会 ...
- flask使用原生ajax、不使用表单(Form)上传文件
〇.知识点 jquery ajax 文档告诉你可以使用默认的 application/x-www-form-urlencoded, multipart/form-data, or text/plain ...
- ML.NET 示例:回归之销售预测
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- Node+GitLab实现小程序CI系统
为什么要实现自动部署 小程序开发迭代里,有以下几个个头痛的问题, 如何准确并快速的的把小程序上传去后台,并让测试人员进行测试? 测试同事找开发要二维码,效率较低 本地生成的二维码会出现携带本地代码.未 ...
- UVA-10375 唯一分解定理
#include<iostream> #include<string.h> #include<algorithm> #include<math.h> # ...
- 一个6亿的表a,一个3亿的表b,通过外间tid关联,你如何最快的查询出满足条件的第50000到第50200中的这200条数据记录
1.如果A表TID是自增长,并且是连续的,B表的ID为索引 select * from a,b where a.tid = b.id and a.tid>500000 limit 200; 2. ...
- Navicat还原出现Finished - Stopped before completion的问题
查看数据库中最大的单个文件容量 SHOW VARIABLES LIKE '%max_allowed_packet%'; 设置最大单个文件容量为10M,单次有效(新建查询---运行) SET GLO ...
- P124黎曼可积性刻画 的两个备注
1.这里为什么是开集? 2.请问为什么说了是开集马上就说是有界可测函数? 开集为可测集
- Python_socket常见的方法、网络编程的安全注意事项、socketsever模块、浏览器中在一段时间记录用户的登录验证机制
1.socket常见的方法 socket_常见方法_服务器端 import socket from socket import SOL_SOCKET,SO_REUSEADDR sk = socket. ...