课程管理

This commit is contained in:
none 2023-02-24 17:41:06 +08:00
parent 97820eb402
commit b47a7adb01
24 changed files with 971 additions and 0 deletions

View File

@ -0,0 +1,65 @@
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);
}
}

View File

@ -44,6 +44,7 @@ public class AdminPermissionCheck implements ApplicationRunner {
{"学员", "15", "学员-删除", BPermissionConstant.USER_DESTROY},
{"课程分类", "0", "课程分类", BPermissionConstant.COURSE_CATEGORY},
{"课程", "0", "课程", BPermissionConstant.COURSE},
};
@Override

View File

@ -30,4 +30,6 @@ public class BPermissionConstant {
public final static String COURSE_CATEGORY = "course—category";
public final static String COURSE = "course";
}

View File

@ -0,0 +1,167 @@
package xyz.playedu.api.controller.backend;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBackendThreadLocal;
import xyz.playedu.api.bus.CourseBus;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.domain.*;
import xyz.playedu.api.event.CourseDestroyEvent;
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
import xyz.playedu.api.request.backend.CourseRequest;
import xyz.playedu.api.service.CategoryCourseService;
import xyz.playedu.api.service.CourseCategoryService;
import xyz.playedu.api.service.CourseDepartmentService;
import xyz.playedu.api.service.CourseService;
import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/24 14:16
*/
@RestController
@Slf4j
@RequestMapping("/backend/v1/course")
public class CourseController {
@Autowired
private CourseService courseService;
@Autowired
private CourseCategoryService categoryService;//课程分类
@Autowired
private CategoryCourseService categoryCourseService;//课程与分类的关联表
@Autowired
private CourseDepartmentService courseDepartmentService;//课程与部门的关联表
@Autowired
protected CourseBus courseBus;
@Autowired
private ApplicationContext ctx;
@GetMapping("/index")
public JsonResponse index(@RequestParam HashMap<String, Object> params) {
Integer page = MapUtils.getInteger(params, "page", 1);
Integer size = MapUtils.getInteger(params, "size", 10);
String sortField = MapUtils.getString(params, "sort_field", "id");
String sortAlgo = MapUtils.getString(params, "sort_algo", "desc");
String title = MapUtils.getString(params, "title");
String depIds = MapUtils.getString(params, "dep_ids");
String categoryIds = MapUtils.getString(params, "category_ids");
CoursePaginateFiler filter = new CoursePaginateFiler();
filter.setTitle(title);
filter.setSortField(sortField);
filter.setSortAlgo(sortAlgo);
if (depIds != null && depIds.length() > 0) {
filter.setDepIds(Arrays.stream(depIds.split(",")).map(Integer::valueOf).toArray(Integer[]::new));
}
if (categoryIds != null && categoryIds.length() > 0) {
filter.setCategoryIds(Arrays.stream(categoryIds.split(",")).map(Integer::valueOf).toArray(Integer[]::new));
}
log.info("filter:" + filter);
PaginationResult<Course> result = courseService.paginate(page, size, filter);
return JsonResponse.data(result);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@GetMapping("/create")
public JsonResponse create() {
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)
@PostMapping("/create")
@Transactional
public JsonResponse store(@RequestBody @Validated CourseRequest request) {
Course course = new Course();
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();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@GetMapping("/{id}")
public JsonResponse edit(@PathVariable(name = "id") Integer id) {
Course course = courseService.getById(id);
if (course == null) {
return JsonResponse.error("课程不存在");
}
List<Integer> depIds = courseDepartmentService.getDepIdsByCourseId(course.getId());
List<Integer> categoryIds = categoryCourseService.getDepIdsByCourseId(course.getId());
HashMap<String, Object> data = new HashMap<>();
data.put("course", course);
data.put("dep_ids", depIds);
data.put("category_ids", categoryIds);
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@PutMapping("/{id}")
@Transactional
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated CourseRequest request) {
Course course = courseService.getById(id);
if (course == null) {
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();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.COURSE)
@DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
courseService.removeById(id);
ctx.publishEvent(new CourseDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), id, new Date()));
return JsonResponse.success();
}
}

View File

@ -0,0 +1,63 @@
package xyz.playedu.api.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
/**
*
* @TableName category_course
*/
@TableName(value ="category_course")
@Data
public class CategoryCourse 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;
}
CategoryCourse other = (CategoryCourse) 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

@ -0,0 +1,125 @@
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 courses
*/
@TableName(value = "courses")
@Data
public class Course implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 课程标题
*/
private String title;
/**
* 课程封面
*/
private String thumb;
/**
* 课程价格()
*/
private Integer charge;
/**
* 课时数
*/
@JsonProperty("class_hour")
private Integer classHour;
/**
* 显示[1:,0:]
*/
@JsonProperty("is_show")
private Integer isShow;
@JsonProperty("created_at")
private Date createdAt;
@JsonProperty("updated_at")
private Date updatedAt;
/**
* 删除时间
*/
private Date deletedAt;
@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;
}
Course other = (Course) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getTitle() == null ? other.getTitle() == null : this.getTitle().equals(other.getTitle()))
&& (this.getThumb() == null ? other.getThumb() == null : this.getThumb().equals(other.getThumb()))
&& (this.getCharge() == null ? other.getCharge() == null : this.getCharge().equals(other.getCharge()))
&& (this.getClassHour() == null ? other.getClassHour() == null : this.getClassHour().equals(other.getClassHour()))
&& (this.getIsShow() == null ? other.getIsShow() == null : this.getIsShow().equals(other.getIsShow()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
&& (this.getUpdatedAt() == null ? other.getUpdatedAt() == null : this.getUpdatedAt().equals(other.getUpdatedAt()))
&& (this.getDeletedAt() == null ? other.getDeletedAt() == null : this.getDeletedAt().equals(other.getDeletedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode());
result = prime * result + ((getThumb() == null) ? 0 : getThumb().hashCode());
result = prime * result + ((getCharge() == null) ? 0 : getCharge().hashCode());
result = prime * result + ((getClassHour() == null) ? 0 : getClassHour().hashCode());
result = prime * result + ((getIsShow() == null) ? 0 : getIsShow().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
result = prime * result + ((getUpdatedAt() == null) ? 0 : getUpdatedAt().hashCode());
result = prime * result + ((getDeletedAt() == null) ? 0 : getDeletedAt().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(", title=").append(title);
sb.append(", thumb=").append(thumb);
sb.append(", charge=").append(charge);
sb.append(", classHour=").append(classHour);
sb.append(", isShow=").append(isShow);
sb.append(", createdAt=").append(createdAt);
sb.append(", updatedAt=").append(updatedAt);
sb.append(", deletedAt=").append(deletedAt);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,65 @@
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 course_department
*/
@TableName(value = "course_department")
@Data
public class CourseDepartment implements Serializable {
@JsonProperty("course_id")
private Integer courseId;
@JsonProperty("dep_id")
private Integer depId;
@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;
}
CourseDepartment other = (CourseDepartment) that;
return (this.getCourseId() == null ? other.getCourseId() == null : this.getCourseId().equals(other.getCourseId()))
&& (this.getDepId() == null ? other.getDepId() == null : this.getDepId().equals(other.getDepId()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getCourseId() == null) ? 0 : getCourseId().hashCode());
result = prime * result + ((getDepId() == null) ? 0 : getDepId().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(", depId=").append(depId);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,28 @@
package xyz.playedu.api.event;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;
import java.util.Date;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/24 14:31
*/
@Getter
@Setter
public class CourseDestroyEvent extends ApplicationEvent {
private Integer courseId;
private Date at;
private Integer adminId;
public CourseDestroyEvent(Object source, Integer adminId, Integer courseId, Date date) {
super(source);
this.courseId = courseId;
this.at = date;
this.adminId = adminId;
}
}

View File

@ -0,0 +1,37 @@
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;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/24 17:19
*/
@Component
public class CourseDestroyListener {
@Autowired
private CourseDepartmentService courseDepartmentService;
@Autowired
private CategoryCourseService categoryCourseService;
@Order(1)
@EventListener
public void departmentRelateRemove(CourseDestroyEvent event) {
courseDepartmentService.removeByCourseId(event.getCourseId());
}
@Order(1)
@EventListener
public void categoryRelateRemove(CourseDestroyEvent event) {
categoryCourseService.removeByCourseId(event.getCourseId());
}
}

View File

@ -0,0 +1,20 @@
package xyz.playedu.api.mapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.CategoryCourse;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author tengteng
* @description 针对表category_course的数据库操作Mapper
* @createDate 2023-02-24 14:48:26
* @Entity xyz.playedu.api.domain.CategoryCourse
*/
@Mapper
public interface CategoryCourseMapper extends BaseMapper<CategoryCourse> {
}

View File

@ -0,0 +1,20 @@
package xyz.playedu.api.mapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.CourseDepartment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author tengteng
* @description 针对表course_department的数据库操作Mapper
* @createDate 2023-02-24 14:53:52
* @Entity xyz.playedu.api.domain.CourseDepartment
*/
@Mapper
public interface CourseDepartmentMapper extends BaseMapper<CourseDepartment> {
}

View File

@ -0,0 +1,20 @@
package xyz.playedu.api.mapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.Course;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author tengteng
* @description 针对表courses的数据库操作Mapper
* @createDate 2023-02-24 14:48:38
* @Entity xyz.playedu.api.domain.Course
*/
@Mapper
public interface CourseMapper extends BaseMapper<Course> {
}

View File

@ -0,0 +1,34 @@
package xyz.playedu.api.request.backend;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/24 14:38
*/
@Data
public class CourseRequest {
@NotNull(message = "title参数不存在")
@NotBlank(message = "请输入课程标题")
private String title;
@NotNull(message = "thumb参数不存在")
@NotBlank(message = "请上传课程封面")
private String thumb;
@NotNull(message = "is_show参数不存在")
@JsonProperty("is_show")
private Integer isShow;
@NotNull(message = "dep_ids参数不存在")
@JsonProperty("dep_ids")
private Integer[] depIds;
@NotNull(message = "category_ids参数不存在")
@JsonProperty("category_ids")
private Integer[] categoryIds;
}

View File

@ -0,0 +1,19 @@
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> getDepIdsByCourseId(Integer id);
void removeByCourseId(Integer courseId);
}

View File

@ -0,0 +1,20 @@
package xyz.playedu.api.service;
import xyz.playedu.api.domain.CourseDepartment;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @author tengteng
* @description 针对表course_department的数据库操作Service
* @createDate 2023-02-24 14:53:52
*/
public interface CourseDepartmentService extends IService<CourseDepartment> {
List<Integer> getCourseIdsByDepIds(Integer[] depIds);
List<Integer> getDepIdsByCourseId(Integer courseId);
void removeByCourseId(Integer courseId);
}

View File

@ -0,0 +1,17 @@
package xyz.playedu.api.service;
import xyz.playedu.api.domain.Course;
import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
/**
* @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);
}

View File

@ -0,0 +1,54 @@
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> getDepIdsByCourseId(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

@ -0,0 +1,54 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.playedu.api.domain.CourseDepartment;
import xyz.playedu.api.service.CourseDepartmentService;
import xyz.playedu.api.mapper.CourseDepartmentMapper;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @author tengteng
* @description 针对表course_department的数据库操作Service实现
* @createDate 2023-02-24 14:53:52
*/
@Service
public class CourseDepartmentServiceImpl extends ServiceImpl<CourseDepartmentMapper, CourseDepartment>
implements CourseDepartmentService {
@Override
public List<Integer> getCourseIdsByDepIds(Integer[] depIds) {
List<Integer> ids = new ArrayList<>();
List<CourseDepartment> courseDepartments = list(query().getWrapper().in("dep_id", depIds));
if (courseDepartments.size() == 0) {
return ids;
}
for (CourseDepartment courseDepartment : courseDepartments) {
ids.add(courseDepartment.getCourseId());
}
return ids;
}
@Override
public List<Integer> getDepIdsByCourseId(Integer courseId) {
List<Integer> ids = new ArrayList<>();
List<CourseDepartment> courseDepartments = list(query().getWrapper().eq("course_id", courseId));
if (courseDepartments.size() == 0) {
return ids;
}
for (CourseDepartment courseDepartment : courseDepartments) {
ids.add(courseDepartment.getDepId());
}
return ids;
}
@Override
public void removeByCourseId(Integer courseId) {
remove(query().getWrapper().eq("course_id", courseId));
}
}

View File

@ -0,0 +1,77 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 xyz.playedu.api.domain.Course;
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.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.util.HelperUtil;
import java.util.List;
/**
* @author tengteng
* @description 针对表courses的数据库操作Service实现
* @createDate 2023-02-24 14:14:01
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
@Autowired
private CourseDepartmentService courseDepartmentService;
@Autowired
private CategoryCourseService categoryCourseService;
@Override
public PaginationResult<Course> paginate(int page, int size, CoursePaginateFiler filter) {
QueryWrapper<Course> wrapper = query().getWrapper().eq("1", "1");
if (filter.getTitle() != null && filter.getTitle().length() > 0) {
wrapper.like("title", "%" + filter.getTitle() + "%");
}
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 (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);
}
}
if (filter.getSortAlgo().equals("desc")) {
wrapper.orderByDesc(filter.getSortField());
} else {
wrapper.orderByAsc(filter.getSortField());
}
IPage<Course> pageObj = new Page<>(page, size);
pageObj = page(pageObj, wrapper);
PaginationResult<Course> pageResult = new PaginationResult<>();
pageResult.setData(pageObj.getRecords());
pageResult.setTotal(pageObj.getTotal());
return pageResult;
}
}

View File

@ -0,0 +1,22 @@
package xyz.playedu.api.types.paginate;
import lombok.Data;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/24 15:53
*/
@Data
public class CoursePaginateFiler {
private String title;
private Integer[] depIds;
private Integer[] categoryIds;
private String sortField;
private String sortAlgo;
}

View File

@ -13,6 +13,13 @@ import java.util.*;
public class HelperUtil {
public static List<Integer> zeroIntegerList() {
List<Integer> list = new ArrayList<>(1);
list.add(0);
return list;
}
public static String MD5(String text) {
return DigestUtils.md5DigestAsHex(text.getBytes(StandardCharsets.UTF_8));
}

View File

@ -0,0 +1,15 @@
<?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.CategoryCourseMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.CategoryCourse">
<result property="courseId" column="course_id" jdbcType="INTEGER"/>
<result property="categoryId" column="category_id" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
course_id,category_id
</sql>
</mapper>

View File

@ -0,0 +1,15 @@
<?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.CourseDepartmentMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.CourseDepartment">
<result property="courseId" column="course_id" jdbcType="INTEGER"/>
<result property="depId" column="dep_id" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
course_id,dep_id
</sql>
</mapper>

View File

@ -0,0 +1,24 @@
<?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.CourseMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.Course">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="title" column="title" jdbcType="VARCHAR"/>
<result property="thumb" column="thumb" jdbcType="VARCHAR"/>
<result property="charge" column="charge" jdbcType="INTEGER"/>
<result property="classHour" column="class_hour" jdbcType="INTEGER"/>
<result property="isShow" column="is_show" jdbcType="TINYINT"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
<result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
<result property="deletedAt" column="deleted_at" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,title,thumb,
charge,class_hour,is_show,
created_at,updated_at,deleted_at
</sql>
</mapper>