Authored by peuei

种草社区表

  1 +package com.yoho.datasync.consumer.dal.repository;
  2 +
  3 +import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
  4 +import org.springframework.data.jpa.repository.JpaRepository;
  5 +
  6 +public interface PublicArticleRepository extends JpaRepository<PublicArticle, Integer> {
  7 +
  8 + PublicArticle findByRelateIdAndArticleType(Integer relateId, Integer articleType);
  9 +}
  1 +package com.yoho.datasync.consumer.handler.listener;
  2 +
  3 +import com.yoho.datasync.consumer.common.EventEnum;
  4 +import com.yoho.datasync.consumer.handler.helper.AnnotationClassFactory;
  5 +import com.yoho.datasync.consumer.handler.mqcomponent.AbstractMqListener;
  6 +import com.yoho.datasync.consumer.service.PublicArticleService;
  7 +import com.yoho.datasync.core.base.annotation.MqConsumerListerner;
  8 +import com.yoho.datasync.core.base.model.yh_grass.GrassArticle;
  9 +import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.stereotype.Component;
  13 +
  14 +import javax.annotation.Resource;
  15 +
  16 +/***
  17 + * @author peuei
  18 + * @date 2019/3/19 10:26
  19 + * @description 对应表 yh_grass.grass_article
  20 + */
  21 +@Component
  22 +@MqConsumerListerner(dbName = "yh_grass", tableName = "grass_article")
  23 +public class GrassArticleListener extends AbstractMqListener<GrassArticle> {
  24 +
  25 + @Resource
  26 + private AnnotationClassFactory annotationClassFactory;
  27 +
  28 + @Resource
  29 + private PublicArticleService publicArticleService;
  30 +
  31 + private static final Logger log = LoggerFactory.getLogger(GrassArticleListener.class);
  32 +
  33 + private static final Integer UNCHECKED = 1;
  34 + private static final Integer CHECK_SUCCESS = 2;
  35 + private static final Integer CHECK_FAILED = 3;
  36 + private static final Integer DELETE = 2;
  37 + private static final Integer DRAFT = 0;
  38 +
  39 + @Override
  40 + protected void deleteData(GrassArticle sourceObject) {
  41 + if (sourceObject == null)
  42 + return;
  43 + try {
  44 + PublicArticle targetObject = beforeHandler(sourceObject);
  45 + publicArticleService.deleteData(targetObject);
  46 + } catch (Exception e) {
  47 + log.error("deleteData goes error, with sourceObject {} and exception {}", sourceObject.toString(), e);
  48 + }
  49 + }
  50 +
  51 +
  52 + @Override
  53 + protected void updateData(GrassArticle sourceObject) {
  54 + if (sourceObject == null)
  55 + return;
  56 + try {
  57 + PublicArticle targetObject = beforeHandler(sourceObject);
  58 + publicArticleService.updateData(targetObject);
  59 + } catch (Exception e) {
  60 + log.error("deleteData goes error, with sourceObject {} and exception {}", sourceObject.toString(), e);
  61 + }
  62 +
  63 + }
  64 +
  65 + @Override
  66 + protected EventEnum getEventReportEnum() {
  67 + return null;
  68 + }
  69 +
  70 + /**
  71 + * 对象转换统一处理
  72 + *
  73 + * @param sourceObject
  74 + * @return
  75 + * @throws Exception
  76 + */
  77 + private PublicArticle beforeHandler(GrassArticle sourceObject) throws Exception {
  78 + /**
  79 + * 将源对象转换成目标对象
  80 + */
  81 + PublicArticle targetObject = annotationClassFactory.convertTargetObjectFromSource(sourceObject, PublicArticle.class);
  82 + /**
  83 + * 个别字段特殊处理
  84 + */
  85 + objectConvertSpecialHandler(targetObject, sourceObject);
  86 + return targetObject;
  87 + }
  88 +
  89 + /**
  90 + * 特殊字段处理
  91 + *
  92 + * @param targetObject
  93 + * @param sourceObject
  94 + */
  95 + private void objectConvertSpecialHandler(PublicArticle targetObject, GrassArticle sourceObject) {
  96 + // TODO : public_article.cover_img字段需要取grass_article_block表中值进行更新
  97 + // grass_article.auth_status(0 未审核, 1 审核通过, 2 审核未通过)
  98 + // public_article.audit_status(1 未审核, 2 审核通过, 3 审核未通过)
  99 + // grass_article.auth_status 为3时 对应public_article.status删除状态
  100 + // grass_article.auth_status 为9时 对应public_article.status草稿状态
  101 + switch (sourceObject.getAuthStatus()) {
  102 + case 0:
  103 + targetObject.setAuditStatus(UNCHECKED);
  104 + break;
  105 + case 1:
  106 + targetObject.setAuditStatus(CHECK_SUCCESS);
  107 + break;
  108 + case 2:
  109 + targetObject.setAuditStatus(CHECK_FAILED);
  110 + break;
  111 + case 3:
  112 + // TODO : 什么时候为正常状态
  113 + targetObject.setStatus(DELETE);
  114 + break;
  115 + case 9:
  116 + targetObject.setStatus(DRAFT);
  117 + break;
  118 + }
  119 + }
  120 +}
  1 +package com.yoho.datasync.consumer.service;
  2 +
  3 +import com.yoho.datasync.consumer.dal.repository.PublicArticleRepository;
  4 +import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
  5 +import org.slf4j.Logger;
  6 +import org.slf4j.LoggerFactory;
  7 +import org.springframework.stereotype.Service;
  8 +
  9 +import javax.annotation.Resource;
  10 +
  11 +@Service
  12 +public class PublicArticleService {
  13 +
  14 + private static final Logger log = LoggerFactory.getLogger(PublicArticleService.class);
  15 +
  16 + @Resource
  17 + private PublicArticleRepository publicArticleRepository;
  18 +
  19 + /**
  20 + * 删除数据
  21 + *
  22 + * @param targetObject
  23 + */
  24 + public void deleteData(PublicArticle targetObject) {
  25 + // 根据 relateId[原id] 和 articleType 可唯一确定一条记录
  26 + Integer relateId = targetObject.getRelateId();
  27 + Integer articleType = targetObject.getArticleType();
  28 + if (relateId == null || articleType == null) {
  29 + log.info("targetObject {} variable relateId or articleType is null", targetObject.toString());
  30 + return;
  31 + }
  32 + PublicArticle existObject = publicArticleRepository.findByRelateIdAndArticleType(relateId, articleType);
  33 + if (existObject == null) {
  34 + log.info("get nothing from database by condition relateId {} and articleType {}", relateId, articleType);
  35 + return;
  36 + }
  37 + publicArticleRepository.deleteById(existObject.getId());
  38 +
  39 + }
  40 +
  41 + /**
  42 + * 跟新数据
  43 + *
  44 + * @param targetObject
  45 + */
  46 + public void updateData(PublicArticle targetObject) {
  47 +
  48 + }
  49 +}