<?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">

<!-- UIWS 이식: 일정/일지/첨부 (tb_uiws_schedule / tb_uiws_diary / tb_uiws_attach). -->
<mapper namespace="com.zioinfo.mall.uiws.schedule.mapper.UiwsScheduleMapper">

    <!-- ===================== schedule ===================== -->
    <insert id="insertSchedule" useGeneratedKeys="true" keyProperty="scheduleId" keyColumn="schedule_id"
            parameterType="com.zioinfo.mall.uiws.schedule.model.UiwsSchedule">
        INSERT INTO tb_uiws_schedule
            (sche_type, title, sche_gubun_cd, importance_cd, start_dt, end_dt, content,
             owner_id, dept_id, charger_id, created_by, created_at)
        VALUES
            (#{scheType}, #{title}, #{scheGubunCd}, #{importanceCd}, #{startDt}, #{endDt}, #{content},
             #{ownerId}, #{deptId}, #{chargerId}, #{createdBy}, #{createdAt})
    </insert>

    <update id="updateSchedule" parameterType="com.zioinfo.mall.uiws.schedule.model.UiwsSchedule">
        UPDATE tb_uiws_schedule SET
            sche_type = #{scheType}, title = #{title}, sche_gubun_cd = #{scheGubunCd},
            importance_cd = #{importanceCd}, start_dt = #{startDt}, end_dt = #{endDt},
            content = #{content}, dept_id = #{deptId}, charger_id = #{chargerId},
            updated_by = #{updatedBy}, updated_at = #{updatedAt}
        WHERE schedule_id = #{scheduleId}
    </update>

    <select id="findScheduleById" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsSchedule">
        SELECT * FROM tb_uiws_schedule WHERE schedule_id = #{scheduleId}
    </select>

    <select id="existsScheduleById" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_schedule WHERE schedule_id = #{scheduleId})
    </select>

    <delete id="deleteScheduleById">
        DELETE FROM tb_uiws_schedule WHERE schedule_id = #{scheduleId}
    </delete>

    <!-- 기간 겹침: start_dt &lt; to(배타 상한) AND end_dt &gt;= from -->
    <select id="findInRange" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsSchedule">
        SELECT * FROM tb_uiws_schedule
        WHERE sche_type = #{scheType}
          AND start_dt &lt; #{to}
          AND end_dt &gt;= #{from}
        <if test="ownerId != null">AND owner_id = #{ownerId}</if>
        <if test="deptId != null">AND dept_id = #{deptId}</if>
        ORDER BY start_dt ASC, schedule_id ASC
    </select>

    <sql id="allWhere">
        WHERE start_dt &lt; #{to}
          AND end_dt &gt;= #{from}
        <if test="scheType != null">AND sche_type = #{scheType}</if>
        <if test="!scopeAll">
            AND owner_id IN
            <foreach collection="ownerIds" item="oid" open="(" separator="," close=")">#{oid}</foreach>
        </if>
    </sql>

    <select id="searchAll" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsSchedule">
        SELECT * FROM tb_uiws_schedule
        <include refid="allWhere"/>
        ORDER BY start_dt DESC, schedule_id DESC
        LIMIT #{limit} OFFSET #{offset}
    </select>

    <select id="countAll" resultType="long">
        SELECT COUNT(*) FROM tb_uiws_schedule
        <include refid="allWhere"/>
    </select>

    <select id="searchPopup" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsSchedule">
        SELECT * FROM tb_uiws_schedule
        WHERE 1=1
        <if test="keyword != null">
            AND (title LIKE '%' || #{keyword} || '%' OR content LIKE '%' || #{keyword} || '%')
        </if>
        <if test="!scopeAll">
            AND owner_id IN
            <foreach collection="ownerIds" item="oid" open="(" separator="," close=")">#{oid}</foreach>
        </if>
        ORDER BY start_dt DESC, schedule_id DESC
        LIMIT 50
    </select>

    <!-- ===================== diary ===================== -->
    <insert id="insertDiary" useGeneratedKeys="true" keyProperty="diaryId" keyColumn="diary_id"
            parameterType="com.zioinfo.mall.uiws.schedule.model.UiwsDiary">
        INSERT INTO tb_uiws_diary
            (title, content, schedule_id, writer_id, diary_date, created_by, created_at)
        VALUES
            (#{title}, #{content}, #{scheduleId}, #{writerId}, #{diaryDate}, #{createdBy}, #{createdAt})
    </insert>

    <update id="updateDiary" parameterType="com.zioinfo.mall.uiws.schedule.model.UiwsDiary">
        UPDATE tb_uiws_diary SET
            title = #{title}, content = #{content}, schedule_id = #{scheduleId}, diary_date = #{diaryDate},
            updated_by = #{updatedBy}, updated_at = #{updatedAt}
        WHERE diary_id = #{diaryId}
    </update>

    <select id="findDiaryById" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsDiary">
        SELECT * FROM tb_uiws_diary WHERE diary_id = #{diaryId}
    </select>

    <select id="existsDiaryById" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_diary WHERE diary_id = #{diaryId})
    </select>

    <delete id="deleteDiaryById">
        DELETE FROM tb_uiws_diary WHERE diary_id = #{diaryId}
    </delete>

    <select id="searchDiary" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsDiary">
        SELECT * FROM tb_uiws_diary
        WHERE (diary_date IS NULL OR diary_date BETWEEN #{from} AND #{to})
        ORDER BY COALESCE(diary_date, created_at::date) DESC, diary_id DESC
        LIMIT #{limit} OFFSET #{offset}
    </select>

    <select id="countDiary" resultType="long">
        SELECT COUNT(*) FROM tb_uiws_diary
        WHERE (diary_date IS NULL OR diary_date BETWEEN #{from} AND #{to})
    </select>

    <!-- ===================== attach ===================== -->
    <insert id="insertAttach" useGeneratedKeys="true" keyProperty="attachId" keyColumn="attach_id"
            parameterType="com.zioinfo.mall.uiws.schedule.model.UiwsAttach">
        INSERT INTO tb_uiws_attach
            (ref_type, ref_id, file_nm, file_path, file_size, created_by, created_at)
        VALUES
            (#{refType}, #{refId}, #{fileNm}, #{filePath}, #{fileSize}, #{createdBy}, #{createdAt})
    </insert>

    <select id="findAttachById" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsAttach">
        SELECT * FROM tb_uiws_attach WHERE attach_id = #{attachId}
    </select>

    <update id="updateAttachRef">
        UPDATE tb_uiws_attach
        SET ref_type = #{refType}, ref_id = #{refId}, updated_by = #{actor}, updated_at = now()
        WHERE attach_id = #{attachId}
    </update>

    <select id="findAttachByRef" resultType="com.zioinfo.mall.uiws.schedule.model.UiwsAttach">
        SELECT * FROM tb_uiws_attach
        WHERE ref_type = #{refType} AND ref_id = #{refId}
        ORDER BY attach_id ASC
    </select>

    <delete id="deleteAttachById">
        DELETE FROM tb_uiws_attach WHERE attach_id = #{attachId}
    </delete>

    <delete id="deleteAttachByRef">
        DELETE FROM tb_uiws_attach WHERE ref_type = #{refType} AND ref_id = #{refId}
    </delete>

    <!-- ===================== 사용자명 라벨 ===================== -->
    <select id="findUserNames" resultType="map">
        SELECT username AS "username", display_name AS "fullName"
        FROM mall_account
        WHERE username IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">#{id}</foreach>
    </select>

</mapper>
