From bdfe500d27eb166043161d8ffbeb2d4f883a18d4 Mon Sep 17 00:00:00 2001 From: ck <851316342@qq.com> Date: Sat, 17 Apr 2021 17:33:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A01=E5=AF=B9=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cms/mapper/ArticleCategoryMapper.java | 12 +++ .../jingcheng/cms/mapper/ArticleMapper.java | 4 +- .../jingcheng/cms/model/ArticleCategory.java | 15 ++++ .../cms/service/impl/ArticleSerivceImpl.java | 81 +++++++++++++++---- .../java/com/jingcheng/cms/vo/ArticleVo.java | 2 +- .../mapper/ArticleCategoryMapper.xml | 14 ++++ src/main/resources/mapper/ArticleMapper.xml | 47 ++++++++--- 7 files changed, 145 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/jingcheng/cms/mapper/ArticleCategoryMapper.java create mode 100644 src/main/java/com/jingcheng/cms/model/ArticleCategory.java create mode 100644 src/main/resources/mapper/ArticleCategoryMapper.xml diff --git a/src/main/java/com/jingcheng/cms/mapper/ArticleCategoryMapper.java b/src/main/java/com/jingcheng/cms/mapper/ArticleCategoryMapper.java new file mode 100644 index 0000000..c891a9c --- /dev/null +++ b/src/main/java/com/jingcheng/cms/mapper/ArticleCategoryMapper.java @@ -0,0 +1,12 @@ +package com.jingcheng.cms.mapper; + +import com.jingcheng.cms.model.ArticleCategory; +import com.jingcheng.cms.util.CommonMapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ArticleCategoryMapper extends CommonMapper { + int addArticleCategory(List articleCategoryList); +} diff --git a/src/main/java/com/jingcheng/cms/mapper/ArticleMapper.java b/src/main/java/com/jingcheng/cms/mapper/ArticleMapper.java index f7191e1..428479f 100644 --- a/src/main/java/com/jingcheng/cms/mapper/ArticleMapper.java +++ b/src/main/java/com/jingcheng/cms/mapper/ArticleMapper.java @@ -14,7 +14,7 @@ public interface ArticleMapper extends CommonMapper
{ int addArticle(Article article); List getArticleListByCondition(@Param("title") String title, - @Param("firstCategory") Long firstCategory, + @Param("firstCategoryName") String firstCategoryName, @Param("secondCategory") Long secondCategory, @Param("fileType") Integer fileType); List getFirstCategory(); @@ -23,6 +23,6 @@ public interface ArticleMapper extends CommonMapper
{ List searchArticleListByCondition(@Param("param") String param, @Param("keyword") String keyword, - @Param("firstCategory") Long firstCategory, + @Param("firstCategoryName") String firstCategoryName, @Param("secondCategory") Long secondCategory); } diff --git a/src/main/java/com/jingcheng/cms/model/ArticleCategory.java b/src/main/java/com/jingcheng/cms/model/ArticleCategory.java new file mode 100644 index 0000000..5dfc415 --- /dev/null +++ b/src/main/java/com/jingcheng/cms/model/ArticleCategory.java @@ -0,0 +1,15 @@ +package com.jingcheng.cms.model; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +@TableName("article_category") +public class ArticleCategory extends BaseEntity{ + + private Long articleId; + + private Long categoryId; +} diff --git a/src/main/java/com/jingcheng/cms/service/impl/ArticleSerivceImpl.java b/src/main/java/com/jingcheng/cms/service/impl/ArticleSerivceImpl.java index d438b16..4fbb8b3 100644 --- a/src/main/java/com/jingcheng/cms/service/impl/ArticleSerivceImpl.java +++ b/src/main/java/com/jingcheng/cms/service/impl/ArticleSerivceImpl.java @@ -3,9 +3,11 @@ package com.jingcheng.cms.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.jingcheng.cms.constants.Constants; +import com.jingcheng.cms.mapper.ArticleCategoryMapper; import com.jingcheng.cms.mapper.ArticleMapper; import com.jingcheng.cms.mapper.CategoryMapper; import com.jingcheng.cms.model.Article; +import com.jingcheng.cms.model.ArticleCategory; import com.jingcheng.cms.model.Category; import com.jingcheng.cms.service.ArticleSerive; import com.jingcheng.cms.util.AjaxResult; @@ -17,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; @Service @@ -27,6 +30,8 @@ public class ArticleSerivceImpl implements ArticleSerive { private ArticleMapper articleMapper; @Resource private CategoryMapper categoryMapper; + @Resource + private ArticleCategoryMapper articleCategoryMapper; @Override @@ -34,7 +39,7 @@ public class ArticleSerivceImpl implements ArticleSerive { AjaxResult ajaxResult = new AjaxResult(); Long id = jsonObject.getLong("id"); String title = jsonObject.getString("title"); - Long firstCategory = jsonObject.getLong("firstCategory"); + JSONArray firstCategory = jsonObject.getJSONArray("firstCategory"); Long secondCategory = jsonObject.getLong("secondCategory"); String keyword = jsonObject.getString("keyword"); Integer fileType = jsonObject.getInteger("fileType"); @@ -76,16 +81,16 @@ public class ArticleSerivceImpl implements ArticleSerive { //新增 Article article = new Article(); article.setTitle(title); - article.setFirstCategory(firstCategory); - article.setSecondCategory(secondCategory); article.setKeyword(keyword); article.setFileType(fileType); article.setState(state); article.setContent(content); article.setDescribe(describe); article.setVideoUrl(videoUrl); + article.setSecondCategory(secondCategory); article.setIsRecommend(isRecommend); articleMapper.addArticle(article); + id = article.getId(); ajaxResult.setRetmsg("成功新增文章"); }else { //修改 @@ -100,8 +105,6 @@ public class ArticleSerivceImpl implements ArticleSerive { criteria.andEqualTo("id",id); article = new Article(); article.setTitle(title); - article.setFirstCategory(firstCategory); - article.setSecondCategory(secondCategory); article.setKeyword(keyword); article.setFileType(fileType); article.setState(state); @@ -110,8 +113,26 @@ public class ArticleSerivceImpl implements ArticleSerive { article.setVideoUrl(videoUrl); article.setIsRecommend(isRecommend); articleMapper.updateByExampleSelective(article,example); + ArticleCategory delArticleCategory = new ArticleCategory(); + delArticleCategory.setArticleId(id); + //清除之前的1级分类 + if (null !=firstCategory && firstCategory.size()>0){ + articleCategoryMapper.delete(delArticleCategory); + } ajaxResult.setRetmsg("成功修改文章"); } + if (null !=firstCategory && firstCategory.size()>0){ + List firstCategoryIdList = JSONObject.parseArray(firstCategory.toJSONString(), Long.class); + List articleCategoryList = new ArrayList<>(); + for (Long categoryId:firstCategoryIdList){ + ArticleCategory articleCategory = new ArticleCategory(); + articleCategory.setArticleId(id); + articleCategory.setCategoryId(categoryId); + articleCategory.setState(Constants.STATE_VALID); + articleCategoryList.add(articleCategory); + } + articleCategoryMapper.addArticleCategory(articleCategoryList); + } return ajaxResult; } @@ -127,7 +148,12 @@ public class ArticleSerivceImpl implements ArticleSerive { Long firstCategory = jsonObject.getLong("firstCategory"); Long secondCategory = jsonObject.getLong("secondCategory"); Integer fileType = jsonObject.getInteger("fileType"); - List CategoryList = articleMapper.getArticleListByCondition(title,firstCategory,secondCategory,fileType); + Category category = categoryMapper.selectByPrimaryKey(firstCategory); + String firstCategoryName = null ; + if (category != null ){ + firstCategoryName = "%" + category.getName() + "%" ; + } + List CategoryList = articleMapper.getArticleListByCondition(title,firstCategoryName,secondCategory,fileType); JSONObject pageJson = PageUtils.page(CategoryList,pageNum,pageSize); return pageJson; } @@ -159,7 +185,12 @@ public class ArticleSerivceImpl implements ArticleSerive { } Long firstCategory = jsonObject.getLong("firstCategory"); Long secondCategory = jsonObject.getLong("secondCategory"); - List CategoryList = articleMapper.searchArticleListByCondition(param,keyword,firstCategory,secondCategory); + Category category = categoryMapper.selectByPrimaryKey(firstCategory); + String firstCategoryName = null ; + if (category != null ){ + firstCategoryName = "%" + category.getName() + "%" ; + } + List CategoryList = articleMapper.searchArticleListByCondition(param,keyword,firstCategoryName,secondCategory); JSONObject pageJson = PageUtils.page(CategoryList,pageNum,pageSize); return pageJson; } @@ -184,6 +215,11 @@ public class ArticleSerivceImpl implements ArticleSerive { article = new Article(); article.setState(Constants.DISABLE); articleMapper.updateByExampleSelective(article,example); + ArticleCategory delArticleCategory = new ArticleCategory(); + delArticleCategory.setArticleId(id); + //清除之前的1级分类 + articleCategoryMapper.delete(delArticleCategory); + } ajaxResult.setRetmsg("批量删除成功"); return ajaxResult; @@ -198,13 +234,15 @@ public class ArticleSerivceImpl implements ArticleSerive { ajaxResult.setRetmsg("未找到该文章"); return ajaxResult; }else { - Long firstCategoryId = article.getFirstCategory(); Long secondCategoryId = article.getSecondCategory(); - Category firstCategory = categoryMapper.selectByPrimaryKey(firstCategoryId); + ArticleCategory findArticleCategory = new ArticleCategory(); + findArticleCategory.setArticleId(id); + findArticleCategory.setState(Constants.STATE_VALID); + List firstCategoryList = articleCategoryMapper.select(findArticleCategory); Category secondCategory = categoryMapper.selectByPrimaryKey(secondCategoryId); ArticleVo articleVo = new ArticleVo(); articleVo.setTitle(article.getTitle()); - articleVo.setFirstCategory(firstCategoryId); +// articleVo.setFirstCategory(firstCategoryId); articleVo.setSecondCategory(secondCategoryId); articleVo.setContent(article.getContent()); articleVo.setFileType(article.getFileType()); @@ -214,15 +252,26 @@ public class ArticleSerivceImpl implements ArticleSerive { articleVo.setCreateDateTime(article.getCreateDateTime()); articleVo.setVideoUrl(article.getVideoUrl()); articleVo.setIsRecommend(article.getIsRecommend()); - if (null != firstCategory ){ - articleVo.setFirstCategoryName(firstCategory.getName()); - articleVo.setFirstLevel(firstCategory.getLevel()); - articleVo.setFirstSortNum(firstCategory.getSortNum()); + if (null != firstCategoryList && firstCategoryList.size()>0 ){ + List firstCategoryNameList = new ArrayList<>(); + List firstSortNumList = new ArrayList<>(); + for (ArticleCategory articleCategory:firstCategoryList){ + Category category = categoryMapper.selectByPrimaryKey(articleCategory.getCategoryId()); + firstCategoryNameList.add(category.getName()); + if (category.getSortNum()!=null){ + firstSortNumList.add(String.valueOf(category.getSortNum())); + }else { + firstSortNumList.add(""); + } + } + String firstCategoryName = String.join(",", firstCategoryNameList); + String firstSortNum = String.join(",", firstSortNumList); + articleVo.setFirstCategoryName(firstCategoryName); + articleVo.setFirstSortNum(firstSortNum); } if (null != secondCategory ){ articleVo.setSecondCategoryName(secondCategory.getName()); - articleVo.setSecondLevel(secondCategory.getLevel()); - articleVo.setFirstSortNum(secondCategory.getSortNum()); + articleVo.setSecondSortNum(secondCategory.getSortNum()); } ajaxResult.setData(articleVo); } diff --git a/src/main/java/com/jingcheng/cms/vo/ArticleVo.java b/src/main/java/com/jingcheng/cms/vo/ArticleVo.java index db09cd3..091b9a1 100644 --- a/src/main/java/com/jingcheng/cms/vo/ArticleVo.java +++ b/src/main/java/com/jingcheng/cms/vo/ArticleVo.java @@ -11,7 +11,7 @@ public class ArticleVo extends Article { private String secondCategoryName; - private Integer firstSortNum; + private String firstSortNum; private Integer secondSortNum; diff --git a/src/main/resources/mapper/ArticleCategoryMapper.xml b/src/main/resources/mapper/ArticleCategoryMapper.xml new file mode 100644 index 0000000..29a8ea5 --- /dev/null +++ b/src/main/resources/mapper/ArticleCategoryMapper.xml @@ -0,0 +1,14 @@ + + + + + + INSERT INTO article_category + (`article_id`,`category_id`,`state`) + values + + (#{item.articleId}, #{item.categoryId}, #{item.state}) + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/ArticleMapper.xml b/src/main/resources/mapper/ArticleMapper.xml index 4ee217a..2c61074 100644 --- a/src/main/resources/mapper/ArticleMapper.xml +++ b/src/main/resources/mapper/ArticleMapper.xml @@ -2,20 +2,30 @@ - + INSERT INTO article(`title`,`first_category`,`second_category`,`keyword`,`file_type`,`content`,`state`,`describe`,`video_url`,`is_recommend`) values (#{title},#{firstCategory},#{secondCategory},#{keyword},#{fileType},#{content},#{state},#{describe},#{videoUrl},#{isRecommend}) \ No newline at end of file