From 5d1a6109c2b8bc2fdf258b5014fb1f6a37c7fbb2 Mon Sep 17 00:00:00 2001 From: wsw Date: Thu, 30 May 2024 16:33:59 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=A0=B9=E6=8D=AE=E9=83=A8=E9=97=A8ID?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=89=80=E6=9C=89=E7=88=B6=E7=BA=A7=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E7=9A=84=E8=AF=BE=E7=A8=8B=20=E5=90=8E=E7=AE=A1-?= =?UTF-8?q?=E5=AD=A6=E5=91=98--=E5=AD=A6=E4=B9=A0=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E3=80=81=E5=AD=A6=E5=91=98-=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E5=AD=A6=E4=B9=A0=E8=BF=9B=E5=BA=A6=E5=8F=8A=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=20pc-=E8=AF=BE=E7=A8=8B=E9=A6=96=E9=A1=B5=202.?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=88=86=E7=B1=BBID=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=89=80=E6=9C=89=E5=AD=90=E5=88=86=E7=B1=BB=E7=9A=84=E8=AF=BE?= =?UTF-8?q?=E7=A8=8B=20pc-=E8=AF=BE=E7=A8=8B=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/DepartmentController.java | 40 ++++++++--------- .../controller/backend/UserController.java | 29 +++++++++--- .../controller/frontend/UserController.java | 44 +++++++++++++++---- .../playedu/course/mapper/CourseMapper.java | 2 +- .../playedu/course/service/CourseService.java | 4 +- .../service/impl/CourseServiceImpl.java | 42 ++++++++++-------- .../main/resources/mapper/CourseMapper.xml | 8 ++-- 7 files changed, 106 insertions(+), 63 deletions(-) diff --git a/playedu-api/src/main/java/xyz/playedu/api/controller/backend/DepartmentController.java b/playedu-api/src/main/java/xyz/playedu/api/controller/backend/DepartmentController.java index 9651755..8572688 100644 --- a/playedu-api/src/main/java/xyz/playedu/api/controller/backend/DepartmentController.java +++ b/playedu-api/src/main/java/xyz/playedu/api/controller/backend/DepartmentController.java @@ -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 depIds = - new ArrayList<>() { - { - add(id); - } - }; + + // 查询所有的父级部门ID + List allDepIds = new ArrayList<>(); + allDepIds.add(id); + Department department = departmentService.findOrFail(id); + String parentChain = department.getParentChain(); + if (StringUtil.isNotEmpty(parentChain)) { + List 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 openCourses = courseService.getOpenCoursesAndShow(10000); - ; if (openCourses != null) { courses.addAll(openCourses); } diff --git a/playedu-api/src/main/java/xyz/playedu/api/controller/backend/UserController.java b/playedu-api/src/main/java/xyz/playedu/api/controller/backend/UserController.java index 3e1ff5e..6c1e56a 100644 --- a/playedu-api/src/main/java/xyz/playedu/api/controller/backend/UserController.java +++ b/playedu-api/src/main/java/xyz/playedu/api/controller/backend/UserController.java @@ -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 departmentMap = new HashMap<>(); + if (StringUtil.isNotEmpty(departments)) { + departmentMap = + departments.stream().collect(Collectors.toMap(Department::getId, e -> e)); + } + Map finalDepartmentMap = departmentMap; depIds.forEach( (depId) -> { - List tmpCourses = - courseService.getDepCoursesAndShow( - new ArrayList<>() { - { - add(depId); - } - }); + // 查询所有的父级部门ID + List allDepIds = new ArrayList<>(); + allDepIds.add(depId); + Department department = finalDepartmentMap.get(depId); + String parentChain = department.getParentChain(); + if (StringUtil.isNotEmpty(parentChain)) { + List parentChainList = + Arrays.stream(parentChain.split(",")) + .map(Integer::parseInt) + .toList(); + if (StringUtil.isNotEmpty(parentChainList)) { + allDepIds.addAll(parentChainList); + } + } + List tmpCourses = courseService.getDepCoursesAndShow(allDepIds); depCourses.put(depId, tmpCourses); if (tmpCourses != null && !tmpCourses.isEmpty()) { diff --git a/playedu-api/src/main/java/xyz/playedu/api/controller/frontend/UserController.java b/playedu-api/src/main/java/xyz/playedu/api/controller/frontend/UserController.java index d154fc8..449bc45 100644 --- a/playedu-api/src/main/java/xyz/playedu/api/controller/frontend/UserController.java +++ b/playedu-api/src/main/java/xyz/playedu/api/controller/frontend/UserController.java @@ -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 params) { Integer depId = MapUtils.getInteger(params, "dep_id"); @@ -120,19 +127,38 @@ public class UserController { HashMap data = new HashMap<>(); data.put("learn_course_records", new HashMap<>()); + // 查询所有的父级部门ID + List allDepIds = new ArrayList<>(); + allDepIds.add(depId); + Department department = departmentService.findOrFail(depId); + String parentChain = department.getParentChain(); + if (StringUtil.isNotEmpty(parentChain)) { + List parentChainList = + Arrays.stream(parentChain.split(",")).map(Integer::parseInt).toList(); + if (StringUtil.isNotEmpty(parentChainList)) { + allDepIds.addAll(parentChainList); + } + } + + // 获取所有子分类ID + List allCategoryIds = new ArrayList<>(); + if (categoryId != null && categoryId > 0) { + allCategoryIds.add(categoryId); + // 查询所有的子分类 + List categoryList = categoryService.getChildCategorysByParentId(categoryId); + if (StringUtil.isNotEmpty(categoryList)) { + for (Category category : categoryList) { + allCategoryIds.add(category.getId()); + } + } + } + // -------- 读取当前学员可以参加的课程 ---------- List courses = new ArrayList<>(); // 读取部门课 - List depCourses = - courseService.getDepCoursesAndShow( - new ArrayList<>() { - { - add(depId); - } - }, - categoryId); + List depCourses = courseService.getDepCoursesAndShow(allDepIds, allCategoryIds); // 全部部门课 - List openCourses = courseService.getOpenCoursesAndShow(500, categoryId); + List openCourses = courseService.getOpenCoursesAndShow(500, allCategoryIds); // 汇总到一个list中 if (depCourses != null && !depCourses.isEmpty()) { courses.addAll(depCourses); diff --git a/playedu-course/src/main/java/xyz/playedu/course/mapper/CourseMapper.java b/playedu-course/src/main/java/xyz/playedu/course/mapper/CourseMapper.java index 14d7b71..5fd0d42 100644 --- a/playedu-course/src/main/java/xyz/playedu/course/mapper/CourseMapper.java +++ b/playedu-course/src/main/java/xyz/playedu/course/mapper/CourseMapper.java @@ -36,5 +36,5 @@ public interface CourseMapper extends BaseMapper { Long paginateCount(CoursePaginateFiler filer); - List openCoursesAndShow(Integer limit, Integer categoryId); + List openCoursesAndShow(Integer limit, List categoryIds); } diff --git a/playedu-course/src/main/java/xyz/playedu/course/service/CourseService.java b/playedu-course/src/main/java/xyz/playedu/course/service/CourseService.java index d71f224..02827cc 100644 --- a/playedu-course/src/main/java/xyz/playedu/course/service/CourseService.java +++ b/playedu-course/src/main/java/xyz/playedu/course/service/CourseService.java @@ -80,11 +80,11 @@ public interface CourseService extends IService { List getOpenCoursesAndShow(Integer limit); - List getOpenCoursesAndShow(Integer limit, Integer categoryId); + List getOpenCoursesAndShow(Integer limit, List categoryIds); List getDepCoursesAndShow(List depIds); - List getDepCoursesAndShow(List depIds, Integer categoryId); + List getDepCoursesAndShow(List depIds, List categoryIds); Map> getCategoryIdsGroup(List courseIds); diff --git a/playedu-course/src/main/java/xyz/playedu/course/service/impl/CourseServiceImpl.java b/playedu-course/src/main/java/xyz/playedu/course/service/impl/CourseServiceImpl.java index 4dc6519..18bac02 100644 --- a/playedu-course/src/main/java/xyz/playedu/course/service/impl/CourseServiceImpl.java +++ b/playedu-course/src/main/java/xyz/playedu/course/service/impl/CourseServiceImpl.java @@ -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 impleme @Override public List getOpenCoursesAndShow(Integer limit) { - return getBaseMapper().openCoursesAndShow(limit, 0); + return getBaseMapper().openCoursesAndShow(limit, new ArrayList<>()); } @Override - public List getOpenCoursesAndShow(Integer limit, Integer categoryId) { - return getBaseMapper().openCoursesAndShow(limit, categoryId); + public List getOpenCoursesAndShow(Integer limit, List categoryIds) { + return getBaseMapper().openCoursesAndShow(limit, categoryIds); } + @SneakyThrows @Override - public List getDepCoursesAndShow(List depIds, Integer categoryId) { - if (depIds == null || depIds.size() == 0) { + public List getDepCoursesAndShow(List depIds, List categoryIds) { + if (StringUtil.isEmpty(depIds)) { return new ArrayList<>(); } + // 获取部门课程ID List courseIds = courseDepartmentService.getCourseIdsByDepIds(depIds); - if (courseIds == null || courseIds.size() == 0) { + if (StringUtil.isEmpty(courseIds)) { return new ArrayList<>(); } - if (categoryId != null && categoryId > 0) { - List tmpCourseIds = - courseCategoryService.getCourseIdsByCategoryIds( - new ArrayList<>() { - { - add(categoryId); - } - }); - if (tmpCourseIds == null || tmpCourseIds.size() == 0) { + + if (StringUtil.isNotEmpty(categoryIds)) { + // 获取分类课程ID + List 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 impleme @Override public List getDepCoursesAndShow(List depIds) { - if (depIds == null || depIds.size() == 0) { + if (StringUtil.isEmpty(depIds)) { return new ArrayList<>(); } + // 获取部门课程ID List 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)); diff --git a/playedu-course/src/main/resources/mapper/CourseMapper.xml b/playedu-course/src/main/resources/mapper/CourseMapper.xml index 86092e0..ebf4fcc 100644 --- a/playedu-course/src/main/resources/mapper/CourseMapper.xml +++ b/playedu-course/src/main/resources/mapper/CourseMapper.xml @@ -192,13 +192,15 @@ SELECT `courses`.* FROM `courses` LEFT JOIN `course_department` ON `course_department`.`course_id` = `courses`.`id` - + INNER JOIN `resource_course_category` ON `resource_course_category`.`course_id` = `courses`.`id` WHERE `course_department`.`course_id` IS NULL AND `courses`.`is_show` = 1 - - AND `resource_course_category`.`category_id` = #{categoryId} + + AND `resource_course_category`.`category_id` IN ( + #{tmpId}) LIMIT #{limit}