Browse Source

文章系统:修改

yusm 6 tháng trước cách đây
mục cha
commit
d8a7c283b8
13 tập tin đã thay đổi với 386 bổ sung3 xóa
  1. 6 0
      fhKeeper/formulahousekeeper/ArticleOperation/pom.xml
  2. 45 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/ArticleController.java
  3. 9 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/ArticleCoverImgController.java
  4. 15 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/mapper/ArticleCoverImgMapper.java
  5. 9 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/mapper/ArticleMapper.java
  6. 12 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/pojo/Article.java
  7. 12 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/pojo/ArticleCoverImg.java
  8. 4 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/ArticleCoverImgService.java
  9. 11 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/ArticleService.java
  10. 8 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/impl/ArticleCoverImgServiceImpl.java
  11. 129 3
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/impl/ArticleServiceImpl.java
  12. 8 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/resources/com/my/bigevent/mapper/ArticleCoverImgMapper.xml
  13. 118 0
      fhKeeper/formulahousekeeper/ArticleOperation/src/main/resources/com/my/bigevent/mapper/ArticleMapper.xml

+ 6 - 0
fhKeeper/formulahousekeeper/ArticleOperation/pom.xml

@@ -89,6 +89,12 @@
             <version>1.2.6</version> <!-- 请检查最新版本 -->
         </dependency>
 
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.78</version> <!-- 请检查最新版本 -->
+        </dependency>
+
 
     </dependencies>
 

+ 45 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/ArticleController.java

@@ -6,6 +6,9 @@ import com.my.bigevent.pojo.Result;
 import com.my.bigevent.service.ArticleService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
 
 @RestController
 @RequestMapping("/article")
@@ -49,5 +52,47 @@ public class ArticleController
         return Result.success(article);
     }
 
+    /**
+     *
+     * @param title
+     * @param categoryIds
+     * @param content
+     * @param state
+     * @param coverImage
+     * @param id
+     * @return
+     */
+    @PostMapping("/insertOrUpdateArticle")
+    public Result insertOrUpdateArticle( @RequestParam(value = "title",required = true) String title,
+                                         @RequestParam(value = "categoryId",required = false) String categoryIds,
+                                         @RequestParam(value = "content",required = true) String content,
+                                         @RequestParam(value = "profile",required = false) String profile,
+                                         @RequestParam(value = "state",required = true) String state,
+                                         @RequestParam(value = "coverImage",required = false) MultipartFile coverImage,
+                                         @RequestParam(value = "id",required = false) Integer id)
+    {
+        articleService.insertOrUpdateArticle(title,categoryIds,profile,content,state,coverImage,id);
+        return Result.success();
+    }
+
+    @GetMapping ("/pageList")  // 这里的 PageBean 是事先定义好的实体类
+    public Result<PageBean<Article>> PageList(Integer pageIndex,Integer pageSize,
+                                          @RequestParam(required = false) String info)
+    {
+        PageBean<Article> pb=articleService.PageList(pageIndex,pageSize,info);
+        return Result.success(pb);
+    }
+
+    @GetMapping ("/latestList")
+    public Result latestList() {
+        List<Article> latestList =articleService.latestList();
+        return Result.success(latestList);
+    }
+
+    @GetMapping ("/relatedList")
+    public Result relatedList(@RequestParam("id") Integer id) {
+        List<Article> relatedList =articleService.relatedList(id);
+        return Result.success(relatedList);
+    }
 
 }

+ 9 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/ArticleCoverImgController.java

@@ -0,0 +1,9 @@
+package com.my.bigevent.controller;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/articleCoverImg")
+public class ArticleCoverImgController {
+}

+ 15 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/mapper/ArticleCoverImgMapper.java

@@ -0,0 +1,15 @@
+package com.my.bigevent.mapper;
+
+import com.my.bigevent.pojo.ArticleCoverImg;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface ArticleCoverImgMapper {
+    @Delete("delete from article_cover_img where article_id=#{articleId}")
+    void deleteByArticleId(Integer articleId);
+
+    @Insert("insert into article_cover_img(article_id,cover_img_data) values (#{articleId},#{coverImgData})")
+    void insert(ArticleCoverImg articleCoverImg);
+}

+ 9 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/mapper/ArticleMapper.java

@@ -17,7 +17,16 @@ public interface ArticleMapper
 
     // 动态实现 sql,所以这里不用注解
     List<Article> list(Integer userId, Integer categoryId, String state);
+    List<Article> pageList(Integer userId, String info);
 
     @Select("select * from article where id = #{id}")
     Article getArticleById(Integer id);
+
+    void insert(Article article);
+
+    void update(Article article);
+
+    List<Article> latestList();
+
+    List<Article> relatedList(List<String> strings);
 }

+ 12 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/pojo/Article.java

@@ -1,8 +1,10 @@
 package com.my.bigevent.pojo;
 
 
+import com.fasterxml.jackson.annotation.JsonFormat;
 import com.my.bigevent.anno.State;
 import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.validation.constraints.NotEmpty;
@@ -17,6 +19,8 @@ public class Article {
     private String title;//文章标题
     @NotEmpty
     private String content;//文章内容
+
+    private String profile;//简介
 //    @NotEmpty
 //    @URL
     private byte[] coverImg;//封面图像
@@ -26,7 +30,15 @@ public class Article {
     private String state;//发布状态 已发布|草稿
     @NotNull
     private Integer categoryId;//文章分类id
+
+    private String categoryIds;//文章分类 category_ids
     private Integer createUser;//创建人ID
+
+    @JsonFormat(pattern = "yyyy-MM-dd")
+    @DateTimeFormat(pattern = "yyyy-MM-dd")
     private LocalDateTime createTime;//创建时间
+
+    @JsonFormat(pattern = "yyyy-MM-dd")
+    @DateTimeFormat(pattern = "yyyy-MM-dd")
     private LocalDateTime updateTime;//更新时间
 }

+ 12 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/pojo/ArticleCoverImg.java

@@ -0,0 +1,12 @@
+package com.my.bigevent.pojo;
+
+import lombok.Data;
+
+@Data
+public class ArticleCoverImg {
+    private Integer id;
+
+    private Integer articleId;
+
+    private byte[] coverImgData;
+}

+ 4 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/ArticleCoverImgService.java

@@ -0,0 +1,4 @@
+package com.my.bigevent.service;
+
+public interface ArticleCoverImgService {
+}

+ 11 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/ArticleService.java

@@ -2,6 +2,9 @@ package com.my.bigevent.service;
 
 import com.my.bigevent.pojo.Article;
 import com.my.bigevent.pojo.PageBean;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
 
 public interface ArticleService
 {
@@ -12,4 +15,12 @@ public interface ArticleService
     PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state);
 
     Article getArticleById(Integer id);
+
+    void insertOrUpdateArticle(String title, String categoryIds,String profile, String content, String state, MultipartFile coverImage,Integer id);
+
+    PageBean<Article> PageList(Integer pageIndex, Integer pageSize, String info);
+
+    List<Article> latestList();
+
+    List<Article> relatedList(Integer articleId);
 }

+ 8 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/impl/ArticleCoverImgServiceImpl.java

@@ -0,0 +1,8 @@
+package com.my.bigevent.service.impl;
+
+import com.my.bigevent.service.ArticleCoverImgService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class ArticleCoverImgServiceImpl implements ArticleCoverImgService {
+}

+ 129 - 3
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/impl/ArticleServiceImpl.java

@@ -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);
+        }
+    }
 }

+ 8 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/resources/com/my/bigevent/mapper/ArticleCoverImgMapper.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<!--这里是 ArticleMapper.java 接口文件的地址-->
+<mapper namespace="com.my.bigevent.mapper.ArticleCoverImgMapper">
+
+</mapper>

+ 118 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/resources/com/my/bigevent/mapper/ArticleMapper.xml

@@ -4,6 +4,85 @@
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <!--这里是 ArticleMapper.java 接口文件的地址-->
 <mapper namespace="com.my.bigevent.mapper.ArticleMapper">
+    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
+        insert into article
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            <if test="title !=null and title !=''">
+                title,
+            </if>
+            <if test="content !=null and content !=''">
+                content,
+            </if>
+            <if test="state !=null and state !=''">
+                state,
+            </if>
+            <if test="categoryIds !=null and categoryIds !=''">
+                category_ids,
+            </if>
+            <if test="profile !=null and profile !=''">
+                profile,
+            </if>
+            <if test="createUser !=null">
+                create_user,
+            </if>
+            <if test="createTime !=null">
+                 create_time,
+            </if>
+            <if test=" updateTime!=null">
+                 update_time,
+            </if>
+        </trim>
+        <trim prefix=" values (" suffix=")" suffixOverrides=",">
+            <if test="title !=null and title !=''">
+                #{title},
+            </if>
+            <if test="content !=null and content !=''">
+                #{content},
+            </if>
+            <if test="state !=null and state !=''">
+                #{state},
+            </if>
+            <if test="categoryIds !=null and categoryIds !=''">
+                #{categoryIds},
+            </if>
+            <if test="profile !=null and profile !=''">
+                #{profile},
+            </if>
+            <if test="createUser !=null">
+                #{createUser},
+            </if>
+            <if test="createTime !=null">
+                #{createTime},
+            </if>
+            <if test="updateTime !=null">
+                #{updateTime},
+            </if>
+        </trim>
+    </insert>
+    <update id="update">
+        update article
+            <set>
+                <if test="title !=null and title !=''">
+                    title=#{title},
+                </if>
+                <if test="content !=null and content !=''">
+                    content=#{content},
+                </if>
+                <if test="state !=null and state !=''">
+                    state=#{state},
+                </if>
+                <if test="categoryIds !=null and categoryIds !=''">
+                    category_ids=#{categoryIds},
+                </if>
+                <if test="profile !=null and profile !=''">
+                    profile=#{profile},
+                </if>
+                <if test="updateTime !=null">
+                    update_time=#{updateTime},
+                </if>
+            </set>
+        where id=#{id}
+    </update>
     <!--动态 sql 写法,真实项目中常用-->
     <select id="list" resultType="com.my.bigevent.pojo.Article">
         select * from article
@@ -18,4 +97,43 @@
         and create_user=#{userId}
         </where>
     </select>
+    <select id="pageList" resultType="com.my.bigevent.pojo.Article">
+        select a.* ,aci.cover_img_data coverImg
+        from article a
+            left join  article_cover_img aci on a.id=aci.article_id
+        <where>
+            a.state='已发布'
+            <if test="info!=null and info !=''">
+               and (a.title LIKE CONCAT('%', #{info}, '%') OR a.content LIKE CONCAT('%', #{info}, '%'))
+            </if>
+            <if test="userId!=null">
+                and create_user=#{userId}
+            </if>
+        </where>
+        order by  update_time DESC
+    </select>
+    <select id="latestList" resultType="com.my.bigevent.pojo.Article">
+        select a.* ,aci.cover_img_data coverImg
+        from article a
+        left join  article_cover_img aci on a.id=aci.article_id
+            where a.state='已发布'
+        order by  update_time DESC
+        limit 3
+    </select>
+    <select id="relatedList" resultType="com.my.bigevent.pojo.Article">
+        select a.* ,aci.cover_img_data coverImg
+        from article a
+        left join  article_cover_img aci on a.id=aci.article_id
+        <where>
+            a.state='已发布'
+            <if test="strings!=null">
+                and
+                <foreach collection="strings" item="item" separator="or" open="(" close=")">
+                    JSON_CONTAINS(category_ids, #{item})
+                </foreach>
+            </if>
+        </where>
+        order by  update_time DESC
+        limit 6
+    </select>
 </mapper>