|
@@ -1,15 +1,23 @@
|
|
|
package com.my.bigevent.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.pagehelper.Page;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
+import com.my.bigevent.mapper.ArticleCoverImgMapper;
|
|
|
import com.my.bigevent.mapper.ArticleMapper;
|
|
|
+import com.my.bigevent.mapper.CategoryMapper;
|
|
|
import com.my.bigevent.pojo.Article;
|
|
|
+import com.my.bigevent.pojo.ArticleCoverImg;
|
|
|
import com.my.bigevent.pojo.PageBean;
|
|
|
import com.my.bigevent.service.ArticleService;
|
|
|
import com.my.bigevent.utils.ThreadLocalUtil;
|
|
|
+import org.junit.platform.commons.util.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -21,14 +29,21 @@ import java.util.Optional;
|
|
|
@Service
|
|
|
public class ArticleServiceImpl implements ArticleService
|
|
|
{
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ArticleServiceImpl.class);
|
|
|
@Autowired
|
|
|
ArticleMapper articleMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ArticleCoverImgMapper coverImgMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CategoryMapper categoryMapper;
|
|
|
+
|
|
|
private static List<String> fileTypeList = new ArrayList<>();
|
|
|
static {
|
|
|
- fileTypeList.add("png");
|
|
|
- fileTypeList.add("jpg");
|
|
|
- fileTypeList.add("jpeg");
|
|
|
+ fileTypeList.add(".png");
|
|
|
+ fileTypeList.add(".jpg");
|
|
|
+ fileTypeList.add(".jpeg");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -104,4 +119,115 @@ public class ArticleServiceImpl implements ArticleService
|
|
|
Article article = articleMapper.getArticleById(id);
|
|
|
return article;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void insertOrUpdateArticle(String title, String categoryIds,String profile, String content, String state, MultipartFile coverImage, Integer id) {
|
|
|
+ Optional.ofNullable(title).orElseThrow(() -> new RuntimeException("请传标题"));
|
|
|
+ Optional.ofNullable(content).orElseThrow(() -> new RuntimeException("请书写文章内容"));
|
|
|
+ Optional.ofNullable(state).orElseThrow(() -> new RuntimeException("请传递发布类型"));
|
|
|
+
|
|
|
+ // 新增
|
|
|
+ if (id == null) {
|
|
|
+ Article article = new Article();
|
|
|
+ article.setTitle(title);
|
|
|
+ article.setContent(content);
|
|
|
+ article.setProfile(profile);
|
|
|
+ if (StringUtils.isNotBlank(categoryIds)) {
|
|
|
+ categoryIds="["+categoryIds+"]";
|
|
|
+ article.setCategoryIds(categoryIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ article.setState(state);
|
|
|
+ article.setCreateTime(LocalDateTime.now());
|
|
|
+ article.setUpdateTime(LocalDateTime.now());
|
|
|
+ article.setCreateUser(6);
|
|
|
+
|
|
|
+ articleMapper.insert(article);
|
|
|
+ log.info("文章的id{}", article.getId());
|
|
|
+
|
|
|
+ if (coverImage != null) {
|
|
|
+ handleCoverImage(coverImage, article.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 修改
|
|
|
+ else {
|
|
|
+ Article article = new Article();
|
|
|
+ article.setTitle(title);
|
|
|
+ article.setContent(content);
|
|
|
+ if (StringUtils.isNotBlank(categoryIds)) {
|
|
|
+ categoryIds="["+categoryIds+"]";
|
|
|
+ article.setCategoryIds(categoryIds);
|
|
|
+ }
|
|
|
+ article.setProfile(profile);
|
|
|
+ article.setState(state);
|
|
|
+ article.setUpdateTime(LocalDateTime.now());
|
|
|
+ article.setId(id);
|
|
|
+ articleMapper.update(article);
|
|
|
+
|
|
|
+ if (coverImage != null) {
|
|
|
+ coverImgMapper.deleteByArticleId(id);
|
|
|
+ handleCoverImage(coverImage, article.getId());
|
|
|
+ } else {
|
|
|
+ coverImgMapper.deleteByArticleId(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageBean<Article> PageList(Integer pageIndex, Integer pageSize, String info) {
|
|
|
+ // 创建 PageBean 对象
|
|
|
+ PageBean<Article> pb=new PageBean<>();
|
|
|
+
|
|
|
+ // 开启分页查询
|
|
|
+ PageHelper.startPage(pageIndex,pageSize);
|
|
|
+
|
|
|
+ // 调用 mapper
|
|
|
+ Map<String,Object> map=ThreadLocalUtil.get();
|
|
|
+ Integer userId=(Integer)map.get("id");
|
|
|
+ List<Article> as=articleMapper.pageList(userId,info);
|
|
|
+ // Page 中提供了方法,可以获取 PageHelper 分页查询后,得到的总记录条数和当前页数据
|
|
|
+ Page<Article> p=(Page<Article>) as;
|
|
|
+
|
|
|
+ // 把数据填充到 PageBean 对象中
|
|
|
+ pb.setTotal(p.getTotal());
|
|
|
+ pb.setItems(p.getResult());
|
|
|
+
|
|
|
+ return pb;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Article> latestList() {
|
|
|
+ return articleMapper.latestList();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Article> relatedList(Integer articleId) {
|
|
|
+ Article articleById = articleMapper.getArticleById(articleId);
|
|
|
+ if (articleById!=null&& StringUtils.isNotBlank(articleById.getCategoryIds())){
|
|
|
+ List<String> strings = JSONObject.parseArray(articleById.getCategoryIds(), String.class);
|
|
|
+ return articleMapper.relatedList(strings);
|
|
|
+ }else {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleCoverImage(MultipartFile coverImage, Integer articleId) {
|
|
|
+ String fileName = coverImage.getOriginalFilename();
|
|
|
+ String fileType = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ if (!fileTypeList.contains(fileType)) {
|
|
|
+ throw new RuntimeException("文件类型有误");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ byte[] bytes = coverImage.getBytes();
|
|
|
+ ArticleCoverImg articleCoverImg = new ArticleCoverImg();
|
|
|
+ articleCoverImg.setArticleId(articleId);
|
|
|
+ articleCoverImg.setCoverImgData(bytes);
|
|
|
+ coverImgMapper.insert(articleCoverImg);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException("保存封面图片失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|