在文章审核成功以后需要在article库中新增文章数据
ArticleFeign 保存app文章,返回app文章id
@PostMapping("/api/v1/article/save")
Long saveArticle(@RequestBody ArticleDto dto);
分布式id生成-雪花算法
在实体类ApArticle中的id上加入如下配置,指定类型为ASSIGN_ID
@TableId(value = "id",type = IdType.ASSIGN_ID)
private Long id;
第二:在application.yml文件中配置数据中心id和机器id
mybatis-plus:
#mapper-locations: classpath*:mapper/*.xml
# 设置别名包扫描路径,通过该属性可以给包中的类注册别名
#type-aliases-package: com.heima.model.article.pojos
global-config:
datacenter-id: 1 #数据中心id
workerId: 1 #机器id
@RestController
public class ArticleController {
@Autowired
private ApArticleService apArticleService;
/**
* 保存文章
*/
@PostMapping("/api/v1/article/save")
public Long saveArticle(@RequestBody ArticleDto dto){
return apArticleService.saveArticle(dto);
}
}
public interface ApArticleService extends IService<ApArticle> { *
保存文章
Long saveArticle(ArticleDto dto);
}
package com.heima.article.service.impl;
* 文章信息表,存储已发布的文章 服务实现类
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
@Autowired
private ApArticleContentService apArticleContentService;
@Autowired
private ApAuthorService apAuthorService;
/**
* 保存文章
* 保存ap_article
* 保存ap_article_content
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Long saveArticle(ArticleDto dto) {
// 根据自媒体文章id 查询文章信息
QueryWrapper<ApArticle> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(ApArticle::getWmNewsId,dto.getWmNewsId());
ApArticle apArticle =this.getOne(queryWrapper);
if(apArticle == null){
apArticle = new ApArticle();
apArticle.setWmNewsId(dto.getWmNewsId());
// 根据自媒体文章作者id查询authorid
ApAuthor apAuthor = apAuthorService.findByWmUserId(dto.getWmUserId());
apArticle.setAuthorId(apAuthor.getId());
apArticle.setAuthorName(apAuthor.getName());
apArticle.setCreatedTime(new Date());
apArticle.setIsDelete(false);
apArticle.setIsDown(false);
apArticle.setIsComment(true);
apArticle.setIsForward(true);
apArticle.setFlag(0);
}
apArticle.setPublishTime(dto.getPublishTime()==null?new Date():dto.getPublishTime());
apArticle.setLabels(dto.getLabels());
apArticle.setImages(dto.getImages());
apArticle.setChannelId(dto.getChannelId());
apArticle.setChannelName(dto.getChannelName());
apArticle.setTitle(dto.getTitle());
apArticle.setLayout(dto.getLayout());
// 保存/修改文章表
boolean b = saveOrUpdate(apArticle);
if(!b){
throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
}
Long articleId = apArticle.getId();
ApArticleContent apArticleContent = new ApArticleContent();
apArticleContent.setArticleId(articleId);
apArticleContent.setContent(dto.getContent());
// 保存/修改 文章内容表
boolean b1 = apArticleContentService.saveOrUpdate(apArticleContent);
if(!b1){
throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
}
return articleId;
}
}