课程分类

This commit is contained in:
none
2023-02-24 14:12:51 +08:00
parent 2ca2b0989a
commit 97820eb402
12 changed files with 516 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
package xyz.playedu.api.service;
import xyz.playedu.api.domain.CourseCategory;
import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.api.exception.NotFoundException;
import java.util.List;
/**
* @author tengteng
* @description 针对表【course_categories】的数据库操作Service
* @createDate 2023-02-24 13:55:19
*/
public interface CourseCategoryService extends IService<CourseCategory> {
List<CourseCategory> listByParentId(Integer id);
List<CourseCategory> all();
CourseCategory findOrFail(Integer id) throws NotFoundException;
void deleteById(Integer id) throws NotFoundException;
void update(CourseCategory category, String name, Integer parentId, Integer sort) throws NotFoundException;
}

View File

@@ -0,0 +1,119 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import xyz.playedu.api.bus.CourseCategoryBus;
import xyz.playedu.api.domain.CourseCategory;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.CourseCategoryService;
import xyz.playedu.api.mapper.CourseCategoryMapper;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @author tengteng
* @description 针对表【course_categories】的数据库操作Service实现
* @createDate 2023-02-24 13:55:19
*/
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {
@Override
public List<CourseCategory> listByParentId(Integer id) {
return list(query().getWrapper().eq("parent_id", id).orderByAsc("sort"));
}
@Override
public List<CourseCategory> all() {
return list(query().getWrapper().orderByAsc("sort"));
}
@Override
public CourseCategory findOrFail(Integer id) throws NotFoundException {
CourseCategory category = getById(id);
if (category == null) {
throw new NotFoundException("分类不存在");
}
return category;
}
@Override
@Transactional
public void deleteById(Integer id) throws NotFoundException {
CourseCategory category = findOrFail(id);
//更新parent_chain
updateParentChain(category.getParentChain(), CourseCategoryBus.childrenParentChain(category));
//删除记录
removeById(category.getId());
}
@Override
@Transactional
public void update(CourseCategory category, String name, Integer parentId, Integer sort) throws NotFoundException {
String childrenChainPrefix = CourseCategoryBus.childrenParentChain(category);
CourseCategory data = new CourseCategory();
data.setId(category.getId());
if (!category.getName().equals(name)) {
data.setName(name);
}
if (!category.getParentId().equals(parentId)) {
data.setParentId(parentId);
if (parentId.equals(0)) {
data.setParentChain("");
} else {
CourseCategory parentCourseCategory = findOrFail(parentId);
data.setParentChain(CourseCategoryBus.childrenParentChain(parentCourseCategory));
}
}
if (!category.getSort().equals(sort)) {
data.setSort(sort);
}
//提交更换
updateById(data);
category = getById(category.getId());
updateParentChain(CourseCategoryBus.childrenParentChain(category), childrenChainPrefix);
}
private void updateParentChain(String newChildrenPC, String oldChildrenPC) {
List<CourseCategory> children = list(query().getWrapper().like("parent_chain", oldChildrenPC + "%"));
if (children.size() == 0) {
return;
}
ArrayList<CourseCategory> updateRows = new ArrayList<>();
for (CourseCategory tmpCourseCategory : children) {
CourseCategory tmpUpdateCourseCategory = new CourseCategory();
tmpUpdateCourseCategory.setId(tmpCourseCategory.getId());
// parentChain计算
String pc = newChildrenPC;
if (!tmpCourseCategory.getParentChain().equals(oldChildrenPC)) {
pc = tmpCourseCategory.getParentChain().replaceFirst(oldChildrenPC + ",", newChildrenPC.length() == 0 ? newChildrenPC : newChildrenPC + ',');
}
tmpUpdateCourseCategory.setParentChain(pc);
// parentId计算
int parentId = 0;
if (pc != null && pc.length() > 0) {
String[] parentIds = pc.split(",");
parentId = Integer.parseInt(parentIds[parentIds.length - 1]);
}
tmpUpdateCourseCategory.setParentId(parentId);
updateRows.add(tmpUpdateCourseCategory);
}
updateBatchById(updateRows);
}
}