<?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="kr.xit.core.biz.batch.mapper.IBatchCmmMapper">
    <select id="selectBatchLockByInstanceId" resultType="kr.xit.core.biz.batch.model.BatchCmmDTO$BatchLock">
        /** batch-cmm-mysql-mapper|findById-배치실행상태조회|julim  */
        SELECT instance_id
             , use_yn
          FROM tb_cmm_batch_lock
         WHERE instance_id = #{instanceId}
        -- FOR UPDATE
    </select>

    <insert id="insertBatchLock">
        /** batch-cmm-mysql-mapper|insertBatchLock-배치락 데이타 생성|julim  */
        INSERT INTO tb_cmm_batch_lock (
            instance_id,
            regist_dt,
            use_yn
        ) VALUES (
            #{instanceId},
            NOW(3),
            #{useYn}
        )
    </insert>

    <insert id="updateBatchLock">
        /** batch-cmm-mysql-mapper|updateBatchLock-배치 실행 상태 및 결과 반영|julim  */
        UPDATE tb_cmm_batch_lock
           SET result = #{result}
             , message = SUBSTRING(#{message}, 1, 500)
             , use_yn = #{useYn}
             , updt_dt = NOW(3)
         WHERE instance_id = #{instanceId}
    </insert>

    <insert id="insertBatchLog">
        /** batch-cmm-mysql-mapper|insertBatchLog-배치 로그 데이타 생성|julim  */
        <selectKey keyProperty="batchLogId" resultType="string" order="BEFORE">
            SELECT LPAD(NEXTVAL(tb_cmm_batch_log_seq), 20, '0')
        </selectKey>
        INSERT INTO tb_cmm_batch_log (
            batch_log_id,
            instance_id,
            trace_id,
            result,
            message,
            regist_dt
        ) VALUES (
            #{batchLogId},
            #{instanceId},
            #{traceId},
            #{result},
            #{message},
            NOW(3)
        )
    </insert>

    <insert id="updateBatchLog">
        /** batch-cmm-mysql-mapper|updateBatchLog-배치 결과 반영|julim  */
        UPDATE tb_cmm_batch_log
           SET trace_id = IFNULL(#{traceId}, trace_id)
             , result = IFNULL(#{result}, result)
             , message = IFNULL(SUBSTRING(#{message}, 1, 500), message)
             , updt_dt = NOW(3)
         WHERE batch_log_id = #{batchLogId}
    </insert>


    <!-- 배치 관리 -->
    <sql id="sqlBatchLock">
      SELECT instance_id
           , instance_name
           , instance_url
           , instance_param
           , result
           , use_yn
           , regist_dt
           , updt_dt
        FROM tb_cmm_batch_lock
    </sql>

    <select id="selectBatchLocks" resultType="kr.xit.core.biz.batch.model.BatchCmmDTO$BatchLock">
        /** batch-cmm-mysql-mapper|selectBatchLocks-배치 목록 조회|julim  */
        <include refid="sqlBatchLock"/>
         -- ORDER BY instance_id
    </select>

    <select id="selectBatchLock" resultType="kr.xit.core.biz.batch.model.BatchCmmDTO$BatchLock">
        /** batch-cmm-mysql-mapper|selectBatchLocks-배치 목록 조회|julim  */
        <include refid="sqlBatchLock"/>
       WHERE instance_id = #{instanceId}
    </select>
</mapper>
