CategoryServiceImpl.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.nosum.deliver.category.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.nosum.common.base.domain.Result;
  8. import com.nosum.common.constant.Constants;
  9. import com.nosum.common.enums.ErrorEnum;
  10. import com.nosum.common.util.ExceptionUtil;
  11. import com.nosum.common.util.PageUtil;
  12. import com.nosum.deliver.category.dao.CategoryDao;
  13. import com.nosum.deliver.category.dao.CategoryTagsDao;
  14. import com.nosum.deliver.category.dao.TagsDao;
  15. import com.nosum.deliver.category.domain.po.Category;
  16. import com.nosum.deliver.category.domain.po.CategoryTags;
  17. import com.nosum.deliver.category.domain.po.Tags;
  18. import com.nosum.deliver.category.domain.vo.CategoryVO;
  19. import com.nosum.deliver.category.domain.vo.TagsVO;
  20. import com.nosum.deliver.category.service.CategoryService;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.util.CollectionUtils;
  25. import java.time.LocalDateTime;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Optional;
  31. import java.util.stream.Collectors;
  32. /**
  33. * <p>
  34. * 服务实现类
  35. * </p>
  36. *
  37. * @author sumbytes
  38. * @since 2019-08-28
  39. */
  40. @Service
  41. public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
  42. @Autowired
  43. private CategoryDao categoryDao;
  44. @Autowired
  45. private TagsDao tagsDao;
  46. @Autowired
  47. private CategoryTagsDao categoryTagsDao;
  48. @Override
  49. public Result saveCategory(CategoryVO categoryVO) {
  50. Category category = new Category().setName(categoryVO.getName()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now());
  51. this.categoryDao.insert(category);
  52. List<TagsVO> tagsList = categoryVO.getTagsList();
  53. if (!CollectionUtils.isEmpty(tagsList)) {
  54. tagsList.forEach(tagsVO -> {
  55. if (tagsVO.getId() == null) {
  56. Tags tags = new Tags().setName(tagsVO.getName()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now());
  57. this.tagsDao.insert(tags);
  58. tagsVO.setId(tags.getId());
  59. }
  60. categoryTagsDao.insert(new CategoryTags().setCategoryId(category.getId()).setTagsId(tagsVO.getId()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
  61. });
  62. }
  63. return Result.createWithSuccessMessage();
  64. }
  65. @Override
  66. public Result getCategoryList(CategoryVO categoryVO) {
  67. List<Category> categoryList = this.categoryDao.selectList(new LambdaQueryWrapper<Category>().orderByDesc(Category::getCreateTime));
  68. List<CategoryVO> categoryPostsTotal = this.categoryDao.selectCategoryPostsTotal();
  69. Map<Long, Integer> map = new HashMap<>();
  70. if (!CollectionUtils.isEmpty(categoryPostsTotal)) {
  71. map = categoryPostsTotal.stream().collect(Collectors.toMap(CategoryVO::getId, CategoryVO::getTotal, Integer::sum));
  72. }
  73. List<CategoryVO> categoryVOList = new ArrayList<>();
  74. if (!CollectionUtils.isEmpty(categoryList)) {
  75. Map<Long, Integer> finalMap = map;
  76. categoryList.forEach(category -> {
  77. Integer total = finalMap.get(category.getId());
  78. categoryVOList.add(new CategoryVO().setId(category.getId()).setName(category.getName()).setTotal(total));
  79. });
  80. }
  81. return Result.createWithModels(categoryVOList);
  82. }
  83. @Override
  84. public Result getCategoryTagsList(CategoryVO categoryVO) {
  85. Page page = Optional.ofNullable(PageUtil.checkAndInitPage(categoryVO)).orElse(PageUtil.initPage());
  86. IPage<Category> categoryIPage = this.categoryDao.selectListPage(page,categoryVO);
  87. List<Category> categoryList = categoryIPage.getRecords();
  88. List<CategoryVO> categoryVOList = new ArrayList<>();
  89. if (!CollectionUtils.isEmpty(categoryList)) {
  90. categoryList.forEach(category -> {
  91. List<CategoryTags> categoryTags =
  92. categoryTagsDao.selectList(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId, category.getId()));
  93. List<TagsVO> tagsVOList = new ArrayList<>();
  94. if (!CollectionUtils.isEmpty(categoryTags)) {
  95. categoryTags.forEach(categoryTags1 -> {
  96. Tags tags =
  97. Optional.ofNullable(this.tagsDao.selectById(categoryTags1.getTagsId())).orElse(new Tags());
  98. tagsVOList.add(new TagsVO().setName(tags.getName()));
  99. });
  100. }
  101. categoryVOList.add(new CategoryVO().setId(category.getId()).setName(category.getName()).setTagsList(tagsVOList));
  102. });
  103. }
  104. return Result.createWithPaging(categoryVOList, PageUtil.initPageInfo(page));
  105. }
  106. @Override
  107. public Result getCategoryTags(Long id) {
  108. Category category = this.categoryDao.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id));
  109. List<CategoryTags> categoryTags =
  110. categoryTagsDao.selectList(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId,
  111. category.getId()));
  112. List<TagsVO> tagsVOList = new ArrayList<>();
  113. if (!CollectionUtils.isEmpty(categoryTags)) {
  114. categoryTags.forEach(categoryTags1 -> {
  115. Tags tags = this.tagsDao.selectById(categoryTags1.getTagsId());
  116. tagsVOList.add(new TagsVO().setId(tags.getId()).setName(tags.getName()));
  117. });
  118. }
  119. CategoryVO categoryVO =
  120. new CategoryVO().setId(category.getId()).setName(category.getName()).setTagsList(tagsVOList);
  121. return Result.createWithModel(categoryVO);
  122. }
  123. @Override
  124. public Result getCategory(Long id) {
  125. Category category = this.categoryDao.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id));
  126. List<CategoryTags> categoryTags =
  127. categoryTagsDao.selectList(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId,
  128. category.getId()));
  129. List<TagsVO> tagsVOList = new ArrayList<>();
  130. if (!CollectionUtils.isEmpty(categoryTags)) {
  131. categoryTags.forEach(categoryTags1 -> {
  132. Tags tags = this.tagsDao.selectById(categoryTags1.getTagsId());
  133. tagsVOList.add(new TagsVO().setId(tags.getId()).setName(tags.getName()));
  134. });
  135. }
  136. CategoryVO categoryVO =
  137. new CategoryVO().setId(category.getId()).setName(category.getName()).setTagsList(tagsVOList);
  138. return Result.createWithModel(categoryVO);
  139. }
  140. @Override
  141. public Result updateCategory(CategoryVO categoryVO) {
  142. Integer count = this.categoryDao.selectCount(new LambdaQueryWrapper<Category>().eq(Category::getId,
  143. categoryVO.getId()));
  144. if (count.equals(Constants.ZERO)) {
  145. ExceptionUtil.rollback(ErrorEnum.DATA_NO_EXIST);
  146. }
  147. Category category = new Category().setId(categoryVO.getId()).setName(categoryVO.getName()).setUpdateTime(LocalDateTime.now());
  148. this.categoryDao.updateById(category);
  149. List<TagsVO> tagsList = categoryVO.getTagsList();
  150. this.categoryTagsDao.delete(new LambdaUpdateWrapper<CategoryTags>().eq(CategoryTags::getCategoryId, category.getId()));
  151. if (!CollectionUtils.isEmpty(tagsList)) {
  152. tagsList.forEach(tagsVO -> {
  153. if (tagsVO.getId() == null) {
  154. // saveLogs
  155. Tags tags =
  156. new Tags().setName(tagsVO.getName()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now());
  157. this.tagsDao.insert(tags);
  158. tagsVO.setId(tags.getId());
  159. }
  160. categoryTagsDao.insert(new CategoryTags().setCategoryId(category.getId()).setTagsId(tagsVO.getId()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
  161. });
  162. }
  163. return Result.createWithSuccessMessage();
  164. }
  165. @Override
  166. public Result deleteCategory(Long id) {
  167. this.categoryDao.deleteById(id);
  168. this.categoryTagsDao.delete(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId, id));
  169. return Result.createWithSuccessMessage();
  170. }
  171. @Override
  172. public Result statisticsList(CategoryVO categoryVO) {
  173. Page page = Optional.of(PageUtil.checkAndInitPage(categoryVO)).orElse(PageUtil.initPage());
  174. LambdaQueryWrapper<CategoryVO> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
  175. if (StringUtils.isNotBlank(categoryVO.getKeywords())) {
  176. categoryLambdaQueryWrapper.like(CategoryVO::getName, categoryVO.getKeywords());
  177. }
  178. IPage<CategoryVO> categoryVOList = this.categoryDao.selectStatistics(page,categoryLambdaQueryWrapper);
  179. return Result.createWithPaging(categoryVOList.getRecords(), PageUtil.initPageInfo(page));
  180. }
  181. }