<?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.sapi.mapper.itemtool.ReadMapper">
    <!-- 所有阅读资源 -->
    <select id="listReadsForWeb" resultType="com.xhkjedu.sapi.model.itemtool.TRead">
        SELECT r.readid,r.readname,r.readthumb,r.readauthor,r.createid,r.readpath,r.readcomm,
        u.username AS createname,r.createtime,r.schoolid,r.readstate,r.readcount,r.belong
        <if test="schoolid==null">
        ,(case when r.belong=3 then (select s.schoolname from t_school s where s.schoolid=r.schoolid) else '区平台' end)belongname
        </if>
         FROM t_read r LEFT JOIN t_user u ON r.createid=u.userid
        <where>
         <if test="schoolid!=null">
             and (r.belong=2 or r.schoolid=#{schoolid})
         </if>
        <if test="readstate!=null and readstate==1">
            and r.readstate=#{readstate}
        </if>
        <if test="readstate!=null and readstate==2">
            and r.readstate in(2,3)
        </if>
          <if test="readname!=null and readname!=''">
              and r.readname like concat('%', #{readname}, '%')
          </if>
        </where>
          ORDER BY r.createtime desc
    </select>

    <!-- 根据id获取阅读详情 -->
    <select id="getObjByReadId" resultType="com.xhkjedu.sapi.model.itemtool.TRead">
        SELECT r.readid,r.readname,r.readthumb,r.readauthor,r.readsize,r.createid,
        u.username AS createname,r.createtime,r.schoolid,r.readstate,r.readcount,
        r.readcomm,r.readpath,r.readsuffix
         FROM t_read r LEFT JOIN t_user u ON r.createid=u.userid
         WHERE r.readid=#{read} limit 1
    </select>

    <!-- 获取分页资源（android） -->
    <select id="listReadsForAndr" resultType="com.xhkjedu.sapi.model.itemtool.TRead">
        SELECT r.readid,readname,readthumb,readauthor,readcount,createtime,readpath
        FROM t_read r
        WHERE (belong=2 or schoolid=#{read.schoolid}) and readstate=1
        <if test="read.rorder=='createtime'">ORDER BY createtime DESC</if>
        <if test="read.rorder=='readcount'">ORDER BY readcount DESC</if>
         limit #{read.page},#{read.pageSize}
    </select>

    <!-- 修改阅读次数 -->
    <update id="updateReadcount" parameterType="java.lang.Integer">
        update t_read set readcount = readcount+1 where readid=#{readid}
    </update>

    <!-- 学生阅读记录前5人 -->
    <select id="listReadStudents" resultType="com.xhkjedu.sapi.vo.itemtool.ReadStudentVo">
        SELECT rs.readid,rs.studentid,MAX(rs.readtime) as readtime,SUM(rs.costtime) AS costtime,u.username
        FROM t_read_student rs LEFT JOIN t_user u ON rs.studentid=u.userid
        WHERE rs.schoolid=#{schoolid} and rs.readid=#{readid} GROUP BY rs.studentid
        <if test="begintime!=0">
            HAVING SUM(costtime)<![CDATA[ >= ]]>#{begintime}
        </if>
        <if test="begintime==0 and endtime!=0">
            HAVING SUM(costtime)<![CDATA[ <= ]]>#{endtime}
        </if>
        <if test="begintime!=0 and endtime!=0">
            AND SUM(costtime)<![CDATA[ <= ]]>#{endtime}
        </if>

        ORDER BY costtime DESC
    </select>

    <select id="listReadSchool" resultType="com.xhkjedu.sapi.vo.itemtool.ReadStudentVo">
        SELECT rs.readid,rs.schoolid,MAX(rs.readtime) as readtime,SUM(rs.costtime) AS costtime,s.schoolname
        FROM t_read_student rs left join t_school s on rs.schoolid=s.schoolid
        WHERE rs.readid=#{readid} group by rs.schoolid
        <if test="begintime!=0">
            HAVING SUM(costtime)<![CDATA[ >= ]]>#{begintime}
        </if>
        <if test="begintime==0 and endtime!=0">
            HAVING SUM(costtime)<![CDATA[ <= ]]>#{endtime}
        </if>
        <if test="begintime!=0 and endtime!=0">
            AND SUM(costtime)<![CDATA[ <= ]]>#{endtime}
        </if>
        ORDER BY costtime DESC
    </select>
    <!--修改上架状态-->
    <update id="updateState">
        update t_read set readstate=#{readstate} where readid=#{readid}
    </update>
    <!--定时任务修改阅读状态-->
    <update id="updateTaskState">
        update t_read set readstate=#{readstate} where readstate=#{readstate2}
    </update>
    <!--获取电子书文件-->
    <select id="getReadPath" resultType="com.xhkjedu.sapi.model.itemtool.TRead">
        SELECT readthumb,readpath FROM t_read WHERE readid=#{readid}
    </select>

    <!--//////////////////////////////////////////////学生WEB端//////////////////////////////////////////////-->
    <!--电子书列表-->
    <select id="listReadForStuWeb" resultType="com.xhkjedu.sapi.model.itemtool.TRead">
        SELECT r.readid,readname,readthumb,readauthor,readcount,createtime,readpath,readcomm
        FROM t_read r
        WHERE (belong=2 or schoolid=#{read.schoolid}) and readstate=1
        <if test="read.rorder=='createtime'">ORDER BY createtime DESC</if>
        <if test="read.rorder=='readcount'">ORDER BY readcount DESC</if>
    </select>
</mapper>
