Browse Source

首页接口添加

zhh 6 years ago
parent
commit
7d80b4329a

+ 4 - 1
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()

+ 32 - 0
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);
+    }
+}

+ 23 - 0
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<PmsBrand> getRecommendBrand();
+    /**
+     * 获取秒杀商品
+     */
+    List<FlashPromotionProduct> getFlashProductList(@Param("flashPromotionId") Long flashPromotionId, @Param("sessionId") Long sessionId);
+}

+ 19 - 0
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;
+}

+ 31 - 0
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<SmsHomeAdvertise> advertiseList;
+    //推荐品牌
+    private List<PmsBrand> brandList;
+    //当前秒杀场次
+    private HomeFlashPromotion homeFlashPromotion;
+    //新品推荐
+    private List<PmsProduct> newProductList;
+    //人气推荐
+    private List<PmsProduct> hotProductList;
+    //推荐专题
+    private List<CmsSubject> subjectList;
+}

+ 22 - 0
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<FlashPromotionProduct> productList;
+}

+ 15 - 0
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();
+}

+ 140 - 0
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<FlashPromotionProduct> 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<SmsFlashPromotionSession> 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<SmsHomeAdvertise> 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<SmsFlashPromotion> 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<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
+        if (!CollectionUtils.isEmpty(promotionSessionList)) {
+            return promotionSessionList.get(0);
+        }
+        return null;
+    }
+}

+ 32 - 0
mall-portal/src/main/resources/dao/HomeDao.xml

@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.macro.mall.portal.dao.HomeDao">
+    <resultMap id="flashPromotionProduct" type="com.macro.mall.portal.domain.FlashPromotionProduct" extends="com.macro.mall.mapper.PmsProductMapper.BaseResultMap">
+        <result column="flash_promotion_price" property="flashPromotionPrice" />
+        <result column="flash_promotion_count" property="flashPromotionCount" />
+        <result column="flash_promotion_limit" property="flashPromotionLimit" />
+    </resultMap>
+    <select id="getRecommendBrand" resultMap="com.macro.mall.mapper.PmsBrandMapper.BaseResultMap">
+        SELECT
+            pb.*
+        FROM
+            sms_home_brand hb
+            LEFT JOIN pms_brand pb ON hb.brand_id = pb.id
+        AND recommend_status = 1
+        ORDER BY
+            hb.sort DESC
+    </select>
+    <select id="getFlashProductList" resultMap="flashPromotionProduct">
+        SELECT
+            pr.flash_promotion_price,
+            pr.flash_promotion_count,
+            pr.flash_promotion_limit,
+            p.*
+        FROM
+            sms_flash_promotion_product_relation pr
+            LEFT JOIN pms_product p ON pr.product_id = p.id
+        WHERE
+            pr.flash_promotion_id = #{flashPromotionId}
+            AND pr.flash_promotion_session_id = #{sessionId}
+    </select>
+</mapper>