mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-22 20:02:42 +08:00
fixed: 资源的列表无分类筛选 && 分类资源数量计算
This commit is contained in:
parent
526d661255
commit
2fd8295113
@ -1,6 +1,8 @@
|
|||||||
package xyz.playedu.api.constant;
|
package xyz.playedu.api.constant;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class BackendConstant {
|
public class BackendConstant {
|
||||||
public final static String SUPER_ADMIN_ROLE = "super-role";
|
public final static String SUPER_ADMIN_ROLE = "super-role";
|
||||||
|
@ -68,8 +68,8 @@ public class ResourceController {
|
|||||||
filter.setType(type);
|
filter.setType(type);
|
||||||
filter.setCategoryIds(categoryIds);
|
filter.setCategoryIds(categoryIds);
|
||||||
filter.setName(name);
|
filter.setName(name);
|
||||||
// 非超管只能读取它自己上传的资源
|
|
||||||
if (!backendBus.isSuperAdmin()) {
|
if (!backendBus.isSuperAdmin()) {// 非超管只能读取它自己上传的资源
|
||||||
filter.setAdminId(PlayEduBContext.getAdminUserID());
|
filter.setAdminId(PlayEduBContext.getAdminUserID());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
import xyz.playedu.api.domain.Resource;
|
import xyz.playedu.api.domain.Resource;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import xyz.playedu.api.types.mapper.ResourceCategoryCountMapper;
|
import xyz.playedu.api.types.mapper.ResourceCategoryCountMapper;
|
||||||
|
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -19,7 +20,11 @@ public interface ResourceMapper extends BaseMapper<Resource> {
|
|||||||
|
|
||||||
List<ResourceCategoryCountMapper> getCategoryCount(String type);
|
List<ResourceCategoryCountMapper> getCategoryCount(String type);
|
||||||
|
|
||||||
Integer getNoneCategoryCount(String type);
|
Integer getNunCategoryCount(String type);
|
||||||
|
|
||||||
|
List<Resource> paginate(ResourcePaginateFilter filter);
|
||||||
|
|
||||||
|
Long paginateCount(ResourcePaginateFilter filter);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
package xyz.playedu.api.service.impl;
|
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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@ -17,9 +14,7 @@ import xyz.playedu.api.service.internal.ResourceCategoryRelationService;
|
|||||||
import xyz.playedu.api.types.mapper.ResourceCategoryCountMapper;
|
import xyz.playedu.api.types.mapper.ResourceCategoryCountMapper;
|
||||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||||
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
|
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
|
||||||
import xyz.playedu.api.util.HelperUtil;
|
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -39,52 +34,13 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter) {
|
public PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter) {
|
||||||
QueryWrapper<Resource> wrapper = query().getWrapper().eq("is_hidden", 0);
|
|
||||||
|
|
||||||
if (filter.getName() != null) {
|
|
||||||
wrapper.like("name", "%" + filter.getName() + "%");
|
|
||||||
}
|
|
||||||
if (filter.getDisk() != null) {
|
|
||||||
wrapper.eq("disk", filter.getDisk());
|
|
||||||
}
|
|
||||||
if (filter.getExtension() != null) {
|
|
||||||
wrapper.eq("extension", filter.getExtension());
|
|
||||||
}
|
|
||||||
if (filter.getType() != null) {
|
|
||||||
wrapper.eq("type", filter.getType());
|
|
||||||
}
|
|
||||||
if (filter.getAdminId() != null && !filter.getAdminId().equals(0)) {
|
|
||||||
wrapper.eq("admin_id", filter.getAdminId());
|
|
||||||
}
|
|
||||||
if (filter.getCategoryIds() != null && filter.getCategoryIds().trim().length() > 0) {
|
|
||||||
List<Integer> categoryIds = Arrays.stream(filter.getCategoryIds().split(",")).map(Integer::valueOf).toList();
|
|
||||||
List<Integer> ids = relationService.getRidsByCids(categoryIds);
|
|
||||||
if (ids == null || ids.size() == 0) {
|
|
||||||
ids = HelperUtil.zeroIntegerList();
|
|
||||||
}
|
|
||||||
wrapper.in("id", ids);
|
|
||||||
}
|
|
||||||
|
|
||||||
String sortFiled = filter.getSortField();
|
|
||||||
if (sortFiled == null || sortFiled.trim().length() == 0) {
|
|
||||||
sortFiled = "id";
|
|
||||||
}
|
|
||||||
String sortAlgo = filter.getSortAlgo();
|
|
||||||
if (sortAlgo == null || sortAlgo.trim().length() == 0) {
|
|
||||||
sortAlgo = "desc";
|
|
||||||
}
|
|
||||||
if ("desc".equals(sortAlgo)) {
|
|
||||||
wrapper.orderByDesc(sortFiled);
|
|
||||||
} else {
|
|
||||||
wrapper.orderByAsc(sortFiled);
|
|
||||||
}
|
|
||||||
|
|
||||||
IPage<Resource> adminPage = new Page<>(page, size);
|
|
||||||
adminPage = page(adminPage, wrapper);
|
|
||||||
|
|
||||||
PaginationResult<Resource> pageResult = new PaginationResult<>();
|
PaginationResult<Resource> pageResult = new PaginationResult<>();
|
||||||
pageResult.setData(adminPage.getRecords());
|
|
||||||
pageResult.setTotal(adminPage.getTotal());
|
filter.setPageStart((page - 1) * size);
|
||||||
|
filter.setPageSize(size);
|
||||||
|
|
||||||
|
pageResult.setData(getBaseMapper().paginate(filter));
|
||||||
|
pageResult.setTotal(getBaseMapper().paginateCount(filter));
|
||||||
|
|
||||||
return pageResult;
|
return pageResult;
|
||||||
}
|
}
|
||||||
@ -158,13 +114,13 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
|
|||||||
@Override
|
@Override
|
||||||
public Map<Integer, Integer> getCategoryCount(String type) {
|
public Map<Integer, Integer> getCategoryCount(String type) {
|
||||||
Map<Integer, Integer> data = getBaseMapper().getCategoryCount(type).stream().collect(Collectors.toMap(ResourceCategoryCountMapper::getCid, ResourceCategoryCountMapper::getTotal));
|
Map<Integer, Integer> data = getBaseMapper().getCategoryCount(type).stream().collect(Collectors.toMap(ResourceCategoryCountMapper::getCid, ResourceCategoryCountMapper::getTotal));
|
||||||
data.put(0, getBaseMapper().getNoneCategoryCount(type));
|
data.put(0, getBaseMapper().getNunCategoryCount(type));
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer total(String type) {
|
public Integer total(String type) {
|
||||||
return Math.toIntExact(count(query().getWrapper().eq("type", type)));
|
return Math.toIntExact(count(query().getWrapper().eq("type", type).eq("is_hidden", 0)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,4 +25,8 @@ public class ResourcePaginateFilter {
|
|||||||
|
|
||||||
private Integer adminId;
|
private Integer adminId;
|
||||||
|
|
||||||
|
private Integer pageStart;
|
||||||
|
|
||||||
|
private Integer pageSize;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,8 @@
|
|||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<sql id="Base_Column_List">
|
<sql id="Base_Column_List">
|
||||||
id,admin_id,type,
|
id
|
||||||
|
,admin_id,type,
|
||||||
name,extension,size,
|
name,extension,size,
|
||||||
disk,file_id,path,
|
disk,file_id,path,
|
||||||
url,created_at,parent_id,
|
url,created_at,parent_id,
|
||||||
@ -33,15 +34,122 @@
|
|||||||
FROM `resources`
|
FROM `resources`
|
||||||
INNER JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
INNER JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
||||||
WHERE `resources`.`type` = #{type}
|
WHERE `resources`.`type` = #{type}
|
||||||
|
AND `resources`.`is_hidden` = 0
|
||||||
GROUP BY `resource_category`.`cid`;
|
GROUP BY `resource_category`.`cid`;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getNoneCategoryCount" resultType="java.lang.Integer">
|
<select id="getNunCategoryCount" resultType="java.lang.Integer">
|
||||||
SELECT count(DISTINCT `resources`.`id`) AS `total`
|
SELECT count(1) AS `total`
|
||||||
FROM `resources`
|
FROM `resources`
|
||||||
LEFT JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
LEFT JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
||||||
WHERE `resources`.`type` = #{type}
|
WHERE `resources`.`type` = #{type}
|
||||||
AND `resource_category`.`cid` IS NULL;
|
AND `resources`.`is_hidden` = 0
|
||||||
|
AND `resource_category`.`rid` IS NULL;
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="paginate" resultType="xyz.playedu.api.domain.Resource">
|
||||||
|
SELECT `resources`.*
|
||||||
|
FROM `resources`
|
||||||
|
<choose>
|
||||||
|
<when test="categoryIds != null and categoryIds != ''">
|
||||||
|
<choose>
|
||||||
|
<when test="categoryIds.indexOf('0') == 0">
|
||||||
|
LEFT JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
||||||
|
WHERE `resources`.`is_hidden` = 0
|
||||||
|
AND `resource_category`.`cid` IS NULL
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
INNER JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
||||||
|
WHERE `resources`.`is_hidden` = 0
|
||||||
|
AND `resource_category`.`cid` IN (#{categoryIds})
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
WHERE `resources`.`is_hidden` = 0
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
<if test="name != null and name != ''">
|
||||||
|
AND `resources`.`name` LIKE concat('%',#{name},'%')
|
||||||
|
</if>
|
||||||
|
<if test="disk != null and disk != ''">
|
||||||
|
AND `resources`.`disk` = #{disk}
|
||||||
|
</if>
|
||||||
|
<if test="extension != null and extension != ''">
|
||||||
|
AND `resources`.`extension` = #{extension}
|
||||||
|
</if>
|
||||||
|
<if test="type != null and type != ''">
|
||||||
|
AND `resources`.`type` = #{type}
|
||||||
|
</if>
|
||||||
|
<if test="adminId != null and adminId != 0">
|
||||||
|
AND `resources`.`admin_id` = #{adminId}
|
||||||
|
</if>
|
||||||
|
<if test="sortAlgo == 'asc'">
|
||||||
|
<choose>
|
||||||
|
<when test="sortField == 'size'">
|
||||||
|
ORDER BY `resources`.`size` ASC
|
||||||
|
</when>
|
||||||
|
<when test="sortField == 'created_at'">
|
||||||
|
ORDER BY `resources`.`size` ASC
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
ORDER BY `resources`.`id` ASC
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
</if>
|
||||||
|
<if test="sortAlgo != 'asc'">
|
||||||
|
<choose>
|
||||||
|
<when test="sortField == 'size'">
|
||||||
|
ORDER BY `resources`.`size` DESC
|
||||||
|
</when>
|
||||||
|
<when test="sortField == 'created_at'">
|
||||||
|
ORDER BY `resources`.`size` DESC
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
ORDER BY `resources`.`id` DESC
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
</if>
|
||||||
|
LIMIT #{pageStart}, #{pageSize};
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="paginateCount" resultType="java.lang.Long">
|
||||||
|
SELECT count(1)
|
||||||
|
FROM `resources`
|
||||||
|
<choose>
|
||||||
|
<when test="categoryIds != null and categoryIds != ''">
|
||||||
|
<choose>
|
||||||
|
<when test="categoryIds.indexOf('0') == 0">
|
||||||
|
LEFT JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
||||||
|
WHERE `resources`.`is_hidden` = 0
|
||||||
|
AND `resource_category`.`cid` IS NULL
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
INNER JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
|
||||||
|
WHERE `resources`.`is_hidden` = 0
|
||||||
|
AND `resource_category`.`cid` IN (#{categoryIds})
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
WHERE `resources`.`is_hidden` = 0
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
<if test="name != null and name != ''">
|
||||||
|
AND `resources`.`name` LIKE concat('%',#{name},'%')
|
||||||
|
</if>
|
||||||
|
<if test="disk != null and disk != ''">
|
||||||
|
AND `resources`.`disk` = #{disk}
|
||||||
|
</if>
|
||||||
|
<if test="extension != null and extension != ''">
|
||||||
|
AND `resources`.`extension` = #{extension}
|
||||||
|
</if>
|
||||||
|
<if test="type != null and type != ''">
|
||||||
|
AND `resources`.`type` = #{type}
|
||||||
|
</if>
|
||||||
|
<if test="adminId != null and adminId != 0">
|
||||||
|
AND `resources`.`admin_id` = #{adminId}
|
||||||
|
</if>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user