package com.mzl.flower.service.film; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mzl.flower.base.Node; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.dto.request.film.FilmCategoryCreateDTO; import com.mzl.flower.dto.request.film.FilmCategoryQueryDTO; import com.mzl.flower.dto.request.film.FilmCategoryUpdateDTO; import com.mzl.flower.dto.response.film.FilmCategoryDTO; import com.mzl.flower.dto.response.film.FilmCategoryTreeDTO; import com.mzl.flower.entity.film.FilmCategory; import com.mzl.flower.mapper.film.FilmCategoryMapper; import com.mzl.flower.service.BaseService; import com.mzl.flower.utils.TreeBuilderUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Slf4j @Service @Transactional public class FilmCategoryService extends BaseService { @Autowired private FilmCategoryMapper categoryMapper; public Long addCategory(FilmCategoryCreateDTO dto) { FilmCategory category = new FilmCategory(); BeanUtils.copyProperties(dto, category); category.setSortBy(dto.getSortBy() == null ? 0 : dto.getSortBy()); category.create(SecurityUtils.getUserId()); if (category.getParentId() != null) { FilmCategory parent = categoryMapper.selectById(category.getParentId()); if (parent == null) { throw new ValidationException("父分类不存在"); } category.setParentName(parent.getName()); } categoryMapper.insert(category); return category.getId(); } public Long updateCategory(FilmCategoryUpdateDTO dto) { Long id = dto.getId(); FilmCategory category = categoryMapper.selectById(id); if (category == null) { throw new ValidationException("分类不存在"); } Boolean shown = category.getShown(); BeanUtils.copyProperties(dto, category); category.setShown(shown); category.setSortBy(dto.getSortBy() == null ? 0 : dto.getSortBy()); category.update(SecurityUtils.getUserId()); category.setParentName(""); if (category.getParentId() != null) { FilmCategory parent = categoryMapper.selectById(category.getParentId()); if (parent == null) { throw new ValidationException("父分类不存在"); } category.setParentName(parent.getName()); } categoryMapper.updateById(category); return id; } public FilmCategoryDTO getCategory(String id) { FilmCategoryDTO dto = new FilmCategoryDTO(); FilmCategory category = categoryMapper.selectById(id); if (category == null) { return null; } BeanUtils.copyProperties(category, dto); return dto; } public List getCategoryList(FilmCategoryQueryDTO dto) { return categoryMapper.selectCategoryList(dto); } public List getCategoryTree(FilmCategoryQueryDTO dto) { List treeList = categoryMapper.selectTreeList(dto); treeList = (List) TreeBuilderUtil.buildListToTree(treeList); if (treeList != null && treeList.size() > 0) { for (FilmCategoryTreeDTO t : treeList) { List children = t.getChildren(); String levelLimit = t.getLevelLimit(); if (children != null && children.size() > 0) { for (Node c : children) { FilmCategoryTreeDTO child = (FilmCategoryTreeDTO) c; child.setLevelLimit(levelLimit); } } } } return treeList; } public void deleteCategory(Long id) { // 检查是否有子分类 int count = categoryMapper.selectCount(new QueryWrapper() .eq("parent_id", id) .eq("deleted", 0)); if (count > 0) { throw new ValidationException("该分类包含子分类,请先删除子分类!"); } categoryMapper.deleteById(id); } public void setCategoryShown(Long id, Boolean shown) { FilmCategory category = categoryMapper.selectById(id); if (category == null) { throw new ValidationException("分类不存在"); } category.setShown(shown); categoryMapper.updateById(category); } }