Authored by csgyoho

now-syn-public

Showing 27 changed files with 1115 additions and 77 deletions
... ... @@ -10,6 +10,11 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>yoho-datasync-fullsync-dal</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.13.sec01</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicArticleLabel;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by shengguo.cai on 2019/3/25.
*/
public interface IPublicArticleLabelRepository extends JpaRepository<PublicArticleLabel, Integer> {
}
... ...
... ... @@ -2,9 +2,20 @@ package com.yoho.datasync.fullsync.dal.repository.pcms;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicArticle;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/22.
*/
public interface IPublicArticleRepository extends JpaRepository<PublicArticle, Integer> {
@Query(nativeQuery = true,value = "select * from public_article where create_time>=?1 and create_time<?2 limit ?3,?4")
List<PublicArticle> selectByTime(long begTime, long endTime, int offset, int size);
@Query(nativeQuery = true,value = "select count(1) from public_article where create_time>=?1 and create_time<?2")
int selectTotal(long begTime, long endTime);
@Query(nativeQuery = true,value = "select * from public_article where relate_id in ?1")
List<PublicArticle> selectByRelateIds(List<Integer> relateIds);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicLabel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/25.
*/
public interface IPublicLabelRepository extends JpaRepository<PublicLabel, Integer> {
@Query("select p from PublicLabel p where p.id in ?1")
List<PublicLabel> selectByIds(List<Integer> ids);
}
... ...
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;
/**
* Created by shengguo.cai on 2019/3/25.
*/
public interface IPublicUserAttentionRepository 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.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/25.
*/
public interface IPublicUserCommentRepository extends JpaRepository<PublicUserComment, Integer> {
@Query(nativeQuery = true,value = "select * from public_user_comment where relate_id in ?1 and src_channel = ?2")
List<PublicUserComment> selectByRelateIds(List<Integer> relateIds, int srcChannel);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicUserPraise;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by shengguo.cai on 2019/3/25.
*/
public interface IPublicUserPraiseRepository extends JpaRepository<PublicUserPraise, Integer> {
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2019/3/25.
*/
@Data
@Entity
@Table(name = "public_article_label")
public class PublicArticleLabel extends BaseEntity implements Serializable {
@Column(name = "label_id")
private Integer labelId;
@Column(name = "article_id")
private Integer articleId;
@Column(name = "src_channel")
private Integer srcChannel;
@Column(name = "create_time")
private Long createTime;
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2019/3/25.
*/
@Data
@Entity
@Table(name = "public_label")
public class PublicLabel implements Serializable {
@javax.persistence.Id
private Integer id;
@Column(name = "label_name")
private String labelName;
@Column(name = "group_id")
private Integer groupId;
private Integer status;
@Column(name = "is_visible")
private Integer isVisible;
@Column(name = "relate_id")
private Integer relateId;
@Column(name = "src_channel")
private Integer srcChannel;
@Column(name = "create_time")
private Long createTime;
@Column(name = "update_time")
private Long updateTime;
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblAttentionUser;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2019/3/25.
*/
@Data
@Entity
@Table(name = "public_user_attention")
public class PublicUserAttention extends BaseEntity implements Serializable {
private Integer uid;
@Column(name = "target_id")
private Integer targetId;
@Column(name = "attention_type")
private Integer attentionType;
@Column(name = "author_type")
private Integer authorType;
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 PublicUserAttention() {
}
public PublicUserAttention(TblAttentionUser tblAttentionUser) {
uid = tblAttentionUser.getUid();
targetId = tblAttentionUser.getUidAttention();
status = 1;
attentionType = 1;
createTime = tblAttentionUser.getCreateTime()==null?System.currentTimeMillis():tblAttentionUser.getCreateTime()*1000;
authorType = 1;
srcChannel = 3;
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblCommentInfo;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2019/3/25.
*/
@Data
@Entity
@Table(name = "public_user_comment")
public class PublicUserComment extends BaseEntity implements Serializable {
private Integer uid;
@Column(name = "to_uid")
private Integer toUid;
@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;
@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 = "src_channel")
private Integer srcChannel;
@Column(name = "audit_status")
private Integer auditStatus;
@Column(name = "audit_account")
private String auditAccount;
@Column(name = "audit_time")
private Long auditTime;
@Column(name = "create_time")
private Long createTime;
@Column(name = "update_time")
private Long updateTime;
public PublicUserComment() {
}
public PublicUserComment(TblCommentInfo comment, Integer articleId, PublicUserComment parent) {
this.articleId = articleId;
this.uid = comment.getUid();
this.auditStatus = 2;
this.toCommentId = null == parent ? null : parent.getId();
this.srcChannel = 3;
this.createTime = comment.getCreateTime() == null ? System.currentTimeMillis():comment.getCreateTime();
this.relateId = comment.getId();
this.relateParentId = null == parent ? null : parent.getRelateId();
this.praiseNum = comment.getRealPraiseNum();
this.rootId = parent == null ? null : parent.getRootId() == null?parent.getId():parent.getRootId();//parent可能是root评论
//设置内容
JSONArray jsonArray = new JSONArray();
JSONObject textJSON = new JSONObject();
textJSON.put("type","text");
textJSON.put("content",comment.getCommentText());
jsonArray.add(textJSON);
JSONArray imgArray = JSON.parseArray(comment.getCommentImage());
if(null != imgArray){
for(int i=0;i<imgArray.size();i++){
imgArray.getJSONObject(i).put("type","image");
}
jsonArray.addAll(imgArray);
}
this.contentData = jsonArray.toJSONString();
}
}
... ...
package com.yoho.datasync.fullsync.dal.repository.pcms.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.PostsPraise;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2019/3/25.
*/
@Data
@Entity
@Table(name = "public_user_praise")
public class PublicUserPraise extends BaseEntity implements Serializable {
@Column(name = "target_id")
private Integer targetId;
private Integer uid;
@Column(name = "praise_type")
private Integer praiseType;
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 PublicUserPraise() {
}
public PublicUserPraise(PostsPraise praise, PublicArticle article) {
uid = praise.getUid();
targetId = article.getId();
createTime = praise.getCreateTime() == null ? System.currentTimeMillis() : praise.getCreateTime();
status = 1;
srcChannel=3;
}
}
... ...
... ... @@ -2,9 +2,23 @@ package com.yoho.datasync.fullsync.dal.repository.yhcms;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblAttentionUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/19.
*/
public interface ITblAttentionUserRepository extends JpaRepository<TblAttentionUser, Integer> {
@Query(nativeQuery = true,value = "select count(1) from tbl_attention_user where uid_attention in ?1 and uid is not null")
int selectTotalByUidAttentions(List<Integer> authorUids);
@Query(nativeQuery = true,value="select * from tbl_attention_user where uid_attention in ?1 and uid is not null order by id desc limit ?2,?3")
List<TblAttentionUser> selectByUidAttentions(List<Integer> authorUids, int offset, int size);
@Query(nativeQuery = true,value = "select count(1) from tbl_attention_user where create_time >= ?1 and create_time < ?2 and uid is not null")
int selectTotalByTime(int begTime, int endTime);
@Query(nativeQuery = true,value = "select * from tbl_attention_user where create_time >= ?1 and create_time < ?2 and uid is not null order by id desc limit ?3,?4")
List<TblAttentionUser> selectByTime(int begTime, int endTime, int offset, int size);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.yhcms;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblAttentionUser;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblCommentInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/19.
*/
public interface ITblCommentInfoRepository extends JpaRepository<TblCommentInfo, Integer> {
@Query(nativeQuery = true,value = "select * from tbl_comment_info where to_comment_id in ?1 and status = 1 and check_status = 2")
List<TblCommentInfo> selectChildByToCommentIds(List<Integer> parentIds);
@Query(nativeQuery = true,value = "select * from tbl_comment_info where content_id in ?1 and status = 1 and check_status = 2 and to_comment_id=0 order by id desc limit ?2,?3")
List<TblCommentInfo> selectRootByContentIds(List<Integer> relateIds, int offset, int size);
@Query(nativeQuery = true,value = "select count(1) from tbl_comment_info where content_id in ?1 and status = 1 and check_status = 2 and to_comment_id=0")
int selectTotalRootByContentIds(List<Integer> relateIds);
@Query(nativeQuery = true,value = "select count(1) from tbl_comment_info where create_time >= ?1 and create_time < ?2 and status = 1 and check_status = 2 and to_comment_id=0")
int selectTotalRootByTime(int begTime, int endTime);
@Query(nativeQuery = true,value = "select * from tbl_comment_info where create_time >= ?1 and create_time < ?2 and status = 1 and check_status = 2 and to_comment_id=0 order by id desc limit ?3,?4")
List<TblCommentInfo> selectRootByTime(int begTime, int endTime, int offset, int size);
}
... ...
... ... @@ -2,9 +2,14 @@ package com.yoho.datasync.fullsync.dal.repository.yhsocial;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.CommunityUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/20.
*/
public interface ICommunityUserRepository extends JpaRepository<CommunityUser, Integer> {
@Query(nativeQuery = true,value = "select uid from community_user where is_virtual=0 and uid in ?1")
List<Integer> selectRealUids(List<Integer> uids);
}
... ...
... ... @@ -13,7 +13,19 @@ import java.util.List;
* Created by shengguo.cai on 2019/3/19.
*/
public interface IPostsPraiseRepository extends JpaRepository<PostsPraise, Integer> {
@Query("select new com.yoho.datasync.fullsync.dal.repository.yhsocial.model.PostsPraiseTotal(count(id),postsId)" +
@Query(value = "select new com.yoho.datasync.fullsync.dal.repository.yhsocial.model.PostsPraiseTotal(count(id),postsId)" +
" from PostsPraise where postsId in :postIds group by postsId")
List<PostsPraiseTotal> selectTotal(@Param("postIds") List<Integer> postIds);
@Query(nativeQuery = true,value = "select count(1) from posts_praise where posts_id in ?1")
int selectTotalByPostIds(List<Integer> relateIds);
@Query(nativeQuery = true,value = "select * from posts_praise where posts_id in ?1 order by id desc limit ?2,?3")
List<PostsPraise> selectByPostIds(List<Integer> relateIds, int offset, int size);
@Query(nativeQuery = true,value = "select count(1) from posts_praise where create_time >= ?1 and create_time < ?2")
int selectTotalByTime(long begTime, long endTime);
@Query(nativeQuery = true,value = "select * from posts_praise where create_time >= ?1 and create_time < ?2 order by id limit ?3,?4")
List<PostsPraise> selectByTime(long begTime, long endTime, int offset, int size);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.yhsocial;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.TopicPost;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/25.
*/
public interface ITopicPostRepository extends JpaRepository<TopicPost, Integer> {
@Query("select t from TopicPost t where t.postId in ?1 ")
List<TopicPost> selectByPostIds(List<Integer> postIds);
}
... ...
package com.yoho.datasync.fullsync.dal.repository.yhsocial;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.Topic;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* Created by shengguo.cai on 2019/3/25.
*/
public interface ITopicRepository extends JpaRepository<Topic, Integer> {
@Query("select t from Topic t where t.id in ?1")
List<Topic> selectByIds(List<Integer> ids);
}
... ...
... ... @@ -23,4 +23,8 @@ public class Posts extends BaseEntity implements Serializable {
private Long createTime;
@Column(name = "update_time")
private Long updateTime;
@Column(name ="reviewe_user_name")
private String revieweUserName;
@Column(name ="reviewe_time")
private Long revieweTime;
}
... ...
... ... @@ -11,10 +11,10 @@ import java.io.Serializable;
@Data
@ToString
public class PostsPraiseTotal implements Serializable {
private Long postsId;
private Integer total;
private Integer postsId;
private Long total;
public PostsPraiseTotal(Long postsId, Integer total) {
public PostsPraiseTotal(Long total, Integer postsId) {
this.postsId = postsId;
this.total = total;
}
... ...
package com.yoho.datasync.fullsync.dal.repository.yhsocial.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2019/3/25.
*/
@Data
@Entity
@Table(name = "topic")
public class Topic extends BaseEntity implements Serializable {
@Column(name = "topic_name")
private String topicName;
@Column(name = "create_time")
private Long createTime;
private Integer hotCount;
@Column(name = "id_recommend")
private Integer idRecommend;
@Column(name = "topic_desc")
private String topicDesc;
@Column(name = "order_type")
private Integer orderType;
}
... ...
package com.yoho.datasync.fullsync.dal.repository.yhsocial.model;
import com.yoho.datasync.fullsync.dal.repository.BaseEntity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by shengguo.cai on 2019/3/25.
*/
@Data
@Entity
@Table(name = "topic_post")
public class TopicPost extends BaseEntity implements Serializable {
@Column(name = "topic_id")
private Integer topicId;
@Column(name = "post_id")
private Integer postId;
@Column(name = "forum_code")
private Integer forumCode;
@Column(name = "create_time")
private Integer createTime;
}
... ...
... ... @@ -6,10 +6,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Created by shengguo.cai on 2019/1/25.
... ... @@ -47,9 +44,12 @@ public class BeanConvertUtils {
return map;
}
try{
Map<String,Field> fieldMap = null;
for(V cell : list){
Field field = cell.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
if(null == fieldMap){
fieldMap = getAllFields(cell.getClass());
}
Field field = fieldMap.get(fieldName);
K key = (K)field.get(cell);
map.put(key,cell);
}
... ... @@ -58,6 +58,19 @@ public class BeanConvertUtils {
}
return map;
}
private static Map<String,Field> getAllFields(Class clazz){
Map<String,Field> fieldMap = new HashMap<>();
while (clazz != null) {//当父类为null的时候说明到达了最上层的父类(Object类).
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields){
field.setAccessible(true);
fieldMap.put(field.getName(),field);
}
clazz = clazz.getSuperclass(); //得到父类,然后赋给自己
}
return fieldMap;
}
public static <K,V> Map<K,List<V>> listToListMap(List<V> list, Class<K> clazz, String fieldName){
Map<K,List<V>> map = new HashMap<>();
if(CollectionUtils.isEmpty(list)){
... ...
package com.yoho.datasync.fullsync.comm;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* @author xieyong
*
*/
public final class DateUtils {
public final static String DEFAULT_FOMARTPATTER="yyyy-MM-dd HH:mm:ss";
public final static String DEFAULT_FOMART_DATE="yyyy-MM-dd";
public static String getYearMonDateString(long time)
{
SimpleDateFormat sdf=new SimpleDateFormat(DEFAULT_FOMART_DATE);
Calendar c=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.setTime(new Date(time*1000));
return sdf.format(c.getTime());
}
/**
* 将数据库中的UNIX_Time(该时间是距离1970年的秒数,在转换过程中先要换算成毫秒)转换成UTC时间
* @param time
* @return
*/
public static String getDateString(long time)
{
SimpleDateFormat sdf=new SimpleDateFormat(DEFAULT_FOMARTPATTER);
Calendar c=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.setTime(new Date (time*1000));
return sdf.format(c.getTime());
}
/**
*
* @param time 获取数据库中的UNIX_Time(该时间是距离1970年的秒数,在转换过程中先要换算成毫秒)中的月份信息
* @return
*/
public static int getDateOfMonth(long time)
{
Calendar c=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.setTime(new Date (time*1000));
return c.get(Calendar.MONTH)+1;
}
/**
* @param time 获取数据库中的UNIX_Time(该时间是距离1970年的秒数,在转换过程中先要换算成毫秒)中的天信息
* @return
*/
public static int getDateOfDay(long time)
{
Calendar c=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.setTime(new Date (time*1000));
return c.get(Calendar.DAY_OF_MONTH);
}
/**获得日期转换的秒
* @param date
* @return
*/
public static int toSecond(Date date) {
long longTime = date.getTime();
return (int)(longTime /1000);
}
/**
* 获取字符串格式的时间毫秒数
* @param time
* @return
*/
public static long getDateOfSenconds(String time)
{
long timeLong = 0L;
SimpleDateFormat sdf=new SimpleDateFormat(DEFAULT_FOMARTPATTER);
Date date;
try {
date = sdf.parse(time);
timeLong = date.getTime()/1000;
} catch (ParseException e) {
}
return timeLong;
}
/**
* 将通过getTime获得的long型的时间,转换成format类型的时间,如"2015-08-09"
*
* @param time long型的时间戳
* @param format 需要转化的类型 “yyyy-MM-dd”
* @return String "2015-08-09"
*/
public static String long2DateStr(long time, String format) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateStr = sdf.format(date);
return dateStr;
}
/**
* 将通过getTime获得的int型的时间,转换成format类型的时间,如"2015-08-09"
*
* @param time int型的时间戳---这里的int时间是秒数
* @param format 需要转化的类型 “yyyy-MM-dd”
* @return String "2015-08-09"
*/
public static String int2DateStr(long time, String format) {
Date date = new Date(time * 1000);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateStr = sdf.format(date);
return dateStr;
}
/**
* 根据Date类型的时间, 将时间转换成字符串类型的时间. format: 时间模版
*
* @param date Date 时间
* @param format 模板,例: "yyyy-MM-dd HH:mm:ss"
* @return String 字符串类型的时间
*
* @author create by dengxinfei on 2016-12-27
*/
public static String getDateFormatStr(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateStr = sdf.format(date);
return dateStr;
}
/**
* 获取当前时间的距离 1970-01-01的秒数
* @return
*/
public static int getCurrentTimeSecond(){
int second = (int)(System.currentTimeMillis() / 1000);
return second;
}
/**
* 按照格式,取得当前时间
* @param str
* @return
*/
public static String getToday(String str) {
return getDateFormatStr(new Date(), str);
}
}
... ...
package com.yoho.datasync.fullsync.controller;
import com.yoho.datasync.fullsync.dal.repository.yhcms.ITblAttentionUserRepository;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblAttentionUser;
import com.yoho.datasync.fullsync.service.YohoNowDataSynService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
... ... @@ -15,19 +12,30 @@ import javax.annotation.Resource;
*/
@RestController
public class YohoNowDataSynController {
@Resource
private ITblAttentionUserRepository tblAttentionUserRepository;
// @Resource
// private ITblAttentionUserRepository tblAttentionUserRepository;
@Resource
private YohoNowDataSynService yohoNowDataSynService;
@RequestMapping("/test")
public TblAttentionUser test(){
TblAttentionUser user = tblAttentionUserRepository.getOne(99);
TblAttentionUser t = new TblAttentionUser();
BeanUtils.copyProperties(user,t);
return t;
}
// @RequestMapping("/test")
// public TblAttentionUser test(){
// TblAttentionUser user = tblAttentionUserRepository.getOne(99);
// TblAttentionUser t = new TblAttentionUser();
// BeanUtils.copyProperties(user,t);
// return t;
// }
@RequestMapping("/synYohoNow/article")
public String synArticle(@RequestParam("begTime") long begTime,@RequestParam("endTime") long endTime){
return yohoNowDataSynService.synArticle(begTime,endTime);
}
/**
* 评论/点赞/收藏
* @param begTime
* @param endTime
* @return
*/
@RequestMapping("/synYohoNow/articleAttach")
public String synArticleAttach(@RequestParam("begTime") long begTime,@RequestParam("endTime") long endTime,@RequestParam("type") int type,@RequestParam("isFromArticle") boolean isFromArticle){
return yohoNowDataSynService.synArticleAttach(begTime,endTime,type,isFromArticle);
}
}
... ...
... ... @@ -4,26 +4,20 @@ import com.alibaba.fastjson.JSONObject;
import com.yoho.datasync.fullsync.comm.BeanConvertUtils;
import com.yoho.datasync.fullsync.comm.ImagesConstant;
import com.yoho.datasync.fullsync.comm.ImagesHelper;
import com.yoho.datasync.fullsync.dal.repository.pcms.IPublicArticleAuditRepository;
import com.yoho.datasync.fullsync.dal.repository.pcms.IPublicArticleBlockRepository;
import com.yoho.datasync.fullsync.dal.repository.pcms.IPublicArticleRepository;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicArticle;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.PublicArticleBlock;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.IPostsBlockRepository;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.IPostsPraiseRepository;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.IPostsRepository;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.Posts;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.PostsBlock;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.PostsPraiseTotal;
import com.yoho.datasync.fullsync.dal.repository.pcms.*;
import com.yoho.datasync.fullsync.dal.repository.pcms.model.*;
import com.yoho.datasync.fullsync.dal.repository.yhcms.ITblAttentionUserRepository;
import com.yoho.datasync.fullsync.dal.repository.yhcms.ITblCommentInfoRepository;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblAttentionUser;
import com.yoho.datasync.fullsync.dal.repository.yhcms.model.TblCommentInfo;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.*;
import com.yoho.datasync.fullsync.dal.repository.yhsocial.model.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
... ... @@ -41,11 +35,32 @@ public class YohoNowDataSynService {
@Resource
private IPublicArticleRepository publicArticleRepository;
@Resource
private IPublicArticleLabelRepository publicArticleLabelRepository;
@Resource
private IPostsRepository postsRepository;
@Resource
private IPostsBlockRepository postsBlockRepository;
@Resource
private IPostsPraiseRepository postsPraiseRepository;
@Resource
private ITopicPostRepository topicPostRepository;
@Resource
private ITopicRepository topicRepository;
@Resource
private IPublicLabelRepository publicLabelRepository;
@Resource
private ITblCommentInfoRepository tblCommentInfoRepository;
@Resource
private IPublicUserCommentRepository publicUserCommentRepository;
@Resource
private ICommunityUserRepository communityUserRepository;
@Resource
private IPublicUserPraiseRepository publicUserPraiseRepository;
@Resource
private IPublicUserAttentionRepository publicUserAttentionRepository;
@Resource
private ITblAttentionUserRepository tblAttentionUserRepository;
public final int size=50;
public String synArticle(long begTime, long endTime) {
... ... @@ -62,10 +77,12 @@ public class YohoNowDataSynService {
Map<Integer,PostsPraiseTotal> postsPraiseTotalMap = BeanConvertUtils.listToMap(praiseTotals,Integer.class,"postsId");
List<PublicArticle> pArticles = buildPublicArticles(postsList,postsBlockMap,postsPraiseTotalMap);
publicArticleRepository.saveAll(pArticles);
//TODO 文章审核表不同步
//文章审核表同步
synArticleAudit(pArticles,postsList);
//文章内容block同步
synArticleBlock(pArticles,postsBlockMap);
//用户文章关联标签同步
synArticleLabel(pArticles);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
... ... @@ -75,10 +92,88 @@ public class YohoNowDataSynService {
return "success";
}
private void synArticleAudit(List<PublicArticle> pArticles, List<Posts> postsList) {
List<PublicArticleAudit> auditList = new ArrayList<>();
Map<Integer,Posts> postsMap = BeanConvertUtils.listToMap(postsList,Integer.class,"id");
for(PublicArticle article : pArticles){
Posts posts = postsMap.get(article.getRelateId());
PublicArticleAudit audit = new PublicArticleAudit();
audit.setArticleId(article.getId());
audit.setAuditAccount(posts.getRevieweUserName());
audit.setAuditTime(posts.getRevieweTime());
audit.setAuditStatus(2);//审核都是通过的
auditList.add(audit);
}
if(!auditList.isEmpty()){
publicArticleAuditRepository.saveAll(auditList);
}
}
private void synArticleLabel( List<PublicArticle> pArticleList){
if(CollectionUtils.isEmpty(pArticleList)){
return;
}
try{
//这批文章的postid
List<Integer> postIds = pArticleList.stream().map(PublicArticle::getRelateId).collect(Collectors.toList());
Map<Integer,Integer> articleIdAndPostIdMap = pArticleList.stream().
collect(Collectors.toMap(PublicArticle::getRelateId, PublicArticle::getId,(k1, k2)->k1));
//获取在grass 模块已存在的uid, 并排除
List<TopicPost> topicPosts = topicPostRepository.selectByPostIds(postIds);
//同步标签
List<Integer> topicIds = topicPosts.stream().map(TopicPost::getTopicId).distinct().collect(Collectors.toList());
List<PublicLabel> needSyncLabelList = new ArrayList<>();
if(!CollectionUtils.isEmpty(topicIds)){
//排除已经同步过来的标签
List<PublicLabel> existLabel = publicLabelRepository.selectByIds(topicIds);
List<Integer> existLabelIds = existLabel.stream().map(PublicLabel::getId).collect(Collectors.toList());
List<Integer> needSyncIds = topicIds.stream().filter(integer -> !existLabelIds.contains(integer)).collect(Collectors.toList());
if(!CollectionUtils.isEmpty(needSyncIds)){
List<Topic> topicList = topicRepository.selectByIds(needSyncIds);
if(!CollectionUtils.isEmpty(topicList)){
long now = System.currentTimeMillis();
topicList.forEach(x -> {
PublicLabel grassLabel = new PublicLabel();
grassLabel.setId(x.getId());
grassLabel.setLabelName(x.getTopicName().replace("#",""));
grassLabel.setStatus(1);
grassLabel.setCreateTime(now);
grassLabel.setIsVisible(2); //用户不可见
needSyncLabelList.add(grassLabel);
});
}
}
}
if(!CollectionUtils.isEmpty(needSyncLabelList)){
publicLabelRepository.saveAll(needSyncLabelList);//TODO id是否也保存了
}
//需要同步过来的文章和话题关联关系
List<PublicArticleLabel> needSyncList = new ArrayList<>();
if(!CollectionUtils.isEmpty(topicPosts)){
long now = System.currentTimeMillis();
topicPosts.forEach(x -> {
PublicArticleLabel articleLabel = new PublicArticleLabel();
articleLabel.setLabelId(x.getTopicId());
articleLabel.setArticleId(articleIdAndPostIdMap.get(x.getPostId()));
articleLabel.setCreateTime(now);
articleLabel.setSrcChannel(3); // now社区
needSyncList.add(articleLabel);
});
}
if(!CollectionUtils.isEmpty(needSyncList)){
publicArticleLabelRepository.saveAll(needSyncList);
}
}catch (Exception e){
e.printStackTrace();
}
}
private void synArticleBlock(List<PublicArticle> pArticles, Map<Integer, List<PostsBlock>> postsBlockMap) {
List<PublicArticleBlock> pBlocks = new ArrayList<>();
for(PublicArticle article : pArticles){
List<PostsBlock> blocks = postsBlockMap.get(article.getRelateId());
Map<Integer,Boolean> coverImgBlockMap = new HashMap<>();
for(PostsBlock block : blocks){
PublicArticleBlock pBlock = new PublicArticleBlock();
pBlock.setArticleId(article.getId());
... ... @@ -86,7 +181,12 @@ public class YohoNowDataSynService {
pBlock.setOrderBy(block.getOrderBy());
pBlock.setContentType(block.getTemplateKey());
if(block.getTemplateKey().equals(PostsBlock.TEMPLATEKEY_IMAGE)){
pBlock.setContentData(getImageBlock(block.getContentData(), block.getSize()));
String isCoverImg="N";
if(null == coverImgBlockMap.get(block.getPostsId())){
isCoverImg="Y";
coverImgBlockMap.put(block.getPostsId(),true);
}
pBlock.setContentData(getImageBlock(block.getContentData(), block.getSize(),isCoverImg));
pBlocks.add(pBlock);
}
if(block.getTemplateKey().equals(PostsBlock.TEMPLATEKEY_TEXT)){
... ... @@ -102,39 +202,27 @@ public class YohoNowDataSynService {
sb.forEach(s -> {
result.set(result.get().replace(s, ""));
});
pBlock.setContentData(getBlock(PostsBlock.TEMPLATEKEY_TEXT, result.get()));
pBlock.setContentData(getTextBlock(result.get()));
pBlocks.add(pBlock);
}
}
}
if(!pBlocks.isEmpty()){
publicArticleBlockRepository.saveAll(pBlocks);
}
}
private String getBlock(String templatName, String data){
JSONObject o = new JSONObject();
private String getTextBlock(String data){
JSONObject dataObject = new JSONObject();
switch (templatName){
case PostsBlock.TEMPLATEKEY_IMAGE:
String urlWithHttp = ImagesHelper.getImageAbsoluteUrl(data, ImagesConstant.BUCKET_GRASS);
String finalUrl = ImagesHelper.convertImageUrlWithDefault(urlWithHttp);
dataObject.put("src",finalUrl);
dataObject.put("width",1);
dataObject.put("height",1);
break;
case PostsBlock.TEMPLATEKEY_TEXT:
dataObject.put("text",data);
break;
default:break;
}
o.put("template_name", templatName);
o.put("data", dataObject.toJSONString());
return o.toJSONString();
dataObject.put("content",data);
dataObject.put("type","text");
return dataObject.toJSONString();
}
private String getImageBlock(String data, String size) {
private String getImageBlock(String data, String size,String isCoverImg) {
if(StringUtils.isEmpty(data) || (!data.startsWith("http://") && !data.startsWith("/"))){
return "";
}
JSONObject o = new JSONObject();
JSONObject dataObject = new JSONObject();
String width = "";
String height = "";
... ... @@ -147,13 +235,12 @@ public class YohoNowDataSynService {
}
String url = ImagesHelper.getImageAbsoluteUrl(data, ImagesConstant.BUCKET_SOCIAL_PIC);
String finalUrl = ImagesHelper.convertImageUrlWithDefault(url);
dataObject.put("src",finalUrl);
dataObject.put("url",finalUrl);
dataObject.put("width",StringUtils.isEmpty(width) ? 1 : width);
dataObject.put("height",StringUtils.isEmpty(height) ? 1 : height);
o.put("template_name", "image");
o.put("data", dataObject.toJSONString());
return o.toJSONString();
dataObject.put("type", "image");
dataObject.put("isCoverImg",isCoverImg);
return dataObject.toJSONString();
}
private List<PublicArticle> buildPublicArticles(List<Posts> postsList, Map<Integer, List<PostsBlock>> postsBlockMap, Map<Integer, PostsPraiseTotal> postsPraiseTotalMap) {
... ... @@ -161,28 +248,359 @@ public class YohoNowDataSynService {
for(Posts posts : postsList){
List<PostsBlock> postsBlocks = postsBlockMap.get(posts.getId());
PostsPraiseTotal postsPraiseTotal = postsPraiseTotalMap.get(posts.getId());
if(!CollectionUtils.isEmpty(postsBlocks)){
postsBlocks = postsBlocks.stream().filter(s -> s.getTemplateKey().equals("image")).collect(Collectors.toList());
if(CollectionUtils.isEmpty(postsBlocks)){
continue;
}
if(CollectionUtils.isEmpty(postsBlocks)){
continue;
}
List<PostsBlock> imgBlockList = postsBlocks.stream().filter(s -> s.getTemplateKey().equals("image")).collect(Collectors.toList());
if(CollectionUtils.isEmpty(imgBlockList)){
continue;
}
PublicArticle article = new PublicArticle();
article.setArticleType(5); //now社区文章同步类型
article.setRelateId(posts.getId());
//article.setMinRelateId();//TODO 二次同步什么鬼?
article.setAuthorUid(posts.getAuthorId());//authorUid
// article.setAuthorType();//authorType TODO 是啥
article.setPraiseNum(postsPraiseTotal == null ? 0 : postsPraiseTotal.getTotal());//praiseCount
article.setPraiseNum(postsPraiseTotal == null ? 0 : postsPraiseTotal.getTotal().intValue());//praiseCount
// article.setFavoriteNum();//favoriteCount TODO
article.setStatus(1);//auth_status
article.setAuditStatus(2);//auth_status 与审核相关
article.setPublishTime(posts.getCreateTime());//create_time
article.setCreateTime(posts.getCreateTime());//create_time
article.setUpdateTime(posts.getUpdateTime());//update_time
article.setCoverImg(postsBlocks.get(0).getContentData());
//设置封面
PostsBlock coverBlock = imgBlockList.get(0);
String coverImgData = getImageBlock(coverBlock.getContentData(), coverBlock.getSize(),"Y");
article.setCoverImg(coverImgData);
articleList.add(article);
}
return articleList;
}
public String synArticleAttach(long begTime, long endTime, int type, boolean isFromArticle) {
switch (type){
case 0:
if(isFromArticle){
synCommentDataByArticle(begTime,endTime);
}else{
synCommentDataByTime((int)(begTime/1000),(int)(endTime/1000));
}
break;
case 1:
if(isFromArticle){
synPraiseDataByArticle(begTime,endTime);
}else{
synPraiseDataByTime(begTime,endTime);
}
break;
case 2:
if(isFromArticle){
synAttentionAuthorByArticle(begTime,endTime);
}else {
synAttentionAuthorByTime((int)(begTime/1000),(int)(endTime/1000));
}
break;
default:
return "false";
}
return "success";
}
private void synAttentionAuthorByArticle(long begTime, long endTime) {
int total = publicArticleRepository.selectTotal(begTime,endTime);
for(int offset=0;offset<total;offset+=size) {
List<PublicArticle> articleList = publicArticleRepository.selectByTime(begTime, endTime, offset, size);
List<Integer> authorUids = articleList.stream().map(PublicArticle::getAuthorUid).collect(Collectors.toList());
if(CollectionUtils.isEmpty(authorUids)){
return;
}
//然后根据uid_attention查找粉丝
int fanUserTotal = tblAttentionUserRepository.selectTotalByUidAttentions(authorUids);
for(int i=0;i<fanUserTotal;i+=size){
List<TblAttentionUser> fanUsersList = tblAttentionUserRepository.selectByUidAttentions(authorUids,i,size);
Map<Integer,List<TblAttentionUser>> fanUsersMap = buildFanUserMap(fanUsersList);
if(null != fanUsersMap && fanUsersMap.size() != 0){
//过滤掉马甲用户
List<Integer> fanUids = fanUsersMap.keySet().stream().collect(Collectors.toList());
List<Integer> filtedFanUids = communityUserRepository.selectRealUids(fanUids);
List<PublicUserAttention> userAttentions = buildSynAttentionAuthor(fanUsersMap,filtedFanUids);
if(!CollectionUtils.isEmpty(userAttentions)){
publicUserAttentionRepository.saveAll(userAttentions);
try{
Thread.sleep(200);
}catch (Exception exp){
exp.printStackTrace();
}
}
}
}
}
}
private void synAttentionAuthorByTime(int begTime, int endTime) {
int total = tblAttentionUserRepository.selectTotalByTime(begTime,endTime);
for(int offset=0;offset<total;offset+=size) {
List<TblAttentionUser> fanUsersList = tblAttentionUserRepository.selectByTime(begTime,endTime,offset,size);
Map<Integer,List<TblAttentionUser>> fanUsersMap = buildFanUserMap(fanUsersList);
if(null != fanUsersMap && fanUsersMap.size() != 0){
//过滤掉马甲用户
List<Integer> fanUids = fanUsersMap.keySet().stream().collect(Collectors.toList());
List<Integer> filtedFanUids = communityUserRepository.selectRealUids(fanUids);
List<PublicUserAttention> userAttentions = buildSynAttentionAuthor(fanUsersMap,filtedFanUids);
if(!CollectionUtils.isEmpty(userAttentions)){
publicUserAttentionRepository.saveAll(userAttentions);
try{
Thread.sleep(200);
}catch (Exception exp){
exp.printStackTrace();
}
}
}
}
}
private List<PublicUserAttention> buildSynAttentionAuthor(Map<Integer, List<TblAttentionUser>> fanUsersMap, List<Integer> filtedFanUids) {
List<PublicUserAttention> userAttentions = new ArrayList<>();
for(Integer fanUid : filtedFanUids){
List<TblAttentionUser> tblAttentionUsers = fanUsersMap.get(fanUid);
for(TblAttentionUser tblAttentionUser : tblAttentionUsers){
PublicUserAttention userAttention = new PublicUserAttention(tblAttentionUser);
userAttentions.add(userAttention);
}
}
return userAttentions;
}
private Map<Integer,List<TblAttentionUser>> buildFanUserMap(List<TblAttentionUser> fanUsersList){
if(CollectionUtils.isEmpty(fanUsersList)){
return null;
}
Map<Integer,List<TblAttentionUser>> fanUsersMap = new HashMap<>();
for(TblAttentionUser attentionUser : fanUsersList){
if(attentionUser.getUid() == null){
continue;
}
List<TblAttentionUser> attentionUsers = fanUsersMap.get(attentionUser.getUid());
if(attentionUsers == null){
attentionUsers = new ArrayList<>();
fanUsersMap.put(attentionUser.getUid(),attentionUsers);
}
attentionUsers.add(attentionUser);
}
return fanUsersMap;
}
private void synPraiseDataByArticle(long begTime, long endTime) {
int total = publicArticleRepository.selectTotal(begTime,endTime);
for(int offset=0;offset<total;offset+=size){
List<PublicArticle> articleList = publicArticleRepository.selectByTime(begTime,endTime,offset,size);
Map<Integer,PublicArticle> articleMap = BeanConvertUtils.listToMap(articleList,Integer.class,"relateId");
List<Integer> relateIds = articleMap.keySet().stream().collect(Collectors.toList());
if(CollectionUtils.isEmpty(relateIds)){
return;
}
//根据文章relate_id去posts_praise.posts_id查找点赞数据,无需过滤马甲
int praiseTotal = postsPraiseRepository.selectTotalByPostIds(relateIds);
for(int i=0;i<praiseTotal;i+=size){
List<PostsPraise> praiseList = postsPraiseRepository.selectByPostIds(relateIds,i,size);
//将点赞数据同步到表grass_article_praise
List<PublicUserPraise> articlePraises = buildSynArticlePraises(articleMap,praiseList);
if(!articlePraises.isEmpty()){
publicUserPraiseRepository.saveAll(articlePraises);
try{
Thread.sleep(500);
}catch (Exception exp){
exp.printStackTrace();
}
}
}
}
}
private void synPraiseDataByTime(long begTime, long endTime) {
int total = postsPraiseRepository.selectTotalByTime(begTime,endTime);
for(int offset=0;offset<total;offset+=size){
List<PostsPraise> praiseList = postsPraiseRepository.selectByTime(begTime,endTime,offset,size);
if(CollectionUtils.isEmpty(praiseList)){
return;
}
List<Integer> postIds = praiseList.stream().map(PostsPraise::getPostsId).collect(Collectors.toList());
List<PublicArticle> articleList = publicArticleRepository.selectByRelateIds(postIds);
Map<Integer,PublicArticle> articleMap = BeanConvertUtils.listToMap(articleList,Integer.class,"relateId");
//将点赞数据同步到表grass_article_praise
List<PublicUserPraise> articlePraises = buildSynArticlePraises(articleMap,praiseList);
if(!articlePraises.isEmpty()){
publicUserPraiseRepository.saveAll(articlePraises);
try{
Thread.sleep(500);
}catch (Exception exp){
exp.printStackTrace();
}
}
}
}
private List<PublicUserPraise> buildSynArticlePraises(Map<Integer, PublicArticle> articleMap, List<PostsPraise> praiseList) {
List<PublicUserPraise> articlePraises = new ArrayList<>();
for(PostsPraise praise : praiseList){
PublicArticle article = articleMap.get(praise.getPostsId());
if(null == article){
continue;
}
PublicUserPraise articlePraise = new PublicUserPraise(praise,article);
articlePraises.add(articlePraise);
}
return articlePraises;
}
private void synCommentDataByArticle(long begTime, long endTime) {
int total = publicArticleRepository.selectTotal(begTime,endTime);
for(int offset=0;offset<total;offset+=size){
List<PublicArticle> articleList = publicArticleRepository.selectByTime(begTime,endTime,offset,size);
Map<Integer,PublicArticle> articleMap = BeanConvertUtils.listToMap(articleList,Integer.class,"relateId");
List<Integer> relateIds = articleMap.keySet().stream().collect(Collectors.toList());
if(CollectionUtils.isEmpty(relateIds)){
return;
}
//先同步根评论
int rootTotal = tblCommentInfoRepository.selectTotalRootByContentIds(relateIds);
for(int i=0;i<rootTotal;i+=size){
List<TblCommentInfo> rootComments = tblCommentInfoRepository.selectRootByContentIds(relateIds,i,size);
if(CollectionUtils.isEmpty(rootComments)){
break;
}
List<PublicUserComment> parentArticleComments = batchSaveNewComment(articleMap,rootComments,null);
if(!CollectionUtils.isEmpty(parentArticleComments)){
List<Integer> parentIds = parentArticleComments.stream().map(comm->comm.getRelateId()).collect(Collectors.toList());
List<TblCommentInfo> childComments = tblCommentInfoRepository.selectChildByToCommentIds(parentIds);
while(!CollectionUtils.isEmpty(childComments)){
parentArticleComments = batchSaveNewComment(articleMap,childComments,parentArticleComments);
if(CollectionUtils.isEmpty(parentArticleComments)){
break;
}
parentIds = parentArticleComments.stream().map(comm->comm.getRelateId()).collect(Collectors.toList());
childComments = tblCommentInfoRepository.selectChildByToCommentIds(parentIds);
}
}
}
}
}
private void synCommentDataByTime(int begTime, int endTime){
int total = tblCommentInfoRepository.selectTotalRootByTime(begTime,endTime);
for(int offset=0;offset<total;offset+=size){
List<TblCommentInfo> rootComments = tblCommentInfoRepository.selectRootByTime(begTime,endTime,offset,size);
if(CollectionUtils.isEmpty(rootComments)){
break;
}
List<Integer> articleRelateIds = rootComments.stream().map(TblCommentInfo::getContentId).collect(Collectors.toList());
List<PublicArticle> articleList = publicArticleRepository.selectByRelateIds(articleRelateIds);
Map<Integer,PublicArticle> articleMap = BeanConvertUtils.listToMap(articleList,Integer.class,"relateId");
List<PublicUserComment> parentArticleComments = batchSaveNewComment(articleMap,rootComments,null);
if(!CollectionUtils.isEmpty(parentArticleComments)){
List<Integer> parentIds = parentArticleComments.stream().map(comm->comm.getRelateId()).collect(Collectors.toList());
List<TblCommentInfo> childComments = tblCommentInfoRepository.selectChildByToCommentIds(parentIds);
while(!CollectionUtils.isEmpty(childComments)){
parentArticleComments = batchSaveNewComment(articleMap,childComments,parentArticleComments);
if(CollectionUtils.isEmpty(parentArticleComments)){
break;
}
parentIds = parentArticleComments.stream().map(comm->comm.getRelateId()).collect(Collectors.toList());
childComments = tblCommentInfoRepository.selectChildByToCommentIds(parentIds);
}
}
}
}
private List<PublicUserComment> batchSaveNewComment(Map<Integer, PublicArticle> articleMap, List<TblCommentInfo> comments,
List<PublicUserComment> parentArticleComments) {
//过滤掉马甲用户
filterTblCommentInfos(comments);
if(CollectionUtils.isEmpty(comments)){
return null;
}
List<PublicUserComment> articleComments = buildSynNewComments(articleMap,comments,parentArticleComments);
if(!CollectionUtils.isEmpty(articleComments)){
publicUserCommentRepository.saveAll(articleComments);
// List<Integer> relateIds = articleComments.stream().map(comm->comm.getRelateId())
// .collect(Collectors.toList());
// articleComments = publicUserCommentRepository.selectByRelateIds(relateIds,3);//src_channel 已经把id带出来了
try{
Thread.sleep(200);
}catch (Exception exp){
exp.printStackTrace();
}
}
return articleComments;
}
private List<PublicUserComment> buildSynNewComments(Map<Integer, PublicArticle> articleMap, List<TblCommentInfo> comments,
List<PublicUserComment> parentArticleComments) {
List<PublicUserComment> articleComments = new ArrayList<>();
for(TblCommentInfo comment : comments){
PublicArticle article = null == comment.getContentId() ? null : articleMap.get(comment.getContentId());
if(article == null){
continue;
}
PublicUserComment parent = findParentGrassComment(comment.getToCommentId(),parentArticleComments);
PublicUserComment articleComment = new PublicUserComment(comment,article.getId(),parent);
articleComments.add(articleComment);
}
return articleComments;
}
private PublicUserComment findParentGrassComment(Integer relateId, List<PublicUserComment> parentArticleComments) {
if(CollectionUtils.isEmpty(parentArticleComments) || relateId == null){
return null;
}
for(PublicUserComment comment : parentArticleComments){
if(relateId.equals(comment.getRelateId())){
return comment;
}
}
return null;
}
private void filterTblCommentInfos(List<TblCommentInfo> comments) {
if(CollectionUtils.isEmpty(comments)){
return;
}
Set<Integer> commentUidSet = new HashSet<>();
comments.stream().forEach(
comm->{
if(comm.getUid()!=null && comm.getUid()!=0){
commentUidSet.add(comm.getUid());
}
if(comm.getToUid()!=null && comm.getToUid()!=0){
commentUidSet.add(comm.getToUid());
}
}
);
List<Integer> commentUids = commentUidSet.stream().collect(Collectors.toList());
if(CollectionUtils.isEmpty(commentUids)){
return;
}
List<Integer> filterUids = communityUserRepository.selectRealUids(commentUids);
for(int i=0;i<comments.size();){
TblCommentInfo socialComments = comments.get(i);
boolean isDelFromid = true;
boolean isDelToid = true;
for(Integer uid : filterUids){
if(socialComments.getUid() == null || socialComments.getUid() == 0 || uid.equals(socialComments.getUid())){
isDelFromid = false;
}
if(socialComments.getToUid() == null || socialComments.getToUid() == 0 || uid.equals(socialComments.getToUid())){
isDelToid = false;
}
}
if(isDelFromid || isDelToid){
comments.remove(i);
}else{
i++;
}
}
}
}
... ...
package com.yoho.datasync.fullsync.starter.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
... ... @@ -26,8 +27,8 @@ public class YhSocialDataSourceConfig {
private Properties jpaProperties;
@Bean(name = "entityManagerYhSocial")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) throws Exception {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
public EntityManager entityManager(@Qualifier("entityManagerFactoryYhSocial") LocalContainerEntityManagerFactoryBean factoryBean) throws Exception {
return factoryBean.getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryYhSocial")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) throws Exception {
... ...