Authored by 张帅

同步 点赞 收藏 评论

Showing 19 changed files with 1453 additions and 29 deletions
package com.yoho.datasync.fullsync.dal.repository.grass;
import com.yoho.datasync.fullsync.dal.repository.grass.model.GrassArticleComment;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface GrassArticleCommentRepository extends JpaRepository<GrassArticleComment, Integer>{
int countByCreateTimeBetween(Integer startTime, Integer endTime);
List<GrassArticleComment> findAllByCreateTimeBetween(Integer startTime, Integer endTime, Pageable pageable);
List<GrassArticleComment> findAllByDestIdInAndArticleTypeInAndStatusIsNotAndParentIdIsNull(List<Integer> articleIds, List<Integer> articleTypes,Integer status);
List<GrassArticleComment> findAllByDestIdInAndArticleTypeInAndStatusIsNotAndParentIdIsNotNull(List<Integer> articleIds, List<Integer> articleTypes,Integer status);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.grass;
import com.yoho.datasync.fullsync.dal.repository.grass.model.GrassUserAttention;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface GrassUserAttentionRepository extends JpaRepository<GrassUserAttention, Integer>{
int countByCreateTimeBetween(Integer startTime, Integer endTime);
List<GrassUserAttention> findAllByCreateTimeBetween(Integer startTime, Integer endTime, Pageable pageable);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.grass;
import com.yoho.datasync.fullsync.dal.repository.grass.model.UserFavoriteArticle;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserFavoriteArticleRepository extends JpaRepository<UserFavoriteArticle, Integer>{
List<UserFavoriteArticle> findAllByArticleIdIn(List<Integer> list);
int countByCreateTimeBetween(Integer startTime, Integer endTime);
List<UserFavoriteArticle> findAllByCreateTimeBetween(Integer startTime, Integer endTime, Pageable pageable);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.grass.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "grass_article_comment")
public class GrassArticleComment extends BaseEntity implements Serializable {
private static final long serialVersionUID = -2270181821264128734L;
@Column(name = "dest_id")
private Integer destId;
//1、用户发布 2、guang文章同步 3、mars文章同步 4、晒单文章同步
@Column(name = "article_type")
private Integer articleType;
//根评论id
@Column(name = "root_id")
private Integer rootId;
//评论父id
@Column(name = "parent_id")
private Integer parentId;
//同步评论id
@Column(name = "relate_id")
private Integer relateId;
//同步父评论id
@Column(name = "relate_parent_id")
private Integer relateParentId;
//点赞总数
@Column(name = "praise_total")
private Integer praiseTotal;
//审核状态(0 待审核,1通过,2未通过)
private Integer status;
//评论内容
private String content;
private Integer uid;
@Column(name = "create_time")
private Integer createTime;
@Column(name = "update_time")
private Integer updateTime;
//审核人
private String reviewer;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public Integer getDestId() {
return destId;
}
public void setDestId(Integer destId) {
this.destId = destId;
}
public Integer getArticleType() {
return articleType;
}
public void setArticleType(Integer articleType) {
this.articleType = articleType;
}
public Integer getRootId() {
return rootId;
}
public void setRootId(Integer rootId) {
this.rootId = rootId;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getRelateId() {
return relateId;
}
public void setRelateId(Integer relateId) {
this.relateId = relateId;
}
public Integer getRelateParentId() {
return relateParentId;
}
public void setRelateParentId(Integer relateParentId) {
this.relateParentId = relateParentId;
}
public Integer getPraiseTotal() {
return praiseTotal;
}
public void setPraiseTotal(Integer praiseTotal) {
this.praiseTotal = praiseTotal;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
public String getReviewer() {
return reviewer;
}
public void setReviewer(String reviewer) {
this.reviewer = reviewer;
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.grass.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "grass_user_attention")
public class GrassUserAttention extends BaseEntity implements Serializable {
private static final long serialVersionUID = -2270181821264128734L;
@Column(name = "article_id")
private Integer articleId;
private Integer uid;
//被关注人uid
@Column(name = "follow_uid")
private Integer followUid;
//0:已关注 1:已取消关注
private Integer status;
//0:关注话题(目前没还有关注话题) 1:关注用户
@Column(name = "attention_type")
private Integer attentionType;
@Column(name = "update_time")
private Integer updateTime;
//作者uid类型 1:有货uid 2:逛小编authorId
@Column(name = "author_type")
private Integer authorType;
@Column(name = "create_time")
private Integer createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getArticleId() {
return articleId;
}
public void setArticleId(Integer articleId) {
this.articleId = articleId;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public Integer getFollowUid() {
return followUid;
}
public void setFollowUid(Integer followUid) {
this.followUid = followUid;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getAttentionType() {
return attentionType;
}
public void setAttentionType(Integer attentionType) {
this.attentionType = attentionType;
}
public Integer getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
public Integer getAuthorType() {
return authorType;
}
public void setAuthorType(Integer authorType) {
this.authorType = authorType;
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.grass.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "user_favorite_article")
public class UserFavoriteArticle extends BaseEntity implements Serializable {
private static final long serialVersionUID = -2270181821264128734L;
@Column(name = "article_id")
private Integer articleId;
private Integer uid;
@Column(name = "create_time")
private Integer createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getArticleId() {
return articleId;
}
public void setArticleId(Integer articleId) {
this.articleId = articleId;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicUserAttention;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PublicUserAttentionRepository extends JpaRepository<PublicUserAttention, Integer>{
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicUserComment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PublicUserCommentRepository extends JpaRepository<PublicUserComment, Integer>{
List<PublicUserComment> findAllByRelateIdInAndSrcChannelIs(List<Integer> relateIds, Integer srcChannel);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicUserFavorite;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PublicUserFavoriteRepository extends JpaRepository<PublicUserFavorite, Integer>{
}
... ...
... ... @@ -18,6 +18,9 @@ public class PublicArticle extends BaseEntity implements Serializable {
@Column(name = "article_title")
private String articleTitle;
@Column(name = "cover_img")
private String coverImg;
//作者uid
@Column(name = "author_uid")
private Integer authorUid;
... ... @@ -167,4 +170,12 @@ public class PublicArticle extends BaseEntity implements Serializable {
public void setPublishTime(Long publishTime) {
this.publishTime = publishTime;
}
public String getCoverImg() {
return coverImg;
}
public void setCoverImg(String coverImg) {
this.coverImg = coverImg;
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "public_user_attention")
@EqualsAndHashCode(callSuper = true)
public class PublicUserAttention extends BaseEntity implements Serializable {
private static final long serialVersionUID = -2778511431046016623L;
//关注对象id
@Column(name = "target_id")
private Integer targetId;
@Column(name = "uid")
private Integer uid;
//作者uid类型 1:有货uid 2:逛小编authorId 3:马甲用户
@Column(name = "author_type")
private Integer authorType;
//关注类型:0-已取消 1-已关注 2-互相关注
@Column(name = "attention_type")
private Integer attentionType;
//状态(1-已收藏,2-已取消)
private Integer status;
@Column(name = "src_channel")
private Integer srcChannel;
@Column(name = "create_time")
private Long createTime;
@Column(name = "update_time")
private Long updateTime;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getSrcChannel() {
return srcChannel;
}
public void setSrcChannel(Integer srcChannel) {
this.srcChannel = srcChannel;
}
public Integer getTargetId() {
return targetId;
}
public void setTargetId(Integer targetId) {
this.targetId = targetId;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public Integer getAuthorType() {
return authorType;
}
public void setAuthorType(Integer authorType) {
this.authorType = authorType;
}
public Integer getAttentionType() {
return attentionType;
}
public void setAttentionType(Integer attentionType) {
this.attentionType = attentionType;
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "public_user_comment")
@EqualsAndHashCode(callSuper = true)
public class PublicUserComment extends BaseEntity implements Serializable {
private static final long serialVersionUID = -2778511431046016623L;
//关注对象id
@Column(name = "to_uid")
private Integer to_uid;
@Column(name = "uid")
private Integer uid;
@Column(name = "article_id")
private Integer articleId;
@Column(name = "to_comment_id")
private Integer toCommentId;
@Column(name = "content_data")
private String contentData;
@Column(name = "root_id")
private Integer rootId;
@Column(name = "floor_num")
private Integer floorNum;
@Column(name = "relate_id")
private Integer relateId;
//同步父评论id
@Column(name = "relate_parent_id")
private Integer relateParentId;
@Column(name = "praise_num")
private Integer praiseNum;
@Column(name = "virtual_praise_num")
private Integer virtualPraiseNum;
@Column(name = "audit_status")
private Integer auditStatus;
@Column(name = "audit_account")
private String auditAccount;
@Column(name = "audit_time")
private Long auditTime;
@Column(name = "src_channel")
private Integer srcChannel;
@Column(name = "create_time")
private Long createTime;
@Column(name = "update_time")
private Long updateTime;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getSrcChannel() {
return srcChannel;
}
public void setSrcChannel(Integer srcChannel) {
this.srcChannel = srcChannel;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public Integer getTo_uid() {
return to_uid;
}
public void setTo_uid(Integer to_uid) {
this.to_uid = to_uid;
}
public Integer getArticleId() {
return articleId;
}
public void setArticleId(Integer articleId) {
this.articleId = articleId;
}
public Integer getToCommentId() {
return toCommentId;
}
public void setToCommentId(Integer toCommentId) {
this.toCommentId = toCommentId;
}
public String getContentData() {
return contentData;
}
public void setContentData(String contentData) {
this.contentData = contentData;
}
public Integer getRootId() {
return rootId;
}
public void setRootId(Integer rootId) {
this.rootId = rootId;
}
public Integer getFloorNum() {
return floorNum;
}
public void setFloorNum(Integer floorNum) {
this.floorNum = floorNum;
}
public Integer getRelateId() {
return relateId;
}
public void setRelateId(Integer relateId) {
this.relateId = relateId;
}
public Integer getRelateParentId() {
return relateParentId;
}
public void setRelateParentId(Integer relateParentId) {
this.relateParentId = relateParentId;
}
public Integer getPraiseNum() {
return praiseNum;
}
public void setPraiseNum(Integer praiseNum) {
this.praiseNum = praiseNum;
}
public Integer getVirtualPraiseNum() {
return virtualPraiseNum;
}
public void setVirtualPraiseNum(Integer virtualPraiseNum) {
this.virtualPraiseNum = virtualPraiseNum;
}
public Integer getAuditStatus() {
return auditStatus;
}
public void setAuditStatus(Integer auditStatus) {
this.auditStatus = auditStatus;
}
public String getAuditAccount() {
return auditAccount;
}
public void setAuditAccount(String auditAccount) {
this.auditAccount = auditAccount;
}
public Long getAuditTime() {
return auditTime;
}
public void setAuditTime(Long auditTime) {
this.auditTime = auditTime;
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "public_user_favorite")
@EqualsAndHashCode(callSuper = true)
public class PublicUserFavorite extends BaseEntity implements Serializable {
private static final long serialVersionUID = -2778511431046016623L;
//收藏对象id
@Column(name = "target_id")
private Integer targetId;
//点赞者uid
@Column(name = "uid")
private Integer uid;
//收藏夹id
@Column(name = "sheet_id")
private Integer sheetId;
//收藏类型: 1、收藏文章
@Column(name = "favorite_type")
private Integer favoriteType;
//状态(1-已收藏,2-已取消)
private Integer status;
@Column(name = "src_channel")
private Integer srcChannel;
@Column(name = "create_time")
private Long createTime;
@Column(name = "update_time")
private Long updateTime;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getSrcChannel() {
return srcChannel;
}
public void setSrcChannel(Integer srcChannel) {
this.srcChannel = srcChannel;
}
public Integer getTargetId() {
return targetId;
}
public void setTargetId(Integer targetId) {
this.targetId = targetId;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public Integer getSheetId() {
return sheetId;
}
public void setSheetId(Integer sheetId) {
this.sheetId = sheetId;
}
public Integer getFavoriteType() {
return favoriteType;
}
public void setFavoriteType(Integer favoriteType) {
this.favoriteType = favoriteType;
}
}
... ...
package com.yoho.datasync.fullsync.constant;
public class UserAttentionConstant {
public static final int GRASS_HAS_ATTENTION = 0;
public static final int GRASS_CANCEL_ATTENTION = 1;
public static final int PUBLIC_HAS_ATTENTION = 1;
public static final int PUBLIC_CANCEL_ATTENTION = 0;
public static final int PUBLIC_MUTUAL_ATTENTION = 2;
}
... ...
package com.yoho.datasync.fullsync.constant;
public class UserCommentConstant {
public static final int GRASS_NOT_AUDIT = 0;
public static final int GRASS_HAS_AUDIT = 1;
public static final int GRASS_AUDIT_REFUSED = 2;
public static final int PUBLIC_NOT_AUDIT = 1;
public static final int PUBLIC_HAS_AUDIT = 2;
public static final int PUBLIC_AUDIT_REFUSED = 3;
}
... ...
... ... @@ -23,7 +23,7 @@ public class GrassDataSyncController {
}
@RequestMapping("/syncInteractiveData")
public String syncInteractiveData(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime,
public String syncInteractiveData(@RequestParam("startTime") Integer startTime, @RequestParam("endTime") Integer endTime,
@RequestParam("timeType") Integer timeType, @RequestParam("syncType") Integer syncType){
return grassInteractiveDataSyncService.syncInteractiveData(startTime, endTime, timeType, syncType);
... ...
... ... @@ -4,6 +4,6 @@ package com.yoho.datasync.fullsync.service;
public interface IGrassInteractiveDataSyncService {
String syncInteractiveData(Long startTime, Long endTime,
String syncInteractiveData(Integer startTime, Integer endTime,
Integer timeType, Integer syncType);
}
... ...
... ... @@ -117,10 +117,14 @@ public class GrassArticleSyncServiceImpl implements IGrassArticleSyncService {
List<GrassArticle> grassArticleList = grassArticleRepository.findAllByCreateTimeAfterAndCreateTimeBeforeAndArticleTypeInAndAuthStatusIn(
startTime, endTime,needSyncArticleType,needSyncArticleStatus,pageReq);
List<Integer> grassArticleIds = grassArticleList.stream().map(GrassArticle::getId).collect(Collectors.toList());
List<GrassArticleBlock> blockList = grassArticleBlockRepository.findAllByArticleIdInAndStatusIs(grassArticleIds, 1);
Map<Integer, List<GrassArticleBlock>> blocksMap = getBlocksMap(blockList);
try{
List<PublicArticle> publicArticleList = new ArrayList<>();
grassArticleList.forEach(grassArticle -> {
PublicArticle publicArticle = convertPublicArticle(grassArticle);
PublicArticle publicArticle = convertPublicArticle(grassArticle,blocksMap);
publicArticleList.add(publicArticle);
});
savedList = publicArticleRepository.saveAll(publicArticleList);
... ... @@ -178,7 +182,7 @@ public class GrassArticleSyncServiceImpl implements IGrassArticleSyncService {
return resultMap;
}
private PublicArticle convertPublicArticle(GrassArticle grassArticle){
private PublicArticle convertPublicArticle(GrassArticle grassArticle,Map<Integer, List<GrassArticleBlock>> blocksMap){
PublicArticle publicArticle = new PublicArticle();
publicArticle.setAuthorUid(grassArticle.getAuthorUid());
publicArticle.setArticleType(grassArticle.getArticleType());
... ... @@ -187,6 +191,7 @@ public class GrassArticleSyncServiceImpl implements IGrassArticleSyncService {
publicArticle.setUpdateTime(grassArticle.getUpdateTime());
publicArticle.setAuthorType(grassArticle.getAuthorType());
publicArticle.setArticleTitle("");
publicArticle.setCoverImg(getCoverImg(blocksMap.get(grassArticle.getId())));
publicArticle.setMinRelateId(grassArticle.getRelateId());
publicArticle.setPraiseNum(grassArticle.getPraiseCount());
publicArticle.setFavoriteNum(grassArticle.getFavoriteCount());
... ... @@ -196,6 +201,27 @@ public class GrassArticleSyncServiceImpl implements IGrassArticleSyncService {
return publicArticle;
}
private String getCoverImg(List<GrassArticleBlock> grassArticleBlocks){
grassArticleBlocks = grassArticleBlocks.stream().sorted((o1, o2) ->{
if(o1.getOrderBy()> o2.getOrderBy()){
return 1;
}else {
return -1;
}
}).collect(Collectors.toList());
for (GrassArticleBlock grassArticleBlock:grassArticleBlocks) {
//第一个image为默认封面图
if("image".equals(grassArticleBlock.getTemplateKey())){
String dataString = JSONObject.parseObject(grassArticleBlock.getContentData()).getString("data");
JSONObject block = JSONObject.parseObject(dataString);
return block.getString("src");
}
}
return "";
}
//两张表审核状态转换
private int getArticleAuditStatus(int authStatus){
switch (authStatus){
... ...
package com.yoho.datasync.fullsync.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.yoho.datasync.fullsync.constant.UserAttentionConstant;
import com.yoho.datasync.fullsync.constant.UserCommentConstant;
import com.yoho.datasync.fullsync.constant.UserPraiseConstant;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import com.yoho.datasync.fullsync.dal.repository.grass.GrassArticlePraiseRepository;
import com.yoho.datasync.fullsync.dal.repository.grass.GrassArticleRepository;
import com.yoho.datasync.fullsync.dal.repository.grass.model.GrassArticle;
import com.yoho.datasync.fullsync.dal.repository.grass.model.GrassArticlePraise;
import com.yoho.datasync.fullsync.dal.repository.pcms.PublicArticleRepository;
import com.yoho.datasync.fullsync.dal.repository.pcms.PublicUserPraiseRepository;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicArticle;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicUserPraise;
import com.yoho.datasync.fullsync.dal.repository.grass.*;
import com.yoho.datasync.fullsync.dal.repository.grass.model.*;
import com.yoho.datasync.fullsync.dal.repository.pcms.*;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.*;
import com.yoho.datasync.fullsync.service.IGrassInteractiveDataSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -48,6 +47,24 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
@Autowired
private PublicArticleRepository publicArticleRepository;
@Autowired
private UserFavoriteArticleRepository userFavoriteArticleRepository;
@Autowired
private PublicUserFavoriteRepository publicUserFavoriteRepository;
@Autowired
private GrassUserAttentionRepository grassUserAttentionRepository;
@Autowired
private PublicUserAttentionRepository publicUserAttentionRepository;
@Autowired
private GrassArticleCommentRepository grassArticleCommentRepository;
@Autowired
private PublicUserCommentRepository publicUserCommentRepository;
private static final int PRAISE = 1;
private static final int COMMENT = 2;
private static final int FAVORITE = 3;
... ... @@ -57,12 +74,21 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
private static final int SLAVE = 2;
@Override
public String syncInteractiveData(Long startTime, Long endTime, Integer timeType, Integer syncType) {
public String syncInteractiveData(Integer startTime, Integer endTime, Integer timeType, Integer syncType) {
// syncType : 1、同步点赞 2、 同步评论 3、 同步收藏 4、 同步关注
switch (syncType){
case PRAISE:
syncPraise(startTime, endTime, timeType);
break;
case FAVORITE:
syncFavorite(startTime, endTime, timeType);
break;
case ATTENTION:
syncUserAttention(startTime, endTime);
break;
case COMMENT:
syncComments(startTime, endTime,timeType);
break;
default:
break;
... ... @@ -70,7 +96,7 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
return "success";
}
private void syncPraise(Long startTime, Long endTime, Integer timeType){
private void syncPraise(Integer startTime, Integer endTime, Integer timeType){
switch (timeType){
case MASTER:
syncPraiseByArticle(startTime,endTime);
... ... @@ -81,11 +107,35 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
}
}
private void syncPraiseByArticle(Long startTime, Long endTime){
private void syncFavorite(Integer startTime, Integer endTime, Integer timeType){
switch (timeType){
case MASTER:
syncFavoriteByArticle(startTime,endTime);
break;
case SLAVE:
syncFavoriteBySelf(startTime,endTime);
break;
}
}
private void syncComments(Integer startTime, Integer endTime, Integer timeType){
switch (timeType){
case MASTER:
syncCommentsByArticle(startTime,endTime);
break;
case SLAVE:
syncCommentsBySelf(startTime,endTime);
break;
}
}
private void syncPraiseByArticle(Integer startTime, Integer endTime){
logger.info("syncPraiseByArticle begin, startTime is {}, endTime is {}", startTime, endTime);
ExecutorService es = Executors.newSingleThreadExecutor();
int pageSize = 100;
int total = grassArticleRepository.countByCreateTimeAfterAndCreateTimeBeforeAndArticleTypeInAndAuthStatusIn(startTime, endTime,needSyncArticleType,needSyncArticleStatus);
Long startTimeLong = (long)startTime * 1000;
Long endTimeLong = (long)endTime * 1000;
int total = grassArticleRepository.countByCreateTimeAfterAndCreateTimeBeforeAndArticleTypeInAndAuthStatusIn(startTimeLong, endTimeLong,needSyncArticleType,needSyncArticleStatus);
logger.info("syncPraiseByArticle get grassArticle count is {}", total);
int totalPage = total % pageSize ==0 ? total / pageSize : (total / pageSize)+1;
try{
... ... @@ -93,7 +143,7 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
for (int index=0; index < totalPage; index++) {
int indexPage = index;
es.execute(() -> {
syncPraiseByArticlePage(startTime, endTime, indexPage, pageSize);
syncPraiseByArticlePage(startTimeLong, endTimeLong, indexPage, pageSize);
});
}
}finally {
... ... @@ -107,9 +157,9 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
startTime, endTime,needSyncArticleType,needSyncArticleStatus,pageReq);
List<Integer> grassArticleIds = grassArticleList.stream().map(BaseEntity::getId).collect(Collectors.toList());
List<PublicArticle> publicUserPraiseList = publicArticleRepository.findAllByRelateIdInAndArticleTypeIn(grassArticleIds, needSyncArticleType);
List<PublicArticle> publicArticleList = publicArticleRepository.findAllByRelateIdInAndArticleTypeIn(grassArticleIds, needSyncArticleType);
Map<Integer, Integer> publicArticleIdAndGrassIdMap = new HashMap<>();
publicUserPraiseList.forEach(publicArticle -> {
publicArticleList.forEach(publicArticle -> {
publicArticleIdAndGrassIdMap.put(publicArticle.getRelateId(), publicArticle.getId());
});
List<GrassArticlePraise> grassArticlePraiseList = grassArticlePraiseRepository.findAllByArticleIdIn(grassArticleIds);
... ... @@ -118,14 +168,18 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
publicUserPraiseRepository.saveAll(needSyncData);
}
private List<PublicUserPraise> buildPublicUserPraise( Map<Integer, Integer> publicArticleIdAndGrassIdMap, List<GrassArticlePraise> grassArticlePraiseList){
private List<PublicUserPraise> buildPublicUserPraise(Map<Integer, Integer> publicArticleIdAndGrassIdMap, List<GrassArticlePraise> grassArticlePraiseList){
List<PublicUserPraise> resultList = Lists.newArrayList();
if(CollectionUtils.isEmpty(grassArticlePraiseList)){
return resultList;
}
grassArticlePraiseList.forEach(grassArticlePraise -> {
Integer targetId = publicArticleIdAndGrassIdMap.get(grassArticlePraise.getArticleId());
if(targetId == null){
return;
}
PublicUserPraise publicUserPraise = new PublicUserPraise();
publicUserPraise.setTargetId(publicArticleIdAndGrassIdMap.get(grassArticlePraise.getArticleId()));
publicUserPraise.setTargetId(targetId);
publicUserPraise.setUid(grassArticlePraise.getUid());
publicUserPraise.setPraiseType(UserPraiseConstant.ARTICLE_PRAISE);
publicUserPraise.setStatus(convertPraiseStatus(grassArticlePraise.getStatus()));
... ... @@ -149,13 +203,11 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
}
}
private void syncPraiseBySelf(Long startTime, Long endTime){
private void syncPraiseBySelf(Integer startTime, Integer endTime){
logger.info("syncPraiseBySelf begin, startTime is {}, endTime is {}", startTime, endTime);
ExecutorService es = Executors.newSingleThreadExecutor();
int pageSize = 100;
Integer startTimeInt = startTime.intValue();
Integer endTimeInt = endTime.intValue();
int total = grassArticlePraiseRepository.countByCreateTimeBetween(startTimeInt, endTimeInt);
int total = grassArticlePraiseRepository.countByCreateTimeBetween(startTime, endTime);
logger.info("syncPraiseBySelf get grassArticlePraise count is {}", total);
int totalPage = total % pageSize ==0 ? total / pageSize : (total / pageSize)+1;
try{
... ... @@ -163,7 +215,7 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
for (int index=0; index < totalPage; index++) {
int indexPage = index;
es.execute(() -> {
syncPraiseBySelfPage(startTimeInt, endTimeInt, indexPage, pageSize);
syncPraiseBySelfPage(startTime, endTime, indexPage, pageSize);
});
}
}finally {
... ... @@ -173,16 +225,455 @@ public class GrassInteractiveDataSyncServiceImpl implements IGrassInteractiveDat
private void syncPraiseBySelfPage(int startTime, int endTime, int indexPage, int pageSize){
Pageable pageReq = PageRequest.of(indexPage, pageSize);
List<GrassArticlePraise> grassArticleList = grassArticlePraiseRepository.findAllByCreateTimeBetween(
List<GrassArticlePraise> grassArticlePraiseList = grassArticlePraiseRepository.findAllByCreateTimeBetween(
startTime, endTime,pageReq);
List<Integer> grassArticleIds = grassArticleList.stream().map(BaseEntity::getId).collect(Collectors.toList());
List<Integer> grassArticleIds = grassArticlePraiseList.stream().map(GrassArticlePraise::getArticleId).collect(Collectors.toList());
List<PublicArticle> publicUserPraiseList = publicArticleRepository.findAllByRelateIdInAndArticleTypeIn(grassArticleIds, needSyncArticleType);
Map<Integer, Integer> publicArticleIdAndGrassIdMap = new HashMap<>();
publicUserPraiseList.forEach(publicArticle -> {
publicArticleIdAndGrassIdMap.put(publicArticle.getRelateId(), publicArticle.getId());
});
List<PublicUserPraise> needSyncData = buildPublicUserPraise(publicArticleIdAndGrassIdMap, grassArticleList);
List<PublicUserPraise> needSyncData = buildPublicUserPraise(publicArticleIdAndGrassIdMap, grassArticlePraiseList);
publicUserPraiseRepository.saveAll(needSyncData);
}
private void syncFavoriteByArticle(Integer startTime, Integer endTime){
logger.info("syncFavoriteByArticle begin, startTime is {}, endTime is {}", startTime, endTime);
ExecutorService es = Executors.newSingleThreadExecutor();
Long startTimeLong = (long)startTime * 1000;
Long endTimeLong = (long)endTime * 1000;
int pageSize = 100;
int total = grassArticleRepository.countByCreateTimeAfterAndCreateTimeBeforeAndArticleTypeInAndAuthStatusIn(startTimeLong, endTimeLong,needSyncArticleType,needSyncArticleStatus);
logger.info("syncFavoriteByArticle get grassArticle count is {}", total);
int totalPage = total % pageSize ==0 ? total / pageSize : (total / pageSize)+1;
try{
//每次查询100条,增加detail
for (int index=0; index < totalPage; index++) {
int indexPage = index;
es.execute(() -> {
syncFavoriteByArticlePage(startTimeLong, endTimeLong, indexPage, pageSize);
});
}
}finally {
es.shutdown();
}
}
private void syncFavoriteByArticlePage(Long startTime, Long endTime, int indexPage, int pageSize){
Pageable pageReq = PageRequest.of(indexPage, pageSize);
List<GrassArticle> grassArticleList = grassArticleRepository.findAllByCreateTimeAfterAndCreateTimeBeforeAndArticleTypeInAndAuthStatusIn(
startTime, endTime,needSyncArticleType,needSyncArticleStatus,pageReq);
List<Integer> grassArticleIds = grassArticleList.stream().map(BaseEntity::getId).collect(Collectors.toList());
List<PublicArticle> publicArticleList = publicArticleRepository.findAllByRelateIdInAndArticleTypeIn(grassArticleIds, needSyncArticleType);
Map<Integer, Integer> publicArticleIdAndGrassIdMap = new HashMap<>();
publicArticleList.forEach(publicArticle -> {
publicArticleIdAndGrassIdMap.put(publicArticle.getRelateId(), publicArticle.getId());
});
List<UserFavoriteArticle> userFavoriteArticleList = userFavoriteArticleRepository.findAllByArticleIdIn(grassArticleIds);
logger.info("syncFavoriteByArticlePage find userFavoriteArticle num is {}", userFavoriteArticleList.size());
List<PublicUserFavorite> needSyncData = buildPublicUserFavorite(publicArticleIdAndGrassIdMap, userFavoriteArticleList);
publicUserFavoriteRepository.saveAll(needSyncData);
}
private List<PublicUserFavorite> buildPublicUserFavorite(Map<Integer, Integer> publicArticleIdAndGrassIdMap, List<UserFavoriteArticle> userFavoriteArticleList){
List<PublicUserFavorite> resultList = Lists.newArrayList();
if(CollectionUtils.isEmpty(userFavoriteArticleList)){
return resultList;
}
userFavoriteArticleList.forEach(userFavoriteArticle -> {
Integer targetId = publicArticleIdAndGrassIdMap.get(userFavoriteArticle.getArticleId());
if(null == targetId){
return;
}
PublicUserFavorite publicUserFavorite = new PublicUserFavorite();
publicUserFavorite.setUid(userFavoriteArticle.getUid());
publicUserFavorite.setStatus(1);
publicUserFavorite.setSrcChannel(1);
publicUserFavorite.setTargetId(targetId);
publicUserFavorite.setCreateTime((long)(userFavoriteArticle.getCreateTime()) * 1000);
publicUserFavorite.setFavoriteType(1);
resultList.add(publicUserFavorite);
});
return resultList;
}
private void syncFavoriteBySelf(Integer startTime, Integer endTime){
logger.info("syncFavoriteBySelf begin, startTime is {}, endTime is {}", startTime, endTime);
ExecutorService es = Executors.newSingleThreadExecutor();
int pageSize = 100;
int total = userFavoriteArticleRepository.countByCreateTimeBetween(startTime, endTime);
logger.info("syncFavoriteBySelf get grassArticlePraise count is {}", total);
int totalPage = total % pageSize ==0 ? total / pageSize : (total / pageSize)+1;
try{
//每次查询100条,增加detail
for (int index=0; index < totalPage; index++) {
int indexPage = index;
es.execute(() -> {
syncFavoriteBySelfPage(startTime, endTime, indexPage, pageSize);
});
}
}finally {
es.shutdown();
}
}
private void syncFavoriteBySelfPage(int startTime, int endTime, int indexPage, int pageSize){
Pageable pageReq = PageRequest.of(indexPage, pageSize);
List<UserFavoriteArticle> userFavoriteArticleList = userFavoriteArticleRepository.findAllByCreateTimeBetween(
startTime, endTime,pageReq);
List<Integer> grassArticleIds = userFavoriteArticleList.stream().map(UserFavoriteArticle::getArticleId).collect(Collectors.toList());
List<PublicArticle> publicArticleList = publicArticleRepository.findAllByRelateIdInAndArticleTypeIn(grassArticleIds, needSyncArticleType);
Map<Integer, Integer> publicArticleIdAndGrassIdMap = new HashMap<>();
publicArticleList.forEach(publicArticle -> {
publicArticleIdAndGrassIdMap.put(publicArticle.getRelateId(), publicArticle.getId());
});
List<PublicUserFavorite> needSyncData = buildPublicUserFavorite(publicArticleIdAndGrassIdMap, userFavoriteArticleList);
publicUserFavoriteRepository.saveAll(needSyncData);
}
private void syncUserAttention(Integer startTime, Integer endTime){
logger.info("syncUserAttention begin, startTime is {}, endTime is {}", startTime, endTime);
ExecutorService es = Executors.newSingleThreadExecutor();
int pageSize = 100;
int total = grassUserAttentionRepository.countByCreateTimeBetween(startTime, endTime);
logger.info("syncUserAttention get attention count is {}", total);
int totalPage = total % pageSize ==0 ? total / pageSize : (total / pageSize)+1;
try{
//每次查询100条,增加detail
for (int index=0; index < totalPage; index++) {
int indexPage = index;
es.execute(() -> {
syncUserAttentionPage(startTime, endTime, indexPage, pageSize);
});
}
}finally {
es.shutdown();
}
}
private void syncUserAttentionPage(int startTime, int endTime, int indexPage, int pageSize){
Pageable pageReq = PageRequest.of(indexPage, pageSize);
List<GrassUserAttention> grassUserAttentionList = grassUserAttentionRepository.findAllByCreateTimeBetween(
startTime, endTime,pageReq);
List<PublicUserAttention> needSyncData = buildPublicUserAttention(grassUserAttentionList);
publicUserAttentionRepository.saveAll(needSyncData);
}
private List<PublicUserAttention> buildPublicUserAttention( List<GrassUserAttention> grassUserAttentionList){
List<PublicUserAttention> resultList = Lists.newArrayList();
if(CollectionUtils.isEmpty(grassUserAttentionList)){
return resultList;
}
grassUserAttentionList.forEach(grassUserAttention -> {
PublicUserAttention publicUserAttention = new PublicUserAttention();
publicUserAttention.setUid(grassUserAttention.getUid());
publicUserAttention.setStatus(convertAttentionStatus(grassUserAttention.getStatus()));
publicUserAttention.setSrcChannel(1);
publicUserAttention.setTargetId(grassUserAttention.getFollowUid());
publicUserAttention.setCreateTime((long)(grassUserAttention.getCreateTime()) *1000);
publicUserAttention.setUpdateTime((long)(Optional.ofNullable(grassUserAttention.getUpdateTime()).orElse(0)) *1000);
publicUserAttention.setAuthorType(grassUserAttention.getAuthorType());
publicUserAttention.setAttentionType(1);
resultList.add(publicUserAttention);
});
return resultList;
}
private int convertAttentionStatus(int grassAttentionStatus){
switch (grassAttentionStatus){
case UserAttentionConstant.GRASS_HAS_ATTENTION:
return UserAttentionConstant.PUBLIC_HAS_ATTENTION;
case UserAttentionConstant.GRASS_CANCEL_ATTENTION:
return UserAttentionConstant.PUBLIC_CANCEL_ATTENTION;
default:
return UserAttentionConstant.PUBLIC_CANCEL_ATTENTION;
}
}
/**
* 根据文章主表的时间区间同步 评论
* 数据构成主要是:1 文章的所有根评论
* 2 所有子评论
* @param startTime
* @param endTime
*/
private void syncCommentsByArticle(Integer startTime, Integer endTime){
logger.info("syncCommentsByArticle begin, startTime is {}, endTime is {}", startTime, endTime);
ExecutorService es = Executors.newSingleThreadExecutor();
int pageSize = 100;
Long startTimeLong = (long)startTime * 1000;
Long endTimeLong = (long)endTime * 1000;
int total = grassArticleRepository.countByCreateTimeAfterAndCreateTimeBeforeAndArticleTypeInAndAuthStatusIn(startTimeLong, endTimeLong,needSyncArticleType,needSyncArticleStatus);
logger.info("syncCommentsByArticle get grassArticle count is {}", total);
int totalPage = total % pageSize ==0 ? total / pageSize : (total / pageSize)+1;
try{
//每次查询100条,增加detail
for (int index=0; index < totalPage; index++) {
int indexPage = index;
es.execute(() -> {
syncCommentsByArticlePage(startTimeLong, endTimeLong, indexPage, pageSize);
});
}
}finally {
es.shutdown();
}
}
/**
* 根据评论表自己的时间区间同步 评论
* 数据构成主要是:1 时间区间内所有新产生的根评论
* 2 时间区间内所有新产生的子评论
* 3 非时间区间内的根评论的子评论
* @param startTime
* @param endTime
*/
private void syncCommentsBySelf(Integer startTime, Integer endTime){
logger.info("syncCommentsBySelf begin, startTime is {}, endTime is {}", startTime, endTime);
ExecutorService es = Executors.newSingleThreadExecutor();
int pageSize = 100;
int total = grassArticleCommentRepository.countByCreateTimeBetween(startTime, endTime);
logger.info("syncCommentsBySelf get grassArticlePraise count is {}", total);
int totalPage = total % pageSize ==0 ? total / pageSize : (total / pageSize)+1;
try{
//每次查询100条,增加detail
for (int index=0; index < totalPage; index++) {
int indexPage = index;
es.execute(() -> {
syncCommentsBySelfPage(startTime, endTime, indexPage, pageSize);
});
}
}finally {
es.shutdown();
}
}
/**
* 根据评论表自己的时间区间同步 评论
* 数据构成主要是:1 时间区间内所有新产生的根评论
* 2 时间区间内所有新产生的子评论
* 3 非时间区间内的根评论的子评论
* @param startTime
* @param endTime
*/
private void syncCommentsBySelfPage(int startTime, int endTime, int indexPage, int pageSize){
Pageable pageReq = PageRequest.of(indexPage, pageSize);
List<GrassArticleComment> grassArticleCommentsList = grassArticleCommentRepository.findAllByCreateTimeBetween(
startTime, endTime,pageReq);
List<Integer> grassArticleIds = grassArticleCommentsList.stream().map(GrassArticleComment::getDestId).collect(Collectors.toList());
List<PublicArticle> publicArticleList = publicArticleRepository.findAllByRelateIdInAndArticleTypeIn(grassArticleIds, needSyncArticleType);
Map<Integer, Integer> publicArticleIdAndGrassIdMap = new HashMap<>();
publicArticleList.forEach(publicArticle -> {
publicArticleIdAndGrassIdMap.put(publicArticle.getRelateId(), publicArticle.getId());
});
//先把时间区间内所有的新产生的根评论同步
List<GrassArticleComment> grassRootArticleComments = grassArticleCommentsList.stream().filter(grassArticleComment -> grassArticleComment.getParentId() == null).collect(Collectors.toList());;
List<PublicUserComment> newRootPublicComments = buildRootComments(publicArticleIdAndGrassIdMap, grassRootArticleComments);
List<PublicUserComment> rootPublicComments = publicUserCommentRepository.saveAll(newRootPublicComments);
//同步之后,查询所有新产生子评论的父评论id
//要同步的子评论数据中包含两部分 1 父评论已经存在在新表中 刚刚同步的 或者是时间区间之前产生的
// 2 父评论也在这一部分子评论中 此时需要递归进行插入
List<GrassArticleComment> grassChildrenArticleComments = grassArticleCommentsList.stream().filter(grassArticleComment -> grassArticleComment.getParentId() != null).collect(Collectors.toList());
List<Integer> rootIds = grassChildrenArticleComments.stream().map(GrassArticleComment::getRootId).distinct().collect(Collectors.toList());
List<Integer> parentIds = grassChildrenArticleComments.stream().map(GrassArticleComment::getParentId).distinct().collect(Collectors.toList());
rootIds.addAll(parentIds);
List<Integer> relatedIds = rootIds.stream().distinct().collect(Collectors.toList());
List<PublicUserComment> publicRootIds = publicUserCommentRepository.findAllByRelateIdInAndSrcChannelIs(relatedIds, 1);
Map<Integer, Integer> relateIdMap = new HashMap<>();
publicRootIds.forEach(publicUserComment -> {
relateIdMap.put(publicUserComment.getRelateId(), publicUserComment.getId());
});
buildAndSaveChildrenCommentBySelf(publicArticleIdAndGrassIdMap, grassChildrenArticleComments, relateIdMap);
}
/**
* 根据文章主表的时间区间同步 评论
* 数据构成主要是:1 文章的所有根评论
* 2 所有子评论
* @param startTime
* @param endTime
*/
private void syncCommentsByArticlePage(Long startTime, Long endTime, int indexPage, int pageSize){
Pageable pageReq = PageRequest.of(indexPage, pageSize);
List<GrassArticle> grassArticleList = grassArticleRepository.findAllByCreateTimeAfterAndCreateTimeBeforeAndArticleTypeInAndAuthStatusIn(
startTime, endTime,needSyncArticleType,needSyncArticleStatus,pageReq);
List<Integer> grassArticleIds = grassArticleList.stream().map(BaseEntity::getId).collect(Collectors.toList());
List<PublicArticle> publicArticleList = publicArticleRepository.findAllByRelateIdInAndArticleTypeIn(grassArticleIds, needSyncArticleType);
Map<Integer, Integer> publicArticleIdAndGrassIdMap = new HashMap<>();
publicArticleList.forEach(publicArticle -> {
publicArticleIdAndGrassIdMap.put(publicArticle.getRelateId(), publicArticle.getId());
});
//先把所有根评论同步
List<PublicUserComment> roots = queryAndBuildRootComment(grassArticleIds, publicArticleIdAndGrassIdMap);
List<PublicUserComment> rootPublicComments = publicUserCommentRepository.saveAll(roots);
//审核未通过的不查
List<GrassArticleComment> childrenComments = grassArticleCommentRepository.findAllByDestIdInAndArticleTypeInAndStatusIsNotAndParentIdIsNotNull(grassArticleIds,
needSyncArticleType, 2);
Map<Integer, Integer> rootIdMap = new HashMap<>();
rootPublicComments.forEach(publicUserComment -> {
rootIdMap.put(publicUserComment.getRelateId(), publicUserComment.getId());
});
//同步所有层级的子评论
buildAndSaveChildrenComment(publicArticleIdAndGrassIdMap,rootPublicComments, childrenComments, rootIdMap);
}
private List<PublicUserComment> queryAndBuildRootComment(List<Integer> grassArticleIds, Map<Integer, Integer> publicArticleIdAndGrassIdMap){
List<PublicUserComment> result;
//审核未通过的不查
List<GrassArticleComment> rootComments = grassArticleCommentRepository.findAllByDestIdInAndArticleTypeInAndStatusIsNotAndParentIdIsNull(grassArticleIds,
needSyncArticleType, 2);
result = buildRootComments(publicArticleIdAndGrassIdMap, rootComments);
return result;
}
private List<PublicUserComment> buildRootComments( Map<Integer, Integer> publicArticleIdAndGrassIdMap, List<GrassArticleComment> grassRootArticleComments){
List<PublicUserComment> result = Lists.newArrayList();
grassRootArticleComments.forEach(grassArticleComment -> {
Integer articleId = publicArticleIdAndGrassIdMap.get(grassArticleComment.getDestId());
if(articleId == null){
return;
}
PublicUserComment publicUserComment = new PublicUserComment();
publicUserComment.setUid(grassArticleComment.getUid());
publicUserComment.setSrcChannel(1);
publicUserComment.setCreateTime((long)(grassArticleComment.getCreateTime()) * 1000);
publicUserComment.setUpdateTime((long)(Optional.ofNullable(grassArticleComment.getUpdateTime()).orElse(0)) *1000);
publicUserComment.setArticleId(articleId);
publicUserComment.setContentData(convertCommentContent(grassArticleComment.getContent()));
publicUserComment.setRelateId(grassArticleComment.getId());
publicUserComment.setPraiseNum(grassArticleComment.getPraiseTotal());
publicUserComment.setAuditStatus(convertCommentStatus(grassArticleComment.getStatus()));
publicUserComment.setAuditAccount(grassArticleComment.getReviewer());
publicUserComment.setAuditTime((long)(Optional.ofNullable(grassArticleComment.getUpdateTime()).orElse(0)) *1000);
result.add(publicUserComment);
});
return result;
}
//同步所有层级的子评论 各层级的自关联子评论 递归处理
private void buildAndSaveChildrenComment( Map<Integer, Integer> publicArticleIdAndGrassIdMap,
List<PublicUserComment> rootPublicComments, List<GrassArticleComment> childrenComments,
Map<Integer, Integer> rootIdMap){
if(CollectionUtils.isEmpty(childrenComments) || CollectionUtils.isEmpty(rootPublicComments)){
return;
}
List<PublicUserComment> syncPublicUserCommentList;
List<Integer> rootIds = rootPublicComments.stream().map(PublicUserComment::getRelateId).collect(Collectors.toList());
Map<Integer, Integer> commentRootIdAndPublicId = new HashMap<>();
rootPublicComments.forEach(publicUserComment -> {
commentRootIdAndPublicId.put(publicUserComment.getRelateId(), publicUserComment.getId());
});
List<GrassArticleComment> needSync = childrenComments.stream().filter(grassArticleComment ->
rootIds.contains(grassArticleComment.getParentId())).collect(Collectors.toList());
List<GrassArticleComment> nextSync = childrenComments.stream().filter(grassArticleComment ->
!rootIds.contains(grassArticleComment.getParentId())).collect(Collectors.toList());
syncPublicUserCommentList = buildChildrenComments(publicArticleIdAndGrassIdMap, needSync, commentRootIdAndPublicId, rootIdMap);
List<PublicUserComment> publicUserCommentList = publicUserCommentRepository.saveAll(syncPublicUserCommentList);
buildAndSaveChildrenComment(publicArticleIdAndGrassIdMap, publicUserCommentList, nextSync, rootIdMap);
}
//同步之后,查询所有新产生子评论的父评论id
//要同步的子评论数据中包含两部分 1 父评论已经存在在新表中 刚刚同步的 或者是时间区间之前产生的
// 2 父评论也在这一部分子评论中 此时需要递归进行插入
private void buildAndSaveChildrenCommentBySelf( Map<Integer, Integer> publicArticleIdAndGrassIdMap,
List<GrassArticleComment> childrenComments,
Map<Integer, Integer> relateIdMap){
if(CollectionUtils.isEmpty(childrenComments) ){
return;
}
List<PublicUserComment> syncPublicUserCommentList;
// 父评论已经存在在新表中
List<GrassArticleComment> needSync = childrenComments.stream().filter(grassArticleComment ->
relateIdMap.keySet().contains(grassArticleComment.getParentId())).collect(Collectors.toList());
//父评论也在这一部分子评论中
List<GrassArticleComment> nextSync = childrenComments.stream().filter(grassArticleComment ->
!relateIdMap.keySet().contains(grassArticleComment.getParentId())).collect(Collectors.toList());
syncPublicUserCommentList = buildChildrenComments(publicArticleIdAndGrassIdMap, needSync, relateIdMap, relateIdMap);
//新同步的数据id对应关系 直接加到map中来
List<PublicUserComment> publicUserCommentList = publicUserCommentRepository.saveAll(syncPublicUserCommentList);
publicUserCommentList.forEach(publicUserComment -> {
relateIdMap.put(publicUserComment.getRelateId(), publicUserComment.getId());
});
buildAndSaveChildrenCommentBySelf(publicArticleIdAndGrassIdMap, nextSync, relateIdMap);
}
private List<PublicUserComment> buildChildrenComments(Map<Integer, Integer> publicArticleIdAndGrassIdMap,List<GrassArticleComment> needSync,
Map<Integer, Integer> commentRootIdAndPublicId,
Map<Integer, Integer> rootIdMap){
List<PublicUserComment> syncPublicUserCommentList = Lists.newArrayList();
needSync.forEach(grassArticleComment -> {
Integer articleId = publicArticleIdAndGrassIdMap.get(grassArticleComment.getDestId());
if(articleId == null){
return;
}
PublicUserComment publicUserComment = new PublicUserComment();
publicUserComment.setUid(grassArticleComment.getUid());
publicUserComment.setSrcChannel(1);
publicUserComment.setCreateTime((long)(grassArticleComment.getCreateTime()) * 1000);
publicUserComment.setUpdateTime((long)(Optional.ofNullable(grassArticleComment.getUpdateTime()).orElse(0)) *1000);
publicUserComment.setArticleId(articleId);
publicUserComment.setToCommentId(commentRootIdAndPublicId.get(grassArticleComment.getParentId()));
publicUserComment.setContentData(convertCommentContent(grassArticleComment.getContent()));
publicUserComment.setRootId(rootIdMap.get(grassArticleComment.getRootId()));
publicUserComment.setRelateId(grassArticleComment.getId());
publicUserComment.setRelateParentId(grassArticleComment.getParentId());
publicUserComment.setPraiseNum(grassArticleComment.getPraiseTotal());
publicUserComment.setAuditStatus(convertCommentStatus(grassArticleComment.getStatus()));
publicUserComment.setAuditAccount(grassArticleComment.getReviewer());
publicUserComment.setAuditTime((long)(Optional.ofNullable(grassArticleComment.getUpdateTime()).orElse(0)) *1000);
syncPublicUserCommentList.add(publicUserComment);
});
return syncPublicUserCommentList;
}
private String convertCommentContent(String text){
JSONObject jsonObject = new JSONObject();
jsonObject.put("type","text");
jsonObject.put("content",text);
return jsonObject.toString();
}
private int convertCommentStatus(int grassCommentStatus){
switch (grassCommentStatus){
case UserCommentConstant.GRASS_NOT_AUDIT:
return UserCommentConstant.PUBLIC_NOT_AUDIT;
case UserCommentConstant.GRASS_HAS_AUDIT:
return UserCommentConstant.PUBLIC_HAS_AUDIT;
case UserCommentConstant.GRASS_AUDIT_REFUSED:
return UserCommentConstant.PUBLIC_AUDIT_REFUSED;
default:
return UserCommentConstant.PUBLIC_NOT_AUDIT;
}
}
}
... ...