From e77065df5dc044a6417715048d4412e93c4bf493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Sat, 5 Mar 2022 14:16:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A2=E8=B4=AD3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flashsale/controller/ItemController.java | 9 +++ .../controller/viewobject/ItemVO.java | 47 ++++++++++++++ .../flashsale/dao/PromoDOMapper.java | 14 +++-- .../flashsale/dataobject/PromoDO.java | 63 ++++++++++++++----- .../flashsale/service/PromoService.java | 8 +++ .../service/impl/ItemServiceImpl.java | 12 ++++ .../service/impl/PromoServiceImpl.java | 47 ++++++++++++++ .../flashsale/service/model/ItemModel.java | 12 ++++ .../flashsale/service/model/PromoModel.java | 24 ++++++- src/main/resources/mapping/PromoDOMapper.xml | 41 ++++++++---- src/main/resources/mybatis-generator.xml | 6 +- 11 files changed, 247 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/cxyxiaomo/flashsale/service/PromoService.java create mode 100644 src/main/java/com/cxyxiaomo/flashsale/service/impl/PromoServiceImpl.java diff --git a/src/main/java/com/cxyxiaomo/flashsale/controller/ItemController.java b/src/main/java/com/cxyxiaomo/flashsale/controller/ItemController.java index 1e06ff5..0ff83a2 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/controller/ItemController.java +++ b/src/main/java/com/cxyxiaomo/flashsale/controller/ItemController.java @@ -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; } } diff --git a/src/main/java/com/cxyxiaomo/flashsale/controller/viewobject/ItemVO.java b/src/main/java/com/cxyxiaomo/flashsale/controller/viewobject/ItemVO.java index ddad392..5ffa7e0 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/controller/viewobject/ItemVO.java +++ b/src/main/java/com/cxyxiaomo/flashsale/controller/viewobject/ItemVO.java @@ -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; + } } diff --git a/src/main/java/com/cxyxiaomo/flashsale/dao/PromoDOMapper.java b/src/main/java/com/cxyxiaomo/flashsale/dao/PromoDOMapper.java index aeea6f9..cffe90c 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/dao/PromoDOMapper.java +++ b/src/main/java/com/cxyxiaomo/flashsale/dao/PromoDOMapper.java @@ -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); } \ No newline at end of file diff --git a/src/main/java/com/cxyxiaomo/flashsale/dataobject/PromoDO.java b/src/main/java/com/cxyxiaomo/flashsale/dataobject/PromoDO.java index 5a132c1..d7f038c 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/dataobject/PromoDO.java +++ b/src/main/java/com/cxyxiaomo/flashsale/dataobject/PromoDO.java @@ -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; diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/PromoService.java b/src/main/java/com/cxyxiaomo/flashsale/service/PromoService.java new file mode 100644 index 0000000..6823b01 --- /dev/null +++ b/src/main/java/com/cxyxiaomo/flashsale/service/PromoService.java @@ -0,0 +1,8 @@ +package com.cxyxiaomo.flashsale.service; + +import com.cxyxiaomo.flashsale.service.model.PromoModel; + +public interface PromoService { + // 根据itemId获取即将进行的或正在进行的秒杀活动 + PromoModel getPromoByItemId(Integer itemId); +} diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java b/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java index f6c81fc..22625cd 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java +++ b/src/main/java/com/cxyxiaomo/flashsale/service/impl/ItemServiceImpl.java @@ -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; } diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/impl/PromoServiceImpl.java b/src/main/java/com/cxyxiaomo/flashsale/service/impl/PromoServiceImpl.java new file mode 100644 index 0000000..949daf9 --- /dev/null +++ b/src/main/java/com/cxyxiaomo/flashsale/service/impl/PromoServiceImpl.java @@ -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; + } +} diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/model/ItemModel.java b/src/main/java/com/cxyxiaomo/flashsale/service/model/ItemModel.java index 68f2522..669da17 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/service/model/ItemModel.java +++ b/src/main/java/com/cxyxiaomo/flashsale/service/model/ItemModel.java @@ -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; + } } diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/model/PromoModel.java b/src/main/java/com/cxyxiaomo/flashsale/service/model/PromoModel.java index db0effa..7aea10c 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/service/model/PromoModel.java +++ b/src/main/java/com/cxyxiaomo/flashsale/service/model/PromoModel.java @@ -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; + } } diff --git a/src/main/resources/mapping/PromoDOMapper.xml b/src/main/resources/mapping/PromoDOMapper.xml index 476d694..82a36c7 100644 --- a/src/main/resources/mapping/PromoDOMapper.xml +++ b/src/main/resources/mapping/PromoDOMapper.xml @@ -5,11 +5,12 @@ + @@ -17,15 +18,15 @@ - id, promo_name, start_date, item_id, prome_item_price + id, promo_name, start_date, end_date, item_id, prome_item_price + select + + from promo_info + where item_id = #{itemId,jdbcType=INTEGER} + \ No newline at end of file diff --git a/src/main/resources/mybatis-generator.xml b/src/main/resources/mybatis-generator.xml index b7bd331..3705b16 100644 --- a/src/main/resources/mybatis-generator.xml +++ b/src/main/resources/mybatis-generator.xml @@ -59,9 +59,9 @@ -
+ + + \ No newline at end of file