package com.aurora.service.impl; import com.aurora.model.dto.CategoryAdminDTO; import com.aurora.model.dto.CategoryDTO; import com.aurora.model.dto.CategoryOptionDTO; import com.aurora.entity.Article; import com.aurora.entity.Category; import com.aurora.exception.BizException; import com.aurora.mapper.ArticleMapper; import com.aurora.mapper.CategoryMapper; import com.aurora.service.CategoryService; import com.aurora.util.BeanCopyUtil; import com.aurora.util.PageUtil; import com.aurora.model.vo.CategoryVO; import com.aurora.model.vo.ConditionVO; import com.aurora.model.dto.PageResultDTO; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Objects; @Service public class CategoryServiceImpl extends ServiceImpl implements CategoryService { @Autowired private CategoryMapper categoryMapper; @Autowired private ArticleMapper articleMapper; @Override public List listCategories() { return categoryMapper.listCategories(); } @SneakyThrows @Override public PageResultDTO listCategoriesAdmin(ConditionVO conditionVO) { Integer count = categoryMapper.selectCount(new LambdaQueryWrapper() .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Category::getCategoryName, conditionVO.getKeywords())); if (count == 0) { return new PageResultDTO<>(); } List categoryList = categoryMapper.listCategoriesAdmin(PageUtil.getLimitCurrent(), PageUtil.getSize(), conditionVO); return new PageResultDTO<>(categoryList, count); } @SneakyThrows @Override public List listCategoriesBySearch(ConditionVO conditionVO) { List categoryList = categoryMapper.selectList(new LambdaQueryWrapper() .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Category::getCategoryName, conditionVO.getKeywords()) .orderByDesc(Category::getId)); return BeanCopyUtil.copyList(categoryList, CategoryOptionDTO.class); } @Override public void deleteCategories(List categoryIds) { Integer count = articleMapper.selectCount(new LambdaQueryWrapper
() .in(Article::getCategoryId, categoryIds)); if (count > 0) { throw new BizException("删除失败,该分类下存在文章"); } categoryMapper.deleteBatchIds(categoryIds); } @Override public void saveOrUpdateCategory(CategoryVO categoryVO) { Category existCategory = categoryMapper.selectOne(new LambdaQueryWrapper() .select(Category::getId) .eq(Category::getCategoryName, categoryVO.getCategoryName())); if (Objects.nonNull(existCategory) && !existCategory.getId().equals(categoryVO.getId())) { throw new BizException("分类名已存在"); } Category category = Category.builder() .id(categoryVO.getId()) .categoryName(categoryVO.getCategoryName()) .build(); this.saveOrUpdate(category); } }