删除courseCategory该用resource_category

This commit is contained in:
none 2023-03-09 10:06:55 +08:00
parent db1f6fc9c6
commit da74e6425d
16 changed files with 192 additions and 540 deletions

View File

@ -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<Integer, List<CourseCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(CourseCategory::getParentId));
HashMap<String, Object> 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<CourseCategory> 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();
}
}

View File

@ -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<Integer, List<CourseCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(CourseCategory::getParentId));
Map<Integer, List<ResourceCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(ResourceCategory::getParentId));
HashMap<String, Object> data = new HashMap<>();
data.put("categories", categories);
return JsonResponse.data(data);

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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());
}
}

View File

@ -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<CourseCategory> {
}

View File

@ -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<ResourceCourseCategory> {
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.playedu.api.mapper.CourseCategoryMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.CourseCategory">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="parentId" column="parent_id" jdbcType="INTEGER"/>
<result property="parentChain" column="parent_chain" jdbcType="VARCHAR"/>
<result property="sort" column="sort" jdbcType="INTEGER"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
<result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,name,parent_id,
parent_chain,sort,created_at,
updated_at
</sql>
</mapper>

View File

@ -2,9 +2,9 @@
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.playedu.api.mapper.CategoryCourseMapper">
<mapper namespace="xyz.playedu.api.mapper.ResourceCourseCategoryMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.CategoryCourse">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.ResourceCourseCategory">
<result property="courseId" column="course_id" jdbcType="INTEGER"/>
<result property="categoryId" column="category_id" jdbcType="INTEGER"/>
</resultMap>