<?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.recite.ReciteMapper">
    <!-- 修改背诵内容 -->
    <update id="updateRecite" parameterType="com.xhkjedu.sstudy.model.recite.TRecite">
        UPDATE t_recite SET recitetitle=#{recite.recitetitle},recitecont=#{recite.recitecont}
        WHERE reciteid=#{recite.reciteid}
    </update>
    <!--获取背诵班级-->
    <select id="listClass" resultType="java.util.Map">
        select rs.classid,c.classname,c.gradeid,count(distinct rs.rsid)stunum
        ,count(distinct case when rs.rsstate>0 then rs.rsid else null end)commitnum
        from t_recite_student rs left join t_class c on rs.classid = c.classid
        where rs.reciteid=#{reciteid} group by rs.classid order by c.classtype,c.classorder
    </select>

    <!-- 背诵分页列表 网页 -->
    <select id="listRecites" resultType="java.util.Map">
        SELECT r.reciteid,r.recitetitle,r.recitecont
        ,(select s.subjectname from t_subject s where s.subjectid=r.subjectid)subjectname
        ,COUNT(DISTINCT CASE WHEN rs.reciteid=r.reciteid THEN rs.rsid ELSE NULL END)stunum
        ,COUNT(DISTINCT CASE WHEN rs.reciteid=r.reciteid AND rs.rsstate>0 THEN rs.rsid ELSE NULL END)commitnum
        ,r.createid,u.username AS createname,r.createtime,rg.starttime,rg.stoptime
        FROM t_recite r LEFT JOIN t_user u ON r.createid=u.userid
        LEFT JOIN t_recite_group rg ON rg.reciteid = r.reciteid
        LEFT JOIN t_recite_student rs ON rg.reciteid=rs.reciteid and rs.classid=rg.classid
        where r.createid=#{recite.createid}
        <if test="recite.recitestate==1">
            AND rg.stoptime<![CDATA[ >= ]]>UNIX_TIMESTAMP()
        </if>
        <if test="recite.recitestate==2">
            AND rg.stoptime<![CDATA[ < ]]>UNIX_TIMESTAMP()
        </if>
        <if test="recite.recitetitle!=null and recite.recitetitle!=''">
            AND r.recitetitle like '%${recite.recitetitle}%'
        </if>
        GROUP BY r.reciteid ORDER BY r.createtime desc
    </select>
</mapper>
