Authored by peuei

种草社区表

package com.yoho.datasync.consumer.dal.repository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PublicArticleRepository extends JpaRepository<PublicArticle, Integer> {
PublicArticle findByRelateIdAndArticleType(Integer relateId, Integer articleType);
}
... ...
package com.yoho.datasync.consumer.handler.listener;
import com.yoho.datasync.consumer.common.EventEnum;
import com.yoho.datasync.consumer.handler.helper.AnnotationClassFactory;
import com.yoho.datasync.consumer.handler.mqcomponent.AbstractMqListener;
import com.yoho.datasync.consumer.service.PublicArticleService;
import com.yoho.datasync.core.base.annotation.MqConsumerListerner;
import com.yoho.datasync.core.base.model.yh_grass.GrassArticle;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/***
* @author peuei
* @date 2019/3/19 10:26
* @description 对应表 yh_grass.grass_article
*/
@Component
@MqConsumerListerner(dbName = "yh_grass", tableName = "grass_article")
public class GrassArticleListener extends AbstractMqListener<GrassArticle> {
@Resource
private AnnotationClassFactory annotationClassFactory;
@Resource
private PublicArticleService publicArticleService;
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;
private static final Integer DELETE = 2;
private static final Integer DRAFT = 0;
@Override
protected void deleteData(GrassArticle sourceObject) {
if (sourceObject == null)
return;
try {
PublicArticle targetObject = beforeHandler(sourceObject);
publicArticleService.deleteData(targetObject);
} catch (Exception e) {
log.error("deleteData goes error, with sourceObject {} and exception {}", sourceObject.toString(), e);
}
}
@Override
protected void updateData(GrassArticle sourceObject) {
if (sourceObject == null)
return;
try {
PublicArticle targetObject = beforeHandler(sourceObject);
publicArticleService.updateData(targetObject);
} catch (Exception e) {
log.error("deleteData goes error, with sourceObject {} and exception {}", sourceObject.toString(), e);
}
}
@Override
protected EventEnum getEventReportEnum() {
return null;
}
/**
* 对象转换统一处理
*
* @param sourceObject
* @return
* @throws Exception
*/
private PublicArticle beforeHandler(GrassArticle sourceObject) throws Exception {
/**
* 将源对象转换成目标对象
*/
PublicArticle targetObject = annotationClassFactory.convertTargetObjectFromSource(sourceObject, PublicArticle.class);
/**
* 个别字段特殊处理
*/
objectConvertSpecialHandler(targetObject, sourceObject);
return targetObject;
}
/**
* 特殊字段处理
*
* @param targetObject
* @param sourceObject
*/
private void objectConvertSpecialHandler(PublicArticle targetObject, GrassArticle sourceObject) {
// TODO : public_article.cover_img字段需要取grass_article_block表中值进行更新
// grass_article.auth_status(0 未审核, 1 审核通过, 2 审核未通过)
// public_article.audit_status(1 未审核, 2 审核通过, 3 审核未通过)
// grass_article.auth_status 为3时 对应public_article.status删除状态
// grass_article.auth_status 为9时 对应public_article.status草稿状态
switch (sourceObject.getAuthStatus()) {
case 0:
targetObject.setAuditStatus(UNCHECKED);
break;
case 1:
targetObject.setAuditStatus(CHECK_SUCCESS);
break;
case 2:
targetObject.setAuditStatus(CHECK_FAILED);
break;
case 3:
// TODO : 什么时候为正常状态
targetObject.setStatus(DELETE);
break;
case 9:
targetObject.setStatus(DRAFT);
break;
}
}
}
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.dal.repository.PublicArticleRepository;
import com.yoho.datasync.core.base.model.yh_pcms.PublicArticle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class PublicArticleService {
private static final Logger log = LoggerFactory.getLogger(PublicArticleService.class);
@Resource
private PublicArticleRepository publicArticleRepository;
/**
* 删除数据
*
* @param targetObject
*/
public void deleteData(PublicArticle targetObject) {
// 根据 relateId[原id] 和 articleType 可唯一确定一条记录
Integer relateId = targetObject.getRelateId();
Integer articleType = targetObject.getArticleType();
if (relateId == null || articleType == null) {
log.info("targetObject {} variable relateId or articleType is null", targetObject.toString());
return;
}
PublicArticle existObject = publicArticleRepository.findByRelateIdAndArticleType(relateId, articleType);
if (existObject == null) {
log.info("get nothing from database by condition relateId {} and articleType {}", relateId, articleType);
return;
}
publicArticleRepository.deleteById(existObject.getId());
}
/**
* 跟新数据
*
* @param targetObject
*/
public void updateData(PublicArticle targetObject) {
}
}
... ...