1
0
mirror of https://gitee.com/coder-xiaomo/flashsale synced 2025-01-10 11:48:14 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2022-03-05 14:16:41 +08:00
parent 90cbb3d430
commit e77065df5d
11 changed files with 247 additions and 36 deletions

View File

@ -76,6 +76,15 @@ public class ItemController extends BaseController {
}
ItemVO itemVO = new ItemVO();
BeanUtils.copyProperties(itemModel, itemVO);
if (itemModel.getPromoModel() != null) {
// 有正在进行或即将进行的秒杀活动
itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
itemVO.setPromoId(itemModel.getId());
itemVO.setStartDate(itemModel.getPromoModel().getStartDate());
itemVO.setPromoPrice(itemModel.getPromoModel().getPromoPrice());
} else {
itemVO.setPromoStatus(0);
}
return itemVO;
}
}

View File

@ -1,5 +1,7 @@
package com.cxyxiaomo.flashsale.controller.viewobject;
import org.joda.time.DateTime;
import java.math.BigDecimal;
public class ItemVO {
@ -24,6 +26,19 @@ public class ItemVO {
// 商品描述图片的URL
private String imgUrl;
// 记录商品是否在秒杀活动中以及对应的状态
// 0没有秒杀活动 2秒杀活动待开始 3秒杀活动进行中
private Integer promoStatus;
// 秒杀活动价格
private BigDecimal promoPrice;
// 秒杀活动Id
private Integer promoId;
// 秒杀活动开始时间
private DateTime startDate;
public Integer getId() {
return id;
}
@ -79,4 +94,36 @@ public class ItemVO {
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public Integer getPromoStatus() {
return promoStatus;
}
public void setPromoStatus(Integer promoStatus) {
this.promoStatus = promoStatus;
}
public BigDecimal getPromoPrice() {
return promoPrice;
}
public void setPromoPrice(BigDecimal promoPrice) {
this.promoPrice = promoPrice;
}
public Integer getPromoId() {
return promoId;
}
public void setPromoId(Integer promoId) {
this.promoId = promoId;
}
public DateTime getStartDate() {
return startDate;
}
public void setStartDate(DateTime startDate) {
this.startDate = startDate;
}
}

View File

@ -7,7 +7,7 @@ public interface PromoDOMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table promo_info
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
int deleteByPrimaryKey(Integer id);
@ -15,7 +15,7 @@ public interface PromoDOMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table promo_info
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
int insert(PromoDO record);
@ -23,7 +23,7 @@ public interface PromoDOMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table promo_info
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
int insertSelective(PromoDO record);
@ -31,7 +31,7 @@ public interface PromoDOMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table promo_info
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
PromoDO selectByPrimaryKey(Integer id);
@ -39,7 +39,7 @@ public interface PromoDOMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table promo_info
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
int updateByPrimaryKeySelective(PromoDO record);
@ -47,7 +47,9 @@ public interface PromoDOMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table promo_info
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
int updateByPrimaryKey(PromoDO record);
PromoDO selectByItemId(Integer itemId);
}

View File

@ -9,7 +9,7 @@ public class PromoDO {
* This field was generated by MyBatis Generator.
* This field corresponds to the database column promo_info.id
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
private Integer id;
@ -18,7 +18,7 @@ public class PromoDO {
* This field was generated by MyBatis Generator.
* This field corresponds to the database column promo_info.promo_name
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
private String promoName;
@ -27,16 +27,25 @@ public class PromoDO {
* This field was generated by MyBatis Generator.
* This field corresponds to the database column promo_info.start_date
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
private Date startDate;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column promo_info.end_date
*
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
private Date endDate;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column promo_info.item_id
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
private Integer itemId;
@ -45,7 +54,7 @@ public class PromoDO {
* This field was generated by MyBatis Generator.
* This field corresponds to the database column promo_info.prome_item_price
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
private BigDecimal promeItemPrice;
@ -55,7 +64,7 @@ public class PromoDO {
*
* @return the value of promo_info.id
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public Integer getId() {
return id;
@ -67,7 +76,7 @@ public class PromoDO {
*
* @param id the value for promo_info.id
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public void setId(Integer id) {
this.id = id;
@ -79,7 +88,7 @@ public class PromoDO {
*
* @return the value of promo_info.promo_name
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public String getPromoName() {
return promoName;
@ -91,7 +100,7 @@ public class PromoDO {
*
* @param promoName the value for promo_info.promo_name
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public void setPromoName(String promoName) {
this.promoName = promoName == null ? null : promoName.trim();
@ -103,7 +112,7 @@ public class PromoDO {
*
* @return the value of promo_info.start_date
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public Date getStartDate() {
return startDate;
@ -115,19 +124,43 @@ public class PromoDO {
*
* @param startDate the value for promo_info.start_date
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column promo_info.end_date
*
* @return the value of promo_info.end_date
*
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public Date getEndDate() {
return endDate;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column promo_info.end_date
*
* @param endDate the value for promo_info.end_date
*
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column promo_info.item_id
*
* @return the value of promo_info.item_id
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public Integer getItemId() {
return itemId;
@ -139,7 +172,7 @@ public class PromoDO {
*
* @param itemId the value for promo_info.item_id
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public void setItemId(Integer itemId) {
this.itemId = itemId;
@ -151,7 +184,7 @@ public class PromoDO {
*
* @return the value of promo_info.prome_item_price
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public BigDecimal getPromeItemPrice() {
return promeItemPrice;
@ -163,7 +196,7 @@ public class PromoDO {
*
* @param promeItemPrice the value for promo_info.prome_item_price
*
* @mbg.generated Sat Mar 05 12:40:27 CST 2022
* @mbg.generated Sat Mar 05 12:55:23 CST 2022
*/
public void setPromeItemPrice(BigDecimal promeItemPrice) {
this.promeItemPrice = promeItemPrice;

View File

@ -0,0 +1,8 @@
package com.cxyxiaomo.flashsale.service;
import com.cxyxiaomo.flashsale.service.model.PromoModel;
public interface PromoService {
// 根据itemId获取即将进行的或正在进行的秒杀活动
PromoModel getPromoByItemId(Integer itemId);
}

View File

@ -7,7 +7,9 @@ import com.cxyxiaomo.flashsale.dataobject.ItemStockDO;
import com.cxyxiaomo.flashsale.error.BusinessException;
import com.cxyxiaomo.flashsale.error.EmBusinessError;
import com.cxyxiaomo.flashsale.service.ItemService;
import com.cxyxiaomo.flashsale.service.PromoService;
import com.cxyxiaomo.flashsale.service.model.ItemModel;
import com.cxyxiaomo.flashsale.service.model.PromoModel;
import com.cxyxiaomo.flashsale.validator.ValidationResult;
import com.cxyxiaomo.flashsale.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
@ -30,6 +32,9 @@ public class ItemServiceImpl implements ItemService {
@Autowired
private ItemStockDOMapper itemStockDOMapper;
@Autowired
private PromoService promoService;
@Override
@Transactional
public ItemModel createItem(ItemModel itemModel) throws BusinessException {
@ -100,6 +105,13 @@ public class ItemServiceImpl implements ItemService {
// 将Data Object -> Model
ItemModel itemModel = convertModelFromDataObject(itemDO, itemStockDO);
// 获取活动商品信息
PromoModel promoModel = promoService.getPromoByItemId(itemModel.getId());
if (promoModel != null && promoModel.getStatus() != 3) {
itemModel.setPromoModel(promoModel);
}
return itemModel;
}

View File

@ -0,0 +1,47 @@
package com.cxyxiaomo.flashsale.service.impl;
import com.cxyxiaomo.flashsale.dao.PromoDOMapper;
import com.cxyxiaomo.flashsale.dataobject.PromoDO;
import com.cxyxiaomo.flashsale.service.PromoService;
import com.cxyxiaomo.flashsale.service.model.PromoModel;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
public class PromoServiceImpl implements PromoService {
@Autowired
private PromoDOMapper promoDOMapper;
@Override
public PromoModel getPromoByItemId(Integer itemId) {
// 获取对应商品的秒杀活动信息
PromoDO promoDO = promoDOMapper.selectByPrimaryKey(itemId);
// Data Object -> Model
PromoModel promoModel = convertFromDataObject(promoDO);
// 判断秒杀活动是否即将开始或正在进行
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.setStartDate(new DateTime(promoDO.getStartDate()));
promoModel.setEndDate(new DateTime(promoDO.getEndDate()));
return promoModel;
}
}

View File

@ -33,6 +33,10 @@ public class ItemModel {
@NotBlank(message = "商品描述图片不能为空")
private String imgUrl;
// 使用聚合模型
// 如果promoModel不为空则表示其还有未结束的秒杀活动
private PromoModel promoModel;
public Integer getId() {
return id;
}
@ -88,4 +92,12 @@ public class ItemModel {
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public PromoModel getPromoModel() {
return promoModel;
}
public void setPromoModel(PromoModel promoModel) {
this.promoModel = promoModel;
}
}

View File

@ -3,7 +3,6 @@ package com.cxyxiaomo.flashsale.service.model;
import org.joda.time.DateTime;
import java.math.BigDecimal;
import java.util.Date;
public class PromoModel {
@ -15,12 +14,19 @@ public class PromoModel {
// 活动开始时间
private DateTime startDate;
// 活动结束时间
private DateTime endDate;
// 秒杀活动适用商品
private Integer itemId;
// 秒杀活动的价格
private BigDecimal promoPrice;
// 秒杀活动状态
// 1还未开始 2进行中 3已结束
private Integer status;
public Integer getId() {
return id;
}
@ -45,6 +51,14 @@ public class PromoModel {
this.startDate = startDate;
}
public DateTime getEndDate() {
return endDate;
}
public void setEndDate(DateTime endDate) {
this.endDate = endDate;
}
public Integer getItemId() {
return itemId;
}
@ -60,4 +74,12 @@ public class PromoModel {
public void setPromoPrice(BigDecimal promoPrice) {
this.promoPrice = promoPrice;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}

View File

@ -5,11 +5,12 @@
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="promo_name" jdbcType="VARCHAR" property="promoName" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
<result column="end_date" jdbcType="TIMESTAMP" property="endDate" />
<result column="item_id" jdbcType="INTEGER" property="itemId" />
<result column="prome_item_price" jdbcType="DECIMAL" property="promeItemPrice" />
</resultMap>
@ -17,15 +18,15 @@
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
id, promo_name, start_date, item_id, prome_item_price
id, promo_name, start_date, end_date, item_id, prome_item_price
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
select
<include refid="Base_Column_List" />
@ -36,7 +37,7 @@
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
delete from promo_info
where id = #{id,jdbcType=INTEGER}
@ -45,18 +46,20 @@
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
insert into promo_info (id, promo_name, start_date,
item_id, prome_item_price)
end_date, item_id, prome_item_price
)
values (#{id,jdbcType=INTEGER}, #{promoName,jdbcType=VARCHAR}, #{startDate,jdbcType=TIMESTAMP},
#{itemId,jdbcType=INTEGER}, #{promeItemPrice,jdbcType=DECIMAL})
#{endDate,jdbcType=TIMESTAMP}, #{itemId,jdbcType=INTEGER}, #{promeItemPrice,jdbcType=DECIMAL}
)
</insert>
<insert id="insertSelective" parameterType="com.cxyxiaomo.flashsale.dataobject.PromoDO">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
insert into promo_info
<trim prefix="(" suffix=")" suffixOverrides=",">
@ -69,6 +72,9 @@
<if test="startDate != null">
start_date,
</if>
<if test="endDate != null">
end_date,
</if>
<if test="itemId != null">
item_id,
</if>
@ -86,6 +92,9 @@
<if test="startDate != null">
#{startDate,jdbcType=TIMESTAMP},
</if>
<if test="endDate != null">
#{endDate,jdbcType=TIMESTAMP},
</if>
<if test="itemId != null">
#{itemId,jdbcType=INTEGER},
</if>
@ -98,7 +107,7 @@
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
update promo_info
<set>
@ -108,6 +117,9 @@
<if test="startDate != null">
start_date = #{startDate,jdbcType=TIMESTAMP},
</if>
<if test="endDate != null">
end_date = #{endDate,jdbcType=TIMESTAMP},
</if>
<if test="itemId != null">
item_id = #{itemId,jdbcType=INTEGER},
</if>
@ -121,13 +133,20 @@
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Mar 05 12:40:27 CST 2022.
This element was generated on Sat Mar 05 12:55:23 CST 2022.
-->
update promo_info
set promo_name = #{promoName,jdbcType=VARCHAR},
start_date = #{startDate,jdbcType=TIMESTAMP},
end_date = #{endDate,jdbcType=TIMESTAMP},
item_id = #{itemId,jdbcType=INTEGER},
prome_item_price = #{promeItemPrice,jdbcType=DECIMAL}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByItemId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from promo_info
where item_id = #{itemId,jdbcType=INTEGER}
</select>
</mapper>

View File

@ -59,9 +59,9 @@
<!--<table tableName="sequence_info" domainObjectName="SequenceDO" enableCountByExample="false"-->
<!-- enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!-- selectByExampleQueryId="false"></table>-->
<table tableName="promo_info" domainObjectName="PromoDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<!--<table tableName="promo_info" domainObjectName="PromoDO" enableCountByExample="false"-->
<!-- enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!-- selectByExampleQueryId="false"></table>-->
</context>
</generatorConfiguration>