@ -16,15 +16,13 @@ import com.luoo.music.util.Constants;
import net.oschina.j2cache.CacheChannel ;
import org.apache.commons.lang.StringUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.data.domain.Example ;
import org.springframework.data.domain.ExampleMatcher ;
import org.springframework.data.domain.Page ;
import org.springframework.data.domain.PageRequest ;
import org.springframework.data.domain.* ;
import org.springframework.data.jpa.domain.Specification ;
import org.springframework.data.mongodb.core.MongoTemplate ;
import org.springframework.data.mongodb.core.aggregation.Aggregation ;
import org.springframework.data.mongodb.core.aggregation.AggregationResults ;
import org.springframework.data.mongodb.core.query.Criteria ;
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.stereotype.Service ;
import org.springframework.util.CollectionUtils ;
import util.IdWorker ;
@ -34,6 +32,7 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate ;
import javax.persistence.criteria.Root ;
import java.time.LocalDateTime ;
import java.time.ZoneOffset ;
import java.util.* ;
/ * *
@ -61,6 +60,9 @@ public class CMSArticleService {
@Autowired
private CacheChannel cacheChannel ;
@Autowired
private RedisTemplate redisTemplate ;
public Result add ( ArticleAddModel paramAdd ) {
String image = null ;
@ -73,25 +75,6 @@ public class CMSArticleService {
}
}
Article article = buildArticle ( paramAdd , image ) ;
Example < Article > example = Example . of ( article , ExampleMatcher . matching ( )
. withIgnorePaths ( "id" )
. withIgnorePaths ( "image" )
. withIgnorePaths ( "pubTime" )
. withIgnorePaths ( "createTime" )
. withIgnorePaths ( "updateTime" )
) ;
if ( ! articleDao . exists ( example ) ) {
articleDao . save ( article ) ;
cleanArticleCache ( article . getId ( ) ) ;
}
return Result . success ( ) ;
}
private Article buildArticle ( ArticleAddModel paramAdd , String image ) {
Article article = new Article ( ) ;
article . setId ( String . valueOf ( idWorker . nextId ( ) ) ) ;
article . setTitle ( paramAdd . getTitle ( ) ) ;
@ -105,43 +88,53 @@ public class CMSArticleService {
UserInfo userInfo = userClient . queryUserInfoById ( paramAdd . getUserId ( ) ) ;
if ( ! Objects . isNull ( userInfo ) ) {
article . setUserId ( userInfo . getId ( ) ) ;
article . setUserName ( userInfo . getName ( ) ) ;
article . setUserType ( userInfo . getType ( ) ) ;
}
if ( Objects . isNull ( userInfo ) )
return Result . failed ( "发布人ID无效" ) ;
article . setUserId ( userInfo . getId ( ) ) ;
article . setUserName ( userInfo . getName ( ) ) ;
article . setUserType ( userInfo . getType ( ) ) ;
article . setAllowCommit ( paramAdd . getAllowCommit ( ) ) ;
article . setAutoPush ( paramAdd . getAutoPush ( ) ? "1" : "0" ) ;
article . setIsPublish ( "1" ) ;
article . setPubTime ( LocalDateTime . now ( ) ) ;
article . setIsScheduled ( paramAdd . getIsScheduled ( ) ) ;
/ * String pubTimeStr = paramAdd . getPubTime ( ) ;
LocalDateTime pubTime = LocalDateTime . now ( ) ;
if ( "1" . equals ( paramAdd . getIsScheduled ( ) ) ) { // 定时发布
if ( StringUtils . isNotBlank ( pubTimeStr ) ) { // 定时发布确定发布时间
pubTime = LocalDateTime . parse ( pubTimeStr , Constants . formatter ) ;
if ( pubTime . isBefore ( LocalDateTime . now ( ) ) ) { //发布时间比现在早
article . setState ( "1" ) ;
article . setIsPublish ( "1" ) ;
article . setPubTime ( LocalDateTime . now ( ) ) ;
} else { //发布时间比现在晚
article . setState ( "0" ) ;
article . setIsPublish ( "0" ) ;
article . setPubTime ( pubTime ) ;
}
} else { // 定时发布未确定发布时间
article . setState ( "1" ) ;
if ( paramAdd . getAutoPush ( ) ) {
article . setAutoPush ( "1" ) ;
article . setIsPublish ( "1" ) ;
article . setPubTime ( LocalDateTime . now ( ) ) ;
article . setIsScheduled ( paramAdd . getIsScheduled ( ) ) ;
} else {
if ( paramAdd . getPubTime ( ) = = null )
return Result . failed ( "定时发布但发布时间为空" ) ;
if ( paramAdd . getPubTime ( ) . isBefore ( LocalDateTime . now ( ) ) ) {
article . setAutoPush ( "0" ) ;
article . setIsPublish ( "1" ) ;
article . setPubTime ( LocalDateTime . now ( ) ) ;
article . setIsScheduled ( "1" ) ;
} else {
article . setAutoPush ( "0" ) ;
article . setIsPublish ( "0" ) ;
article . setPubTime ( paramAdd . getPubTime ( ) ) ;
article . setIsScheduled ( "0" ) ;
redisTemplate . opsForHash ( ) . put ( Constants . REDIS_KEY_ARTICLE_SCHEDULED , article . getId ( ) , paramAdd . getPubTime ( ) ) ;
}
} else { //非定时发布
article . setState ( "1" ) ;
article . setIsPublish ( "1" ) ;
article . setPubTime ( LocalDateTime . now ( ) ) ;
} * /
return article ;
}
Example < Article > example = Example . of ( article , ExampleMatcher . matching ( )
. withIgnorePaths ( "id" )
. withIgnorePaths ( "image" )
. withIgnorePaths ( "pubTime" )
. withIgnorePaths ( "createTime" )
. withIgnorePaths ( "updateTime" )
) ;
if ( ! articleDao . exists ( example ) ) {
articleDao . save ( article ) ;
cleanArticleCache ( article . getId ( ) ) ;
} else {
redisTemplate . opsForHash ( ) . delete ( Constants . REDIS_KEY_ARTICLE_SCHEDULED , article . getId ( ) ) ;
}
return Result . success ( ) ;
}
private String moveArticleImage ( String srcKey , String image ) {
@ -163,6 +156,7 @@ public class CMSArticleService {
article . setIsDeleted ( "1" ) ;
articleDao . save ( article ) ;
cleanArticleCache ( article . getId ( ) ) ;
redisTemplate . opsForHash ( ) . delete ( Constants . REDIS_KEY_ARTICLE_SCHEDULED , article . getId ( ) ) ;
return Result . success ( ) ;
}
return Result . failed ( "文章不存在" ) ;
@ -199,24 +193,38 @@ public class CMSArticleService {
}
}
article . setTitle ( param . getTitle ( ) ) ;
article . setType ( param . getType ( ) ) ;
article . setSummary ( contentToSummary ( param . getContent ( ) ) ) ;
article . setContent ( param . getContent ( ) ) ;
article . setTitle ( param . getTitle ( ) ) ;
article . setType ( param . getType ( ) ) ;
article . setSummary ( contentToSummary ( param . getContent ( ) ) ) ;
article . setContent ( param . getContent ( ) ) ;
article . setAllowCommit ( param . getAllowCommit ( ) ) ;
article . setAllowCommit ( param . getAllowCommit ( ) ) ;
article . setAutoPush ( param . getAutoPush ( ) ? "1" : "0" ) ;
article . setIsScheduled ( param . getIsScheduled ( ) ) ;
redisTemplate . opsForHash ( ) . delete ( Constants . REDIS_KEY_ARTICLE_SCHEDULED , article . getId ( ) ) ;
if ( param . getAutoPush ( ) ) {
article . setAutoPush ( "1" ) ;
article . setIsPublish ( "1" ) ;
article . setPubTime ( LocalDateTime . now ( ) ) ;
article . setIsScheduled ( "1" ) ;
} else {
if ( param . getPubTime ( ) = = null )
return Result . failed ( "定时发布但发布时间为空" ) ;
article . setPubTime ( param . getPubTime ( ) ) ;
if ( param . getPubTime ( ) . isBefore ( LocalDateTime . now ( ) ) ) {
article . setAutoPush ( "0" ) ;
article . setIsPublish ( "1" ) ;
article . setPubTime ( LocalDateTime . now ( ) ) ;
article . setIsScheduled ( "1" ) ;
} else {
article . setAutoPush ( "0" ) ;
article . setIsPublish ( "0" ) ;
article . setPubTime ( param . getPubTime ( ) ) ;
article . setIsScheduled ( "0" ) ;
redisTemplate . opsForHash ( ) . put ( Constants . REDIS_KEY_ARTICLE_SCHEDULED , article . getId ( ) , param . getPubTime ( ) ) ;
}
}
articleDao . save ( article ) ;
cleanArticleCache ( article . getId ( ) ) ;
//TODO: 发布方式
return Result . success ( ) ;
}
@ -270,12 +278,20 @@ public class CMSArticleService {
return Result . success ( new PageResult < > ( Long . valueOf ( pageArticleFromCache . size ( ) ) , pageArticleFromCache ) ) ;
}
private Specification < Article > buildSearchSpecification ( ) {
//isFront : true 前台数据, 只显示Publish的
//isFront : false 后台数据,全都显示
private Specification < Article > buildSearchSpecification ( boolean isFrost ) {
return ( Root < Article > root , CriteriaQuery < ? > query , CriteriaBuilder builder ) - > {
List < Predicate > predicateList = new ArrayList < Predicate > ( ) ;
Predicate isDeleted = builder . equal ( root . get ( "isDeleted" ) , "0" ) ;
Predicate isPublish = builder . equal ( root . get ( "isPublish" ) , "1" ) ;
predicateList . add ( builder . and ( isDeleted , isPublish ) ) ;
if ( isFrost ) {
Predicate isPublish = builder . equal ( root . get ( "isPublish" ) , "1" ) ;
predicateList . add ( builder . and ( isDeleted , isPublish ) ) ;
} else {
predicateList . add ( builder . and ( isDeleted ) ) ;
}
return builder . and ( predicateList . toArray ( new Predicate [ predicateList . size ( ) ] ) ) ;
} ;
}
@ -293,14 +309,23 @@ public class CMSArticleService {
return cleanedString . length ( ) > = 150 ? cleanedString . substring ( 0 , 150 ) : cleanedString ;
}
public void releaseArticle ( String articleId ) {
Article article = JSON . parseObject ( getArticleFromCache ( articleId ) , Article . class ) ;
if ( article ! = null ) {
article . setIsPublish ( "1" ) ;
article . setIsScheduled ( "1" ) ;
}
articleDao . save ( article ) ;
cleanArticleCache ( article . getId ( ) ) ;
redisTemplate . opsForHash ( ) . delete ( Constants . REDIS_KEY_ARTICLE_SCHEDULED , article . getId ( ) ) ;
}
private List < ArticleRespDTO > getPageArticleFromCache ( int page , int size ) {
return ( List < ArticleRespDTO > ) cacheChannel . get ( constants . Constants . J2CACHE_REGION_ARTICLE_PAGE , page + "," + size , key - > getPageArticle ( page , size ) , false ) . getValue ( ) ;
}
private List < ArticleDTO > getPageArticleDTOFromCache ( int page , int size ) {
return ( List < ArticleDTO > ) cacheChannel . get ( constants . Constants . J2CACHE_REGION_ARTICLE_DTO_PAGE , page + "," + size , key - > getPageArticleDTO ( page , size ) , false ) . getValue ( ) ;
}
@ -308,8 +333,8 @@ public class CMSArticleService {
private List < ArticleRespDTO > getPageArticle ( int page , int size ) {
List < ArticleRespDTO > result = new ArrayList < > ( ) ;
PageRequest pageRequest = PageRequest . of ( page - 1 , size );
Specification < Article > articleSpecification = buildSearchSpecification ( ) ;
PageRequest pageRequest = PageRequest . of ( page - 1 , size , Sort . by ( "pubTime" ) . descending ( ) );
Specification < Article > articleSpecification = buildSearchSpecification ( false ) ;
Page < Article > ArticlePage = articleDao . findAll ( articleSpecification , pageRequest ) ;
List < Article > content = ArticlePage . getContent ( ) ;
@ -325,8 +350,8 @@ public class CMSArticleService {
public List < ArticleDTO > getPageArticleDTO ( int page , int size ) {
List < ArticleDTO > result = new ArrayList < > ( ) ;
PageRequest pageRequest = PageRequest . of ( page - 1 , size );
Specification < Article > articleSpecification = buildSearchSpecification ( ) ;
PageRequest pageRequest = PageRequest . of ( page - 1 , size , Sort . by ( "pubTime" ) . descending ( ) );
Specification < Article > articleSpecification = buildSearchSpecification ( true ) ;
Page < Article > ArticlePage = articleDao . findAll ( articleSpecification , pageRequest ) ;
List < Article > content = ArticlePage . getContent ( ) ;
@ -337,7 +362,6 @@ public class CMSArticleService {
}
}
return result ;
}
private String getArticleFromCache ( String id ) {
@ -351,5 +375,6 @@ public class CMSArticleService {
private void cleanArticleCache ( String id ) {
cacheChannel . evict ( constants . Constants . J2CACHE_REGION_ARTICLE , id ) ;
cacheChannel . clear ( constants . Constants . J2CACHE_REGION_ARTICLE_PAGE ) ;
cacheChannel . clear ( constants . Constants . J2CACHE_REGION_ARTICLE_DTO_PAGE ) ;
}
}