<?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="com.xhkjedu.sstudy.mapper.resource.ResourceMapper">
    <!--删除-->
    <update id="deleteById">
        update t_resource set resourcestate=10 where resourceid=#{resourceid} or resourcepid=#{resourceid}
    </update>
    <delete id="deleteByIdReal">
        delete from t_resource where resourceid=#{resourceid}
    </delete>
    <!--列表-->
    <select id="findAll" resultType="java.util.Map">
        select r.resourceid,r.resourcename,r.createtime,r.suffix,u.username createname,r.resourceurl,rd.lsbid,
        rd.directorid,r.resourceclass,r.resourcetype,r.readcount,r.downcount,r.pdfurl,r.converted,r.resourcecover
        ,r.comm,r.duration,r.resourcesize,r.mp4code,r.createid,r.belong,r.schoolid
        ,r.resourcegrade,r.source,r.year,r.region,r.schoolname
        from t_resource r left join t_user u on u.userid=r.createid
        left join t_resource_director rd on r.resourceid=rd.resourceid
        <if test="resource.belong!=null and resource.belong==5">
            left join t_resource_collect rc on r.resourceid = rc.resourceid
        </if>
        left join t_director d on rd.directorid=d.directorid
        where d.disabled=0 and r.resourcestate=1
        <choose>
            <when test="resource.belong==0">
                and (r.belong in(1,2)
                <if test="resource.schoolid!=null">
                    or (r.belong in(3,10) and r.schoolid=#{resource.schoolid})
                </if>
                <if test="resource.createid!=null">
                    or (r.belong=4 and r.createid=#{resource.createid})
                </if>
                )
            </when>
            <when test="resource.belong==2">
                and r.belong in(1,2)
            </when>
            <when test="resource.belong==3 or resource.belong==10">
                and r.schoolid=#{resource.schoolid} and r.belong=#{resource.belong}
            </when>
            <when test="resource.belong==4">
                and r.schoolid=#{resource.schoolid} and r.createid=#{resource.createid}
            </when>
            <when test="resource.belong==5">
                and rc.userid=#{resource.createid}
            </when>
            <when test="resource.belong==6">
                and r.schoolid=#{resource.schoolid} and r.createid=#{resource.createid}
            </when>
        </choose>
        <if test="resource.resourcename!=null and resource.resourcename!=''">
            and r.resourcename like '%${resource.resourcename}%'
        </if>
        <if test="resource.subjectid!=null and resource.subjectid!='0'.toString() and resource.subjectid!=''">
            and r.subjectid=#{resource.subjectid}
        </if>
        <if test="resource.lsbid!=null and resource.lsbid!='0'.toString() and resource.lsbid!=''">
            and rd.lsbid=#{resource.lsbid}
        </if>
        <if test="resource.level!=null and resource.level!=0">
            and r.level=#{resource.level}
        </if>
        <if test="resource.directorid!=null and resource.directorid!='0'.toString() and resource.directorid!=''">
            and rd.directorid=#{resource.directorid}
        </if>
        <if test="resource.resourceclass!=null and resource.resourceclass!=0">
            and r.resourceclass=#{resource.resourceclass}
        </if>
        <if test="resource.resourcetype!=null and resource.resourcetype!=0">
            and r.resourcetype=#{resource.resourcetype}
        </if>
        <if test="resource.year!=null and resource.year!=0">
            and FROM_UNIXTIME(r.createtime,'%Y')=#{resource.year}
        </if>
        group by r.resourceid
        <choose>
            <when test="resource.readcount!=null and resource.readcount!=0">
                order by r.readcount desc
            </when>
            <otherwise>
                order by r.createtime desc
            </otherwise>
        </choose>
        ,r.resourcename asc,r.resourceid asc
    </select>
    <!--教师备课、预习导学资源列表-->
    <!--2020.11.16 教师备课只获取自己上传到个人空间文件，不获取收藏的文件-->
    <select id="listMyResource" resultType="java.util.Map">
        select r.resourceid,r.resourcename,r.createtime,r.suffix,u.username createname,r.resourceurl,rd.lsbid,
        rd.directorid,r.resourceclass,r.resourcetype,r.readcount,r.downcount,r.pdfurl,r.converted,r.resourcecover
        ,r.comm,r.duration,r.resourcesize,r.mp4code
        from t_resource r left join t_user u on u.userid=r.createid
        left join t_resource_director rd on r.resourceid=rd.resourceid
        left join t_director d on rd.directorid=d.directorid
        where d.disabled=0 and r.resourcestate=1 and r.level=#{resource.level} and r.createid=#{resource.createid}
        <if test="resource.resourcename!=null and resource.resourcename!=''">
            and r.resourcename like '%${resource.resourcename}%'
        </if>
        <if test="resource.resourceclass!=null and resource.resourceclass!=0">
            and r.resourceclass=#{resource.resourceclass}
        </if>
        <if test="resource.subjectid!=null and resource.subjectid!='0'.toString() and resource.subjectid!=''">
            and r.subjectid=#{resource.subjectid}
        </if>
        <if test="resource.lsbid!=null and resource.lsbid!='0'.toString() and resource.lsbid!=''">
            and rd.lsbid=#{resource.lsbid}
        </if>
        <if test="resource.directorid!=null and resource.directorid!='0'.toString() and resource.directorid!=''">
            and rd.directorid=#{resource.directorid}
        </if>
        order by r.createtime desc,r.resourcename,r.resourceid
    </select>
    <!--资源是否分享过-->
    <select id="listShareNumByIds" resultType="java.util.Map">
        select resourcepid,count(*) as sharenum
        from t_resource where resourcestate=1 and resourcepid in('${shareqids}')
        group by resourcepid
    </select>

    <!--详情-->
    <resultMap id="resourceResult" type="com.xhkjedu.sstudy.vo.resource.ResourceVo">
        <result property="resourceid" column="resourceid"/>
        <result property="resourcename" column="resourcename"/>
        <result property="readcount" column="readcount"/>
        <result property="downcount" column="downcount"/>
        <result property="resourceclass" column="resourceclass"/>
        <result property="resourcetype" column="resourcetype"/>
        <result property="belong" column="belong"/>
        <result property="resourcegrade" column="resourcegrade"/>
        <result property="source" column="source"/>
        <result property="year" column="year"/>
        <result property="region" column="region"/>
        <result property="schoolname" column="schoolname"/>
        <result property="converted" column="converted"/>
        <result property="pdfurl" column="pdfurl"/>
        <result property="resourceurl" column="resourceurl"/>
        <result property="resourcecover" column="resourcecover"/>
        <result property="suffix" column="suffix"/>
        <result property="mp4code" column="mp4code"/>
        <result property="resourcesize" column="resourcesize"/>
        <result property="duration" column="duration"/>
        <result property="createid" column="createid"/>
        <result property="createname" column="createname"/>
        <result property="createtime" column="createtime"/>
        <result property="comm" column="comm"/>
        <result property="lsbnum" column="lsbnum"/>
        <result property="directornum" column="directornum"/>
        <result property="areaall" column="areaall"/>
        <result property="sharenum" column="sharenum"/>
        <result property="resourcepid" column="resourcepid"/>
        <collection property="directors" ofType="com.xhkjedu.sstudy.vo.subjectbook.DirectorVo" javaType="java.util.List">
            <result property="lsbid" column="lsbid"/>
            <result property="directorid" column="directorid"/>
            <result property="directorname" column="directorname"/>
            <result property="level" column="level"/>
            <result property="subjectid" column="subjectid"/>
            <result property="subjectname" column="subjectname"/>
            <result property="versionname" column="versionname"/>
            <result property="lsbname" column="lsbname"/>
        </collection>
    </resultMap>
    <select id="findById" resultMap="resourceResult">
        select r.*,u.username createname,rd.lsbid,rd.directorid,d.directorall as directorname
        ,sb.level,sb.subjectid,s.subjectname,v.versionname,sb.lsbname
        ,(select a.areaall from t_areazone a where a.areacode=r.region)areaall
        ,(select count(*) from t_resource r2 where r2.resourcepid=r.resourceid and r.resourcestate=1)sharenum
        from t_resource r left join t_user u on u.userid=r.createid
        left join t_resource_director rd on r.resourceid = rd.resourceid
        left join t_director d on rd.directorid = d.directorid
        left join t_subject_book sb on d.lsbid = sb.lsbid
        left join t_subject s on sb.subjectid = s.subjectid
        left join t_version v on sb.versionid = v.versionid
        where r.resourceid=#{resourceid};
    </select>
    <select id="findById2" resultType="com.xhkjedu.sstudy.vo.resource.ResourceVo">
        select r.resourceid,r.resourceclass,r.resourceurl,r.pdfurl,r.converted,r.suffix,r.mp4code
        ,r.belong,r.resourcepid
        from t_resource r where r.resourceid=#{resourceid}
    </select>
    <!--更新下载次数-->
    <update id="updateDownCount">
        update t_resource r set r.downcount=r.downcount+1 where r.resourceid=#{resourceid}
    </update>

    <!--学生同步视频资源列表-->
    <select id="studentVideoList" resultType="com.xhkjedu.sstudy.model.resource.TResource">
        select r.resourceid,r.resourcename,r.createtime,r.suffix,u.username createname
        ,r.readcount,r.comm,r.resourceurl,r.resourcecover,r.duration,r.resourcesize
        ,r.resourcegrade,r.source,r.year,r.region,r.schoolname,r.belong
        from t_resource r left join t_user u on u.userid=r.createid
        left join t_resource_director rd on r.resourceid=rd.resourceid
        left join t_director d on rd.directorid=d.directorid
        where d.disabled=0 and r.resourcestate=1 and (r.belong in(1,2) or (r.belong in(3,10) and r.schoolid=#{resource.schoolid}))
        and r.level=4 and r.subjectid=#{resource.subjectid}
        <if test="resource.lsbid!=null and resource.lsbid!='0'.toString() and resource.lsbid!=''">
            and rd.lsbid=#{resource.lsbid}
        </if>
        <if test="resource.directorid!=null and resource.directorid!='0'.toString() and resource.directorid!=''">
            and rd.directorid=#{resource.directorid}
        </if>
        <if test="resource.resourcename!=null and resource.resourcename!=''">
            and r.resourcename like '%${resource.resourcename}%'
        </if>
        group by r.resourceid
        <choose>
            <when test="resource.readcount!=null and resource.readcount!=0">
                order by r.readcount desc
            </when>
            <otherwise>
                order by r.createtime desc
            </otherwise>
        </choose>
        ,r.resourcename asc,r.resourceid asc limit #{resource.page},#{resource.pageSize}
    </select>

    <!-- 获取资源是否已经分享到校本 -->
    <select id="getLevelResourceForxb" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM t_resource r left join t_resource_director rd on r.resourceid=rd.resourceid
        LEFT JOIN t_resource r2 ON r.resourceurl=r2.resourceurl
        left join t_resource_director rd2 on r2.resourceid=rd2.resourceid and rd.lsbid=rd2.lsbid
        WHERE r2.resourceid=#{resource.resourceid} AND r.level=1 AND rd.directorid=#{resource.directorid} and r.resourcestate=1
        AND (r.belong IN(1,2) OR r.schoolid=#{resource.schoolid})
    </select>

    <!--更新阅读次数-->
    <update id="updateReadCount">
        update t_resource set readcount=readcount+1 where resourceid=#{resourceid}
    </update>
    <!--获取资源分享到预习导学的资源id-->
    <select id="getResourceIdForyx" resultType="java.lang.String">
        SELECT r.resourceid FROM t_resource r
        left join t_resource_director rd on r.resourceid=rd.resourceid
        LEFT JOIN t_resource r2 ON r.resourceurl=r2.resourceurl
        WHERE r2.resourceid=#{resource.resourceid} AND r.level=3 AND r.createid=#{resource.createid}
        AND rd.directorid=#{resource.directorid} and r.resourcestate=1
        AND (r.belong IN(1,2) OR r.schoolid=#{resource.schoolid}) limit 1
    </select>

    <!--//////////////////////////////////////////////学生WEB端//////////////////////////////////////////////-->
    <!--学生同步视频资源列表-->
    <select id="studentVideoWebList" resultType="com.xhkjedu.sstudy.model.resource.TResource">
        select r.resourceid,r.resourcename,r.createtime,r.suffix,u.username createname
        ,r.readcount,r.comm,r.resourceurl,r.resourcecover,r.duration,r.resourcesize,r.mp4code
        ,r.resourcegrade,r.source,r.year,r.region,r.schoolname,r.converted,r.belong
        from t_resource r left join t_user u on u.userid=r.createid
        left join t_resource_director rd on r.resourceid=rd.resourceid
        left join t_director d on rd.directorid=d.directorid
        where d.disabled=0 and r.resourcestate=1 and (r.belong in(1,2) or (r.belong in(3,10) and r.schoolid=#{resource.schoolid}))
        and r.level=4
        <if test="resource.subjectid!=null and resource.subjectid!='0'.toString() and resource.subjectid!=''">
            and r.subjectid=#{resource.subjectid}
        </if>
        <if test="resource.lsbid!=null and resource.lsbid!='0'.toString() and resource.lsbid!=''">
            and rd.lsbid=#{resource.lsbid}
        </if>
        <if test="resource.directorid!=null and resource.directorid!='0'.toString() and resource.directorid!=''">
            and rd.directorid=#{resource.directorid}
        </if>
        <if test="resource.resourcename!=null and resource.resourcename!=''">
            and r.resourcename like '%${resource.resourcename}%'
        </if>
        group by r.resourceid
        <choose>
            <when test="resource.readcount!=null and resource.readcount!=0">
                order by r.readcount desc
            </when>
            <otherwise>
                order by r.createtime desc
            </otherwise>
        </choose>
        ,r.resourcename asc,r.resourceid asc
    </select>

    <!--转换pdf-->
    <update id="convert">
        update t_resource set pdfurl=#{pdfurl},converted=1 where resourceid=#{resourceid}
    </update>

    <!--获取资源是否我的空间-->
    <select id="getResourceNumByLevelAndResourceid" resultType="java.lang.Integer">
        select count(*) from t_resource where level=2 and resourceid=#{resourceid}
    </select>
    <!--获取分享的伙伴校-->
    <select id="listSharePartnerSchool" resultType="java.util.Map">
        select s.schoolid,s.schoolname
        from t_resource r left join t_school s on r.schoolid=s.schoolid
        where r.resourcepid=#{resourceid} and s.schoolstate=1
        group by s.schoolid order by s.schoolid
    </select>
    <!--获取资源详情及关联章节、伙伴校-->
    <resultMap id="resourceMap" type="com.xhkjedu.sstudy.model.resource.TResource">
        <collection property="directors" select="listDirector" column="resourceid"/>
        <collection property="guideGroups" select="listSchool" column="resourceid"/>
    </resultMap>
    <select id="listDirector" resultType="com.xhkjedu.sstudy.model.resource.TResourceDirector">
        select * from t_resource_director where resourceid=#{resourceid}
    </select>
    <select id="listSchool" resultType="com.xhkjedu.sstudy.model.resource.TGuideGroup">
        select schoolid from t_resource where resourcepid=#{resourceid} group by schoolid
    </select>
    <select id="getResourceAndSchoolById" resultMap="resourceMap">
        select r.* from t_resource r where r.resourceid=#{resourceid}
    </select>
    <!--更新分享过伙伴校资源的时间-->
    <update id="batchUpdateTime">
        update t_resource set createtime=#{timestamp}
        where resourcepid=#{resourceid} and schoolid in
        <foreach collection="list" item="schoolid" open="(" separator="," close=")">${schoolid}</foreach>
    </update>

</mapper>
