fixed: 资源的列表无分类筛选 && 分类资源数量计算

This commit is contained in:
none 2023-03-17 15:46:04 +08:00
parent 526d661255
commit 2fd8295113
6 changed files with 147 additions and 72 deletions

View File

@ -1,6 +1,8 @@
package xyz.playedu.api.constant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class BackendConstant {
public final static String SUPER_ADMIN_ROLE = "super-role";

View File

@ -68,8 +68,8 @@ public class ResourceController {
filter.setType(type);
filter.setCategoryIds(categoryIds);
filter.setName(name);
// 非超管只能读取它自己上传的资源
if (!backendBus.isSuperAdmin()) {
if (!backendBus.isSuperAdmin()) {// 非超管只能读取它自己上传的资源
filter.setAdminId(PlayEduBContext.getAdminUserID());
}

View File

@ -4,6 +4,7 @@ import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.Resource;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import xyz.playedu.api.types.mapper.ResourceCategoryCountMapper;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
import java.util.List;
import java.util.Map;
@ -19,7 +20,11 @@ public interface ResourceMapper extends BaseMapper<Resource> {
List<ResourceCategoryCountMapper> getCategoryCount(String type);
Integer getNoneCategoryCount(String type);
Integer getNunCategoryCount(String type);
List<Resource> paginate(ResourcePaginateFilter filter);
Long paginateCount(ResourcePaginateFilter filter);
}

View File

@ -1,8 +1,5 @@
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 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.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
import xyz.playedu.api.util.HelperUtil;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
@ -39,52 +34,13 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
@Override
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<>();
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;
}
@ -158,13 +114,13 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
@Override
public Map<Integer, Integer> getCategoryCount(String type) {
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;
}
@Override
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)));
}
}

View File

@ -25,4 +25,8 @@ public class ResourcePaginateFilter {
private Integer adminId;
private Integer pageStart;
private Integer pageSize;
}

View File

@ -5,23 +5,24 @@
<mapper namespace="xyz.playedu.api.mapper.ResourceMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.Resource">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="adminId" column="admin_id" jdbcType="INTEGER"/>
<result property="type" column="type" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="extension" column="extension" jdbcType="VARCHAR"/>
<result property="size" column="size" jdbcType="BIGINT"/>
<result property="disk" column="disk" jdbcType="VARCHAR"/>
<result property="fileId" column="file_id" jdbcType="VARCHAR"/>
<result property="path" column="path" jdbcType="VARCHAR"/>
<result property="url" column="url" jdbcType="VARCHAR"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
<result property="parentId" column="parent_id" jdbcType="INTEGER"/>
<result property="isHidden" column="is_hidden" jdbcType="TINYINT"/>
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="adminId" column="admin_id" jdbcType="INTEGER"/>
<result property="type" column="type" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="extension" column="extension" jdbcType="VARCHAR"/>
<result property="size" column="size" jdbcType="BIGINT"/>
<result property="disk" column="disk" jdbcType="VARCHAR"/>
<result property="fileId" column="file_id" jdbcType="VARCHAR"/>
<result property="path" column="path" jdbcType="VARCHAR"/>
<result property="url" column="url" jdbcType="VARCHAR"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
<result property="parentId" column="parent_id" jdbcType="INTEGER"/>
<result property="isHidden" column="is_hidden" jdbcType="TINYINT"/>
</resultMap>
<sql id="Base_Column_List">
id,admin_id,type,
id
,admin_id,type,
name,extension,size,
disk,file_id,path,
url,created_at,parent_id,
@ -33,15 +34,122 @@
FROM `resources`
INNER JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
WHERE `resources`.`type` = #{type}
AND `resources`.`is_hidden` = 0
GROUP BY `resource_category`.`cid`;
</select>
<select id="getNoneCategoryCount" resultType="java.lang.Integer">
SELECT count(DISTINCT `resources`.`id`) AS `total`
<select id="getNunCategoryCount" resultType="java.lang.Integer">
SELECT count(1) AS `total`
FROM `resources`
LEFT JOIN `resource_category` ON `resource_category`.`rid` = `resources`.`id`
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>
</mapper>