🐛 文章标签问题修复

This commit is contained in:
x ronger 2020-09-16 22:55:51 +08:00
parent cf1a1f7d8f
commit 3189b59192

View File

@ -37,11 +37,11 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
private ArticleMapper articleMapper; private ArticleMapper articleMapper;
@Override @Override
@Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class }) @Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class})
public Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException { public Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException {
User user = UserUtils.getCurrentUserByToken(); User user = UserUtils.getCurrentUserByToken();
String articleTags = article.getArticleTags(); String articleTags = article.getArticleTags();
if(StringUtils.isNotBlank(articleTags)){ if (StringUtils.isNotBlank(articleTags)) {
String[] tags = articleTags.split(","); String[] tags = articleTags.split(",");
List<ArticleTagDTO> articleTagDTOList = articleMapper.selectTags(article.getIdArticle()); List<ArticleTagDTO> articleTagDTOList = articleMapper.selectTags(article.getIdArticle());
for (int i = 0; i < tags.length; i++) { for (int i = 0; i < tags.length; i++) {
@ -50,10 +50,10 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
Tag tag = new Tag(); Tag tag = new Tag();
tag.setTagTitle(tags[i]); tag.setTagTitle(tags[i]);
tag = tagMapper.selectOne(tag); tag = tagMapper.selectOne(tag);
if(tag == null){ if (tag == null) {
tag = new Tag(); tag = new Tag();
tag.setTagTitle(tags[i]); tag.setTagTitle(tags[i]);
tag.setTagUri(URLEncoder.encode(tag.getTagTitle(),"UTF-8")); tag.setTagUri(URLEncoder.encode(tag.getTagTitle(), "UTF-8"));
tag.setCreatedTime(new Date()); tag.setCreatedTime(new Date());
tag.setUpdatedTime(tag.getCreatedTime()); tag.setUpdatedTime(tag.getCreatedTime());
tag.setTagArticleCount(1); tag.setTagArticleCount(1);
@ -61,31 +61,34 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
addTagArticle = true; addTagArticle = true;
addUserTag = true; addUserTag = true;
} else { } else {
for(int m=0,n=articleTagDTOList.size()-1;m<n; m++) { int n = articleTagDTOList.size();
for (int m = 0; m < n; m++) {
ArticleTagDTO articleTag = articleTagDTOList.get(m); ArticleTagDTO articleTag = articleTagDTOList.get(m);
if (articleTag.getIdTag().equals(tag.getIdTag())) { if (articleTag.getIdTag().equals(tag.getIdTag())) {
articleTagDTOList.remove(articleTag); articleTagDTOList.remove(articleTag);
m--;
n--;
} }
} }
Integer count = tagMapper.selectCountTagArticleById(tag.getIdTag(),article.getIdArticle()); Integer count = tagMapper.selectCountTagArticleById(tag.getIdTag(), article.getIdArticle());
if(count == 0){ if (count == 0) {
tag.setTagArticleCount(tag.getTagArticleCount() + 1); tag.setTagArticleCount(tag.getTagArticleCount() + 1);
tagMapper.updateByPrimaryKeySelective(tag); tagMapper.updateByPrimaryKeySelective(tag);
addTagArticle = true; addTagArticle = true;
} }
Integer countUserTag = tagMapper.selectCountUserTagById(user.getIdUser(),tag.getIdTag()); Integer countUserTag = tagMapper.selectCountUserTagById(user.getIdUser(), tag.getIdTag());
if(countUserTag == 0){ if (countUserTag == 0) {
addUserTag = true; addUserTag = true;
} }
} }
articleTagDTOList.forEach(articleTagDTO -> { articleTagDTOList.forEach(articleTagDTO -> {
articleMapper.deleteUnusedArticleTag(articleTagDTO.getIdArticleTag()); articleMapper.deleteUnusedArticleTag(articleTagDTO.getIdArticleTag());
}); });
if(addTagArticle){ if (addTagArticle) {
tagMapper.insertTagArticle(tag.getIdTag(),article.getIdArticle()); tagMapper.insertTagArticle(tag.getIdTag(), article.getIdArticle());
} }
if(addUserTag){ if (addUserTag) {
tagMapper.insertUserTag(tag.getIdTag(),user.getIdUser(),1); tagMapper.insertUserTag(tag.getIdTag(), user.getIdUser(), 1);
} }
} }
return 1; return 1;
@ -109,14 +112,14 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
Map map = new HashMap(1); Map map = new HashMap(1);
if (tag.getIdTag() == null) { if (tag.getIdTag() == null) {
if (StringUtils.isBlank(tag.getTagTitle())) { if (StringUtils.isBlank(tag.getTagTitle())) {
map.put("message","标签名不能为空!"); map.put("message", "标签名不能为空!");
return map; return map;
} else { } else {
Condition tagCondition = new Condition(Tag.class); Condition tagCondition = new Condition(Tag.class);
tagCondition.createCriteria().andCondition("tag_title =", tag.getTagTitle()); tagCondition.createCriteria().andCondition("tag_title =", tag.getTagTitle());
List<Tag> tags = tagMapper.selectByCondition(tagCondition); List<Tag> tags = tagMapper.selectByCondition(tagCondition);
if (!tags.isEmpty()) { if (!tags.isEmpty()) {
map.put("message","标签 '" + tag.getTagTitle() + "' 已存在!"); map.put("message", "标签 '" + tag.getTagTitle() + "' 已存在!");
return map; return map;
} }
} }
@ -132,10 +135,10 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
result = tagMapper.insertSelective(newTag); result = tagMapper.insertSelective(newTag);
} else { } else {
tag.setUpdatedTime(new Date()); tag.setUpdatedTime(new Date());
result = tagMapper.update(tag.getIdTag(),tag.getTagUri(),tag.getTagIconPath(),tag.getTagStatus(),tag.getTagDescription(),tag.getTagReservation()); result = tagMapper.update(tag.getIdTag(), tag.getTagUri(), tag.getTagIconPath(), tag.getTagStatus(), tag.getTagDescription(), tag.getTagReservation());
} }
if (result == 0) { if (result == 0) {
map.put("message","操作失败!"); map.put("message", "操作失败!");
} else { } else {
map.put("tag", tag); map.put("tag", tag);
} }