1.update Carousel from db

main
Gary 9 months ago
parent 58d7980ea2
commit 54b731450c

@ -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<Carousel, Integer> {
@Query(value = "select * from tb_carousel where status=1 order by publish_time desc limit 5", nativeQuery = true)
public List<Carousel> getValidCarousels();
}

@ -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;
}

@ -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<Carousel> 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<Carousel> loadCarousel() {
return DEFAULT_CAROUSEL_LIST;
List<Carousel> carousels = (List<Carousel>) 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;
}
}

Loading…
Cancel
Save