<?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="cokr.xit.ens.modules.kkotalk.mapper.IKkoTalkMapper">
    <!-- // FIXME: 카카오톡 신규 추가 -->
    <!-- =================================================================================== -->
    <!-- ================================ accept =========================================== -->
    <!-- =================================================================================== -->
    <insert id="saveSndDtlKkoTalk" parameterType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|saveSndDtlKkoTalk-카카오톡발송상세생성|julim  */
        INSERT INTO ens_snd_dtl_kko_talk (
            send_detail_id,
            title,
            link,
            hash,
            guide,
            payload,
            read_expires_at,
            review_expires_at,
            use_non_personalized_noti,
            status,
            ci,
            phone_number,
            name,
            birthday,
            external_id,
            mk_jid,
            mk_tmplt_msg_json_data,
            error_code,
            error_message,
            mk_bill_use_yn,
            bill_uid,
            send_mast_id,
            regist_dt
        ) VALUES (
            #{sendDetailId},
            #{title},
            #{link},
            #{hash},
            #{guide},
            #{payload},
            #{readExpiresAt},
            #{reviewExpiresAt},
            #{useNonPersonalizedNotification},
            #{status},
            #{ci},
            #{phoneNumber},
            #{name},
            #{birthday},
            #{externalId},
            #{mkJid},
            #{mkTmpltMsgJsonData},
            #{errorCode},
            #{errorMessage},
            #{mkBillUseYn},
            #{billUid},
            #{sendMastId},
            sysdate
        )
    </insert>

    <update id="updateSndDtlKkoTalk" parameterType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|updateSndDtlKkoTalk-카카오톡발송상세변경|julim  */
        UPDATE ens_snd_dtl_kko_talk
           SET title = #{title}
             , hash = #{hash}
             , guide = #{guide}
             , payload = #{payload}
             , use_non_personalized_noti = #{useNonPersonalizedNotification}
             , ci = #{ci}
             , phone_number = #{phoneNumber}
             , name = #{name}
             , birthday = #{birthday}
             , external_id = #{externalId}
             , error_code = #{errorCode}
             , error_message = #{errorMessage}
             , last_updt_dt = sysdate
         WHERE send_detail_id = #{sendDetailId}
    </update>

    <update id="updateKakaotalkSendBulksResult" parameterType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|updateKakaotalkSendBulksResult-카카오톡 문서요청 결과 반영|limju  */
        UPDATE ens_snd_dtl_kko_talk
           SET external_id = #{externalId}
             , envelope_id = #{envelopeId}
             , status = #{status}
             , error_code = #{errorCode}
             , error_message = #{errorMessage}
             , last_updt_dt = sysdate
         WHERE send_detail_id = #{sendDetailId}
    </update>

    <update id="updateKakaotalkStatusBulksResult" parameterType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|updateKakaotalkStatusBulksResult-카카오톡 문서 상태조회 결과 반영|limju  */
        UPDATE ens_snd_dtl_kko_talk
           SET status = #{status}
             , sent_at = #{sentAt}
             , received_at = #{receivedAt}
             , read_at = #{readAt}
             , read_expires_at = #{readExpiredAt}
             , authenticated_at = #{authenticatedAt}
             , ott_verified_at = #{ottVerifiedAt}
             , user_notified_at = #{userNotifiedAt}
             , distribution_received_at = #{distributionReceivedAt}
             , payload = #{payload}
             , error_code = #{errorCode}
             , error_message = #{errorMessage}
             , last_updt_dt = sysdate
         WHERE send_detail_id = #{sendDetailId}
    </update>

    <select id="findAllBySendMastId" parameterType="long" resultType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|findAllBySendMastId-카카오톡발송대상 조회|julim  */
        <include refid="sqlSelectSendDetailKkoTalk"/>
          FROM ens_snd_dtl_kko_talk
         WHERE send_mast_id = #{sendMastId}
    </select>

    <select id="findAllBySendMastAndEnvlopeIdIsNotNull" parameterType="cokr.xit.ens.modules.common.domain.SendMast" resultType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|findAllBySendMastAndEnvlopeIdIsNotNull-카카오톡 발송 결과 대상?? 조회|julim  */
        <include refid="sqlSelectSendDetailKkoTalk"/>
          FROM ens_snd_dtl_kko_talk
         WHERE send_mast_id = #{sendMastId}
           AND envelope_id IS NOT NULL
    </select>

    <select id="findAllFetchBySendMastId" parameterType="long" resultType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|findAllFetchBySendMastId-카카오톡 발송 결과 대상 조회|julim  */
        <include refid="sqlSelectSendDetailKkoTalk"/>
        FROM ens_snd_dtl_kko_talk
        WHERE send_mast_id = #{sendMastId}
    </select>

    <select id="findFetchByExternalIdAndEnvelopeId" parameterType="map" resultType="cokr.xit.ens.modules.kkotalk.model.SendDetailKkoTalkDTO">
        /** iup-kkotalk-mapper|findFetchByExternalIdAndEnvelopeId-카카오톡 발송 결과 대상 조회|julim  */
        <include refid="sqlSelectSendDetailKkoTalk"/>
        FROM ens_snd_dtl_kko_talk
        WHERE envelope_id = #{envelopeId}
          <if test="externalId != null and externalId != ''">
          AND external_id = #{externalId}
          </if>
    </select>

    <select id="selectSendMastByExternalIdAndEnvelopeId" parameterType="map" resultType="cokr.xit.ens.modules.common.domain.SendMast">
        /** iup-kkotalk-mapper|selectSendMastByExternalIdAndEnvelopeId-전송마스터 정보 조회|julim  */
        SELECT esm.org_cd
             , esm.post_se
          FROM ens_snd_mast esm
          JOIN ens_snd_dtl_kko_talk esdkt
            ON esm.send_mast_id = esdkt.send_mast_id
         WHERE esdkt.external_id = #{externalId}
           AND esdkt.envelope_id = #{envelopeId}
    </select>

    <select id="selectBillByBillUid" parameterType="map" resultType="cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.Bill">
        /** iup-kkotalk-mapper|selectBillByBillUid-Bill 정보 조회|julim  */
        SELECT bill_id
             , bill_uid
             , bill_se_cd
        FROM ens_bill
        WHERE bill_uid = #{billUid}
    </select>

    <sql id="sqlSelectSendDetailKkoTalk">
        SELECT send_detail_id,
               title,
               link,
               hash,
               guide,
               payload,
               read_expires_at,
               review_expires_at,
               use_non_personalized_noti AS useNonPersonalizedNotification,
               status,
               ci,
               phone_number,
               name,
               birthday,
               external_id,
               envelope_id,
               mk_jid,
               mk_tmplt_msg_json_data,
               error_code,
               error_message,
               mk_bill_use_yn,
               bill_uid,
               send_mast_id,
               regist_dt
    </sql>

</mapper>
