<?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,s.schoolid,s.schoolname,count(distinct rs.rsid)stunum
        ,count(distinct if(rs.rsstate>0, rs.rsid, null))commitnum
        from t_recite_student rs left join t_class c on rs.classid = c.classid
        left join t_school s on rs.schoolid=s.schoolid
        left join t_recite r on rs.reciteid=r.reciteid
        where rs.reciteid=#{recite.reciteid}
        <if test="recite.classids!=null and recite.classids!=''">
            and rs.classid in (${recite.classids})
        </if>
        group by rs.classid
        order by if(s.schoolid=#{recite.schoolid},0,s.schoolid),c.classtype,c.classorder
    </select>

    <!-- 背诵分页列表 网页 -->
    <resultMap id="listRecitesMap" type="java.util.Map">
        <result property="reciteid" column="reciteid"/>
        <result property="recitetitle" column="recitetitle"/>
        <result property="recitecont" column="recitecont"/>
        <result property="schoolid" column="schoolid"/>
        <result property="subjectname" column="subjectname"/>
        <result property="createid" column="createid"/>
        <result property="createname" column="createname"/>
        <result property="createtime" column="createtime"/>
        <result property="starttime" column="starttime"/>
        <result property="stoptime" column="stoptime"/>
        <result property="hashb" column="hashb"/>
        <collection property="reciteStus" ofType="java.util.Map" select="listReciteStu"
                    javaType="java.util.List" column="reciteid">
            <result property="rsid" column="rsid"/>
            <result property="classid" column="classid"/>
            <result property="studentid" column="studentid"/>
            <result property="studentid" column="studentid"/>
            <result property="schoolid" column="schoolid"/>
        </collection>
    </resultMap>
    <select id="listRecites" resultMap="listRecitesMap">
        SELECT r.reciteid,r.recitetitle,r.recitecont,r.schoolid,r.hashb
        ,(select s.subjectname from t_subject s where s.subjectid=r.subjectid)subjectname
        ,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_teacher_subjectbook ts on r.subjectid=ts.subjectid
        where
            <if test="usertype !=null and usertype == 0">
                r.schoolid=#{recite.schoolid}
            </if>
            <if test="usertype!= 0">
                ts.teacherid=#{recite.createid} and r.subjectid=ts.subjectid
                and ((r.hashb=1 and r.schoolid=#{recite.schoolid} and rg.classid in (${recite.classids})) or
                (rg.classid in (${recite.bxclassids})) or r.createid=#{recite.createid})
            </if>
        <if test="recite.recitestate==1">
            AND rg.stoptime <![CDATA[ >= ]]>#{recite.nowtime}
        </if>
        <if test="recite.recitestate==2">
            AND rg.stoptime <![CDATA[ < ]]>#{recite.nowtime}
        </if>
        <if test="recite.recitetitle!=null and recite.recitetitle!=''">
            AND r.recitetitle like '%${recite.recitetitle}%'
        </if>
        GROUP BY r.reciteid ORDER BY rg.starttime desc,r.reciteid
    </select>

    <!--背诵列表-我的-->
    <select id="listMyRecites" resultMap="listRecitesMap">
        SELECT r.reciteid,r.recitetitle,r.recitecont,r.schoolid,r.hashb
        ,(select s.subjectname from t_subject s where s.subjectid=r.subjectid)subjectname
        ,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_teacher_subjectbook ts on r.subjectid=ts.subjectid
        where r.createid=#{recite.createid}
        <if test="recite.recitestate==1">
            AND rg.stoptime <![CDATA[ >= ]]>#{recite.nowtime}
        </if>
        <if test="recite.recitestate==2">
            AND rg.stoptime <![CDATA[ < ]]>#{recite.nowtime}
        </if>
        <if test="recite.recitetitle!=null and recite.recitetitle!=''">
            AND r.recitetitle like '%${recite.recitetitle}%'
        </if>
        GROUP BY r.reciteid ORDER BY rg.starttime desc,r.reciteid
    </select>
    <select id="listReciteStu" resultType="java.util.Map">
        select rsid,classid,studentid,rsstate,schoolid from t_recite_student where reciteid=#{reciteid}
    </select>
    <!--获取背诵年份-->
    <select id="getYearById" resultType="java.lang.Integer">
        select year from t_recite where reciteid=#{reciteid}
    </select>
</mapper>
