mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-18 00:46:57 +08:00
优化courseCategory
This commit is contained in:
parent
138e1f5b98
commit
ba101e508b
@ -1,40 +0,0 @@
|
|||||||
package xyz.playedu.api.bus;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import xyz.playedu.api.domain.CourseCategory;
|
|
||||||
import xyz.playedu.api.exception.NotFoundException;
|
|
||||||
import xyz.playedu.api.service.CourseCategoryService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author 杭州白书科技有限公司
|
|
||||||
* @create 2023/2/24 13:57
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class CourseCategoryBus {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CourseCategoryService categoryService;
|
|
||||||
|
|
||||||
public String compParentChain(Integer parentId) throws NotFoundException {
|
|
||||||
String parentChain = "";
|
|
||||||
if (parentId != 0) {
|
|
||||||
CourseCategory parentCourseCategory = categoryService.getById(parentId);
|
|
||||||
if (parentCourseCategory == null) {
|
|
||||||
throw new NotFoundException("父级分类不存在");
|
|
||||||
}
|
|
||||||
String pc = parentCourseCategory.getParentChain();
|
|
||||||
parentChain = pc == null || pc.length() == 0 ? parentId + "" : pc + "," + parentId;
|
|
||||||
}
|
|
||||||
return parentChain;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String childrenParentChain(CourseCategory CourseCategory) {
|
|
||||||
String prefix = CourseCategory.getId() + "";
|
|
||||||
if (CourseCategory.getParentChain() != null && CourseCategory.getParentChain().length() > 0) {
|
|
||||||
prefix = CourseCategory.getParentChain() + "," + prefix;
|
|
||||||
}
|
|
||||||
return prefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -4,7 +4,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import xyz.playedu.api.bus.CourseCategoryBus;
|
|
||||||
import xyz.playedu.api.constant.BPermissionConstant;
|
import xyz.playedu.api.constant.BPermissionConstant;
|
||||||
import xyz.playedu.api.domain.CourseCategory;
|
import xyz.playedu.api.domain.CourseCategory;
|
||||||
import xyz.playedu.api.event.CourseCategoryDestroyEvent;
|
import xyz.playedu.api.event.CourseCategoryDestroyEvent;
|
||||||
|
@ -25,4 +25,8 @@ public interface CourseCategoryService extends IService<CourseCategory> {
|
|||||||
|
|
||||||
void create(String name, Integer parentId, Integer sort) throws NotFoundException;
|
void create(String name, Integer parentId, Integer sort) throws NotFoundException;
|
||||||
|
|
||||||
|
String childrenParentChain(CourseCategory category);
|
||||||
|
|
||||||
|
String compParentChain(Integer parentId) throws NotFoundException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package xyz.playedu.api.service.impl;
|
|||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import xyz.playedu.api.bus.CourseCategoryBus;
|
|
||||||
import xyz.playedu.api.domain.CourseCategory;
|
import xyz.playedu.api.domain.CourseCategory;
|
||||||
import xyz.playedu.api.exception.NotFoundException;
|
import xyz.playedu.api.exception.NotFoundException;
|
||||||
import xyz.playedu.api.service.CourseCategoryService;
|
import xyz.playedu.api.service.CourseCategoryService;
|
||||||
@ -22,9 +21,6 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {
|
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CourseCategoryBus categoryBus;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CourseCategory> listByParentId(Integer id) {
|
public List<CourseCategory> listByParentId(Integer id) {
|
||||||
return list(query().getWrapper().eq("parent_id", id).orderByAsc("sort"));
|
return list(query().getWrapper().eq("parent_id", id).orderByAsc("sort"));
|
||||||
@ -49,7 +45,7 @@ public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper,
|
|||||||
public void deleteById(Integer id) throws NotFoundException {
|
public void deleteById(Integer id) throws NotFoundException {
|
||||||
CourseCategory category = findOrFail(id);
|
CourseCategory category = findOrFail(id);
|
||||||
//更新parent_chain
|
//更新parent_chain
|
||||||
updateParentChain(category.getParentChain(), CourseCategoryBus.childrenParentChain(category));
|
updateParentChain(category.getParentChain(), childrenParentChain(category));
|
||||||
//删除记录
|
//删除记录
|
||||||
removeById(category.getId());
|
removeById(category.getId());
|
||||||
}
|
}
|
||||||
@ -57,7 +53,7 @@ public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper,
|
|||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void update(CourseCategory category, String name, Integer parentId, Integer sort) throws NotFoundException {
|
public void update(CourseCategory category, String name, Integer parentId, Integer sort) throws NotFoundException {
|
||||||
String childrenChainPrefix = CourseCategoryBus.childrenParentChain(category);
|
String childrenChainPrefix = childrenParentChain(category);
|
||||||
|
|
||||||
CourseCategory data = new CourseCategory();
|
CourseCategory data = new CourseCategory();
|
||||||
data.setId(category.getId());
|
data.setId(category.getId());
|
||||||
@ -72,7 +68,7 @@ public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper,
|
|||||||
data.setParentChain("");
|
data.setParentChain("");
|
||||||
} else {
|
} else {
|
||||||
CourseCategory parentCourseCategory = findOrFail(parentId);
|
CourseCategory parentCourseCategory = findOrFail(parentId);
|
||||||
data.setParentChain(CourseCategoryBus.childrenParentChain(parentCourseCategory));
|
data.setParentChain(childrenParentChain(parentCourseCategory));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!category.getSort().equals(sort)) {
|
if (!category.getSort().equals(sort)) {
|
||||||
@ -83,7 +79,7 @@ public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper,
|
|||||||
updateById(data);
|
updateById(data);
|
||||||
|
|
||||||
category = getById(category.getId());
|
category = getById(category.getId());
|
||||||
updateParentChain(CourseCategoryBus.childrenParentChain(category), childrenChainPrefix);
|
updateParentChain(childrenParentChain(category), childrenChainPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateParentChain(String newChildrenPC, String oldChildrenPC) {
|
private void updateParentChain(String newChildrenPC, String oldChildrenPC) {
|
||||||
@ -122,7 +118,7 @@ public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper,
|
|||||||
public void create(String name, Integer parentId, Integer sort) throws NotFoundException {
|
public void create(String name, Integer parentId, Integer sort) throws NotFoundException {
|
||||||
String parentChain = "";
|
String parentChain = "";
|
||||||
if (parentId != 0) {
|
if (parentId != 0) {
|
||||||
parentChain = categoryBus.compParentChain(parentId);
|
parentChain = compParentChain(parentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
CourseCategory category = new CourseCategory();
|
CourseCategory category = new CourseCategory();
|
||||||
@ -135,6 +131,29 @@ public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper,
|
|||||||
|
|
||||||
save(category);
|
save(category);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String childrenParentChain(CourseCategory category) {
|
||||||
|
String prefix = category.getId() + "";
|
||||||
|
if (category.getParentChain() != null && category.getParentChain().length() > 0) {
|
||||||
|
prefix = category.getParentChain() + "," + prefix;
|
||||||
|
}
|
||||||
|
return prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String compParentChain(Integer parentId) throws NotFoundException {
|
||||||
|
String parentChain = "";
|
||||||
|
if (parentId != 0) {
|
||||||
|
CourseCategory parentCourseCategory = getById(parentId);
|
||||||
|
if (parentCourseCategory == null) {
|
||||||
|
throw new NotFoundException("父级分类不存在");
|
||||||
|
}
|
||||||
|
String pc = parentCourseCategory.getParentChain();
|
||||||
|
parentChain = pc == null || pc.length() == 0 ? parentId + "" : pc + "," + parentId;
|
||||||
|
}
|
||||||
|
return parentChain;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user