mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-29 00:42:50 +08:00
课程代码优化
This commit is contained in:
parent
ba101e508b
commit
bfa1040a54
@ -1,65 +0,0 @@
|
|||||||
package xyz.playedu.api.bus;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import xyz.playedu.api.domain.CategoryCourse;
|
|
||||||
import xyz.playedu.api.domain.Course;
|
|
||||||
import xyz.playedu.api.domain.CourseDepartment;
|
|
||||||
import xyz.playedu.api.service.CategoryCourseService;
|
|
||||||
import xyz.playedu.api.service.CourseDepartmentService;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author 杭州白书科技有限公司
|
|
||||||
* @create 2023/2/24 17:12
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class CourseBus {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CourseDepartmentService courseDepartmentService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CategoryCourseService categoryCourseService;
|
|
||||||
|
|
||||||
public void departmentRelate(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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void resetDepartmentRelate(Course course, Integer[] depIds) {
|
|
||||||
courseDepartmentService.removeByCourseId(course.getId());
|
|
||||||
departmentRelate(course, depIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void categoryRelate(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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void resetCategoryRelate(Course course, Integer[] categoryIds) {
|
|
||||||
categoryCourseService.removeByCourseId(course.getId());
|
|
||||||
categoryRelate(course, categoryIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -8,15 +8,13 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
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.PlayEduBackendThreadLocal;
|
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||||
import xyz.playedu.api.bus.CourseBus;
|
|
||||||
import xyz.playedu.api.constant.BPermissionConstant;
|
import xyz.playedu.api.constant.BPermissionConstant;
|
||||||
import xyz.playedu.api.domain.*;
|
import xyz.playedu.api.domain.*;
|
||||||
import xyz.playedu.api.event.CourseDestroyEvent;
|
import xyz.playedu.api.event.CourseDestroyEvent;
|
||||||
|
import xyz.playedu.api.exception.NotFoundException;
|
||||||
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
|
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
|
||||||
import xyz.playedu.api.request.backend.CourseRequest;
|
import xyz.playedu.api.request.backend.CourseRequest;
|
||||||
import xyz.playedu.api.service.CategoryCourseService;
|
|
||||||
import xyz.playedu.api.service.CourseCategoryService;
|
import xyz.playedu.api.service.CourseCategoryService;
|
||||||
import xyz.playedu.api.service.CourseDepartmentService;
|
|
||||||
import xyz.playedu.api.service.CourseService;
|
import xyz.playedu.api.service.CourseService;
|
||||||
import xyz.playedu.api.types.JsonResponse;
|
import xyz.playedu.api.types.JsonResponse;
|
||||||
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
|
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
|
||||||
@ -40,15 +38,6 @@ public class CourseController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CourseCategoryService categoryService;//课程分类
|
private CourseCategoryService categoryService;//课程分类
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CategoryCourseService categoryCourseService;//课程与分类的关联表
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CourseDepartmentService courseDepartmentService;//课程与部门的关联表
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
protected CourseBus courseBus;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext ctx;
|
private ApplicationContext ctx;
|
||||||
|
|
||||||
@ -91,31 +80,17 @@ public class CourseController {
|
|||||||
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
|
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
@Transactional
|
@Transactional
|
||||||
public JsonResponse store(@RequestBody @Validated CourseRequest request) {
|
public JsonResponse store(@RequestBody @Validated CourseRequest req) {
|
||||||
Course course = new Course();
|
courseService.createWithCategoryIdsAndDepIds(req.getTitle(), req.getThumb(), req.getIsShow(), req.getCategoryIds(), req.getDepIds());
|
||||||
course.setTitle(request.getTitle());
|
|
||||||
course.setThumb(request.getThumb());
|
|
||||||
course.setIsShow(request.getIsShow());
|
|
||||||
course.setCreatedAt(new Date());
|
|
||||||
course.setUpdatedAt(new Date());
|
|
||||||
|
|
||||||
courseService.save(course);
|
|
||||||
|
|
||||||
courseBus.departmentRelate(course, request.getDepIds());
|
|
||||||
courseBus.categoryRelate(course, request.getCategoryIds());
|
|
||||||
|
|
||||||
return JsonResponse.success();
|
return JsonResponse.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
|
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public JsonResponse edit(@PathVariable(name = "id") Integer id) {
|
public JsonResponse edit(@PathVariable(name = "id") Integer id) throws NotFoundException {
|
||||||
Course course = courseService.getById(id);
|
Course course = courseService.findOrFail(id);
|
||||||
if (course == null) {
|
List<Integer> depIds = courseService.getDepIdsByCourseId(course.getId());
|
||||||
return JsonResponse.error("课程不存在");
|
List<Integer> categoryIds = courseService.getCategoryIdsByCourseId(course.getId());
|
||||||
}
|
|
||||||
List<Integer> depIds = courseDepartmentService.getDepIdsByCourseId(course.getId());
|
|
||||||
List<Integer> categoryIds = categoryCourseService.getDepIdsByCourseId(course.getId());
|
|
||||||
|
|
||||||
HashMap<String, Object> data = new HashMap<>();
|
HashMap<String, Object> data = new HashMap<>();
|
||||||
data.put("course", course);
|
data.put("course", course);
|
||||||
@ -128,31 +103,9 @@ public class CourseController {
|
|||||||
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
|
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
@Transactional
|
@Transactional
|
||||||
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated CourseRequest request) {
|
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated CourseRequest req) throws NotFoundException {
|
||||||
Course course = courseService.getById(id);
|
Course course = courseService.findOrFail(id);
|
||||||
if (course == null) {
|
courseService.updateWithCategoryIdsAndDepIds(course, req.getTitle(), req.getThumb(), req.getIsShow(), req.getCategoryIds(), req.getDepIds());
|
||||||
return JsonResponse.error("课程不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
Course newCourse = new Course();
|
|
||||||
newCourse.setId(course.getId());
|
|
||||||
|
|
||||||
if (!course.getTitle().equals(request.getTitle())) {
|
|
||||||
newCourse.setTitle(request.getTitle());
|
|
||||||
}
|
|
||||||
if (!course.getThumb().equals(request.getThumb())) {
|
|
||||||
newCourse.setThumb(request.getThumb());
|
|
||||||
}
|
|
||||||
if (!course.getIsShow().equals(request.getIsShow())) {
|
|
||||||
newCourse.setIsShow(request.getIsShow());
|
|
||||||
}
|
|
||||||
courseService.updateById(newCourse);
|
|
||||||
|
|
||||||
// 清空depIds
|
|
||||||
courseBus.resetDepartmentRelate(newCourse, request.getDepIds());
|
|
||||||
// 清空categoryIds
|
|
||||||
courseBus.resetCategoryRelate(newCourse, request.getCategoryIds());
|
|
||||||
|
|
||||||
return JsonResponse.success();
|
return JsonResponse.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ import java.util.List;
|
|||||||
public interface CategoryCourseService extends IService<CategoryCourse> {
|
public interface CategoryCourseService extends IService<CategoryCourse> {
|
||||||
List<Integer> getCourseIdsByCategoryIds(Integer[] categoryIds);
|
List<Integer> getCourseIdsByCategoryIds(Integer[] categoryIds);
|
||||||
|
|
||||||
List<Integer> getDepIdsByCourseId(Integer id);
|
List<Integer> getCategoryIdsByCourseId(Integer id);
|
||||||
|
|
||||||
void removeByCourseId(Integer courseId);
|
void removeByCourseId(Integer courseId);
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,36 @@ package xyz.playedu.api.service;
|
|||||||
|
|
||||||
import xyz.playedu.api.domain.Course;
|
import xyz.playedu.api.domain.Course;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
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.CoursePaginateFiler;
|
||||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author tengteng
|
* @author tengteng
|
||||||
* @description 针对表【courses】的数据库操作Service
|
* @description 针对表【courses】的数据库操作Service
|
||||||
* @createDate 2023-02-24 14:14:01
|
* @createDate 2023-02-24 14:14:01
|
||||||
*/
|
*/
|
||||||
public interface CourseService extends IService<Course> {
|
public interface CourseService extends IService<Course> {
|
||||||
|
|
||||||
PaginationResult<Course> paginate(int page, int size, CoursePaginateFiler filter);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ public class CategoryCourseServiceImpl extends ServiceImpl<CategoryCourseMapper,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Integer> getDepIdsByCourseId(Integer id) {
|
public List<Integer> getCategoryIdsByCourseId(Integer id) {
|
||||||
List<Integer> ids = new ArrayList<>();
|
List<Integer> ids = new ArrayList<>();
|
||||||
List<CategoryCourse> categoryCourses = list(query().getWrapper().eq("course_id", id));
|
List<CategoryCourse> categoryCourses = list(query().getWrapper().eq("course_id", id));
|
||||||
if (categoryCourses.size() == 0) {
|
if (categoryCourses.size() == 0) {
|
||||||
|
@ -5,7 +5,11 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
|||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
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 xyz.playedu.api.domain.CategoryCourse;
|
||||||
import xyz.playedu.api.domain.Course;
|
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.CategoryCourseService;
|
||||||
import xyz.playedu.api.service.CourseDepartmentService;
|
import xyz.playedu.api.service.CourseDepartmentService;
|
||||||
import xyz.playedu.api.service.CourseService;
|
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.types.paginate.PaginationResult;
|
||||||
import xyz.playedu.api.util.HelperUtil;
|
import xyz.playedu.api.util.HelperUtil;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,6 +76,105 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
|
|||||||
|
|
||||||
return pageResult;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user