<?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.yida.data.school.news.mapper.EduNoticeSchoolMapper">
  <resultMap id="BaseResultMap" type="com.yida.data.common.core.entity.notice.EduNoticeSchool">
    <!--@mbg.generated-->
    <!--@Table edu_notice_school-->
    <id column="id" property="id"/>
    <result column="create_date" property="createDate"/>
    <result column="create_id" property="createId"/>
    <result column="create_name" property="createName"/>
    <result column="update_date" property="updateDate"/>
    <result column="update_id" property="updateId"/>
    <result column="del_flag" property="delFlag"/>
    <result column="title" property="title"/>
    <result column="description" property="description"/>
    <result column="cover_url" property="coverUrl"/>
    <result column="content" property="content"/>
    <result column="content_url" property="contentUrl"/>
    <result column="type" property="type"/>
    <result column="is_timing" property="isTiming"/>
    <result column="send_date" property="sendDate"/>
    <result column="is_confirm" property="isConfirm"/>
    <result column="status" property="status"/>
    <result column="school_id" property="schoolId"/>
    <result column="school_name" property="schoolName"/>
    <result column="campus_id" property="campusId"/>
    <result column="campus_name" property="campusName"/>
    <result column="is_public" property="isPublic"/>
    <collection property="receiverList"
      ofType="com.yida.data.common.core.entity.notice.EduNoticeSchoolReceiver">
      <id property="id" column="rId"/>
      <result property="deptId" column="dept_id"/>
      <result property="deptWxId" column="dept_wx_id"/>
      <result property="deptType" column="dept_type"/>
      <result property="deptName" column="dept_name"/>
      <result property="studentId" column="student_id"/>
      <result property="studentName" column="student_name"/>
      <result property="parentId" column="parent_id"/>
      <result property="parentWxId" column="parent_wx_id"/>
      <result property="parentName" column="parent_name"/>
    </collection>
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, create_date, create_id, create_name, update_date, update_id, del_flag, title,
    cover_url, content, content_url, `type`, is_timing, send_date, is_confirm, `status`,
    school_id, school_name, campus_id, campus_name
  </sql>

  <select id="getNoticeSchool" resultMap="BaseResultMap">
    select a.*,
           b.id rId,
           b.dept_id,
           b.dept_type,
           b.dept_name,
           b.dept_wx_id,
           b.student_id,
           b.student_name,
           b.parent_id,
           b.parent_wx_id,
           b.parent_name
    from edu_notice_school a
           left join edu_notice_school_receiver b on a.id = b.notice_id
    where a.del_flag = 0
      and a.id = #{noticeId}
  </select>

  <select id="listNotice" resultType="com.yida.data.school.vo.news.SchoolNoticePageVO">
    select a.id,
    a.send_date,
    a.title,
    a.description,
    a.cover_url,
    a.content,
    a.content_url,
    a.status,
    a.type,
    a.is_confirm,
    a.create_name author,
    count(b.parent_id) confirm_num
    from edu_notice_school a
    LEFT JOIN ( SELECT notice_id, parent_id FROM edu_notice_school_receive_student WHERE is_confirm = 1 GROUP BY notice_id,
    parent_id ) b ON a.id = b.notice_id
    where a.del_flag = 0 and a.status <![CDATA[!=]]> 2
    <if test="dto.status != null">
      and a.status = #{dto.status}
    </if>
    <if test="dto.title != null and dto.title != ''">
      and (a.title like concat('%', #{dto.title}, '%')
      or a.content like concat('%', #{dto.title}, '%'))
    </if>
    <if test="dto.schoolId != null">
      and a.school_id = #{dto.schoolId}
    </if>
    <if test="dto.isPublic != null">
      and a.is_public = #{dto.isPublic}
    </if>
    <if test="dto.notShowId != null">
      and a.id <![CDATA[!=]]> #{dto.notShowId}
    </if>
    group by a.id, a.send_date, a.title, a.description, a.cover_url, a.content, a.content_url, a.status, a.type,
    a.receiver_num, a.create_date
    order by a.create_date desc
  </select>

  <select id="listShowSchoolPage" resultType="com.yida.data.school.vo.news.SchoolNoticePageVO">
    select distinct a.id,
    a.send_date,
    a.title,
    a.description,
    a.cover_url,
    a.content,
    a.content_url,
    a.type,
    a.create_name author
    from edu_notice_school a
    left join edu_notice_school_receive_student b on a.id = b.notice_id
    where a.del_flag = 0
    <if test="isPublic != null and isPublic == 0">
      and b.parent_id = #{parentId}
    </if>
    <choose>
      <when test="schoolId != null">
        and school_id = #{schoolId}
      </when>
      <otherwise>
        and 0 = 1
      </otherwise>
    </choose>
    and a.status = 1
    and a.is_public = #{isPublic}
    order by a.send_date desc
  </select>

  <select id="listPublishNoticeByStaffPage" resultType="com.yida.data.school.vo.news.SchollNoticeTeacherPublishVO">
    select a.id,
    a.title,
    a.receiver_num,
    a.send_date,
    a.is_confirm,
    a.status,
    count(distinct b.parent_id) confirmNum,
    count(distinct c.parent_id)
    readParentNum,
    (select count(1) from edu_notice_school_read where notice_id=a.id) readNum
    from edu_notice_school a
    left join edu_notice_school_receive_student b on a.id = b.notice_id and b.is_confirm = 1
    left join edu_notice_school_read c on a.id = c.notice_id
    where a.del_flag = 0
    and a.type = 0
    and a.is_draft = 0
    and a.school_id = #{schoolId}
    and a.create_id = #{userId}
    <if test="title != null and title != ''">
      and a.title like concat('%', #{title}, '%')
    </if>
    group by a.id, a.title, a.receiver_num, a.send_date
    order by a.send_date desc
  </select>
</mapper>