Authored by peuei

fix fix fix

... ... @@ -2,7 +2,8 @@ package com.yoho.datasync.consumer.common;
public class ServiceConstant {
public static int[] articleTypes = new int[]{1, 2, 4, 5};//限定文章类型 1-种草用户发布 2-逛资讯 3-mars文章 4-晒单文章 5-now社区;',
public static int[] articleTypes = new int[]{1, 2, 4};//限定文章类型 1-种草用户发布 2-逛资讯 3-mars文章 4-晒单文章 5-now社区;',
public static final Integer ARTICLE_TYPE_NOW = 5;
public interface SRCCHANNEL_TYPE {
/**
... ...
... ... @@ -19,10 +19,10 @@ public interface PublicArticleRepository extends JpaRepository<PublicArticle, In
@Query("select id from PublicArticle where relateId = ?1 and articleType in (?2)")
Integer findByArticleId(Integer articleId, int[] types);
@Transactional // required
@Modifying
@Query("update PublicArticle set coverImg = ?1 where id = ?2 and articleType in (?3)")
void updateCoverImgByArticleIdAndArticleTypes(String coverImg, Integer articleId, int[] articleTypes);
PublicArticle findByRelateIdAndArticleType(Integer relateId, Integer articleType);
}
... ...
... ... @@ -40,14 +40,15 @@ public class GrassArticleBlockListener extends AbstractMqListener<GrassArticleBl
@Resource
private PublicArticleBlockService publicArticleBlockService;
@Resource
/**
* @param sourceObject
* @param checkResult public_article_block表的article_id字段
*/
@Override
protected void deleteData(GrassArticleBlock sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticleBlock targetObject = doBeforeHandler(sourceObject, checkResult);
if (targetObject == null) {
log.error("convert object to PublicArticleBlock goes error ...");
... ... @@ -62,8 +63,7 @@ public class GrassArticleBlockListener extends AbstractMqListener<GrassArticleBl
*/
@Override
protected void updateData(GrassArticleBlock sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticleBlock targetObject = doBeforeHandler(sourceObject, checkResult);
if (targetObject == null) {
log.error("convert object to PublicArticleBlock goes error ...");
... ... @@ -88,9 +88,8 @@ public class GrassArticleBlockListener extends AbstractMqListener<GrassArticleBl
protected Object checkData(GrassArticleBlock sourceObject, String action) {
/**
* public_article_block表的article_id字段为grass_article表中对应的数据同步到public_article表之后的自增主键id
* select id from public_article where relate_id=#{grass_article_block.article_id} and article_type in (1,2,4)
*/
final Integer relatedId = publicArticleService.getByArticleIdAndType(sourceObject.getArticleId());
final Integer relatedId = publicArticleService.getPublicArticleIdStage1(sourceObject.getArticleId());
if (relatedId == null) {
log.info("can not get public_article primary key with original articleId {}", sourceObject.getArticleId());
return null;
... ...
... ... @@ -50,8 +50,7 @@ public class GrassArticleLabelListener extends AbstractMqListener<GrassArticleLa
*/
@Override
protected void deleteData(GrassArticleLabel sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticleLabel targetObject = doBeforeHandler(sourceObject, checkResult);
if (targetObject == null) {
log.error("convert object to PublicArticleLabel goes error ...");
... ... @@ -69,8 +68,7 @@ public class GrassArticleLabelListener extends AbstractMqListener<GrassArticleLa
*/
@Override
protected void updateData(GrassArticleLabel sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticleLabel targetObject = doBeforeHandler(sourceObject, checkResult);
if (targetObject == null) {
log.error("convert object to PublicArticleLabel goes error ...");
... ... @@ -95,9 +93,8 @@ public class GrassArticleLabelListener extends AbstractMqListener<GrassArticleLa
protected Object checkData(GrassArticleLabel sourceObject, String action) {
/**
* public_article_label 表的 article_id 字段为 grass_article_label.article_id 在 grass_article 表中对应的数据同步到 public_article 表之后的自增主键id
* select id from public_article where relate_id=#{grass_article_label.article_id} and article_type in (1,2,4)
*/
final Integer trueId = publicArticleService.getByArticleIdAndType(sourceObject.getArticleId());
final Integer trueId = publicArticleService.getPublicArticleIdStage1(sourceObject.getArticleId());
if (trueId == null) {
log.info("can not get public_article primary key with articleId {}", sourceObject.getArticleId());
return null;
... ...
... ... @@ -10,8 +10,6 @@ import com.yoho.datasync.core.base.constant.DatasyncConstant;
import com.yoho.datasync.core.base.model.yh_grass.GrassArticle;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleAudit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
... ... @@ -34,8 +32,6 @@ public class GrassArticleListener extends AbstractMqListener<GrassArticle> {
@Resource
private PublicArticleBlockService publicArticleBlockService;
private static final Logger log = LoggerFactory.getLogger(GrassArticleListener.class);
private static final Integer UNCHECKED = 1;
private static final Integer CHECK_SUCCESS = 2;
private static final Integer CHECK_FAILED = 3;
... ... @@ -45,8 +41,7 @@ public class GrassArticleListener extends AbstractMqListener<GrassArticle> {
@Override
protected void deleteData(GrassArticle sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticle targetObject = beforeHandlerPlugFirst(sourceObject);
publicArticleService.deleteData(targetObject);
}
... ... @@ -54,8 +49,7 @@ public class GrassArticleListener extends AbstractMqListener<GrassArticle> {
@Override
protected void updateData(GrassArticle sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticle publicArticle = beforeHandlerPlugFirst(sourceObject);
final PublicArticleAudit publicArticleAudit = beforeHandlerPlugSecond(sourceObject);
publicArticleService.updateData(publicArticle, publicArticleAudit, checkResult);
... ... @@ -70,34 +64,19 @@ public class GrassArticleListener extends AbstractMqListener<GrassArticle> {
* 获取 cover_img
* public_article.cover_img 字段需要取 grass_article_block 表中content_data字段中的src值进行更新
* select content_data from grass_article_block where template_key='image' order by order_by asc limit 1
* 文章id关联关系为:grass_article_block.articleId = public_article.relate_id and public_article.article_type in (1,2,4);
*
* @param sourceObject
* @return
* @return cover_img
*/
@Override
protected Object checkData(GrassArticle sourceObject, String action) {
/**
* 找不到 cover_img 就返回空字符串
*/
if (DatasyncConstant.ACTION_UPDATE.equals(action)) {
// 更新操作才去查找 cover_img
Integer trueId = publicArticleService.getByArticleIdAndType(sourceObject.getId());
if (trueId == null) {
return "";
}
Integer trueId = publicArticleService.getPublicArticleIdStage2(sourceObject);
if (trueId == null) return "";
String contentData = publicArticleBlockService.findContentData(trueId);
if (contentData == null) {
return "";
}
if (contentData == null) return "";
return contentData;
/*try {
Map map = ContentConvertUtil.jsonString2Map(contentData);
String coverImg = (String) map.get("url");
return coverImg;
} catch (Exception e) {
return "";
}*/
}
// 如果不是更新操作,直接返回
return "";
... ...
... ... @@ -45,8 +45,7 @@ public class GrassArticleProductListener extends AbstractMqListener<GrassArticle
*/
@Override
protected void deleteData(GrassArticleProduct sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticleProduct targetObject = doBeforeHandler(sourceObject, checkResult);
if (targetObject == null) {
log.error("convert object to PublicArticleProduct goes error ...");
... ... @@ -63,8 +62,7 @@ public class GrassArticleProductListener extends AbstractMqListener<GrassArticle
*/
@Override
protected void updateData(GrassArticleProduct sourceObject, Object checkResult) throws Exception {
if (sourceObject == null)
return;
if (sourceObject == null) return;
final PublicArticleProduct targetObject = doBeforeHandler(sourceObject, checkResult);
if (targetObject == null) {
log.error("convert object to PublicArticleProduct goes error ...");
... ... @@ -82,9 +80,8 @@ public class GrassArticleProductListener extends AbstractMqListener<GrassArticle
protected Object checkData(GrassArticleProduct sourceObject, String action) {
/**
* public_article_product 表的 article_id 字段为 grass_article_product.article_id 在 grass_article 表中对应的数据同步到 public_article 表之后的自增主键id
* select id from public_article where relate_id=#{grass_article_product.article_id} and article_type in (1,2,4);
*/
final Integer trueId = publicArticleService.getByArticleIdAndType(sourceObject.getArticleId());
final Integer trueId = publicArticleService.getPublicArticleIdStage1(sourceObject.getArticleId());
if (trueId == null) {
log.info("can not get public_article primary key with articleId {}", sourceObject.getArticleId());
return null;
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.dal.repository.grass.GrassArticleRepository;
import com.yoho.datasync.core.base.model.yh_grass.GrassArticle;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Optional;
@Service
public class GrassArticleService {
@Resource
private GrassArticleRepository grassArticleRepository;
public GrassArticle getGrassArticleById(Integer id) {
Optional<GrassArticle> result = grassArticleRepository.findById(id);
return result == null ? null : result.get();
}
}
... ...
... ... @@ -80,14 +80,6 @@ public class PublicArticleBlockService {
if (targetObject.getContentData() != null) {
publicArticleRepository.updateCoverImgByArticleIdAndArticleTypes(targetObject.getContentData(), targetObject.getArticleId(), ServiceConstant.articleTypes);
}
/*try {
Map map = ContentConvertUtil.jsonString2Map(targetObject.getContentData());
String coverImg = (String) map.get("url");
if (coverImg != null)
publicArticleRepository.updateCoverImgByArticleIdAndArticleTypes(coverImg, targetObject.getArticleId(), ServiceConstant.articleTypes);
} catch (Exception e) {
log.error("update public_article.cover_img goes error with exception {}", e);
}*/
}
}
... ...
... ... @@ -4,6 +4,7 @@ import com.yoho.datasync.consumer.common.BeanCopyUtils;
import com.yoho.datasync.consumer.common.ServiceConstant;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleAuditRepository;
import com.yoho.datasync.consumer.dal.repository.pcms.PublicArticleRepository;
import com.yoho.datasync.core.base.model.yh_grass.GrassArticle;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticleAudit;
import org.slf4j.Logger;
... ... @@ -20,6 +21,9 @@ public class PublicArticleService {
private static final Logger log = LoggerFactory.getLogger(PublicArticleService.class);
@Resource
private GrassArticleService grassArticleService;
@Resource
private PublicArticleRepository publicArticleRepository;
@Resource
... ... @@ -43,7 +47,6 @@ public class PublicArticleService {
* 2019.4.4 将文章表改为逻辑删除
*/
publicArticleRepository.save(existObject);
// publicArticleRepository.deleteById(existObject.getId());
}
... ... @@ -56,19 +59,28 @@ public class PublicArticleService {
*/
public void updateData(PublicArticle targetObject, PublicArticleAudit publicArticleAudit, Object checkResult) {
// public_article_audit表中的article_id字段为种草文章同步到公共库public_article表中的自增id字段
// select id from public_article where relate_id=#{grass_article.article_id} and article_type in (1,2,4)
PublicArticle existObject = validateInfoAndGetRecord(targetObject);
boolean mark = true;
PublicArticle saveObject;
if (existObject != null) {
// update
mark = false;
targetObject.setCoverImg((String) checkResult);
if (targetObject.getId() != null) {
log.error("something goes wrong ...");
return;
if (targetObject.getArticleType() == ServiceConstant.ARTICLE_TYPE_NOW) {
// relate_id 不能被覆盖
existObject.setAuthorUid(targetObject.getAuthorUid());
existObject.setPraiseNum(targetObject.getPraiseNum());
existObject.setFavoriteNum(targetObject.getFavoriteNum());
existObject.setStatus(targetObject.getStatus());
existObject.setAuditStatus(targetObject.getAuditStatus());
existObject.setAuthorType(targetObject.getAuthorType());
existObject.setUpdateTime(targetObject.getUpdateTime());
existObject.setCreateTime(targetObject.getCreateTime());
existObject.setPublishTime(targetObject.getPublishTime());
existObject.setCoverImg((String) checkResult);
} else {
targetObject.setCoverImg((String) checkResult);
existObject = BeanCopyUtils.copyNonNullProperties(targetObject, existObject, PublicArticle.class);
}
existObject = BeanCopyUtils.copyNonNullProperties(targetObject, existObject, PublicArticle.class);
/**
* public_article_audit 表的操作
... ... @@ -119,25 +131,42 @@ public class PublicArticleService {
* @throws ValidationException
*/
private PublicArticle validateInfoAndGetRecord(PublicArticle targetObject) {
// 根据 relateId[原id] 和 articleType 可唯一确定一条记录
final Integer relateId = targetObject.getRelateId();
if (relateId == null) {
log.info("targetObject {} variable relateId is null", targetObject.toString());
return null;
if (targetObject.getArticleType() == ServiceConstant.ARTICLE_TYPE_NOW) {
// now 来源的数据需要特别处理 min_relate_id 即时原grass表的 relate_id
final Integer minRelateId = targetObject.getMinRelateId();
if (minRelateId == null) {
log.warn("targetObject {} variable minRelateId is null", targetObject.toString());
return null;
}
return publicArticleRepository.findByRelateIdAndArticleType(minRelateId, ServiceConstant.ARTICLE_TYPE_NOW);
} else {
final Integer relateId = targetObject.getRelateId();
if (relateId == null) {
log.info("targetObject {} variable relateId is null", targetObject.toString());
return null;
}
return publicArticleRepository.findByRelateIdInArticleTypes(relateId, ServiceConstant.articleTypes);
}
return publicArticleRepository.findByRelateIdInArticleTypes(relateId, ServiceConstant.articleTypes);
}
/**
* 根据articleId获得记录
* 根据原 grass_article_id 获取 public_article_id
*
* @param articleId
* @param grassArticleId
* @return
*/
public Integer getByArticleIdAndType(Integer articleId) {
if (articleId == null)
return null;
return publicArticleRepository.findByArticleId(articleId, ServiceConstant.articleTypes);
public Integer getPublicArticleIdStage1(final Integer grassArticleId) {
final GrassArticle grassArticle = grassArticleService.getGrassArticleById(grassArticleId);
return getPublicArticleIdStage2(grassArticle);
}
public Integer getPublicArticleIdStage2(final GrassArticle grassArticle) {
if (grassArticle != null && grassArticle.getArticleType() == ServiceConstant.ARTICLE_TYPE_NOW) {
final PublicArticle result = publicArticleRepository.findByRelateIdAndArticleType(grassArticle.getRelateId(), ServiceConstant.ARTICLE_TYPE_NOW);
return result == null ? null : result.getId();
} else {
return publicArticleRepository.findByArticleId(grassArticle.getId(), ServiceConstant.articleTypes);
}
}
public Integer findArticleIdByRelateIdInArticleTypes(Integer relateId, int[] articleTypes) {
... ...