<?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.its.web.mapper.its.common.CommonMapper">
    <select id="findAllOrganization" resultType="com.its.web.dto.common.TbWwwOrgDto">
        SELECT organ_id, name, url FROM TB_WWW_ORGAN
    </select>

    <select id="findDayList" resultType="com.its.web.dto.common.CodeDto">
        SELECT
            CMMN_CD AS code,
            CMMN_CD_KOR_NM AS name
        FROM TB_CMMN_CD
        WHERE CMMN_CLSF_CD = 'DTW'
    </select>

    <insert id="insertConnHs">
        MERGE INTO TB_WWW_CONN_HS A USING DUAL
           ON (A.CONN_DAY = TO_CHAR(sysdate, 'YYYYMMDD'))
         WHEN MATCHED THEN UPDATE SET A.CONN_CNT = (A.CONN_CNT + 1)
         WHEN NOT MATCHED THEN INSERT (CONN_DAY, CONN_CNT) VALUES (TO_CHAR(sysdate, 'YYYYMMDD'), 1)
    </insert>

    <select id="getConnStatistics" resultType="com.its.web.dto.common.ConnStatisticsDto" parameterType="java.util.HashMap">
        SELECT
            TO_CHAR(TO_DATE(A.DAYS, 'YYYYMMDD'), 'YYYY-MM-DD') AS conn_dt,
            NVL(B.conn_cnt, 0) AS conn_cnt
          FROM ( SELECT #{toDate} DAYS FROM DUAL
                 UNION ALL
                 SELECT TO_CHAR(TRUNC(TO_DATE(#{toDate}), 'DD') - LEVEL, 'YYYYMMDD') DAYS FROM DUAL
                 CONNECT BY LEVEL <![CDATA[<=]]> (TO_DATE(#{toDate}) - TO_DATE(#{fromDate}))
                ) A
          LEFT OUTER JOIN TB_WWW_CONN_HS B
                       ON A.DAYS = B.conn_day
         ORDER BY conn_dt ASC
    </select>

    <select id="getConnWeekStatistics" resultType="com.its.web.dto.common.ConnStatisticsDto" parameterType="java.util.HashMap">
        SELECT
            TO_CHAR(TO_DATE(A.start_dt, 'YYYYMMDD'), 'YYYY-MM-DD') || ' ~ ' || TO_CHAR(TO_DATE(A.end_dt, 'YYYYMMDD'), 'YYYY-MM-DD') AS conn_dt,
            SUM(NVL(B.conn_cnt, 0)) AS conn_cnt
          FROM (SELECT TO_CHAR(TRUNC(TO_DATE(#{toDate}), 'DD') - 6, 'YYYYMMDD') AS start_dt, #{toDate} AS end_dt FROM DUAL
                UNION ALL
                SELECT
                    TO_CHAR(TRUNC(TO_DATE(#{toDate}), 'DD') - (7 * (LEVEL + 1) - 1), 'YYYYMMDD') AS start_dt,
                    TO_CHAR(TRUNC(TO_DATE(#{toDate}), 'DD') - (7 * LEVEL), 'YYYYMMDD') AS end_dt
                  FROM DUAL
                CONNECT BY LEVEL <![CDATA[<=]]> FLOOR((TO_DATE(#{toDate}) - TO_DATE(#{fromDate})) / 7)) A
        LEFT OUTER JOIN TB_WWW_CONN_HS B ON TO_DATE(B.conn_day, 'YYYYMMDD') BETWEEN A.start_dt and A.end_dt
        GROUP BY TO_CHAR(TO_DATE(A.start_dt, 'YYYYMMDD'), 'YYYY-MM-DD') || ' ~ ' || TO_CHAR(TO_DATE(A.end_dt, 'YYYYMMDD'), 'YYYY-MM-DD')
        ORDER BY TO_CHAR(TO_DATE(A.start_dt, 'YYYYMMDD'), 'YYYY-MM-DD') || ' ~ ' || TO_CHAR(TO_DATE(A.end_dt, 'YYYYMMDD'), 'YYYY-MM-DD')
    </select>

    <select id="getConnMonthStatistics" resultType="com.its.web.dto.common.ConnStatisticsDto" parameterType="java.util.HashMap">
        SELECT
            TO_CHAR(TO_DATE(A.MONTHS, 'YYYYMM'), 'YYYY-MM') AS conn_dt,
            SUM(NVL(B.conn_cnt, 0)) AS conn_cnt
        FROM ( SELECT #{toDate} AS MONTHS FROM DUAL
               UNION ALL
               SELECT TO_CHAR(ADD_MONTHS(TO_DATE(#{toDate}, 'YYYYMM'), -LEVEL), 'YYYYMM') MONTHS FROM DUAL
               CONNECT BY LEVEL <![CDATA[<=]]> MONTHS_BETWEEN(TO_DATE(#{toDate}, 'YYYYMM'), TO_DATE(#{fromDate}, 'YYYYMM'))
              ) A
        LEFT OUTER JOIN TB_WWW_CONN_HS B ON A.MONTHS = SUBSTR(B.conn_day, 0, 6)
        GROUP BY TO_CHAR(TO_DATE(A.MONTHS, 'YYYYMM'), 'YYYY-MM')
        ORDER BY TO_CHAR(TO_DATE(A.MONTHS, 'YYYYMM'), 'YYYY-MM')
    </select>

</mapper>