<?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.fpiMgt.monthPlan.mapper.MonthPlanMapper">
  <sql id="selectPlanBoardListWhere">
    <where>
      <if test='wrtUserSeq != null and wrtUserSeq != ""'>
        and a.wrt_user_seq = #{wrtUserSeq}
      </if>
      <if test='wrtUserNm != null and wrtUserNm != ""'>
        and a.wrt_user_nm like '%'||#{wrtUserNm}||'%'
      </if>
      <if test='wrtOrgan != null and wrtOrgan != ""'>
        and a.wrt_organ = #{wrtOrgan}
      </if>
      <if test='contentTitle != null and contentTitle != ""'>
        and a.content_title like '%'||#{contentTitle}||'%'
      </if>
      <if test='planState != null and planState != ""'>
        and a.plan_state = #{planState}
      </if>
      <if test='dateSelector == "planDt"'>
        <if test='startDate != null and startDate != ""'>
          and a.plan_dt >= #{startDate}::date
        </if>
        <if test='endDate != null and endDate != ""'>
          and a.plan_dt &lt;= #{endDate}::date
        </if>
      </if>
      <if test='dateSelector == "wrtDt"'>
        <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
        </if>
      </if>
      <if test="organCdList != null">
        and a.wrt_organ in
        <foreach collection="organCdList" item="organCd" separator="," open="(" close=")">
          #{organCd}
        </foreach>
      </if>
      <if test='userType != null and userType != ""'>
        <if test='userType == "normalStayList"'>
          and a.plan_state in ('DST002', 'DST004')
        </if>
        <if test='userType == "normalCommitList"'>
          and a.plan_state in ('DST003', 'DST005', 'DST006')
        </if>
        <if test='userType == "sectionCommitList"'>
          and a.plan_state in ('DST003', 'DST004', 'DST005', 'DST006')
        </if>
        <if test='userType == "headCommitList"'>
          and a.plan_state in ('DST003', 'DST005', 'DST006')
        </if>
        <if test='userType == "managerStayList"'>
          and a.plan_state in ('DST002', 'DST004')
        </if>
        <if test='userType == "managerCommitList"'>
          and a.plan_state in ('DST003', 'DST005', 'DST006')
        </if>
      </if>
    </where>
  </sql>
  <select id="selectPlanBoardList" resultType="PlanBoard" parameterType="PlanBoard">
    select a.plan_key,
           a.content_title,
           a.plan_state,
           a.plan_dt,
           a.wrt_organ,
           a.wrt_part,
           a.wrt_user_nm,
           a.wrt_user_seq,
           a.wrt_dt,
           b.fileCnt
    from plan_board a
           left outer join (select plan_key,
                                   count(file_seq) as fileCnt
                            from plan_file
                            group by plan_key) b
             on a.plan_key = b.plan_key
    <include refid="selectPlanBoardListWhere"></include>
    order by plan_key desc
    limit #{rowCnt} offset #{firstIndex}
  </select>
  <select id="selectPlanBoardListCnt" resultType="int" parameterType="PlanBoard">
    select count(*)
    from plan_board a
    <include refid="selectPlanBoardListWhere"></include>
  </select>
</mapper>