<?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.dbnt.faisp.publicBoard.mapper.PublicBoardMapper">
  <sql id="selectContentListWhere">
    <where>
      <if test='publicType != null and publicType != ""'>
        and a.public_type = #{publicType}
      </if>
      <if test='wrtOrgan != null and wrtOrgan != ""'>
        and a.wrt_organ = #{wrtOrgan}
      </if>
      <if test="title != null and title != ''">
        AND a.title LIKE CONCAT('%', #{title}, '%')
      </if>
      <if test='startDate != null and startDate != ""'>
        and a.wrt_dt >= #{startDate}::date
      </if>
      <if test='endDate != null and endDate != ""'>
        and a.wrt_dt &lt;= #{endDate}::date+1
      </if>
      <if test='tabStatus != null and tabStatus != ""'>
        and a.tab_status = #{tabStatus}
      </if>
      <if test="downOrganCdList != null or upOrganCdList != null">
        and a.public_key in (
          <if test="downOrganCdList != null">
            select public_key from public_board where wrt_organ in
            <foreach collection="downOrganCdList" item="organCd" separator="," open="(" close=")">
              #{organCd}
            </foreach>
          </if>
          <if test="downOrganCdList != null and upOrganCdList != null">
            union all
          </if>
          <if test="upOrganCdList != null">
            select public_key from public_board where organ_chk = 'F' and wrt_organ in
            <foreach collection="upOrganCdList" item="organCd" separator="," open="(" close=")">
              #{organCd}
            </foreach>
          </if>
        )
      </if>
    </where>
  </sql>
  <select id="selectContentList" resultType="PublicBoard" parameterType="PublicBoard">
    select a.public_key,
           a.public_type,
           a.title,
           a.content,
           a.wrt_organ,
           a.wrt_part,
           a.wrt_user_nm,
           a.wrt_user_seq,
           a.wrt_dt,
           a.organ_chk,
           b.fileCnt,
           c.commentCnt
    from public_board a
           left outer join (select public_key,
                                   count(file_seq) as fileCnt
                            from public_file
                            group by public_key) b
                  on a.public_key = b.public_key
           left outer join (select public_key,
                                   count(comment_key) as commentCnt
                            from public_comment
                            group by public_key) c
                  on a.public_key = c.public_key
    <include refid="selectContentListWhere"></include>
    order by public_key desc
    limit #{rowCnt} offset #{firstIndex}
  </select>
  <select id="selectContentListCnt" resultType="int" parameterType="PublicBoard">
    select count(*)
    from public_board a
    <include refid="selectContentListWhere"></include>
  </select>
</mapper>