<?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.fims.base.dao.AdminMapper">

<select id="selectAllList" parameterType="map">
<choose>
	<when test="backupTable != null">
SELECT *
  FROM ${backupTable}
 WHERE ${pkName} NOT IN (
       SELECT ${pkName}
         FROM ${originalTable}
 )
	</when>
	<otherwise>
SELECT *
  FROM ${originalTable}
	</otherwise>
</choose>
ORDER BY ${pkName}
</select>

<insert id="insertOriginalData" parameterType="cokr.xit.fims.base.Backup">
INSERT
  INTO ${originalTable}
SELECT *
  FROM ${backupTable}
 WHERE ${pkName} IN (<foreach collection="pks" item="pk" separator=",">#{pk}</foreach>)
</insert>

<delete id="deleteBackupData" parameterType="cokr.xit.fims.base.Backup">
DELETE
  FROM ${backupTable}
 WHERE ${pkName} IN (<foreach collection="pks" item="pk" separator=",">#{pk}</foreach>)
</delete>

<insert id="insertBackupData" parameterType="cokr.xit.fims.base.Backup">
INSERT
  INTO ${backupTable}
SELECT *
  FROM ${originalTable}
 WHERE ${pkName} IN (<foreach collection="pks" item="pk" separator=",">#{pk}</foreach>)
</insert>

<delete id="deleteOriginalData" parameterType="cokr.xit.fims.base.Backup">
DELETE
  FROM ${originalTable}
 WHERE ${pkName} IN (<foreach collection="pks" item="pk" separator=",">#{pk}</foreach>)
</delete>

<insert id="copyTask" parameterType="map">
INSERT
  INTO TB_TASK
SELECT #{trgtSgg} AS SGG_CD
     , B.TASK_SE_CD
     , B.TASK_SE_NM
     , B.ADVNTCE_DAY_CNT
     , B.ADVNTCE_RDCRT
     , B.WKSN_RDCRT
     , B.FFNLG_ADTN_YMD
     , B.FFNLG_ADTN_RT
     , B.FFNLG_INADTN_RT
     , B.FFNLG_INADTN_MAX_CNT
     , B.NXRP_LINK_SE_CD
     , B.NXRP_LINK_SE_DTL_SN
     , B.USE_YN
     , B.REG_DT
     , B.RGTR
     , B.MDFCN_DT
     , B.MDFR
  FROM TB_TASK B
 WHERE B.SGG_CD = #{srcSgg}
   AND B.TASK_SE_CD = #{taskSeCd}
</insert>

<select id="selectVltnList" parameterType="map" resultType="string">
SELECT A.VLTN_CD
  FROM TB_VLTN A
 WHERE A.SGG_CD = #{srcSgg}
   AND A.TASK_SE_CD = #{taskSeCd}
</select>

<insert id="copyVltn" parameterType="map">
	<selectKey resultType="string" keyProperty="vltnId" keyColumn="NEW_ID" order="BEFORE">
	SELECT
	       LPAD(CAST(IFNULL(MAX(VLTN_ID) + 1, 1) AS INT), 10, '0')
	       AS NEW_ID
	FROM TB_VLTN
	</selectKey>
	INSERT
	  INTO TB_VLTN
	SELECT (CASE B.VLTN_CD
	        <foreach collection="vltnCds" item="vltnCd" index="index" separator=" ">
	        WHEN #{vltnCd} THEN LPAD(CAST(${vltnId}+${index} AS INT), 10, '0')
            </foreach>
	        ELSE '' END) AS VLTN_ID
	     , #{trgtSgg} AS SGG_CD
	     , B.TASK_SE_CD
         , B.VLTN_CD
         , B.VLTN_ARTCL
         , B.VLTN_LAW_NM
         , B.VLTN_LAW1
         , B.VLTN_LAW2
         , B.VLTN_LAW_ETC
         , B.ACNTG_SE_CD
         , B.ACNTG_SE_NM
         , B.TXITM_CD
         , B.TXITM_NM
         , B.OPER_ITEM_CD
         , B.OPER_ITEM_NM
         , B.SPCL_BIZ_CD
         , B.SPCL_BIZ_NM
         , B.USE_YN
         , B.REG_DT
         , B.RGTR
         , B.MDFCN_DT
         , B.MDFR
	  FROM TB_VLTN B
	 WHERE B.SGG_CD = #{srcSgg}
	   AND B.TASK_SE_CD = #{taskSeCd}
	   AND B.VLTN_CD IN (<foreach collection="vltnCds" item="vltnCd" separator=",">#{vltnCd}</foreach>)
</insert>

</mapper>