“djh”
3 天以前 9bc1958825de5c9427659a8824a9e86864c2a457
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
package com.gkhy.exam.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.enums.UserTypeEnum;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.SysCategory;
import com.gkhy.exam.system.mapper.ExCourseMapper;
import com.gkhy.exam.system.mapper.ExQuestionBankMapper;
import com.gkhy.exam.system.mapper.SysCategoryMapper;
import com.gkhy.exam.system.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 课程分类表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-05 11:15:14
 */
@Service
public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory> implements SysCategoryService {
    @Autowired
    private ExCourseMapper courseMapper;
    @Autowired
    private ExQuestionBankMapper questionBankMapper;
 
    @Override
    public List<SysCategory> selectCategoryList(SysCategory category) {
        List<SysCategory> categoryList=baseMapper.selectCategoryList(category);
        return getChildCategory(categoryList,0L);
    }
 
    public List<SysCategory> getChildCategory(List<SysCategory> categoryList, Long parentId){
        List<SysCategory> childList=categoryList.stream().filter(m -> Objects.equals(m.getParentId(),parentId)).map(m -> {
                    m.setChildren(getChildCategory(categoryList, m.getId()));
                    return m;
                }).sorted(Comparator.comparingInt(m -> (m.getSort()==null?0:m.getSort())))
                .collect(Collectors.toList());
        return childList;
    }
 
 
    @Override
    public SysCategory selectCategoryById(Long categoryId) {
        return baseMapper.selectById(categoryId);
    }
 
    @Override
    public int insertCategory(SysCategory category) {
        if(!checkNameUnique(category)){
            throw new ApiException("新增课程分类名称已存在");
        }
        checkUserAllowed();
        int row =baseMapper.insert(category);
        if(row<1){
            throw new ApiException("新增课程分类失败");
        }
        return row;
    }
 
    @Override
    public int updateCategory(SysCategory category) {
        checkUserAllowed();
        int row=baseMapper.updateById(category);
        if(row<1){
            throw new ApiException("修改课程分类失败");
        }
        return row;
    }
 
    public void checkUserAllowed() {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("没有权限操作");
        }
    }
 
    @Override
    public int deleteCategoryById(Long categoryId) {
        //校验课程分类是否存在课程或者题目
        checkUserAllowed();
        int courseCount=courseMapper.selectCountByCategoryId(categoryId);
        if(courseCount>0){
            throw new ApiException("已绑定课程,无法删除");
        }
        int bankCount=questionBankMapper.selectCountByCategoryId(categoryId);
        if(bankCount>0){
            throw new ApiException("已绑定题库,无法删除");
        }
        int row=baseMapper.deleteByCategoryId(categoryId);
        if(row<1){
            throw new ApiException("删除课程分类失败");
        }
        return row;
    }
 
    @Override
    public boolean checkNameUnique(SysCategory category) {
        if(category.getParentId()==null){
            category.setParentId(0L);
        }
        Long categoryId=category.getId()==null?-1L:category.getId();
        SysCategory ca= baseMapper.checkNameUnique(category.getName(),category.getParentId());
        if(ca!=null&&ca.getId().longValue()!=categoryId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
}