123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package com.nosum.deliver.category.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.nosum.common.base.domain.Result;
- import com.nosum.common.constant.Constants;
- import com.nosum.common.enums.ErrorEnum;
- import com.nosum.common.util.ExceptionUtil;
- import com.nosum.common.util.PageUtil;
- import com.nosum.deliver.category.dao.CategoryDao;
- import com.nosum.deliver.category.dao.CategoryTagsDao;
- import com.nosum.deliver.category.dao.TagsDao;
- import com.nosum.deliver.category.domain.po.Category;
- import com.nosum.deliver.category.domain.po.CategoryTags;
- import com.nosum.deliver.category.domain.po.Tags;
- import com.nosum.deliver.category.domain.vo.CategoryVO;
- import com.nosum.deliver.category.domain.vo.TagsVO;
- import com.nosum.deliver.category.service.CategoryService;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author sumbytes
- * @since 2019-08-28
- */
- @Service
- public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
- @Autowired
- private CategoryDao categoryDao;
- @Autowired
- private TagsDao tagsDao;
- @Autowired
- private CategoryTagsDao categoryTagsDao;
- @Override
- public Result saveCategory(CategoryVO categoryVO) {
- Category category = new Category().setName(categoryVO.getName()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now());
- this.categoryDao.insert(category);
- List<TagsVO> tagsList = categoryVO.getTagsList();
- if (!CollectionUtils.isEmpty(tagsList)) {
- tagsList.forEach(tagsVO -> {
- if (tagsVO.getId() == null) {
- Tags tags = new Tags().setName(tagsVO.getName()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now());
- this.tagsDao.insert(tags);
- tagsVO.setId(tags.getId());
- }
- categoryTagsDao.insert(new CategoryTags().setCategoryId(category.getId()).setTagsId(tagsVO.getId()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
- });
- }
- return Result.createWithSuccessMessage();
- }
- @Override
- public Result getCategoryList(CategoryVO categoryVO) {
- List<Category> categoryList = this.categoryDao.selectList(new LambdaQueryWrapper<Category>().orderByDesc(Category::getCreateTime));
- List<CategoryVO> categoryPostsTotal = this.categoryDao.selectCategoryPostsTotal();
- Map<Long, Integer> map = new HashMap<>();
- if (!CollectionUtils.isEmpty(categoryPostsTotal)) {
- map = categoryPostsTotal.stream().collect(Collectors.toMap(CategoryVO::getId, CategoryVO::getTotal, Integer::sum));
- }
- List<CategoryVO> categoryVOList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(categoryList)) {
- Map<Long, Integer> finalMap = map;
- categoryList.forEach(category -> {
- Integer total = finalMap.get(category.getId());
- categoryVOList.add(new CategoryVO().setId(category.getId()).setName(category.getName()).setTotal(total));
- });
- }
- return Result.createWithModels(categoryVOList);
- }
- @Override
- public Result getCategoryTagsList(CategoryVO categoryVO) {
- Page page = Optional.ofNullable(PageUtil.checkAndInitPage(categoryVO)).orElse(PageUtil.initPage());
- IPage<Category> categoryIPage = this.categoryDao.selectListPage(page,categoryVO);
- List<Category> categoryList = categoryIPage.getRecords();
- List<CategoryVO> categoryVOList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(categoryList)) {
- categoryList.forEach(category -> {
- List<CategoryTags> categoryTags =
- categoryTagsDao.selectList(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId, category.getId()));
- List<TagsVO> tagsVOList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(categoryTags)) {
- categoryTags.forEach(categoryTags1 -> {
- Tags tags =
- Optional.ofNullable(this.tagsDao.selectById(categoryTags1.getTagsId())).orElse(new Tags());
- tagsVOList.add(new TagsVO().setName(tags.getName()));
- });
- }
- categoryVOList.add(new CategoryVO().setId(category.getId()).setName(category.getName()).setTagsList(tagsVOList));
- });
- }
- return Result.createWithPaging(categoryVOList, PageUtil.initPageInfo(page));
- }
- @Override
- public Result getCategoryTags(Long id) {
- Category category = this.categoryDao.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id));
- List<CategoryTags> categoryTags =
- categoryTagsDao.selectList(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId,
- category.getId()));
- List<TagsVO> tagsVOList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(categoryTags)) {
- categoryTags.forEach(categoryTags1 -> {
- Tags tags = this.tagsDao.selectById(categoryTags1.getTagsId());
- tagsVOList.add(new TagsVO().setId(tags.getId()).setName(tags.getName()));
- });
- }
- CategoryVO categoryVO =
- new CategoryVO().setId(category.getId()).setName(category.getName()).setTagsList(tagsVOList);
- return Result.createWithModel(categoryVO);
- }
- @Override
- public Result getCategory(Long id) {
- Category category = this.categoryDao.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id));
- List<CategoryTags> categoryTags =
- categoryTagsDao.selectList(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId,
- category.getId()));
- List<TagsVO> tagsVOList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(categoryTags)) {
- categoryTags.forEach(categoryTags1 -> {
- Tags tags = this.tagsDao.selectById(categoryTags1.getTagsId());
- tagsVOList.add(new TagsVO().setId(tags.getId()).setName(tags.getName()));
- });
- }
- CategoryVO categoryVO =
- new CategoryVO().setId(category.getId()).setName(category.getName()).setTagsList(tagsVOList);
- return Result.createWithModel(categoryVO);
- }
- @Override
- public Result updateCategory(CategoryVO categoryVO) {
- Integer count = this.categoryDao.selectCount(new LambdaQueryWrapper<Category>().eq(Category::getId,
- categoryVO.getId()));
- if (count.equals(Constants.ZERO)) {
- ExceptionUtil.rollback(ErrorEnum.DATA_NO_EXIST);
- }
- Category category = new Category().setId(categoryVO.getId()).setName(categoryVO.getName()).setUpdateTime(LocalDateTime.now());
- this.categoryDao.updateById(category);
- List<TagsVO> tagsList = categoryVO.getTagsList();
- this.categoryTagsDao.delete(new LambdaUpdateWrapper<CategoryTags>().eq(CategoryTags::getCategoryId, category.getId()));
- if (!CollectionUtils.isEmpty(tagsList)) {
- tagsList.forEach(tagsVO -> {
- if (tagsVO.getId() == null) {
- // saveLogs
- Tags tags =
- new Tags().setName(tagsVO.getName()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now());
- this.tagsDao.insert(tags);
- tagsVO.setId(tags.getId());
- }
- categoryTagsDao.insert(new CategoryTags().setCategoryId(category.getId()).setTagsId(tagsVO.getId()).setCreateTime(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
- });
- }
- return Result.createWithSuccessMessage();
- }
- @Override
- public Result deleteCategory(Long id) {
- this.categoryDao.deleteById(id);
- this.categoryTagsDao.delete(new LambdaQueryWrapper<CategoryTags>().eq(CategoryTags::getCategoryId, id));
- return Result.createWithSuccessMessage();
- }
- @Override
- public Result statisticsList(CategoryVO categoryVO) {
- Page page = Optional.of(PageUtil.checkAndInitPage(categoryVO)).orElse(PageUtil.initPage());
- LambdaQueryWrapper<CategoryVO> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
- if (StringUtils.isNotBlank(categoryVO.getKeywords())) {
- categoryLambdaQueryWrapper.like(CategoryVO::getName, categoryVO.getKeywords());
- }
- IPage<CategoryVO> categoryVOList = this.categoryDao.selectStatistics(page,categoryLambdaQueryWrapper);
- return Result.createWithPaging(categoryVOList.getRecords(), PageUtil.initPageInfo(page));
- }
- }
|