TagsServiceImpl.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.nosum.deliver.category.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.nosum.common.base.domain.Result;
  7. import com.nosum.common.constant.Constants;
  8. import com.nosum.common.enums.ErrorEnum;
  9. import com.nosum.common.util.ExceptionUtil;
  10. import com.nosum.common.util.PageUtil;
  11. import com.nosum.deliver.category.dao.CategoryTagsDao;
  12. import com.nosum.deliver.category.dao.TagsDao;
  13. import com.nosum.deliver.category.domain.po.CategoryTags;
  14. import com.nosum.deliver.category.domain.po.Tags;
  15. import com.nosum.deliver.category.domain.vo.TagsVO;
  16. import com.nosum.deliver.category.service.TagsService;
  17. import com.nosum.deliver.posts.dao.PostsTagsDao;
  18. import com.nosum.deliver.posts.domain.po.PostsTags;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.CollectionUtils;
  23. import java.time.LocalDateTime;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. * @author sumbytes
  28. * @since 2019-08-28
  29. */
  30. @Service
  31. public class TagsServiceImpl extends ServiceImpl<TagsDao, Tags> implements TagsService {
  32. @Autowired
  33. private TagsDao tagsDao;
  34. @Autowired
  35. private PostsTagsDao postsTagsDao;
  36. @Autowired
  37. private CategoryTagsDao categoryTagsDao;
  38. @Override
  39. public Result<TagsVO> getTagsAndArticleQuantityList(TagsVO tagsVO) {
  40. List<Tags> records = this.tagsDao.selectList(new LambdaQueryWrapper<Tags>().orderByDesc(Tags::getCreateTime));
  41. List<TagsVO> tagsList = new ArrayList<>();
  42. if (!CollectionUtils.isEmpty(records)) {
  43. records.forEach(tags -> {
  44. Integer total = postsTagsDao.selectCount(new LambdaQueryWrapper<PostsTags>().eq(PostsTags::getTagsId, tags.getId()));
  45. tagsList.add(new TagsVO().setId(tags.getId()).setPostsTotal(total).setName(tags.getName()));
  46. });
  47. }
  48. return Result.createWithModels(tagsList);
  49. }
  50. @Override
  51. public Result<TagsVO> getTagsList(TagsVO tagsVO) {
  52. List<TagsVO> tagsList = new ArrayList<>();
  53. if (tagsVO == null || tagsVO.getPage() == null || tagsVO.getSize() == null) {
  54. List<Tags> records = this.tagsDao.selectList(new LambdaQueryWrapper<Tags>().orderByDesc(Tags::getCreateTime));
  55. if (!CollectionUtils.isEmpty(records)) {
  56. records.forEach(tags -> {
  57. tagsList.add(new TagsVO().setId(tags.getId()).setName(tags.getName()));
  58. });
  59. }
  60. return Result.createWithModels(tagsList);
  61. }
  62. LambdaQueryWrapper<Tags> tagsLambdaQueryWrapper = new LambdaQueryWrapper<Tags>();
  63. if (StringUtils.isNotBlank(tagsVO.getKeywords())){
  64. tagsLambdaQueryWrapper.like(Tags::getName, tagsVO.getKeywords());
  65. }
  66. if (StringUtils.isNotBlank(tagsVO.getName())){
  67. tagsLambdaQueryWrapper.eq(Tags::getName, tagsVO.getName());
  68. }
  69. Page page = PageUtil.checkAndInitPage(tagsVO);
  70. IPage<TagsVO> tagsIPage = this.tagsDao.selectPage(page,tagsLambdaQueryWrapper.orderByDesc(Tags::getCreateTime));
  71. return Result.createWithPaging(tagsIPage.getRecords(), PageUtil.initPageInfo(page));
  72. }
  73. @Override
  74. public Result<TagsVO> getTags(Long id) {
  75. Tags tags = this.tagsDao.selectById(id);
  76. return Result.createWithModel(new TagsVO().setId(tags.getId()).setName(tags.getName()));
  77. }
  78. @Override
  79. public Result<TagsVO> updateTags(TagsVO tagsVO) {
  80. Integer count = this.tagsDao.selectCount(new LambdaQueryWrapper<Tags>().eq(Tags::getId,tagsVO.getId()));
  81. if (count.equals(Constants.ZERO)) {
  82. ExceptionUtil.rollback(ErrorEnum.DATA_NO_EXIST);
  83. }
  84. this.tagsDao.updateById(new Tags().setId(tagsVO.getId()).setName(tagsVO.getName()).setUpdateTime(LocalDateTime.now()));
  85. return Result.createWithSuccessMessage();
  86. }
  87. @Override
  88. public Result<TagsVO> deleteTags(Long id) {
  89. this.tagsDao.deleteById(id);
  90. this.categoryTagsDao.delete(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getTagsId, id));
  91. this.postsTagsDao.delete(new LambdaQueryWrapper<PostsTags>().eq(PostsTags::getTagsId, id));
  92. return Result.createWithSuccessMessage();
  93. }
  94. @Override
  95. public Result<TagsVO> saveTags(TagsVO tagsVO) {
  96. this.tagsDao.insert(new Tags().setName(tagsVO.getName()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
  97. return Result.createWithSuccessMessage();
  98. }
  99. }