<?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.notice.NoticeUserMapper">
    <!--批量添加-->
    <insert id="batchSave" parameterType="com.xhkjedu.sapi.model.notice.TNotice">
        INSERT INTO t_notice_user (noticeid,userid,createtime,gradeid,classid,subjectid)
        VALUES
        <foreach collection ="notice.users" item="t" index= "index" separator =",">
            (#{notice.noticeid},#{t.userid},#{notice.createtime}
            ,#{t.gradeid},#{t.classid},#{t.subjectid})
        </foreach>
    </insert>
    <!--查看-->
    <update id="reade">
        update t_notice_user nt set nt.readed=1,nt.readtime=#{timestamp}
        where nt.noticeid=#{noticeid} and nt.userid=#{userid}
    </update>
    <!--删除通知对象-->
    <delete id="deleteByNoticeId">
        delete from t_notice_user where noticeid=#{noticeid}
    </delete>
    <!--通知对象-->
    <resultMap id="userResult" type="java.util.Map">
        <result property="gradeid" column="gradeid"/>
        <result property="classid" column="classid"/>
        <result property="subjectid" column="subjectid"/>
        <result property="name" column="name"/>
        <collection property="users" ofType="java.util.Map" javaType="java.util.List">
            <result property="userid" column="userid"/>
            <result property="readed" column="readed"/>
            <result property="readtime" column="readtime"/>
            <result property="username" column="username"/>
            <result property="headpic" column="headpic"/>
        </collection>
    </resultMap>
    <select id="listByNoticeId" resultMap="userResult">
        select nu.userid,nu.readed,nu.readtime,u.username,u.headpic,nu.gradeid,nu.classid,nu.subjectid
        ,if(nu.classid is not null,(select c.classname from t_class c where c.classid=nu.classid)
            ,(select s.subjectname from t_subject s where s.subjectid=nu.subjectid))name
        from t_notice_user nu left join t_user u on nu.userid = u.userid
        where nu.noticeid=#{nu.noticeid}
        <if test="nu.noticetype!=null and nu.noticetype==2">and nu.classid is null</if>
        <if test="nu.noticetype!=null and nu.noticetype==3">and nu.subjectid is null</if>
        <if test="nu.gradeid!=null and nu.gradeid!=0">and nu.gradeid=#{nu.gradeid}</if>
        <if test="nu.subjectid!=null and nu.subjectid!=''">and nu.subjectid=#{nu.subjectid}</if>
        <if test="nu.classid!=null and nu.classid!=0">and nu.classid=#{nu.classid}</if>
        <if test="nu.readed!=null and nu.readed!=-1">and nu.readed=#{nu.readed}</if>
        <if test="nu.username!=null and nu.username!=''">and u.username like '%${nu.username}%'</if>
    </select>
    <!--获取通知科目和班级-->
    <select id="listSubjectAndClass" resultType="com.xhkjedu.sapi.model.notice.TNoticeUser">
        select nu.gradeid,nu.classid,nu.subjectid
        ,if(nu.classid is not null,(select concat(c.classname,'_',c.classtype,'-',c.classorder) from t_class c where c.classid=nu.classid)
            ,(select concat(s.subjectname,'_',s.subjectorder) from t_subject s where s.subjectid=nu.subjectid))username
        from t_notice_user nu where noticeid=#{noticeid} and (nu.classid is not null or nu.subjectid is not null)
        group by nu.gradeid,nu.classid,nu.subjectid
    </select>
</mapper>
