<?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.ivsgtMgt.boardInvestigation.mapper.BoardInvestigationMapper">
    <sql id="selectBoardInvestigationListWhere">
    <where>
        <if test='ivsgtType != null and ivsgtType != ""'>
            AND ivsgt_type = #{ivsgtType}
        </if>
        <if test='wrtNm != null and wrtNm != ""'>
            AND a.wrt_nm LIKE '%'||#{wrtNm}||'%'
        </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='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+1
            </if>
        </if>
    </where>
    </sql>
    <select id="selectBoardInvestigationList" resultType="BoardInvestigation" parameterType="BoardInvestigation">
    SELECT
        a.ivsgt_key,
        a.ivsgt_type,
        a.content_title,
        a.content_info,
        a.content_main,
        a.content_status,
        a.wrt_organ,
        a.wrt_user_seq,
        a.wrt_nm,
        a.wrt_dt,
        b.fileCnt,
        t.arrest_type_key,
        t.arrest_cd,
        t.arrest_cd2,
        cm.item_value AS arrestCdName,
        cm2.item_value AS arrestCd2Name
    FROM board_investigation a
    LEFT OUTER JOIN (
        SELECT
            ivsgt_key,
            count(file_seq) AS fileCnt
        FROM ivsgt_file
        GROUP BY ivsgt_key
    ) b
    ON a.ivsgt_key = b.ivsgt_key
    INNER JOIN arrest_type t
    ON a.ivsgt_key = t.ivsgt_key
    INNER JOIN code_mgt cm
    ON t.arrest_cd  = cm.item_cd
    INNER JOIN code_mgt cm2
    ON t.arrest_cd2  = cm2.item_cd
    <include refid="selectBoardInvestigationListWhere"></include>
    ORDER BY ivsgt_key DESC
    LIMIT #{rowCnt} OFFSET #{firstIndex}
    </select>
    <select id="selectBoardInvestigationListCnt" resultType="int" parameterType="BoardInvestigation">
    SELECT count(*)
    FROM board_investigation a
    <include refid="selectBoardInvestigationListWhere"></include>
    </select>
    <select id="selectHashTags" resultType="string" parameterType="int">
        SELECT aa.hashTags
        FROM (
            SELECT
                array_to_string(array_agg(b.tag_nm), ' ') AS hashTags
            FROM hash_tag_link_ivsgt a
            INNER JOIN hash_tag b ON a.tag_key = b.tag_key
            WHERE a.ivsgt_key = #{ivsgtKey}
            GROUP BY a.ivsgt_key) aa
    </select>
    <select id="selectArrestType" resultType="ArrestType" parameterType="int">
        SELECT
            a.arrest_type_key,
            a.ivsgt_key,
            a.arrest_cd,
            a.arrest_cd2,
            cm.item_value AS arrestCdName,
            cm2.item_value AS arrestCd2Name
        FROM arrest_type a
        INNER JOIN board_investigation i
        ON i.ivsgt_key = a.ivsgt_key
        INNER JOIN code_mgt cm
        ON a.arrest_cd = cm.item_cd
        INNER JOIN code_mgt cm2
        ON a.arrest_cd2 = cm2.item_cd
        WHERE a.ivsgt_key = #{ivsgtKey}
    </select>

    <select id="selectRelatedReportsList" resultType="RelatedReports" parameterType="int">
        SELECT
            a.related_reports_key,
            a.child_ivsgt_key,
            a.parent_ivsgt_key,
            i.content_title
        FROM related_reports a
        INNER JOIN board_investigation i
        ON i.ivsgt_key = a.child_ivsgt_key
        WHERE a.parent_ivsgt_key = #{ivsgtKey}
    </select>
</mapper>