<?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.NoticeMapper">
    <!-- 所有通知 -->
    <select id="listAll" resultType="com.xhkjedu.sapi.vo.notice.NoticeVo">
        select n.noticeid,n.noticetitle,n.createtime,u.username createname,n.noticetype
        from t_notice n left join t_user u on n.createid=u.userid
        <if test="notice.schoolid!=null">
        left join t_notice_school ns on n.noticeid = ns.noticeid
        </if>
        <where>
            <if test="notice.schoolid!=null">
                and ns.schoolid=#{notice.schoolid}
            </if>
            <if test="notice.schoolid==null">
                and n.schoolid is null
            </if>
            <if test="notice.createid!=null and notice.createid!=0">
                and n.createid=#{notice.createid}
            </if>
        </where>
        order by n.createtime desc
    </select>
    <!--详情-->
    <!--通知班级列表-->
    <select id="listClass" resultType="com.xhkjedu.sapi.vo.gradeclass.ClassVo">
        select c.gradeid,nc.classid,c.classname
        from t_notice_class nc left join t_class c on c.classid=nc.classid
        where nc.noticeid=#{noticeid}
        order by c.gradeid,c.classtype,c.classorder
    </select>
    <!--通知教师列表-->
    <select id="listTeacher" resultType="com.xhkjedu.sapi.vo.notice.NoticeTeacherVo">
        select t.teacherid,tu.username teachername,t.readed
        from t_notice_teacher t left join t_user tu on t.teacherid = tu.userid
        where t.noticeid=#{noticeid}
    </select>
    <!--通知学校列表-->
    <select id="listSchool" resultType="com.xhkjedu.sapi.model.system.TSchool">
        select ns.schoolid,s.schoolname
        from t_notice_school ns left join t_school s on ns.schoolid = s.schoolid
        where ns.noticeid=#{noticeid}
    </select>
    <select id="findById" resultType="com.xhkjedu.sapi.model.notice.TNotice">
        select n.*,u.username createname
        from t_notice n left join t_user u on n.createid=u.userid
        where n.noticeid=#{noticeid} limit 1
    </select>
    <!--教师、学生查看详情-->
    <select id="findById2" resultType="com.xhkjedu.sapi.model.notice.TNotice">
        select n.*,u.username createname
        from t_notice n left join t_user u on n.createid=u.userid
        where n.noticeid=#{noticeid} limit 1
    </select>
    <!-- 教师通知列表 -->
    <select id="listMy" resultType="com.xhkjedu.sapi.vo.notice.NoticeVo">
        select n.noticeid,n.noticetitle,n.createtime,u.username createname,IFNULL(nt.readed,0)readed
        from t_notice n left join t_user u on n.createid=u.userid
        left join t_notice_teacher nt on n.noticeid = nt.noticeid
        where nt.teacherid=#{notice.createid}
        <if test="notice.readed!=null"> and nt.readed=#{notice.readed}</if>
        group by n.noticeid order by nt.readed asc,n.createtime desc
        <if test="notice.page==null">
            limit #{notice.pageSize}
        </if>
    </select>
    <!--教师未看通知数量-->
    <select id="noReadNum" resultType="java.lang.Integer">
        select count(*) from t_notice_teacher t where t.teacherid=#{teacherid} and t.readed=0
    </select>
    <!--学生通知列表-->
    <select id="listStudent" resultType="com.xhkjedu.sapi.vo.notice.NoticeVo">
        select n.noticeid,n.noticetitle,n.noticetxt,n.createtime,u.username createname
        from t_notice n left join t_user u on n.createid=u.userid
        left join t_notice_class nc on n.noticeid = nc.noticeid
        left join t_class_student cs on nc.classid=cs.classid
        where cs.studentid=#{notice.createid}
        <if test="notice.createtime!=null and notice.createtime!=0">
            and n.createtime>=#{notice.createtime}
        </if>
        group by n.noticeid order by n.createtime desc
    </select>

    <!--//////////////////////////////////////////////学生WEB端//////////////////////////////////////////////-->
    <!--学生通知列表-->
    <select id="listNoticeForStuWeb" resultType="com.xhkjedu.sapi.vo.notice.NoticeVo">
        select n.noticeid,n.noticetitle,n.noticecontent,n.noticetxt,n.createtime,u.username createname
        from t_notice n left join t_user u on n.createid=u.userid
        left join t_notice_class nc on n.noticeid = nc.noticeid
        left join t_class_student cs on nc.classid=cs.classid
        where cs.studentid=#{notice.createid}
        <if test="notice.starttime!=null and notice.starttime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(n.createtime,'%Y%m%d'))>=#{notice.starttime}
        </if>
        <if test="notice.stoptime!=null and notice.stoptime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(n.createtime,'%Y%m%d'))&lt;=#{notice.stoptime}
        </if>
        group by n.noticeid order by n.createtime desc
    </select>
    <!--根据班级删除通知-->
    <delete id="deleteByClassId">
        delete n.* from t_notice n left join t_notice_class nc on n.noticeid = nc.noticeid
        where nc.classid=#{classid}
    </delete>
</mapper>
