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.SysCategoryMapper;
|
import com.gkhy.exam.system.service.SysCategoryService;
|
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 {
|
|
@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=baseMapper.selectCountOfCoure(categoryId);
|
if(courseCount>0){
|
throw new ApiException("已绑定课程,无法删除");
|
}
|
int bankCount=baseMapper.selectCountOfBank(categoryId);
|
if(bankCount>0){
|
throw new ApiException("已绑定题库,无法删除");
|
}
|
int row=baseMapper.deleteById(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;
|
}
|
}
|