mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-12-22 18:29:51 +08:00
删除courseCategory该用resource_category
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import xyz.playedu.api.domain.CategoryCourse;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【category_course】的数据库操作Service
|
||||
* @createDate 2023-02-24 14:48:26
|
||||
*/
|
||||
public interface CategoryCourseService extends IService<CategoryCourse> {
|
||||
List<Integer> getCourseIdsByCategoryIds(Integer[] categoryIds);
|
||||
|
||||
List<Integer> getCategoryIdsByCourseId(Integer id);
|
||||
|
||||
void removeByCourseId(Integer courseId);
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
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;
|
||||
|
||||
void create(String name, Integer parentId, Integer sort) throws NotFoundException;
|
||||
|
||||
String childrenParentChain(CourseCategory category);
|
||||
|
||||
String compParentChain(Integer parentId) throws NotFoundException;
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import xyz.playedu.api.domain.CategoryCourse;
|
||||
import xyz.playedu.api.service.CategoryCourseService;
|
||||
import xyz.playedu.api.mapper.CategoryCourseMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【category_course】的数据库操作Service实现
|
||||
* @createDate 2023-02-24 14:48:26
|
||||
*/
|
||||
@Service
|
||||
public class CategoryCourseServiceImpl extends ServiceImpl<CategoryCourseMapper, CategoryCourse>
|
||||
implements CategoryCourseService {
|
||||
@Override
|
||||
public List<Integer> getCourseIdsByCategoryIds(Integer[] categoryIds) {
|
||||
List<Integer> ids = new ArrayList<>();
|
||||
List<CategoryCourse> categoryCourses = list(query().getWrapper().in("category_id", categoryIds));
|
||||
if (categoryCourses.size() == 0) {
|
||||
return ids;
|
||||
}
|
||||
for (CategoryCourse categoryCourse : categoryCourses) {
|
||||
ids.add(categoryCourse.getCourseId());
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getCategoryIdsByCourseId(Integer id) {
|
||||
List<Integer> ids = new ArrayList<>();
|
||||
List<CategoryCourse> categoryCourses = list(query().getWrapper().eq("course_id", id));
|
||||
if (categoryCourses.size() == 0) {
|
||||
return ids;
|
||||
}
|
||||
for (CategoryCourse categoryCourse : categoryCourses) {
|
||||
ids.add(categoryCourse.getCategoryId());
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByCourseId(Integer courseId) {
|
||||
remove(query().getWrapper().eq("course_id", courseId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
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.Date;
|
||||
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(), childrenParentChain(category));
|
||||
//删除记录
|
||||
removeById(category.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void update(CourseCategory category, String name, Integer parentId, Integer sort) throws NotFoundException {
|
||||
String childrenChainPrefix = 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(childrenParentChain(parentCourseCategory));
|
||||
}
|
||||
}
|
||||
if (!category.getSort().equals(sort)) {
|
||||
data.setSort(sort);
|
||||
}
|
||||
|
||||
//提交更换
|
||||
updateById(data);
|
||||
|
||||
category = getById(category.getId());
|
||||
updateParentChain(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);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void create(String name, Integer parentId, Integer sort) throws NotFoundException {
|
||||
String parentChain = "";
|
||||
if (parentId != 0) {
|
||||
parentChain = compParentChain(parentId);
|
||||
}
|
||||
|
||||
CourseCategory category = new CourseCategory();
|
||||
category.setName(name);
|
||||
category.setParentId(parentId);
|
||||
category.setParentChain(parentChain);
|
||||
category.setSort(sort);
|
||||
category.setCreatedAt(new Date());
|
||||
category.setUpdatedAt(new Date());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -6,15 +6,15 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import xyz.playedu.api.domain.CategoryCourse;
|
||||
import xyz.playedu.api.domain.ResourceCourseCategory;
|
||||
import xyz.playedu.api.domain.Course;
|
||||
import xyz.playedu.api.domain.CourseDepartment;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
import xyz.playedu.api.service.CategoryCourseService;
|
||||
import xyz.playedu.api.service.CourseDepartmentService;
|
||||
import xyz.playedu.api.service.CourseService;
|
||||
import xyz.playedu.api.mapper.CourseMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xyz.playedu.api.service.internal.ResourceCourseCategoryService;
|
||||
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
@@ -35,7 +35,7 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
||||
private CourseDepartmentService courseDepartmentService;
|
||||
|
||||
@Autowired
|
||||
private CategoryCourseService categoryCourseService;
|
||||
private ResourceCourseCategoryService courseCategoryService;
|
||||
|
||||
@Override
|
||||
public PaginationResult<Course> paginate(int page, int size, CoursePaginateFiler filter) {
|
||||
@@ -46,19 +46,17 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
||||
}
|
||||
if (filter.getDepIds() != null && filter.getDepIds().length > 0) {
|
||||
List<Integer> courseIds = courseDepartmentService.getCourseIdsByDepIds(filter.getDepIds());
|
||||
if (courseIds.size() == 0) {
|
||||
wrapper.in("id", HelperUtil.zeroIntegerList());
|
||||
} else {
|
||||
wrapper.in("id", courseIds);
|
||||
if (courseIds == null || courseIds.size() == 0) {
|
||||
courseIds = HelperUtil.zeroIntegerList();
|
||||
}
|
||||
wrapper.in("id", courseIds);
|
||||
}
|
||||
if (filter.getCategoryIds() != null && filter.getCategoryIds().length > 0) {
|
||||
List<Integer> courseIds = categoryCourseService.getCourseIdsByCategoryIds(filter.getCategoryIds());
|
||||
if (courseIds.size() == 0) {
|
||||
wrapper.in("id", HelperUtil.zeroIntegerList());
|
||||
} else {
|
||||
wrapper.in("id", courseIds);
|
||||
List<Integer> courseIds = courseCategoryService.getCourseIdsByCategoryIds(List.of(filter.getCategoryIds()));
|
||||
if (courseIds == null || courseIds.size() == 0) {
|
||||
courseIds = HelperUtil.zeroIntegerList();
|
||||
}
|
||||
wrapper.in("id", courseIds);
|
||||
}
|
||||
|
||||
String sortFiled = filter.getSortField();
|
||||
@@ -88,16 +86,17 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
||||
@Override
|
||||
@Transactional
|
||||
public void createWithCategoryIdsAndDepIds(String title, String thumb, Integer isShow, Integer[] categoryIds, Integer[] depIds) {
|
||||
// 创建课程
|
||||
Course course = new Course();
|
||||
course.setTitle(title);
|
||||
course.setThumb(thumb);
|
||||
course.setIsShow(isShow);
|
||||
course.setCreatedAt(new Date());
|
||||
course.setUpdatedAt(new Date());
|
||||
|
||||
save(course);
|
||||
|
||||
// 关联分类
|
||||
relateCategories(course, categoryIds);
|
||||
// 关联部门
|
||||
relateDepartments(course, depIds);
|
||||
}
|
||||
|
||||
@@ -108,10 +107,11 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
||||
}
|
||||
List<CourseDepartment> courseDepartments = new ArrayList<>();
|
||||
for (int i = 0; i < depIds.length; i++) {
|
||||
CourseDepartment courseDepartment = new CourseDepartment();
|
||||
courseDepartment.setCourseId(course.getId());
|
||||
courseDepartment.setDepId(depIds[i]);
|
||||
courseDepartments.add(courseDepartment);
|
||||
Integer tmpDepId = depIds[i];
|
||||
courseDepartments.add(new CourseDepartment() {{
|
||||
setCourseId(course.getId());
|
||||
setDepId(tmpDepId);
|
||||
}});
|
||||
}
|
||||
courseDepartmentService.saveBatch(courseDepartments);
|
||||
}
|
||||
@@ -127,19 +127,20 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
||||
if (categoryIds == null || categoryIds.length == 0) {
|
||||
return;
|
||||
}
|
||||
List<CategoryCourse> categoryCourses = new ArrayList<>();
|
||||
List<ResourceCourseCategory> resourceCourseCategories = new ArrayList<>();
|
||||
for (int i = 0; i < categoryIds.length; i++) {
|
||||
CategoryCourse categoryCourse = new CategoryCourse();
|
||||
categoryCourse.setCourseId(course.getId());
|
||||
categoryCourse.setCategoryId(categoryIds[i]);
|
||||
categoryCourses.add(categoryCourse);
|
||||
Integer tmpCategoryId = categoryIds[i];
|
||||
resourceCourseCategories.add(new ResourceCourseCategory() {{
|
||||
setCategoryId(tmpCategoryId);
|
||||
setCourseId(course.getId());
|
||||
}});
|
||||
}
|
||||
categoryCourseService.saveBatch(categoryCourses);
|
||||
courseCategoryService.saveBatch(resourceCourseCategories);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetRelateCategories(Course course, Integer[] categoryIds) {
|
||||
categoryCourseService.removeByCourseId(course.getId());
|
||||
courseCategoryService.removeByCourseId(course.getId());
|
||||
relateCategories(course, categoryIds);
|
||||
}
|
||||
|
||||
@@ -181,7 +182,7 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
||||
|
||||
@Override
|
||||
public List<Integer> getCategoryIdsByCourseId(Integer courseId) {
|
||||
return categoryCourseService.getCategoryIdsByCourseId(courseId);
|
||||
return courseCategoryService.getCategoryIdsByCourseId(courseId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -194,7 +195,7 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
||||
|
||||
@Override
|
||||
public void removeCategoryIdRelate(Integer categoryId) {
|
||||
categoryCourseService.remove(categoryCourseService.query().getWrapper().eq("category_id", categoryId));
|
||||
courseCategoryService.removeByCategoryId(categoryId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package xyz.playedu.api.service.impl.internal;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import xyz.playedu.api.domain.ResourceCourseCategory;
|
||||
import xyz.playedu.api.service.internal.ResourceCourseCategoryService;
|
||||
import xyz.playedu.api.mapper.ResourceCourseCategoryMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【resource_course_category】的数据库操作Service实现
|
||||
* @createDate 2023-03-09 09:54:22
|
||||
*/
|
||||
@Service
|
||||
public class ResourceCourseCategoryServiceImpl extends ServiceImpl<ResourceCourseCategoryMapper, ResourceCourseCategory>
|
||||
implements ResourceCourseCategoryService {
|
||||
|
||||
@Override
|
||||
public List<Integer> getCourseIdsByCategoryIds(List<Integer> categoryIds) {
|
||||
return list(query().getWrapper().eq("id", "category_id")).stream().map(ResourceCourseCategory::getCourseId).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByCourseId(Integer id) {
|
||||
remove(query().getWrapper().eq("course_id", id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByCategoryId(Integer id) {
|
||||
remove(query().getWrapper().eq("category_id", id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getCategoryIdsByCourseId(Integer courseId) {
|
||||
return list(query().getWrapper().eq("course_id", courseId)).stream().map(ResourceCourseCategory::getCategoryId).toList();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package xyz.playedu.api.service.internal;
|
||||
|
||||
import xyz.playedu.api.domain.ResourceCourseCategory;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【resource_course_category】的数据库操作Service
|
||||
* @createDate 2023-03-09 09:54:22
|
||||
*/
|
||||
public interface ResourceCourseCategoryService extends IService<ResourceCourseCategory> {
|
||||
|
||||
List<Integer> getCourseIdsByCategoryIds(List<Integer> categoryIds);
|
||||
|
||||
void removeByCourseId(Integer id);
|
||||
|
||||
void removeByCategoryId(Integer id);
|
||||
|
||||
List<Integer> getCategoryIdsByCourseId(Integer courseId);
|
||||
}
|
||||
Reference in New Issue
Block a user