From 54b731450c1e11e201ec90e703c4ec32915e476f Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 28 Feb 2024 00:39:59 +0800 Subject: [PATCH] 1.update Carousel from db --- .../java/com/luoo/user/dao/CarouselDao.java | 13 +++++++ .../java/com/luoo/user/pojo/Carousel.java | 37 ++++++++++++++++--- .../luoo/user/service/CarouselService.java | 28 +++++++++----- 3 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/CarouselDao.java diff --git a/luoo_user/src/main/java/com/luoo/user/dao/CarouselDao.java b/luoo_user/src/main/java/com/luoo/user/dao/CarouselDao.java new file mode 100644 index 0000000..2e7ddd3 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/CarouselDao.java @@ -0,0 +1,13 @@ +package com.luoo.user.dao; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import com.luoo.user.pojo.Carousel; + +public interface CarouselDao extends JpaRepository { + @Query(value = "select * from tb_carousel where status=1 order by publish_time desc limit 5", nativeQuery = true) + public List getValidCarousels(); +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/Carousel.java b/luoo_user/src/main/java/com/luoo/user/pojo/Carousel.java index 637946d..c5c8a26 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/Carousel.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/Carousel.java @@ -1,7 +1,19 @@ package com.luoo.user.pojo; +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.springframework.format.annotation.DateTimeFormat; + +import com.fasterxml.jackson.annotation.JsonFormat; + import io.swagger.annotations.ApiModelProperty; -import lombok.Builder; import lombok.Getter; import lombok.Setter; @@ -11,13 +23,17 @@ import lombok.Setter; */ @Getter @Setter -@Builder -public class Carousel{ +@Entity +@Table(name="tb_carousel") +public class Carousel implements Serializable { + private static final long serialVersionUID = 1L; /** * 自增ID */ @ApiModelProperty(value = "自增ID") + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Integer carouselId; /** @@ -27,9 +43,9 @@ public class Carousel{ private String imgPath; /** - * 0:期刊 1:外部连接 + * 0:歌曲 1:期刊 2:外部连接 3:文章 */ - @ApiModelProperty(value = "0:歌曲 1:期刊 2:外部连接") + @ApiModelProperty(value = "0:歌曲 1:期刊 2:外部连接 3:文章") private Integer objectType; /** @@ -43,4 +59,15 @@ public class Carousel{ */ @ApiModelProperty(value = "外部连接") private String outerLink; + + /** + * 启停状态 0: 停用 1:启用 + */ + @ApiModelProperty(value = "0: 停用 1:启用") + private int status; + + @ApiModelProperty(value = "发布时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date publishTime; } diff --git a/luoo_user/src/main/java/com/luoo/user/service/CarouselService.java b/luoo_user/src/main/java/com/luoo/user/service/CarouselService.java index 9d95fd9..2a2cadb 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/CarouselService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/CarouselService.java @@ -1,24 +1,32 @@ package com.luoo.user.service; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import com.luoo.user.dao.CarouselDao; import com.luoo.user.pojo.Carousel; @Service public class CarouselService { - private static final List DEFAULT_CAROUSEL_LIST=new ArrayList<>(); - static { - DEFAULT_CAROUSEL_LIST.add(Carousel.builder().carouselId(1).imgPath("http://cdn.indie.cn/music/10016/07.jpg").objectType(0).objectId("1643965109602750464").outerLink(null).build()); - DEFAULT_CAROUSEL_LIST.add(Carousel.builder().carouselId(2).imgPath("http://cdn.indie.cn/music/00985/00.jpg").objectType(1).objectId("1635099768424370176").outerLink(null).build()); - DEFAULT_CAROUSEL_LIST.add(Carousel.builder().carouselId(3).imgPath("http://cdn.indie.cn/music/00984/00.jpg").objectType(1).objectId("1635099812296790016").outerLink(null).build()); - DEFAULT_CAROUSEL_LIST.add(Carousel.builder().carouselId(4).imgPath("http://cdn.indie.cn/music/00942/00.jpg").objectType(2).objectId(null).outerLink("http://116.62.145.60:3000/music").build()); - DEFAULT_CAROUSEL_LIST.add(Carousel.builder().carouselId(5).imgPath("http://cdn.indie.cn/music/00982/00.jpg").objectType(1).objectId("1635099717706846208").outerLink(null).build()); - } + private static final String REDIS_KEY_CAROUSE = "carousels"; + @Autowired + private CarouselDao carouselDao; + + @Autowired + private RedisTemplate redisTemplate; + public List loadCarousel() { - return DEFAULT_CAROUSEL_LIST; + List carousels = (List) redisTemplate.opsForValue().get(REDIS_KEY_CAROUSE); + if (CollectionUtils.isEmpty(carousels)) { + carousels = carouselDao.getValidCarousels(); + redisTemplate.opsForValue().set(REDIS_KEY_CAROUSE, carousels, 24, TimeUnit.HOURS); + } + return carousels; } }