<?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.letter.mapper.EduLetterBoxMapper">
  <select id="findListViewLetter" parameterType="com.yida.data.common.core.entity.letter.EduLetterBox"
    resultType="com.yida.data.common.core.entity.letter.EduLetterBox">
    select id
    from edu_letter_box
      where del_flag = '0'
    <if test="status != null and status != ''">
      and status = #{status}
    </if>
    <if test="title != null and title != ''">
      and title like concat('%', #{title}, '%')
    </if>
    <if test="reply != null and reply != ''">
      and reply = #{reply}
    </if>
    <if test="schoolId != null and schoolId != ''">
      and school_id = #{schoolId}
    </if>
    <if test="appId != null and appId != ''">
      and app_id = #{appId}
    </if>
    <if test="contentType != null and contentType != ''">
      and content_type = #{contentType}
    </if>
    <if test="createDate != null">
      and DATE_FORMAT(create_date, '%Y-%m-%d') = DATE_FORMAT(#{createDate}, '%Y-%m-%d')
    </if>
    order by sort_date is null, sort_date desc, status asc, reply, top desc
  </select>

  <select id="findListPage" resultType="com.yida.data.common.core.entity.letter.EduLetterBox">
    select *
    from edu_letter_box a
      where a.del_flag = '0'
    <if test="title != null and title != ''">
      and a.title LIKE CONCAT('%', #{title}, '%')
    </if>
    <if test="contentType != null and contentType != ''">
      and a.content_type = #{contentType}
    </if>
    <if test="reply != null">
      and a.reply = #{reply}
    </if>
    <if test="appPhone != null and appPhone != ''">
      and a.app_phone LIKE CONCAT('%', #{appPhone}, '%')
    </if>
    and a.school_id = #{schoolId}
      ORDER BY a.top desc, a.create_date desc
  </select>

  <select id="getTopNum" resultType="java.lang.Integer">
    SELECT count(top) as top_num
    FROM edu_letter_box
    WHERE top = 1
      and school_id = #{schoolId}
  </select>

  <select id="findViewList" resultType="com.yida.data.common.core.entity.letter.EduLetterBox">
    select *
    from edu_letter_box a
      where 1 = 1
        and a.del_flag = '0'
    <if test="title != null and title != ''">
      and a.title like concat('%', #{title}, '%')
    </if>
    <if test="schoolId != null and schoolId != ''">
      and a.school_id = #{schoolId}
    </if>
    <if test="identityId != null">
      and a.identity_id = #{identityId}
        order by a.create_date desc
    </if>
    <if test="identityId == null">
      and a.open = 1
    </if>
    <if test="identityId == null">
      <if test="viewsNum == 1">
        order by a.top DESC, a.views desc
      </if>
      <if test="viewsNum == null">
        order by a.top DESC, a.create_date desc
      </if>
    </if>
  </select>
  <update id="updateTop">
    UPDATE edu_letter_box
    SET top = null
    WHERE id = #{id}
  </update>
  <update id="updateByReply">
    update edu_letter_box
    set status        = 0,
        reply_content = null,
        `open`        = 0,
        views         = 0,
        reply         = 0,
        top           = NULL
    where id = #{id}
  </update>

  <select id="getStaData" resultType="com.yida.data.school.vo.letterbox.StaLetterVO">
    select count(1) receiveNum, ifnull(sum(if(reply = 0, 1, 0)),0) notReplyNum, ifnull(sum(if(reply = 0, 0, 1)),0) replyNum
    from edu_letter_box
      where del_flag = 0
        and create_date between concat(#{startDate}, ' 00:00:00') and concat(#{endDate}, ' 23:59:59')
        and school_id in
    <foreach collection="schoolIds" close=")" open="(" separator="," item="item">
      #{item}
    </foreach>
  </select>

  <select id="listTitle" resultType="java.lang.String">
    select title
    from edu_letter_box
    where del_flag = 0
    and create_date between concat(#{startDate}, ' 00:00:00') and concat(#{endDate}, ' 23:59:59')
    and school_id in
    <foreach collection="schoolIds" close=")" open="(" separator="," item="item">
      #{item}
    </foreach>
  </select>
</mapper>
