1.根据部门ID获取所有父级部门的课程

后管-学员--学习课程列表、学员-部门学习进度及导出
pc-课程首页
2.根据分类ID获取所有子分类的课程
pc-课程首页
This commit is contained in:
wsw 2024-05-30 16:33:59 +08:00
parent eb02def070
commit 5d1a6109c2
7 changed files with 106 additions and 63 deletions

View File

@ -42,6 +42,7 @@ import xyz.playedu.common.service.UserService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.common.types.paginate.UserPaginateFilter;
import xyz.playedu.common.util.StringUtil;
import xyz.playedu.course.domain.Course;
import xyz.playedu.course.domain.UserCourseRecord;
import xyz.playedu.course.service.CourseDepartmentService;
@ -207,6 +208,7 @@ public class DepartmentController {
return JsonResponse.success();
}
@SneakyThrows
@BackendPermission(slug = BPermissionConstant.DEPARTMENT_USER_LEARN)
@GetMapping("/{id}/users")
@Log(title = "部门-学员", businessType = BusinessTypeConstant.GET)
@ -220,12 +222,19 @@ public class DepartmentController {
String name = MapUtils.getString(params, "name");
String email = MapUtils.getString(params, "email");
String idCard = MapUtils.getString(params, "id_card");
List<Integer> depIds =
new ArrayList<>() {
{
add(id);
}
};
// 查询所有的父级部门ID
List<Integer> allDepIds = new ArrayList<>();
allDepIds.add(id);
Department department = departmentService.findOrFail(id);
String parentChain = department.getParentChain();
if (StringUtil.isNotEmpty(parentChain)) {
List<Integer> parentChainList =
Arrays.stream(parentChain.split(",")).map(Integer::parseInt).toList();
if (StringUtil.isNotEmpty(parentChainList)) {
allDepIds.addAll(parentChainList);
}
}
String courseIdsStr = MapUtils.getString(params, "course_ids");
String showMode = MapUtils.getString(params, "show_mode");
@ -236,7 +245,7 @@ public class DepartmentController {
setName(name);
setEmail(email);
setIdCard(idCard);
setDepIds(depIds);
setDepIds(allDepIds);
setSortAlgo(sortAlgo);
setSortField(sortField);
}
@ -256,24 +265,11 @@ public class DepartmentController {
courses = courseService.getOpenCoursesAndShow(10000);
} else if ("only_dep".equals(showMode)) {
// 部门关联线上课
courses =
courseService.getDepCoursesAndShow(
new ArrayList<>() {
{
add(id);
}
});
courses = courseService.getDepCoursesAndShow(allDepIds);
} else {
// 部门关联线上课
courses =
courseService.getDepCoursesAndShow(
new ArrayList<>() {
{
add(id);
}
});
courses = courseService.getDepCoursesAndShow(allDepIds);
List<Course> openCourses = courseService.getOpenCoursesAndShow(10000);
;
if (openCourses != null) {
courses.addAll(openCourses);
}

View File

@ -51,6 +51,7 @@ import xyz.playedu.common.types.paginate.UserCourseHourRecordPaginateFilter;
import xyz.playedu.common.types.paginate.UserCourseRecordPaginateFilter;
import xyz.playedu.common.types.paginate.UserPaginateFilter;
import xyz.playedu.common.util.HelperUtil;
import xyz.playedu.common.util.StringUtil;
import xyz.playedu.course.domain.*;
import xyz.playedu.course.service.*;
@ -478,15 +479,29 @@ public class UserController {
if (depIds != null && !depIds.isEmpty()) {
departments = departmentService.chunk(depIds);
Map<Integer, Department> departmentMap = new HashMap<>();
if (StringUtil.isNotEmpty(departments)) {
departmentMap =
departments.stream().collect(Collectors.toMap(Department::getId, e -> e));
}
Map<Integer, Department> finalDepartmentMap = departmentMap;
depIds.forEach(
(depId) -> {
List<Course> tmpCourses =
courseService.getDepCoursesAndShow(
new ArrayList<>() {
{
add(depId);
}
});
// 查询所有的父级部门ID
List<Integer> allDepIds = new ArrayList<>();
allDepIds.add(depId);
Department department = finalDepartmentMap.get(depId);
String parentChain = department.getParentChain();
if (StringUtil.isNotEmpty(parentChain)) {
List<Integer> parentChainList =
Arrays.stream(parentChain.split(","))
.map(Integer::parseInt)
.toList();
if (StringUtil.isNotEmpty(parentChainList)) {
allDepIds.addAll(parentChainList);
}
}
List<Course> tmpCourses = courseService.getDepCoursesAndShow(allDepIds);
depCourses.put(depId, tmpCourses);
if (tmpCourses != null && !tmpCourses.isEmpty()) {

View File

@ -15,6 +15,7 @@
*/
package xyz.playedu.api.controller.frontend;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
@ -26,15 +27,18 @@ import org.springframework.web.multipart.MultipartFile;
import xyz.playedu.api.request.frontend.ChangePasswordRequest;
import xyz.playedu.common.constant.FrontendConstant;
import xyz.playedu.common.context.FCtx;
import xyz.playedu.common.domain.Category;
import xyz.playedu.common.domain.Department;
import xyz.playedu.common.domain.User;
import xyz.playedu.common.domain.UserUploadImageLog;
import xyz.playedu.common.exception.ServiceException;
import xyz.playedu.common.service.CategoryService;
import xyz.playedu.common.service.DepartmentService;
import xyz.playedu.common.service.UserService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.mapper.UserCourseHourRecordCourseCountMapper;
import xyz.playedu.common.util.PrivacyUtil;
import xyz.playedu.common.util.StringUtil;
import xyz.playedu.course.domain.*;
import xyz.playedu.course.service.*;
import xyz.playedu.resource.service.UploadService;
@ -63,6 +67,8 @@ public class UserController {
@Autowired private UploadService uploadService;
@Autowired private CategoryService categoryService;
@GetMapping("/detail")
public JsonResponse detail() {
User user = FCtx.getUser();
@ -100,6 +106,7 @@ public class UserController {
return JsonResponse.success();
}
@SneakyThrows
@GetMapping("/courses")
public JsonResponse courses(@RequestParam HashMap<String, Object> params) {
Integer depId = MapUtils.getInteger(params, "dep_id");
@ -120,19 +127,38 @@ public class UserController {
HashMap<String, Object> data = new HashMap<>();
data.put("learn_course_records", new HashMap<>());
// 查询所有的父级部门ID
List<Integer> allDepIds = new ArrayList<>();
allDepIds.add(depId);
Department department = departmentService.findOrFail(depId);
String parentChain = department.getParentChain();
if (StringUtil.isNotEmpty(parentChain)) {
List<Integer> parentChainList =
Arrays.stream(parentChain.split(",")).map(Integer::parseInt).toList();
if (StringUtil.isNotEmpty(parentChainList)) {
allDepIds.addAll(parentChainList);
}
}
// 获取所有子分类ID
List<Integer> allCategoryIds = new ArrayList<>();
if (categoryId != null && categoryId > 0) {
allCategoryIds.add(categoryId);
// 查询所有的子分类
List<Category> categoryList = categoryService.getChildCategorysByParentId(categoryId);
if (StringUtil.isNotEmpty(categoryList)) {
for (Category category : categoryList) {
allCategoryIds.add(category.getId());
}
}
}
// -------- 读取当前学员可以参加的课程 ----------
List<Course> courses = new ArrayList<>();
// 读取部门课
List<Course> depCourses =
courseService.getDepCoursesAndShow(
new ArrayList<>() {
{
add(depId);
}
},
categoryId);
List<Course> depCourses = courseService.getDepCoursesAndShow(allDepIds, allCategoryIds);
// 全部部门课
List<Course> openCourses = courseService.getOpenCoursesAndShow(500, categoryId);
List<Course> openCourses = courseService.getOpenCoursesAndShow(500, allCategoryIds);
// 汇总到一个list中
if (depCourses != null && !depCourses.isEmpty()) {
courses.addAll(depCourses);

View File

@ -36,5 +36,5 @@ public interface CourseMapper extends BaseMapper<Course> {
Long paginateCount(CoursePaginateFiler filer);
List<Course> openCoursesAndShow(Integer limit, Integer categoryId);
List<Course> openCoursesAndShow(Integer limit, List<Integer> categoryIds);
}

View File

@ -80,11 +80,11 @@ public interface CourseService extends IService<Course> {
List<Course> getOpenCoursesAndShow(Integer limit);
List<Course> getOpenCoursesAndShow(Integer limit, Integer categoryId);
List<Course> getOpenCoursesAndShow(Integer limit, List<Integer> categoryIds);
List<Course> getDepCoursesAndShow(List<Integer> depIds);
List<Course> getDepCoursesAndShow(List<Integer> depIds, Integer categoryId);
List<Course> getDepCoursesAndShow(List<Integer> depIds, List<Integer> categoryIds);
Map<Integer, List<Integer>> getCategoryIdsGroup(List<Integer> courseIds);

View File

@ -17,6 +17,8 @@ package xyz.playedu.course.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -24,6 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
import xyz.playedu.common.exception.NotFoundException;
import xyz.playedu.common.types.paginate.CoursePaginateFiler;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.common.util.StringUtil;
import xyz.playedu.course.domain.Course;
import xyz.playedu.course.domain.CourseCategory;
import xyz.playedu.course.domain.CourseDepartment;
@ -217,36 +220,36 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
@Override
public List<Course> getOpenCoursesAndShow(Integer limit) {
return getBaseMapper().openCoursesAndShow(limit, 0);
return getBaseMapper().openCoursesAndShow(limit, new ArrayList<>());
}
@Override
public List<Course> getOpenCoursesAndShow(Integer limit, Integer categoryId) {
return getBaseMapper().openCoursesAndShow(limit, categoryId);
public List<Course> getOpenCoursesAndShow(Integer limit, List<Integer> categoryIds) {
return getBaseMapper().openCoursesAndShow(limit, categoryIds);
}
@SneakyThrows
@Override
public List<Course> getDepCoursesAndShow(List<Integer> depIds, Integer categoryId) {
if (depIds == null || depIds.size() == 0) {
public List<Course> getDepCoursesAndShow(List<Integer> depIds, List<Integer> categoryIds) {
if (StringUtil.isEmpty(depIds)) {
return new ArrayList<>();
}
// 获取部门课程ID
List<Integer> courseIds = courseDepartmentService.getCourseIdsByDepIds(depIds);
if (courseIds == null || courseIds.size() == 0) {
if (StringUtil.isEmpty(courseIds)) {
return new ArrayList<>();
}
if (categoryId != null && categoryId > 0) {
List<Integer> tmpCourseIds =
courseCategoryService.getCourseIdsByCategoryIds(
new ArrayList<>() {
{
add(categoryId);
}
});
if (tmpCourseIds == null || tmpCourseIds.size() == 0) {
if (StringUtil.isNotEmpty(categoryIds)) {
// 获取分类课程ID
List<Integer> catCourseIds =
courseCategoryService.getCourseIdsByCategoryIds(categoryIds);
if (StringUtil.isEmpty(catCourseIds)) {
return new ArrayList<>();
}
courseIds = courseIds.stream().filter(tmpCourseIds::contains).toList();
if (courseIds.size() == 0) {
// 求课程ID交集
courseIds = courseIds.stream().filter(catCourseIds::contains).toList();
if (StringUtil.isEmpty(courseIds)) {
return new ArrayList<>();
}
}
@ -255,11 +258,12 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
@Override
public List<Course> getDepCoursesAndShow(List<Integer> depIds) {
if (depIds == null || depIds.size() == 0) {
if (StringUtil.isEmpty(depIds)) {
return new ArrayList<>();
}
// 获取部门课程ID
List<Integer> courseIds = courseDepartmentService.getCourseIdsByDepIds(depIds);
if (courseIds == null || courseIds.size() == 0) {
if (StringUtil.isEmpty(courseIds)) {
return new ArrayList<>();
}
return list(query().getWrapper().in("id", courseIds).eq("is_show", 1));

View File

@ -192,13 +192,15 @@
SELECT `courses`.*
FROM `courses`
LEFT JOIN `course_department` ON `course_department`.`course_id` = `courses`.`id`
<if test="categoryId != null and categoryId > 0">
<if test="categoryIds != null and !categoryIds.isEmpty()">
INNER JOIN `resource_course_category` ON `resource_course_category`.`course_id` = `courses`.`id`
</if>
WHERE `course_department`.`course_id` IS NULL
AND `courses`.`is_show` = 1
<if test="categoryId != null and categoryId > 0">
AND `resource_course_category`.`category_id` = #{categoryId}
<if test="categoryIds != null and !categoryIds.isEmpty()">
AND `resource_course_category`.`category_id` IN (<foreach collection="categoryIds" item="tmpId"
separator=",">
#{tmpId}</foreach>)
</if>
LIMIT #{limit}
</select>