代码优化

This commit is contained in:
meng 2019-04-30 22:24:41 +08:00
parent 50d6b813c1
commit 541d033492
3 changed files with 197 additions and 184 deletions

View File

@ -1,10 +1,10 @@
package com.len.controller;
import com.len.core.LenUser;
import com.len.entity.*;
import com.len.entity.ArticleDetail;
import com.len.entity.BlogArticle;
import com.len.entity.BlogCategory;
import com.len.model.Article;
import com.len.service.*;
import com.len.util.BeanUtil;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import com.len.util.UploadUtil;
@ -12,15 +12,13 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @author zhuxiaomeng
@ -44,22 +42,9 @@ public class BlogAdminController {
@Autowired
private UploadUtil uploadUtil;
@Autowired
private BlogTagService tagService;
@Autowired
private BlogCategoryService categoryService;
@Autowired
private ArticleCategoryService articleCategoryService;
@Autowired
private ArticleTagService articleTagService;
private static final Pattern IMG = Pattern.compile("<(img)(.*?)(/>|></img>|>)");
private static final Pattern SRC = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)");
@GetMapping("/article/getList")
public ReType getArticleList(BlogArticle article, Integer page, Integer limit) {
@ -101,7 +86,6 @@ public class BlogAdminController {
}
@PostMapping("/article/add")
@Transactional
public JsonUtil addArticle(@RequestBody ArticleDetail detail) {
JsonUtil json = new JsonUtil();
json.setStatus(400);
@ -127,78 +111,13 @@ public class BlogAdminController {
return json;
}
String articleId = UUID.randomUUID().toString().replace("-", "");
article.setCode(generatorCode());
article.setId(articleId);
article.setCreateDate(new Date());
article.setCreateBy(LenUser.getPrincipal().getUserId());
article.setReadNumber(0);
String content = article.getContent();
Matcher matcher = IMG.matcher(content);
boolean exists = matcher.find();
if (exists) {
String img = matcher.group();
Matcher m = SRC.matcher(img);
boolean b = m.find();
if (b) {
article.setFirstImg(m.group(1));
}
}
BlogArticle blogArticle = new BlogArticle();
BeanUtil.copyNotNullBean(article, blogArticle);
articleService.insert(blogArticle);
List<ArticleCategory> categories = new ArrayList<>();
for (String cateId : detail.getCategory()) {
ArticleCategory articleCategory = new ArticleCategory();
articleCategory.setId(UUID.randomUUID().toString().replace("-", ""));
articleCategory.setArticleId(articleId);
articleCategory.setCategoryId(cateId);
categories.add(articleCategory);
}
articleCategoryService.insertList(categories);
List<ArticleTag> articleTags = new ArrayList<>();
List<BlogTag> blogTags = new ArrayList<>();
ArticleTag articleTag;
BlogTag blogTag;
BlogTag oldTag;
for (String tag : detail.getTags()) {
articleTag = new ArticleTag();
articleTags.add(articleTag);
articleTag.setArticleId(articleId);
blogTag = new BlogTag();
blogTag.setTagCode(tag);
oldTag = tagService.selectOne(blogTag);
if (oldTag != null) {
articleTag.setTagId(oldTag.getId());
} else {
blogTags.add(blogTag);
String id = UUID.randomUUID().toString().replace("-", "");
blogTag.setId(id);
blogTag.setTagName(tag);
articleTag.setTagId(id);
}
}
articleTagService.insertList(articleTags);
if (!blogTags.isEmpty()) {
tagService.insertList(blogTags);
}
articleService.addArticle(detail);
json.setStatus(200);
json.setMsg("文章发表成功");
return json;
}
@PostMapping("/article/update")
@Transactional
public JsonUtil updateArticle(@RequestBody ArticleDetail detail) {
JsonUtil json = new JsonUtil();
Article article = detail.getArticle();
@ -226,104 +145,10 @@ public class BlogAdminController {
json.setMsg("标签不能为空");
return json;
}
article.setUpdateBy(LenUser.getPrincipal().getUserId());
article.setUpdateDate(new Date());
String content = article.getContent();
Matcher matcher = IMG.matcher(content);
boolean exists = matcher.find();
if (exists) {
String img = matcher.group();
Matcher m = SRC.matcher(img);
boolean b = m.find();
if (b) {
article.setFirstImg(m.group(1));
}
}
BlogArticle blogArticle = new BlogArticle();
BeanUtil.copyNotNullBean(article, blogArticle);
articleService.updateByPrimaryKey(blogArticle);
ArticleTag articleTag = new ArticleTag();
articleTag.setArticleId(article.getId());
articleTagService.delete(articleTag);
List<ArticleTag> articleTags = new ArrayList<>();
List<BlogTag> blogTags = new ArrayList<>();
BlogTag needAddTag;
for (String tag : tags) {
articleTag = new ArticleTag();
articleTags.add(articleTag);
articleTag.setArticleId(article.getId());
BlogTag blogTag = new BlogTag();
blogTag.setTagCode(tag);
BlogTag tag1 = tagService.selectOne(blogTag);
if (tag1 != null) {
articleTag.setTagId(tag1.getId());
} else {
String tagId = UUID.randomUUID().toString().replace("-", "");
articleTag.setTagId(tagId);
needAddTag = new BlogTag();
blogTags.add(needAddTag);
needAddTag.setId(tagId);
needAddTag.setTagCode(tag);
needAddTag.setTagName(tag);
}
}
if (!blogTags.isEmpty()) {
tagService.insertList(blogTags);
}
articleTagService.insertList(articleTags);
ArticleCategory articleCategory = new ArticleCategory();
articleCategory.setArticleId(article.getId());
List<ArticleCategory> categories = articleCategoryService.select(articleCategory);
if (!categories.isEmpty()) {
List<String> cateIds = categories.stream().map(ArticleCategory::getCategoryId)
.collect(Collectors.toList());
List<String> collect = cateIds.stream().filter(categoryIds::contains)
.collect(Collectors.toList());
categoryIds.removeAll(collect);
cateIds.removeAll(collect);
if (!cateIds.isEmpty()) {
List<String> delCategoryIds = categories.stream().filter(s -> cateIds.contains(s.getCategoryId()))
.map(ArticleCategory::getId)
.collect(Collectors.toList());
articleCategoryService.delByIds(delCategoryIds);
}
}
if (!categoryIds.isEmpty()) {
List<ArticleCategory> articleCategories = new ArrayList<>();
ArticleCategory category;
for (String ca : categoryIds) {
category = new ArticleCategory();
articleCategories.add(category);
category.setId(UUID.randomUUID().toString().replace("-", ""));
category.setArticleId(article.getId());
category.setCategoryId(ca);
}
articleCategoryService.insertList(articleCategories);
}
articleService.updateArticle(article, categoryIds, tags);
json.setStatus(200);
json.setMsg("更新成功");
return json;
}
private String generatorCode() {
Random random = new Random();
StringBuilder result = new StringBuilder();
for (int i = 0; i < 8; i++) {
result.append(random.nextInt(9) + 1);
}
BlogArticle article = new BlogArticle();
article.setCode(result.toString());
BlogArticle blogArticle = articleService.selectOne(article);
if (blogArticle == null) {
return result.toString();
} else {
return generatorCode();
}
}
}

View File

@ -1,9 +1,11 @@
package com.len.service;
import com.len.base.BaseService;
import com.len.entity.ArticleDetail;
import com.len.entity.BlogArticle;
import com.len.model.Article;
import com.len.util.JsonUtil;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Date;
import java.util.List;
@ -29,4 +31,8 @@ public interface BlogArticleService extends BaseService<BlogArticle, String> {
BlogArticle selectNext(Date date);
boolean addArticle(ArticleDetail articleDetail);
boolean updateArticle(Article article,List<String> categoryIds,List<String> tags);
}

View File

@ -3,6 +3,7 @@ package com.len.service.impl;
import com.github.pagehelper.PageHelper;
import com.len.base.BaseMapper;
import com.len.base.impl.BaseServiceImpl;
import com.len.core.LenUser;
import com.len.entity.*;
import com.len.mapper.BlogArticleMapper;
import com.len.model.Article;
@ -14,10 +15,12 @@ import com.len.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
@ -46,6 +49,10 @@ public class BlogArticleServiceImpl extends BaseServiceImpl<BlogArticle, String>
@Autowired
private SysUserService sysUserService;
private static final Pattern IMG = Pattern.compile("<(img)(.*?)(/>|></img>|>)");
private static final Pattern SRC = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)");
@Override
public BaseMapper<BlogArticle, String> getMappser() {
@ -176,6 +183,181 @@ public class BlogArticleServiceImpl extends BaseServiceImpl<BlogArticle, String>
return blogArticleMapper.selectNext(date);
}
private String generatorCode() {
Random random = new Random();
StringBuilder result = new StringBuilder();
for (int i = 0; i < 8; i++) {
result.append(random.nextInt(9) + 1);
}
BlogArticle article = new BlogArticle();
article.setCode(result.toString());
BlogArticle blogArticle = selectOne(article);
if (blogArticle == null) {
return result.toString();
} else {
return generatorCode();
}
}
@Override
@Transactional
public boolean addArticle(ArticleDetail articleDetail) {
Article article = articleDetail.getArticle();
String articleId = UUID.randomUUID().toString().replace("-", "");
article.setCode(generatorCode());
article.setId(articleId);
article.setCreateDate(new Date());
article.setCreateBy(LenUser.getPrincipal().getUserId());
article.setReadNumber(0);
String content = article.getContent();
Matcher matcher = IMG.matcher(content);
boolean exists = matcher.find();
if (exists) {
String img = matcher.group();
Matcher m = SRC.matcher(img);
boolean b = m.find();
if (b) {
article.setFirstImg(m.group(1));
}
}
BlogArticle blogArticle = new BlogArticle();
BeanUtil.copyNotNullBean(article, blogArticle);
int result = insert(blogArticle);
List<ArticleCategory> categories = new ArrayList<>();
for (String cateId : articleDetail.getCategory()) {
ArticleCategory articleCategory = new ArticleCategory();
articleCategory.setId(UUID.randomUUID().toString().replace("-", ""));
articleCategory.setArticleId(articleId);
articleCategory.setCategoryId(cateId);
categories.add(articleCategory);
}
int insertResult = articleCategoryService.insertList(categories);
List<ArticleTag> articleTags = new ArrayList<>();
List<BlogTag> blogTags = new ArrayList<>();
ArticleTag articleTag;
BlogTag blogTag;
BlogTag oldTag;
for (String tag : articleDetail.getTags()) {
articleTag = new ArticleTag();
articleTags.add(articleTag);
articleTag.setArticleId(articleId);
blogTag = new BlogTag();
blogTag.setTagCode(tag);
oldTag = tagService.selectOne(blogTag);
if (oldTag != null) {
articleTag.setTagId(oldTag.getId());
} else {
blogTags.add(blogTag);
String id = UUID.randomUUID().toString().replace("-", "");
blogTag.setId(id);
blogTag.setTagName(tag);
articleTag.setTagId(id);
}
}
articleTagService.insertList(articleTags);
if (!blogTags.isEmpty()) {
tagService.insertList(blogTags);
}
return result > 0 && insertResult > 0;
}
@Override
@Transactional
public boolean updateArticle(Article article, List<String> categoryIds, List<String> tags) {
article.setUpdateBy(LenUser.getPrincipal().getUserId());
article.setUpdateDate(new Date());
String content = article.getContent();
Matcher matcher = IMG.matcher(content);
boolean exists = matcher.find();
if (exists) {
String img = matcher.group();
Matcher m = SRC.matcher(img);
boolean b = m.find();
if (b) {
article.setFirstImg(m.group(1));
}
}
BlogArticle blogArticle = new BlogArticle();
BeanUtil.copyNotNullBean(article, blogArticle);
updateByPrimaryKey(blogArticle);
ArticleTag articleTag = new ArticleTag();
articleTag.setArticleId(article.getId());
articleTagService.delete(articleTag);
List<ArticleTag> articleTags = new ArrayList<>();
List<BlogTag> blogTags = new ArrayList<>();
BlogTag needAddTag;
for (String tag : tags) {
articleTag = new ArticleTag();
articleTags.add(articleTag);
articleTag.setArticleId(article.getId());
BlogTag blogTag = new BlogTag();
blogTag.setTagCode(tag);
BlogTag tag1 = tagService.selectOne(blogTag);
if (tag1 != null) {
articleTag.setTagId(tag1.getId());
} else {
String tagId = UUID.randomUUID().toString().replace("-", "");
articleTag.setTagId(tagId);
needAddTag = new BlogTag();
blogTags.add(needAddTag);
needAddTag.setId(tagId);
needAddTag.setTagCode(tag);
needAddTag.setTagName(tag);
}
}
if (!blogTags.isEmpty()) {
tagService.insertList(blogTags);
}
articleTagService.insertList(articleTags);
ArticleCategory articleCategory = new ArticleCategory();
articleCategory.setArticleId(article.getId());
List<ArticleCategory> categories = articleCategoryService.select(articleCategory);
if (!categories.isEmpty()) {
List<String> cateIds = categories.stream().map(ArticleCategory::getCategoryId)
.collect(Collectors.toList());
List<String> collect = cateIds.stream().filter(categoryIds::contains)
.collect(Collectors.toList());
categoryIds.removeAll(collect);
cateIds.removeAll(collect);
if (!cateIds.isEmpty()) {
List<String> delCategoryIds = categories.stream().filter(s -> cateIds.contains(s.getCategoryId()))
.map(ArticleCategory::getId)
.collect(Collectors.toList());
articleCategoryService.delByIds(delCategoryIds);
}
}
if (!categoryIds.isEmpty()) {
List<ArticleCategory> articleCategories = new ArrayList<>();
ArticleCategory category;
for (String ca : categoryIds) {
category = new ArticleCategory();
articleCategories.add(category);
category.setId(UUID.randomUUID().toString().replace("-", ""));
category.setArticleId(article.getId());
category.setCategoryId(ca);
}
articleCategoryService.insertList(articleCategories);
}
return true;
}
/**
* 半小时增加一次有效点击数
*