后管-线上课、资源:选择分类、部门递归查询该子类、子部门数据

This commit is contained in:
wsw 2024-05-30 14:44:03 +08:00
parent 8beae72101
commit eb02def070
11 changed files with 165 additions and 69 deletions

View File

@ -34,11 +34,14 @@ import xyz.playedu.common.constant.BPermissionConstant;
import xyz.playedu.common.constant.BusinessTypeConstant;
import xyz.playedu.common.context.BCtx;
import xyz.playedu.common.domain.AdminUser;
import xyz.playedu.common.domain.Category;
import xyz.playedu.common.domain.Department;
import xyz.playedu.common.exception.NotFoundException;
import xyz.playedu.common.service.*;
import xyz.playedu.common.types.JsonResponse;
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.CourseAttachment;
import xyz.playedu.course.domain.CourseChapter;
@ -94,12 +97,66 @@ public class CourseController {
String categoryIds = MapUtils.getString(params, "category_ids");
Integer isRequired = MapUtils.getInteger(params, "is_required");
// 获取所有子部门
Set<Integer> alldepIdsSet = new HashSet<>();
if (StringUtil.isNotEmpty(depIds)) {
String[] depIdArr = depIds.split(",");
if (StringUtil.isNotEmpty(depIdArr)) {
for (String depIdStr : depIdArr) {
Integer depId = Integer.parseInt(depIdStr);
alldepIdsSet.add(depId);
// 查询所有的子部门
List<Department> departmentList =
departmentService.getChildDepartmentsByParentId(depId);
if (StringUtil.isNotEmpty(departmentList)) {
for (Department department : departmentList) {
alldepIdsSet.add(department.getId());
}
}
}
}
}
List<Integer> alldepIds = new ArrayList<>();
if ("0".equals(depIds)) {
alldepIds.add(0);
}
if (StringUtil.isNotEmpty(alldepIdsSet)) {
alldepIds.addAll(alldepIdsSet);
}
// 获取所有子类
Set<Integer> allCategoryIdsSet = new HashSet<>();
if (StringUtil.isNotEmpty(categoryIds)) {
String[] categoryIdArr = categoryIds.split(",");
if (StringUtil.isNotEmpty(categoryIdArr)) {
for (String categoryIdStr : categoryIdArr) {
Integer categoryId = Integer.parseInt(categoryIdStr);
allCategoryIdsSet.add(categoryId);
// 查询所有的子分类
List<Category> categoryList =
categoryService.getChildCategorysByParentId(categoryId);
if (StringUtil.isNotEmpty(categoryList)) {
for (Category category : categoryList) {
allCategoryIdsSet.add(category.getId());
}
}
}
}
}
List<Integer> allCategoryIds = new ArrayList<>();
if ("0".equals(categoryIds)) {
allCategoryIds.add(0);
}
if (StringUtil.isNotEmpty(allCategoryIdsSet)) {
allCategoryIds.addAll(allCategoryIdsSet);
}
CoursePaginateFiler filter = new CoursePaginateFiler();
filter.setTitle(title);
filter.setSortField(sortField);
filter.setSortAlgo(sortAlgo);
filter.setCategoryIds(categoryIds);
filter.setDepIds(depIds);
filter.setCategoryIds(allCategoryIds);
filter.setDepIds(alldepIds);
filter.setIsRequired(isRequired);
if (!backendBus.isSuperAdmin()) {

View File

@ -31,14 +31,17 @@ import xyz.playedu.common.constant.BackendConstant;
import xyz.playedu.common.constant.BusinessTypeConstant;
import xyz.playedu.common.context.BCtx;
import xyz.playedu.common.domain.AdminUser;
import xyz.playedu.common.domain.Category;
import xyz.playedu.common.exception.NotFoundException;
import xyz.playedu.common.exception.ServiceException;
import xyz.playedu.common.service.AdminUserService;
import xyz.playedu.common.service.AppConfigService;
import xyz.playedu.common.service.CategoryService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.common.types.paginate.ResourcePaginateFilter;
import xyz.playedu.common.util.S3Util;
import xyz.playedu.common.util.StringUtil;
import xyz.playedu.resource.domain.Resource;
import xyz.playedu.resource.domain.ResourceVideo;
import xyz.playedu.resource.service.ResourceService;
@ -61,6 +64,8 @@ public class ResourceController {
@Autowired private BackendBus backendBus;
@Autowired private CategoryService categoryService;
@GetMapping("/index")
@Log(title = "资源-列表", businessType = BusinessTypeConstant.GET)
public JsonResponse index(@RequestParam HashMap<String, Object> params) {
@ -76,11 +81,39 @@ public class ResourceController {
return JsonResponse.error("请选择资源类型");
}
// 获取所有子类
Set<Integer> allCategoryIdsSet = new HashSet<>();
if (StringUtil.isNotEmpty(categoryIds)) {
String[] categoryIdArr = categoryIds.split(",");
if (StringUtil.isNotEmpty(categoryIdArr)) {
for (String categoryIdStr : categoryIdArr) {
Integer categoryId = Integer.parseInt(categoryIdStr);
allCategoryIdsSet.add(categoryId);
// 查询所有的子分类
List<Category> categoryList =
categoryService.getChildCategorysByParentId(categoryId);
if (StringUtil.isNotEmpty(categoryList)) {
for (Category category : categoryList) {
allCategoryIdsSet.add(category.getId());
}
}
}
}
}
List<Integer> allCategoryIds = new ArrayList<>();
if ("0".equals(categoryIds)) {
allCategoryIds.add(0);
}
if (StringUtil.isNotEmpty(allCategoryIdsSet)) {
allCategoryIds.addAll(allCategoryIdsSet);
}
ResourcePaginateFilter filter = new ResourcePaginateFilter();
filter.setSortAlgo(sortAlgo);
filter.setSortField(sortField);
filter.setType(type);
filter.setCategoryIds(categoryIds);
filter.setCategoryIds(allCategoryIds);
filter.setName(name);
if (!backendBus.isSuperAdmin()) { // 非超管只能读取它自己上传的资源

View File

@ -15,20 +15,13 @@
*/
package xyz.playedu.api.controller.frontend;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.common.domain.Department;
import xyz.playedu.common.exception.NotFoundException;
import xyz.playedu.common.service.DepartmentService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.paginate.CoursePaginateFiler;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.course.domain.Course;
import xyz.playedu.course.service.CourseService;
import java.util.HashMap;
import java.util.stream.Collectors;
/**
@ -42,38 +35,10 @@ public class DepartmentController {
@Autowired private DepartmentService departmentService;
@Autowired private CourseService courseService;
@GetMapping("/index")
public JsonResponse index() {
return JsonResponse.data(
departmentService.all().stream()
.collect(Collectors.groupingBy(Department::getParentId)));
}
@GetMapping("/{id}/courses")
public JsonResponse courses(
@PathVariable(name = "id") Integer id, @RequestParam HashMap<String, Object> params)
throws NotFoundException {
Integer page = MapUtils.getInteger(params, "page", 1);
Integer size = MapUtils.getInteger(params, "size", 10);
CoursePaginateFiler filer = new CoursePaginateFiler();
filer.setIsShow(1);
if (id == 0) {
filer.setDepIds("0"); // 无部门所属的线上课
} else {
Department department = departmentService.findOrFail(id);
filer.setDepIds(department.getId() + "");
}
PaginationResult<Course> result = courseService.paginate(page, size, filer);
HashMap<String, Object> data = new HashMap<>();
data.put("data", result.getData());
data.put("total", result.getTotal());
return JsonResponse.data(data);
}
}

View File

@ -56,4 +56,6 @@ public interface CategoryService extends IService<Category> {
Map<Integer, String> id2name();
Long total();
List<Category> getChildCategorysByParentId(Integer parentId);
}

View File

@ -68,4 +68,6 @@ public interface DepartmentService extends IService<Department> {
Integer createWithChainList(List<String> ou);
Department findByName(String name, Integer parentId);
List<Department> getChildDepartmentsByParentId(Integer parentId);
}

View File

@ -224,4 +224,13 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
public Long total() {
return count();
}
@Override
public List<Category> getChildCategorysByParentId(Integer parentId) {
return list(
query().getWrapper()
.eq("parent_id", parentId)
.or()
.likeRight("parent_chain", parentId + ","));
}
}

View File

@ -293,4 +293,13 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
public Department findByName(String name, Integer parentId) {
return getOne(query().getWrapper().eq("name", name).eq("parent_id", parentId));
}
@Override
public List<Department> getChildDepartmentsByParentId(Integer parentId) {
return list(
query().getWrapper()
.eq("parent_id", parentId)
.or()
.likeRight("parent_chain", parentId + ","));
}
}

View File

@ -17,6 +17,8 @@ package xyz.playedu.common.types.paginate;
import lombok.Data;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
*
@ -27,9 +29,9 @@ public class CoursePaginateFiler {
private String title;
private String depIds;
private List<Integer> depIds;
private String categoryIds;
private List<Integer> categoryIds;
private Integer isRequired;

View File

@ -17,6 +17,8 @@ package xyz.playedu.common.types.paginate;
import lombok.Data;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
*
@ -35,7 +37,7 @@ public class ResourcePaginateFilter {
private String sortAlgo;
private String categoryIds;
private List<Integer> categoryIds;
private String type;

View File

@ -27,11 +27,11 @@
</sql>
<select id="paginate" resultType="xyz.playedu.course.domain.Course">
SELECT `courses`.*
SELECT DISTINCT `courses`.*
FROM `courses`
<if test="depIds != null and depIds != ''">
<if test="depIds != null and !depIds.isEmpty()">
<choose>
<when test="depIds.indexOf('0')==0">
<when test="depIds.get(0) == 0">
LEFT JOIN `course_department` ON `course_department`.`course_id` = `courses`.`id`
</when>
<otherwise>
@ -39,9 +39,9 @@
</otherwise>
</choose>
</if>
<if test="categoryIds != null and categoryIds != ''">
<if test="categoryIds != null and !categoryIds.isEmpty()">
<choose>
<when test="categoryIds.indexOf('0')==0">
<when test="categoryIds.get(0) == 0">
LEFT JOIN `resource_course_category` ON `resource_course_category`.`course_id` = `courses`.`id`
</when>
<otherwise>
@ -51,23 +51,25 @@
</if>
<where>
<if test="depIds != null and depIds != ''">
<if test="depIds != null and !depIds.isEmpty()">
<choose>
<when test="depIds.indexOf('0')==0">
<when test="depIds.get(0) == 0">
AND `course_department`.`course_id` IS NULL
</when>
<otherwise>
AND `course_department`.`dep_id` IN (#{depIds})
AND `course_department`.`dep_id` IN (<foreach collection="depIds" item="tmpId" separator=",">
#{tmpId}</foreach>)
</otherwise>
</choose>
</if>
<if test="categoryIds != null and categoryIds != ''">
<if test="categoryIds != null and !categoryIds.isEmpty()">
<choose>
<when test="categoryIds.indexOf('0')==0">
<when test="categoryIds.get(0) == 0">
AND `resource_course_category`.`course_id` IS NULL
</when>
<otherwise>
AND `resource_course_category`.`category_id` IN (#{categoryIds})
AND `resource_course_category`.`category_id` IN (<foreach collection="categoryIds" item="tmpId" separator=",">
#{tmpId}</foreach>)
</otherwise>
</choose>
</if>
@ -94,6 +96,9 @@
<when test="sortField == 'created_at'">
ORDER BY `courses`.`created_at` ASC
</when>
<when test="sortField == 'sort_at'">
ORDER BY `courses`.`sort_at` ASC
</when>
<otherwise>
ORDER BY `courses`.`id` ASC
</otherwise>
@ -110,6 +115,9 @@
<when test="sortField == 'created_at'">
ORDER BY `courses`.`created_at` DESC
</when>
<when test="sortField == 'sort_at'">
ORDER BY `courses`.`sort_at` DESC
</when>
<otherwise>
ORDER BY `courses`.`id` DESC
</otherwise>
@ -119,11 +127,12 @@
</select>
<select id="paginateCount" resultType="java.lang.Long">
SELECT count(1)
SELECT count(1) FROM (
SELECT DISTINCT `courses`.*
FROM `courses`
<if test="depIds != null and depIds != ''">
<if test="depIds != null and !depIds.isEmpty()">
<choose>
<when test="depIds.indexOf('0')==0">
<when test="depIds.get(0) == 0">
LEFT JOIN `course_department` ON `course_department`.`course_id` = `courses`.`id`
</when>
<otherwise>
@ -131,9 +140,9 @@
</otherwise>
</choose>
</if>
<if test="categoryIds != null and categoryIds != ''">
<if test="categoryIds != null and !categoryIds.isEmpty()">
<choose>
<when test="categoryIds.indexOf('0')==0">
<when test="categoryIds.get(0) == 0">
LEFT JOIN `resource_course_category` ON `resource_course_category`.`course_id` = `courses`.`id`
</when>
<otherwise>
@ -143,23 +152,26 @@
</if>
<where>
<if test="depIds != null and depIds != ''">
<if test="depIds != null and !depIds.isEmpty()">
<choose>
<when test="depIds.indexOf('0')==0">
<when test="depIds.get(0) == 0">
AND `course_department`.`course_id` IS NULL
</when>
<otherwise>
AND `course_department`.`dep_id` IN (#{depIds})
AND `course_department`.`dep_id` IN (<foreach collection="depIds" item="tmpId" separator=",">
#{tmpId}</foreach>)
</otherwise>
</choose>
</if>
<if test="categoryIds != null and categoryIds != ''">
<if test="categoryIds != null and !categoryIds.isEmpty()">
<choose>
<when test="categoryIds.indexOf('0')==0">
<when test="categoryIds.get(0) == 0">
AND `resource_course_category`.`course_id` IS NULL
</when>
<otherwise>
AND `resource_course_category`.`category_id` IN (#{categoryIds})
AND `resource_course_category`.`category_id` IN (<foreach collection="categoryIds" item="tmpId"
separator=",">
#{tmpId}</foreach>)
</otherwise>
</choose>
</if>
@ -174,6 +186,7 @@
AND `courses`.`is_required` = #{isRequired}
</if>
</where>
) m
</select>
<select id="openCoursesAndShow" resultType="xyz.playedu.course.domain.Course">
SELECT `courses`.*

View File

@ -33,9 +33,9 @@
SELECT `resources`.*
FROM `resources`
<choose>
<when test="categoryIds != null and categoryIds != ''">
<when test="categoryIds != null and !categoryIds.isEmpty()">
<choose>
<when test="categoryIds.indexOf('0') == 0">
<when test="categoryIds.get(0) == 0">
LEFT JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
WHERE `resources`.`is_hidden` = 0
AND `resource_category`.`cid` IS NULL
@ -43,7 +43,8 @@
<otherwise>
INNER JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
WHERE `resources`.`is_hidden` = 0
AND `resource_category`.`cid` IN (#{categoryIds})
AND `resource_category`.`cid` IN (<foreach collection="categoryIds" item="tmpId" separator=",">
#{tmpId}</foreach>)
</otherwise>
</choose>
</when>
@ -102,9 +103,9 @@
SELECT count(1)
FROM `resources`
<choose>
<when test="categoryIds != null and categoryIds != ''">
<when test="categoryIds != null and !categoryIds.isEmpty()">
<choose>
<when test="categoryIds.indexOf('0') == 0">
<when test="categoryIds.get(0) == 0">
LEFT JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
WHERE `resources`.`is_hidden` = 0
AND `resource_category`.`cid` IS NULL
@ -112,7 +113,8 @@
<otherwise>
INNER JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
WHERE `resources`.`is_hidden` = 0
AND `resource_category`.`cid` IN (#{categoryIds})
AND `resource_category`.`cid` IN (<foreach collection="categoryIds" item="tmpId" separator=",">
#{tmpId}</foreach>)
</otherwise>
</choose>
</when>