mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-12-22 18:29:51 +08:00
课程代码优化
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user