diff --git a/src/main/java/xyz/playedu/api/controller/backend/CourseCategoryController.java b/src/main/java/xyz/playedu/api/controller/backend/CourseCategoryController.java deleted file mode 100644 index ddc2baf..0000000 --- a/src/main/java/xyz/playedu/api/controller/backend/CourseCategoryController.java +++ /dev/null @@ -1,85 +0,0 @@ -package xyz.playedu.api.controller.backend; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; -import xyz.playedu.api.PlayEduBackendThreadLocal; -import xyz.playedu.api.constant.BPermissionConstant; -import xyz.playedu.api.domain.CourseCategory; -import xyz.playedu.api.event.CourseCategoryDestroyEvent; -import xyz.playedu.api.exception.NotFoundException; -import xyz.playedu.api.middleware.BackendPermissionMiddleware; -import xyz.playedu.api.request.backend.CourseCategoryRequest; -import xyz.playedu.api.service.CourseCategoryService; -import xyz.playedu.api.types.JsonResponse; - -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * @Author 杭州白书科技有限公司 - * @create 2023/2/24 13:57 - */ -@RestController -@RequestMapping("/backend/v1/course-category") -public class CourseCategoryController { - - @Autowired - private CourseCategoryService categoryService; - - @Autowired - private ApplicationContext ctx; - - @GetMapping("/index") - public JsonResponse index() { - Map> categories = categoryService.all().stream().collect(Collectors.groupingBy(CourseCategory::getParentId)); - - HashMap data = new HashMap<>(); - data.put("categories", categories); - - return JsonResponse.data(data); - } - - @BackendPermissionMiddleware(slug = BPermissionConstant.COURSE_CATEGORY) - @GetMapping("/create") - public JsonResponse create(@RequestParam(name = "parent_id", defaultValue = "0") Integer parentId) { - List data = categoryService.listByParentId(parentId); - return JsonResponse.data(data); - } - - @BackendPermissionMiddleware(slug = BPermissionConstant.COURSE_CATEGORY) - @PostMapping("/create") - public JsonResponse store(@RequestBody @Validated CourseCategoryRequest req) throws NotFoundException { - categoryService.create(req.getName(), req.getParentId(), req.getSort()); - return JsonResponse.success(); - } - - @BackendPermissionMiddleware(slug = BPermissionConstant.COURSE_CATEGORY) - @GetMapping("/{id}") - public JsonResponse edit(@PathVariable Integer id) throws NotFoundException { - CourseCategory category = categoryService.findOrFail(id); - return JsonResponse.data(category); - } - - @BackendPermissionMiddleware(slug = BPermissionConstant.COURSE_CATEGORY) - @PutMapping("/{id}") - public JsonResponse update(@PathVariable Integer id, @RequestBody CourseCategoryRequest req) throws NotFoundException { - CourseCategory category = categoryService.findOrFail(id); - categoryService.update(category, req.getName(), req.getParentId(), req.getSort()); - return JsonResponse.success(); - } - - @BackendPermissionMiddleware(slug = BPermissionConstant.COURSE_CATEGORY) - @DeleteMapping("/{id}") - public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException { - CourseCategory category = categoryService.findOrFail(id); - categoryService.deleteById(category.getId()); - ctx.publishEvent(new CourseCategoryDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), category.getId(), new Date())); - return JsonResponse.success(); - } - -} diff --git a/src/main/java/xyz/playedu/api/controller/backend/CourseController.java b/src/main/java/xyz/playedu/api/controller/backend/CourseController.java index fb4a938..2953cfb 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/CourseController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/CourseController.java @@ -14,8 +14,8 @@ import xyz.playedu.api.event.CourseDestroyEvent; import xyz.playedu.api.exception.NotFoundException; import xyz.playedu.api.middleware.BackendPermissionMiddleware; import xyz.playedu.api.request.backend.CourseRequest; -import xyz.playedu.api.service.CourseCategoryService; import xyz.playedu.api.service.CourseService; +import xyz.playedu.api.service.ResourceCategoryService; import xyz.playedu.api.types.JsonResponse; import xyz.playedu.api.types.paginate.CoursePaginateFiler; import xyz.playedu.api.types.paginate.PaginationResult; @@ -36,7 +36,7 @@ public class CourseController { private CourseService courseService; @Autowired - private CourseCategoryService categoryService;//课程分类 + private ResourceCategoryService categoryService; @Autowired private ApplicationContext ctx; @@ -71,7 +71,7 @@ public class CourseController { @BackendPermissionMiddleware(slug = BPermissionConstant.COURSE) @GetMapping("/create") public JsonResponse create() { - Map> categories = categoryService.all().stream().collect(Collectors.groupingBy(CourseCategory::getParentId)); + Map> categories = categoryService.all().stream().collect(Collectors.groupingBy(ResourceCategory::getParentId)); HashMap data = new HashMap<>(); data.put("categories", categories); return JsonResponse.data(data); diff --git a/src/main/java/xyz/playedu/api/domain/CourseCategory.java b/src/main/java/xyz/playedu/api/domain/CourseCategory.java deleted file mode 100644 index 49dbd81..0000000 --- a/src/main/java/xyz/playedu/api/domain/CourseCategory.java +++ /dev/null @@ -1,109 +0,0 @@ -package xyz.playedu.api.domain; - -import com.baomidou.mybatisplus.annotation.IdType; -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; - -import java.io.Serializable; -import java.util.Date; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Data; - -/** - * @TableName course_categories - */ -@TableName(value = "course_categories") -@Data -public class CourseCategory implements Serializable { - /** - * - */ - @TableId(type = IdType.AUTO) - private Integer id; - - /** - * 部门名 - */ - private String name; - - /** - * 父id - */ - @JsonProperty("parent_id") - private Integer parentId; - - /** - * 父链 - */ - @JsonProperty("parent_chain") - private String parentChain; - - /** - * 升序 - */ - private Integer sort; - - @JsonProperty("created_at") - private Date createdAt; - - @JsonProperty("updated_at") - private Date updatedAt; - - @TableField(exist = false) - private static final long serialVersionUID = 1L; - - @Override - public boolean equals(Object that) { - if (this == that) { - return true; - } - if (that == null) { - return false; - } - if (getClass() != that.getClass()) { - return false; - } - CourseCategory other = (CourseCategory) that; - return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) - && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) - && (this.getParentId() == null ? other.getParentId() == null : this.getParentId().equals(other.getParentId())) - && (this.getParentChain() == null ? other.getParentChain() == null : this.getParentChain().equals(other.getParentChain())) - && (this.getSort() == null ? other.getSort() == null : this.getSort().equals(other.getSort())) - && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt())) - && (this.getUpdatedAt() == null ? other.getUpdatedAt() == null : this.getUpdatedAt().equals(other.getUpdatedAt())); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); - result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); - result = prime * result + ((getParentId() == null) ? 0 : getParentId().hashCode()); - result = prime * result + ((getParentChain() == null) ? 0 : getParentChain().hashCode()); - result = prime * result + ((getSort() == null) ? 0 : getSort().hashCode()); - result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode()); - result = prime * result + ((getUpdatedAt() == null) ? 0 : getUpdatedAt().hashCode()); - return result; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName()); - sb.append(" ["); - sb.append("Hash = ").append(hashCode()); - sb.append(", id=").append(id); - sb.append(", name=").append(name); - sb.append(", parentId=").append(parentId); - sb.append(", parentChain=").append(parentChain); - sb.append(", sort=").append(sort); - sb.append(", createdAt=").append(createdAt); - sb.append(", updatedAt=").append(updatedAt); - sb.append(", serialVersionUID=").append(serialVersionUID); - sb.append("]"); - return sb.toString(); - } -} \ No newline at end of file diff --git a/src/main/java/xyz/playedu/api/domain/ResourceCourseCategory.java b/src/main/java/xyz/playedu/api/domain/ResourceCourseCategory.java new file mode 100644 index 0000000..9c2c48b --- /dev/null +++ b/src/main/java/xyz/playedu/api/domain/ResourceCourseCategory.java @@ -0,0 +1,71 @@ +package xyz.playedu.api.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +/** + * + * @TableName resource_course_category + */ +@TableName(value ="resource_course_category") +@Data +public class ResourceCourseCategory implements Serializable { + /** + * + */ + @JsonProperty("course_id") + private Integer courseId; + + /** + * + */ + @JsonProperty("category_id") + private Integer categoryId; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + ResourceCourseCategory other = (ResourceCourseCategory) that; + return (this.getCourseId() == null ? other.getCourseId() == null : this.getCourseId().equals(other.getCourseId())) + && (this.getCategoryId() == null ? other.getCategoryId() == null : this.getCategoryId().equals(other.getCategoryId())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getCourseId() == null) ? 0 : getCourseId().hashCode()); + result = prime * result + ((getCategoryId() == null) ? 0 : getCategoryId().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", courseId=").append(courseId); + sb.append(", categoryId=").append(categoryId); + sb.append(", serialVersionUID=").append(serialVersionUID); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/xyz/playedu/api/listener/CourseDestroyListener.java b/src/main/java/xyz/playedu/api/listener/CourseDestroyListener.java index 913b40c..42a0b90 100644 --- a/src/main/java/xyz/playedu/api/listener/CourseDestroyListener.java +++ b/src/main/java/xyz/playedu/api/listener/CourseDestroyListener.java @@ -2,11 +2,10 @@ package xyz.playedu.api.listener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.event.EventListener; -import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import xyz.playedu.api.event.CourseDestroyEvent; -import xyz.playedu.api.service.CategoryCourseService; import xyz.playedu.api.service.CourseDepartmentService; +import xyz.playedu.api.service.internal.ResourceCourseCategoryService; /** * @Author 杭州白书科技有限公司 @@ -20,18 +19,16 @@ public class CourseDestroyListener { private CourseDepartmentService courseDepartmentService; @Autowired - private CategoryCourseService categoryCourseService; + private ResourceCourseCategoryService courseCategoryService; - @Order(1) @EventListener public void departmentRelateRemove(CourseDestroyEvent event) { courseDepartmentService.removeByCourseId(event.getCourseId()); } - @Order(1) @EventListener public void categoryRelateRemove(CourseDestroyEvent event) { - categoryCourseService.removeByCourseId(event.getCourseId()); + courseCategoryService.removeByCourseId(event.getCourseId()); } } diff --git a/src/main/java/xyz/playedu/api/mapper/CourseCategoryMapper.java b/src/main/java/xyz/playedu/api/mapper/CourseCategoryMapper.java deleted file mode 100644 index f6bf31f..0000000 --- a/src/main/java/xyz/playedu/api/mapper/CourseCategoryMapper.java +++ /dev/null @@ -1,20 +0,0 @@ -package xyz.playedu.api.mapper; - -import org.apache.ibatis.annotations.Mapper; -import xyz.playedu.api.domain.CourseCategory; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; - -/** -* @author tengteng -* @description 针对表【course_categories】的数据库操作Mapper -* @createDate 2023-02-24 13:55:19 -* @Entity xyz.playedu.api.domain.CourseCategory -*/ -@Mapper -public interface CourseCategoryMapper extends BaseMapper { - -} - - - - diff --git a/src/main/java/xyz/playedu/api/mapper/ResourceCourseCategoryMapper.java b/src/main/java/xyz/playedu/api/mapper/ResourceCourseCategoryMapper.java new file mode 100644 index 0000000..fe1b88e --- /dev/null +++ b/src/main/java/xyz/playedu/api/mapper/ResourceCourseCategoryMapper.java @@ -0,0 +1,20 @@ +package xyz.playedu.api.mapper; + +import org.apache.ibatis.annotations.Mapper; +import xyz.playedu.api.domain.ResourceCourseCategory; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* @author tengteng +* @description 针对表【resource_course_category】的数据库操作Mapper +* @createDate 2023-03-09 09:54:22 +* @Entity xyz.playedu.api.domain.ResourceCourseCategory +*/ +@Mapper +public interface ResourceCourseCategoryMapper extends BaseMapper { + +} + + + + diff --git a/src/main/java/xyz/playedu/api/service/CategoryCourseService.java b/src/main/java/xyz/playedu/api/service/CategoryCourseService.java deleted file mode 100644 index 1490a58..0000000 --- a/src/main/java/xyz/playedu/api/service/CategoryCourseService.java +++ /dev/null @@ -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 { - List getCourseIdsByCategoryIds(Integer[] categoryIds); - - List getCategoryIdsByCourseId(Integer id); - - void removeByCourseId(Integer courseId); -} diff --git a/src/main/java/xyz/playedu/api/service/CourseCategoryService.java b/src/main/java/xyz/playedu/api/service/CourseCategoryService.java deleted file mode 100644 index 5f6292e..0000000 --- a/src/main/java/xyz/playedu/api/service/CourseCategoryService.java +++ /dev/null @@ -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 { - - List listByParentId(Integer id); - - List 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; - -} diff --git a/src/main/java/xyz/playedu/api/service/impl/CategoryCourseServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/CategoryCourseServiceImpl.java deleted file mode 100644 index 52adb11..0000000 --- a/src/main/java/xyz/playedu/api/service/impl/CategoryCourseServiceImpl.java +++ /dev/null @@ -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 - implements CategoryCourseService { - @Override - public List getCourseIdsByCategoryIds(Integer[] categoryIds) { - List ids = new ArrayList<>(); - List 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 getCategoryIdsByCourseId(Integer id) { - List ids = new ArrayList<>(); - List 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)); - } -} - - - - diff --git a/src/main/java/xyz/playedu/api/service/impl/CourseCategoryServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/CourseCategoryServiceImpl.java deleted file mode 100644 index a941354..0000000 --- a/src/main/java/xyz/playedu/api/service/impl/CourseCategoryServiceImpl.java +++ /dev/null @@ -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 implements CourseCategoryService { - - @Override - public List listByParentId(Integer id) { - return list(query().getWrapper().eq("parent_id", id).orderByAsc("sort")); - } - - @Override - public List 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 children = list(query().getWrapper().like("parent_chain", oldChildrenPC + "%")); - if (children.size() == 0) { - return; - } - - ArrayList 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; - } -} - - - - diff --git a/src/main/java/xyz/playedu/api/service/impl/CourseServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/CourseServiceImpl.java index 5aef9e6..852e49c 100644 --- a/src/main/java/xyz/playedu/api/service/impl/CourseServiceImpl.java +++ b/src/main/java/xyz/playedu/api/service/impl/CourseServiceImpl.java @@ -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 impleme private CourseDepartmentService courseDepartmentService; @Autowired - private CategoryCourseService categoryCourseService; + private ResourceCourseCategoryService courseCategoryService; @Override public PaginationResult paginate(int page, int size, CoursePaginateFiler filter) { @@ -46,19 +46,17 @@ public class CourseServiceImpl extends ServiceImpl impleme } if (filter.getDepIds() != null && filter.getDepIds().length > 0) { List 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 courseIds = categoryCourseService.getCourseIdsByCategoryIds(filter.getCategoryIds()); - if (courseIds.size() == 0) { - wrapper.in("id", HelperUtil.zeroIntegerList()); - } else { - wrapper.in("id", courseIds); + List 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 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 impleme } List 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 impleme if (categoryIds == null || categoryIds.length == 0) { return; } - List categoryCourses = new ArrayList<>(); + List 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 impleme @Override public List getCategoryIdsByCourseId(Integer courseId) { - return categoryCourseService.getCategoryIdsByCourseId(courseId); + return courseCategoryService.getCategoryIdsByCourseId(courseId); } @Override @@ -194,7 +195,7 @@ public class CourseServiceImpl extends ServiceImpl impleme @Override public void removeCategoryIdRelate(Integer categoryId) { - categoryCourseService.remove(categoryCourseService.query().getWrapper().eq("category_id", categoryId)); + courseCategoryService.removeByCategoryId(categoryId); } } diff --git a/src/main/java/xyz/playedu/api/service/impl/internal/ResourceCourseCategoryServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/internal/ResourceCourseCategoryServiceImpl.java new file mode 100644 index 0000000..325e9c1 --- /dev/null +++ b/src/main/java/xyz/playedu/api/service/impl/internal/ResourceCourseCategoryServiceImpl.java @@ -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 + implements ResourceCourseCategoryService { + + @Override + public List getCourseIdsByCategoryIds(List 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 getCategoryIdsByCourseId(Integer courseId) { + return list(query().getWrapper().eq("course_id", courseId)).stream().map(ResourceCourseCategory::getCategoryId).toList(); + } +} + + + + diff --git a/src/main/java/xyz/playedu/api/service/internal/ResourceCourseCategoryService.java b/src/main/java/xyz/playedu/api/service/internal/ResourceCourseCategoryService.java new file mode 100644 index 0000000..327a17c --- /dev/null +++ b/src/main/java/xyz/playedu/api/service/internal/ResourceCourseCategoryService.java @@ -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 { + + List getCourseIdsByCategoryIds(List categoryIds); + + void removeByCourseId(Integer id); + + void removeByCategoryId(Integer id); + + List getCategoryIdsByCourseId(Integer courseId); +} diff --git a/src/main/resources/mapper/CourseCategoryMapper.xml b/src/main/resources/mapper/CourseCategoryMapper.xml deleted file mode 100644 index e6a8933..0000000 --- a/src/main/resources/mapper/CourseCategoryMapper.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - id,name,parent_id, - parent_chain,sort,created_at, - updated_at - - diff --git a/src/main/resources/mapper/CategoryCourseMapper.xml b/src/main/resources/mapper/ResourceCourseCategoryMapper.xml similarity index 70% rename from src/main/resources/mapper/CategoryCourseMapper.xml rename to src/main/resources/mapper/ResourceCourseCategoryMapper.xml index 12810f5..c9b8b65 100644 --- a/src/main/resources/mapper/CategoryCourseMapper.xml +++ b/src/main/resources/mapper/ResourceCourseCategoryMapper.xml @@ -2,9 +2,9 @@ - + - +