cloudroam
2025-06-06 70f642c9a5090f051814d46c8f5bbce5e366ba89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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<FilmCategoryTreeDTO> getCategoryList(FilmCategoryQueryDTO dto) {
        return categoryMapper.selectCategoryList(dto);
    }
 
    public List<FilmCategoryTreeDTO> getCategoryTree(FilmCategoryQueryDTO dto) {
        List<FilmCategoryTreeDTO> treeList = categoryMapper.selectTreeList(dto);
        treeList = (List<FilmCategoryTreeDTO>) TreeBuilderUtil.buildListToTree(treeList);
        if (treeList != null && treeList.size() > 0) {
            for (FilmCategoryTreeDTO t : treeList) {
                List<Node> 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<FilmCategory>()
                .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);
    }
 
    public List<FilmCategory> getParentCategoryList() {
        return categoryMapper.getParentCategoryList();
    }
}