From 7d80b4329a25c1c7845cdb4a5831f8e429e6058f Mon Sep 17 00:00:00 2001 From: zhh Date: Mon, 28 Jan 2019 17:12:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=8E=A5=E5=8F=A3=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mall/portal/config/SecurityConfig.java | 5 +- .../portal/controller/HomeController.java | 32 ++++ .../com/macro/mall/portal/dao/HomeDao.java | 23 +++ .../portal/domain/FlashPromotionProduct.java | 19 +++ .../mall/portal/domain/HomeContentResult.java | 31 ++++ .../portal/domain/HomeFlashPromotion.java | 22 +++ .../mall/portal/service/HomeService.java | 15 ++ .../portal/service/impl/HomeServiceImpl.java | 140 ++++++++++++++++++ .../src/main/resources/dao/HomeDao.xml | 32 ++++ 9 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 mall-portal/src/main/java/com/macro/mall/portal/controller/HomeController.java create mode 100644 mall-portal/src/main/java/com/macro/mall/portal/dao/HomeDao.java create mode 100644 mall-portal/src/main/java/com/macro/mall/portal/domain/FlashPromotionProduct.java create mode 100644 mall-portal/src/main/java/com/macro/mall/portal/domain/HomeContentResult.java create mode 100644 mall-portal/src/main/java/com/macro/mall/portal/domain/HomeFlashPromotion.java create mode 100644 mall-portal/src/main/java/com/macro/mall/portal/service/HomeService.java create mode 100644 mall-portal/src/main/java/com/macro/mall/portal/service/impl/HomeServiceImpl.java create mode 100644 mall-portal/src/main/resources/dao/HomeDao.xml diff --git a/mall-portal/src/main/java/com/macro/mall/portal/config/SecurityConfig.java b/mall-portal/src/main/java/com/macro/mall/portal/config/SecurityConfig.java index 61dfa7b..accee43 100644 --- a/mall-portal/src/main/java/com/macro/mall/portal/config/SecurityConfig.java +++ b/mall-portal/src/main/java/com/macro/mall/portal/config/SecurityConfig.java @@ -43,7 +43,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .permitAll() .antMatchers(HttpMethod.OPTIONS)//跨域请求会先进行一次options请求 .permitAll() - .antMatchers("/sso/*")// 对登录注册要允许匿名访问 + .antMatchers( + "/sso/*",//登录注册 + "/home/*"//首页接口 + ) .permitAll() .antMatchers("/member/**","/returnApply/**")// 测试时开启 .permitAll() diff --git a/mall-portal/src/main/java/com/macro/mall/portal/controller/HomeController.java b/mall-portal/src/main/java/com/macro/mall/portal/controller/HomeController.java new file mode 100644 index 0000000..230dac2 --- /dev/null +++ b/mall-portal/src/main/java/com/macro/mall/portal/controller/HomeController.java @@ -0,0 +1,32 @@ +package com.macro.mall.portal.controller; + +import com.macro.mall.portal.domain.CommonResult; +import com.macro.mall.portal.domain.HomeContentResult; +import com.macro.mall.portal.service.HomeService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * 首页内容管理Controller + * Created by macro on 2019/1/28. + */ +@Controller +@Api(tags = "HomeController", description = "首页内容管理") +@RequestMapping("/home") +public class HomeController { + @Autowired + private HomeService homeService; + + @ApiOperation("首页内容页信息展示") + @RequestMapping(value = "/content", method = RequestMethod.GET) + @ResponseBody + public Object content() { + HomeContentResult contentResult = homeService.content(); + return new CommonResult().success(contentResult); + } +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/dao/HomeDao.java b/mall-portal/src/main/java/com/macro/mall/portal/dao/HomeDao.java new file mode 100644 index 0000000..77bbf3c --- /dev/null +++ b/mall-portal/src/main/java/com/macro/mall/portal/dao/HomeDao.java @@ -0,0 +1,23 @@ +package com.macro.mall.portal.dao; + +import com.macro.mall.model.PmsBrand; +import com.macro.mall.portal.domain.FlashPromotionProduct; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * 首页内容管理自定义Dao + * Created by macro on 2019/1/28. + */ +public interface HomeDao { + + /** + * 获取推荐品牌 + */ + List getRecommendBrand(); + /** + * 获取秒杀商品 + */ + List getFlashProductList(@Param("flashPromotionId") Long flashPromotionId, @Param("sessionId") Long sessionId); +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/domain/FlashPromotionProduct.java b/mall-portal/src/main/java/com/macro/mall/portal/domain/FlashPromotionProduct.java new file mode 100644 index 0000000..9640d51 --- /dev/null +++ b/mall-portal/src/main/java/com/macro/mall/portal/domain/FlashPromotionProduct.java @@ -0,0 +1,19 @@ +package com.macro.mall.portal.domain; + +import com.macro.mall.model.PmsProduct; +import lombok.Getter; +import lombok.Setter; + +import java.math.BigDecimal; + +/** + * 秒杀信息和商品对象封装 + * Created by macro on 2019/1/28. + */ +@Getter +@Setter +public class FlashPromotionProduct extends PmsProduct{ + private BigDecimal flashPromotionPrice; + private Integer flashPromotionCount; + private Integer flashPromotionLimit; +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/domain/HomeContentResult.java b/mall-portal/src/main/java/com/macro/mall/portal/domain/HomeContentResult.java new file mode 100644 index 0000000..1f1de9d --- /dev/null +++ b/mall-portal/src/main/java/com/macro/mall/portal/domain/HomeContentResult.java @@ -0,0 +1,31 @@ +package com.macro.mall.portal.domain; + +import com.macro.mall.model.CmsSubject; +import com.macro.mall.model.PmsBrand; +import com.macro.mall.model.PmsProduct; +import com.macro.mall.model.SmsHomeAdvertise; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +/** + * 首页内容返回信息封装 + * Created by macro on 2019/1/28. + */ +@Getter +@Setter +public class HomeContentResult { + //轮播广告 + private List advertiseList; + //推荐品牌 + private List brandList; + //当前秒杀场次 + private HomeFlashPromotion homeFlashPromotion; + //新品推荐 + private List newProductList; + //人气推荐 + private List hotProductList; + //推荐专题 + private List subjectList; +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/domain/HomeFlashPromotion.java b/mall-portal/src/main/java/com/macro/mall/portal/domain/HomeFlashPromotion.java new file mode 100644 index 0000000..c461c06 --- /dev/null +++ b/mall-portal/src/main/java/com/macro/mall/portal/domain/HomeFlashPromotion.java @@ -0,0 +1,22 @@ +package com.macro.mall.portal.domain; + +import lombok.Getter; +import lombok.Setter; + +import java.util.Date; +import java.util.List; + +/** + * 首页当前秒杀场次信息 + * Created by macro on 2019/1/28. + */ +@Getter +@Setter +public class HomeFlashPromotion { + private Date startTime; + private Date endTime; + private Date nextStartTime; + private Date nextEndTime; + //属于该秒杀活动的商品 + private List productList; +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/service/HomeService.java b/mall-portal/src/main/java/com/macro/mall/portal/service/HomeService.java new file mode 100644 index 0000000..daf75d8 --- /dev/null +++ b/mall-portal/src/main/java/com/macro/mall/portal/service/HomeService.java @@ -0,0 +1,15 @@ +package com.macro.mall.portal.service; + +import com.macro.mall.portal.domain.HomeContentResult; + +/** + * 首页内容管理Service + * Created by macro on 2019/1/28. + */ +public interface HomeService { + + /** + * 获取首页内容 + */ + HomeContentResult content(); +} diff --git a/mall-portal/src/main/java/com/macro/mall/portal/service/impl/HomeServiceImpl.java b/mall-portal/src/main/java/com/macro/mall/portal/service/impl/HomeServiceImpl.java new file mode 100644 index 0000000..2873a73 --- /dev/null +++ b/mall-portal/src/main/java/com/macro/mall/portal/service/impl/HomeServiceImpl.java @@ -0,0 +1,140 @@ +package com.macro.mall.portal.service.impl; + +import com.macro.mall.mapper.SmsFlashPromotionMapper; +import com.macro.mall.mapper.SmsFlashPromotionSessionMapper; +import com.macro.mall.mapper.SmsHomeAdvertiseMapper; +import com.macro.mall.model.*; +import com.macro.mall.portal.dao.HomeDao; +import com.macro.mall.portal.domain.FlashPromotionProduct; +import com.macro.mall.portal.domain.HomeContentResult; +import com.macro.mall.portal.domain.HomeFlashPromotion; +import com.macro.mall.portal.service.HomeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +/** + * 首页内容管理Service实现类 + * Created by macro on 2019/1/28. + */ +@Service +public class HomeServiceImpl implements HomeService { + @Autowired + private SmsHomeAdvertiseMapper advertiseMapper; + @Autowired + private HomeDao homeDao; + @Autowired + private SmsFlashPromotionMapper flashPromotionMapper; + @Autowired + private SmsFlashPromotionSessionMapper promotionSessionMapper; + + @Override + public HomeContentResult content() { + HomeContentResult result = new HomeContentResult(); + //获取首页广告 + result.setAdvertiseList(getHomeAdvertiseList()); + //获取推荐品牌 + result.setBrandList(homeDao.getRecommendBrand()); + //获取秒杀信息 + result.setHomeFlashPromotion(getHomeFlashPromotion()); + return result; + } + + private HomeFlashPromotion getHomeFlashPromotion() { + HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion(); + //获取当前秒杀活动 + Date now = new Date(); + SmsFlashPromotion flashPromotion = getFlashPromotion(now); + if (flashPromotion != null) { + //获取当前秒杀场次 + SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now); + if (flashPromotionSession != null) { + homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime()); + homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime()); + //获取下一个秒杀场次 + SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime()); + if(nextSession!=null){ + homeFlashPromotion.setNextStartTime(nextSession.getStartTime()); + homeFlashPromotion.setNextEndTime(nextSession.getEndTime()); + } + //获取秒杀商品 + List flashProductList = homeDao.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId()); + homeFlashPromotion.setProductList(flashProductList); + } + } + return homeFlashPromotion; + } + + //获取下一个场次信息 + private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) { + SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample(); + sessionExample.createCriteria() + .andStartTimeGreaterThan(date); + sessionExample.setOrderByClause("start_time asc"); + List promotionSessionList = promotionSessionMapper.selectByExample(sessionExample); + if (!CollectionUtils.isEmpty(promotionSessionList)) { + return promotionSessionList.get(0); + } + return null; + } + + //从当前日期中提取日期时间戳 + private Date getDate(Date now) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(now); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + return calendar.getTime(); + } + + //从当前日期中提取时间时间戳 + private Date getTime(Date now) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(now); + calendar.set(Calendar.YEAR, 1970); + calendar.set(Calendar.MONTH, 0); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return calendar.getTime(); + } + + private List getHomeAdvertiseList() { + SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample(); + example.createCriteria().andTypeEqualTo(1).andStatusEqualTo(1); + example.setOrderByClause("sort desc"); + return advertiseMapper.selectByExample(example); + } + + //根据时间获取秒杀活动 + private SmsFlashPromotion getFlashPromotion(Date date) { + Date currDate = getDate(date); + SmsFlashPromotionExample example = new SmsFlashPromotionExample(); + example.createCriteria() + .andStatusEqualTo(1) + .andStartDateLessThanOrEqualTo(currDate) + .andEndDateGreaterThanOrEqualTo(currDate); + List flashPromotionList = flashPromotionMapper.selectByExample(example); + if (!CollectionUtils.isEmpty(flashPromotionList)) { + return flashPromotionList.get(0); + } + return null; + } + + //根据时间获取秒杀场次 + private SmsFlashPromotionSession getFlashPromotionSession(Date date) { + Date currTime = getTime(date); + SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample(); + sessionExample.createCriteria() + .andStartTimeLessThanOrEqualTo(currTime) + .andEndTimeGreaterThanOrEqualTo(currTime); + List promotionSessionList = promotionSessionMapper.selectByExample(sessionExample); + if (!CollectionUtils.isEmpty(promotionSessionList)) { + return promotionSessionList.get(0); + } + return null; + } +} diff --git a/mall-portal/src/main/resources/dao/HomeDao.xml b/mall-portal/src/main/resources/dao/HomeDao.xml new file mode 100644 index 0000000..7349966 --- /dev/null +++ b/mall-portal/src/main/resources/dao/HomeDao.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + \ No newline at end of file