<?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.research.ResearchUserMapper">
    <!-- 新增 -->
    <insert id="saveBatchResearchUser" parameterType="java.util.List">
        INSERT INTO t_research_user (researchid,userid,rustate,schoolid,createtime)
        VALUES
        <foreach collection ="list" item="ru" index= "index" separator =",">
            (#{researchid},#{ru.userid},#{ru.rustate},#{ru.schoolid},#{ru.createtime})
        </foreach>
    </insert>

    <!-- 删除教研教师 教研id -->
    <delete id="delByResearchid">
        DELETE FROM t_research_user WHERE researchid=#{researchid}
    </delete>

    <!-- 教研id获取所有参与教研的用户id -->
    <select id="listRuserIdByResearchid" resultType="com.xhkjedu.sapi.model.research.TResearchUser">
        select ru.rustate,u.username as createname,ru.schoolid,s.schoolname
        from t_research_user ru left join t_user u on ru.userid = u.userid
        left join t_school s on ru.schoolid=s.schoolid
        where ru.researchid=#{researchid} order by s.schoolid,convert(u.username using gbk)
    </select>

    <!-- 教研id获取素有教研信息 -->
    <select id="listRusersByReaearchid" resultType="java.util.Map">
        SELECT ru.ruid,ru.userid,ru.content,ru.reviewtime,ru.createtime,u.username AS createname,
        u.headpic,ru.schoolid,s.schoolname
        FROM t_research_user ru LEFT JOIN t_user u ON ru.userid=u.userid
        left join t_school s on ru.schoolid=s.schoolid
        WHERE ru.researchid=#{researchid} AND ru.rustate=1
        <if test="schoolid!=null and schoolid!=0">
            and ru.schoolid=#{schoolid}
        </if>
        ORDER BY ru.reviewtime desc
    </select>

    <!-- 教师参与教研列表 -->
    <select id="listUserResearchs" resultType="java.util.Map">
         SELECT r.researchid,r.researchname,r.researchcover,r.duration,r.filesize,r.starttime,r.stoptime,r.mp4code,
        r.createid,r.createtime,u.username AS createname,r.readnum,r.researchpath,r.publiced,r.schoolid,r.hashb,ru.rustate
        FROM t_research_user ru LEFT JOIN t_research r ON ru.researchid=r.researchid
        LEFT JOIN t_user u ON r.createid=u.userid
        WHERE ru.userid=#{userid} and r.starttime&lt;=#{timestamp}
        <if test="rustate!=null"> and ru.rustate=#{rustate}</if>
        GROUP BY r.researchid ORDER BY r.createtime desc
        <if test="rorder=='stoptime'">,r.stoptime DESC</if>
        <if test="rorder=='readnum'">,r.readnum DESC</if>
        <if test="rorder=='createtime'">,r.createtime DESC</if>
    </select>
    <!--教师未评价教研数量-->
    <select id="noEvaluatedNum" resultType="java.lang.Integer">
        select count(*) from t_research_user ru left join t_research r on ru.researchid = r.researchid
        where ru.userid=#{userid} and ru.rustate=0 and r.starttime&lt;=#{timestamp} and r.stoptime>=#{timestamp}
    </select>

    <!-- 根据id获取教研详细信息 -->
    <select id="getResearchUserDetail" resultType="com.xhkjedu.sapi.vo.research.ResearchUserVo">
        SELECT r.researchid,r.researchname,r.researchcontent,r.researchpath,r.researchcover,r.duration,r.filesize,ru.schoolid,
        r.starttime,r.stoptime,r.readnum,r.mp4code,r.publiced,r.hashb,ru.rustate,ru.ruid,ru.content,ru.reviewtime,u.username createname
        FROM t_research_user ru LEFT JOIN t_research r ON ru.researchid=r.researchid
        left join t_user u on r.createid=u.userid
        WHERE r.researchid=#{researchid} AND ru.userid=#{userid} limit 1
    </select>

    <!-- 保存教师教研评价 -->
    <update id="updateUserResearch" parameterType="com.xhkjedu.sapi.model.research.TResearchUser">
        UPDATE t_research_user SET rustate=#{ru.rustate},content=#{ru.content},reviewtime=#{ru.reviewtime}
        WHERE researchid=#{ru.researchid} AND userid=#{ru.userid}
    </update>
    <!-- 其他教研活动列表 -->
    <select id="otherList" resultType="java.util.Map">
        SELECT r.researchid,r.researchname,r.researchcover,r.duration,r.readnum
        ,ru.rustate,r.starttime,r.stoptime,r.createtime,r.hashb,r.publiced
        ,(select u.username from t_user u where u.userid=r.createid)createname
        FROM t_research_user ru LEFT JOIN t_research r ON ru.researchid=r.researchid
        WHERE ru.userid=#{research.userid} AND r.researchid!=#{research.researchid} and r.starttime&lt;=#{research.createtime}
        ORDER BY r.createtime DESC
    </select>
    <!--获取学校下用户教研评价-->
    <select id="listUserByRidAndSid" resultType="java.util.Map">
        select ru.ruid,ru.userid,ru.content,ru.reviewtime,ru.createtime,u.username,u.headpic
        from t_research_user ru left join t_user u on ru.userid=u.userid
        where ru.researchid=#{researchid} and ru.rustate=1 and ru.schoolid=#{schoolid}
        <if test="userid!=null and userid!=0">and ru.userid=#{userid}</if>
        order by ru.reviewtime desc
    </select>

    <resultMap id="researchSchoolUser" type="java.util.LinkedHashMap">
        <result column="schoolid" property="schoolid"></result>
        <result column="schoolname" property="schoolname"></result>
        <collection property="rusers" ofType="java.util.Map" javaType="java.util.List">
            <result column="ruid" property="ruid"></result>
            <result column="userid" property="userid"></result>
            <result column="content" property="content"></result>
            <result column="reviewtime" property="reviewtime"></result>
            <result column="rustate" property="rustate"></result>
            <result column="createname" property="createname"></result>
            <result column="headpic" property="headpic"></result>
        </collection>
    </resultMap>
    <!--教研学校用户-->
    <select id="listReaearchSchools" resultType="java.util.Map">
        select ru.schoolid,s.schoolname
        ,count(case when ru.rustate=1 then ru.ruid else null end)num
        from t_research_user ru
        left join t_school s on ru.schoolid=s.schoolid
        where ru.researchid=#{researchid}
        <if test="publiced!=null and publiced==1">
            and ru.schoolid=#{schoolid}
        </if>
        group by ru.schoolid
        order by if(ru.schoolid=#{schoolid},0,ru.schoolid),ru.reviewtime desc
    </select>

    <!--根据教研教师id获取教研截至时间-->
    <select id="getResearchStoptime" resultType="com.xhkjedu.sapi.model.research.TResearch">
        select r.stoptime,ru.userid from t_research r left join t_research_user ru on r.researchid=ru.researchid
        where ru.ruid=#{ruid}
    </select>

    <!--删除已评价教研-->
    <update id="delTeacherResearch">
        update t_research_user set rustate=0,content=null,reviewtime=null where ruid=#{ruid}
    </update>

    <!--修改已评价教研-->
    <update id="updateTeacherResearch" parameterType="com.xhkjedu.sapi.model.research.TResearchUser">
        update t_research_user set content=#{ru.content},reviewtime=#{ru.reviewtime} where ruid=#{ru.ruid}
    </update>

    <!--获取教研教师其他信息-->
    <select id="listResearchTeacherOthers" resultType="com.xhkjedu.sapi.vo.research.RteacherOtherVo">
        select tp.subjectid,c.gradeid,tp.teacherid
        from t_school s
                 left join t_class c on c.schoolid=s.schoolid and c.year=s.year
                 left join t_class_teacher ct on c.classid=ct.classid
                 left join t_teacher_powersb tp on ct.teacherid=tp.teacherid
        where c.classtype in(1,2) and c.classstate=1 and ct.teacherid in
        <foreach collection="userids" item="u" open="(" close=")" separator=",">
            ${u}
        </foreach>
        group by tp.teacherid
    </select>
</mapper>
