<?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.sims.fpiMgt.affairResult.mapper.ResultMapper">
  <sql id="selectResultBoardListWhere">
    <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='clearTitle != null and clearTitle != ""'>
        and a.clear_title like '%'||#{clearTitle}||'%'
      </if>
      <if test='resultTitle != null and resultTitle != ""'>
        and a.result_title like '%'||#{resultTitle}||'%'
      </if>
      <if test='resultState != null and resultState != ""'>
        and a.result_state = #{resultState}
      </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="downOrganCdList != null">
        and a.wrt_organ in
        <foreach collection="downOrganCdList" item="organCd" separator="," open="(" close=")">
          #{organCd}
        </foreach>
      </if>
      <if test='userType != null and userType != ""'>
        <if test='userType == "normalStayList"'>
          and a.result_state in ('DST002', 'DST004')
        </if>
        <if test='userType == "normalCommitList"'>
          and a.result_state in ('DST003', 'DST005', 'DST006')
        </if>
        <if test='userType == "sectionCommitList"'>
          and a.result_state in ('DST003', 'DST004', 'DST005', 'DST006')
        </if>
        <if test='userType == "headCommitList"'>
          and a.result_state in ('DST003', 'DST005', 'DST006')
        </if>
        <if test='userType == "managerStayList"'>
          and a.result_state in ('DST002', 'DST004')
        </if>
        <if test='userType == "managerCommitList"'>
          and a.result_state in ('DST003', 'DST005', 'DST006')
        </if>
      </if>
    </where>
  </sql>
  <select id="selectResultBoardList" resultType="ResultBoard" parameterType="ResultBoard">
    select a.result_key,
           a.work_start_dt,
           a.work_end_dt,
           a.result_title,
           a.result_state,
           a.wrt_organ,
           a.wrt_part,
           a.wrt_user_nm,
           a.wrt_user_seq,
           a.wrt_dt,
           b.fileCnt,
           c.affairCnt
    from result_board a
           left outer join (select result_key,
                                   count(file_seq) as fileCnt
                            from result_file
                            group by result_key) b
                on a.result_key = b.result_key
           left outer join (select result_key,
                                   count(*) as affairCnt
                            from result_to_affair
                            group by result_key) c
                on a.result_key = c.result_key
    <include refid="selectResultBoardListWhere"></include>
    order by result_key desc
    limit #{rowCnt} offset #{firstIndex}
  </select>
  <select id="selectResultBoardListCnt" resultType="int" parameterType="ResultBoard">
    select count(*)
    from result_board a
    <include refid="selectResultBoardListWhere"></include>
  </select>

  <select id="selectAffairToResultKey" resultType="AffairBoard" parameterType="int">
    select a.affair_key,
           a.title,
           a.wrt_dt,
           b.affair_rate,
           b.organ_up
    from affair_board a
            inner join affair_rating b
                on a.affair_key = b.affair_key and a.wrt_organ = b.rating_organ
            inner join result_to_affair c
                on a.affair_key = c.affair_key
    where c.result_key = #{resultKey}
  </select>
</mapper>