课程代码优化

This commit is contained in:
none
2023-02-25 11:03:50 +08:00
parent ba101e508b
commit bfa1040a54
6 changed files with 141 additions and 128 deletions

View File

@@ -13,7 +13,7 @@ import java.util.List;
public interface CategoryCourseService extends IService<CategoryCourse> {
List<Integer> getCourseIdsByCategoryIds(Integer[] categoryIds);
List<Integer> getDepIdsByCourseId(Integer id);
List<Integer> getCategoryIdsByCourseId(Integer id);
void removeByCourseId(Integer courseId);
}

View File

@@ -2,16 +2,36 @@ package xyz.playedu.api.service;
import xyz.playedu.api.domain.Course;
import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
import java.util.List;
/**
* @author tengteng
* @description 针对表【courses】的数据库操作Service
* @createDate 2023-02-24 14:14:01
*/
* @author tengteng
* @description 针对表【courses】的数据库操作Service
* @createDate 2023-02-24 14:14:01
*/
public interface CourseService extends IService<Course> {
PaginationResult<Course> paginate(int page, int size, CoursePaginateFiler filter);
void createWithCategoryIdsAndDepIds(String title, String thumb, Integer isShow, Integer[] categoryIds, Integer[] depIds);
void updateWithCategoryIdsAndDepIds(Course course, String title, String thumb, Integer isShow, Integer[] categoryIds, Integer[] depIds);
void relateDepartments(Course course, Integer[] depIds);
void resetRelateDepartments(Course course, Integer[] depIds);
void relateCategories(Course course, Integer[] categoryIds);
void resetRelateCategories(Course course, Integer[] categoryIds);
Course findOrFail(Integer id) throws NotFoundException;
List<Integer> getDepIdsByCourseId(Integer courseId);
List<Integer> getCategoryIdsByCourseId(Integer courseId);
}

View File

@@ -31,7 +31,7 @@ public class CategoryCourseServiceImpl extends ServiceImpl<CategoryCourseMapper,
}
@Override
public List<Integer> getDepIdsByCourseId(Integer id) {
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) {

View File

@@ -5,7 +5,11 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
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.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;
@@ -15,6 +19,8 @@ import xyz.playedu.api.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.util.HelperUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@@ -70,6 +76,105 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
return pageResult;
}
@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);
}
@Override
public void relateDepartments(Course course, Integer[] depIds) {
if (depIds == null || depIds.length == 0) {
return;
}
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);
}
courseDepartmentService.saveBatch(courseDepartments);
}
@Override
public void resetRelateDepartments(Course course, Integer[] depIds) {
courseDepartmentService.removeByCourseId(course.getId());
relateDepartments(course, depIds);
}
@Override
public void relateCategories(Course course, Integer[] categoryIds) {
if (categoryIds == null || categoryIds.length == 0) {
return;
}
List<CategoryCourse> categoryCourses = 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);
}
categoryCourseService.saveBatch(categoryCourses);
}
@Override
public void resetRelateCategories(Course course, Integer[] categoryIds) {
categoryCourseService.removeByCourseId(course.getId());
relateCategories(course, categoryIds);
}
@Override
@Transactional
public void updateWithCategoryIdsAndDepIds(Course course, String title, String thumb, Integer isShow, Integer[] categoryIds, Integer[] depIds) {
Course newCourse = new Course();
newCourse.setId(course.getId());
if (!course.getTitle().equals(title)) {
newCourse.setTitle(title);
}
if (!course.getThumb().equals(thumb)) {
newCourse.setThumb(thumb);
}
if (!course.getIsShow().equals(isShow)) {
newCourse.setIsShow(isShow);
}
updateById(newCourse);
resetRelateCategories(newCourse, categoryIds);
resetRelateDepartments(newCourse, depIds);
}
@Override
public Course findOrFail(Integer id) throws NotFoundException {
Course course = getOne(query().getWrapper().eq("id", id));
if (course == null) {
throw new NotFoundException("课程不存在");
}
return course;
}
@Override
public List<Integer> getDepIdsByCourseId(Integer courseId) {
return courseDepartmentService.getDepIdsByCourseId(courseId);
}
@Override
public List<Integer> getCategoryIdsByCourseId(Integer courseId) {
return categoryCourseService.getCategoryIdsByCourseId(courseId);
}
}