PlayEdu/src/main/resources/mapper/ResourceMapper.xml

156 lines
6.4 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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"/>
</resultMap>
<sql id="Base_Column_List">
id
,admin_id,type,
name,extension,size,
disk,file_id,path,
url,created_at,parent_id,
is_hidden
</sql>
<select id="getCategoryCount" resultType="xyz.playedu.api.types.mapper.ResourceCategoryCountMapper">
SELECT `resource_category`.`cid` as `cid`, count(*) as `total`
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="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 `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>