<?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.co.i4way.mapper.mssql.MssqlMapper">

	<update id="insertCCCUser" parameterType="java.util.List"> 
	    MERGE INTO TBL_CCC_USER A USING (
		<foreach item="item" collection="list" open="" close="" index="index" separator="UNION" >
		SELECT #{item.USER_ID} as user_id
				,#{item.USER_NM} as user_nm
				,#{item.CUSC_ORG_NO} as cusc_org_no
				,#{item.AUTH_GRP_NO} as auth_grp_no
				,#{item.HP_TN} as hp_tn
				,#{item.EML_ADR} as eml_adr
				,#{item.CNSR_TCD} as cnsr_tcd
				,#{item.EE_YN} as ee_yn
				,#{item.USE_YN} as use_yn
				,#{item.FIN_UPDR_ID} as fin_updr_id
				,dbo.FN_HD_CONV_DATETIME(#{item.FIN_MDFY_TISMP}) as fin_mdfy_tismp
				,#{item.ETCP_YMD} as etcp_ymd
				,#{item.LVCO_YMD} as lvco_ymd
				,#{item.LVCO_YN} as lvco_yn
				,#{item.CTI_USE_YN} as cti_use_yn
				,#{item.CTI_CNNC_USER_ID} as cti_cnnc_user_id
				,#{item.AT_RCPM_YN} as at_rcpm_yn
				,#{item.WKHM_YN} as wkhm_yn
				,#{item.ODU_THMS_TCD} as odu_thms_tcd
				,#{item.USE_XPR_PARR_YMD} as use_xpr_parr_ymd
				,#{item.INLN_TN} as inln_tn
				,#{item.REM_SBC} as rem_sbc
				,#{item.CTI_USER_MGMT_NO} as cti_user_mgmt_no
		</foreach>
		) B ON (A.user_id = B.user_id)
		WHEN MATCHED THEN 
			UPDATE
				SET 
					A.USER_NM = B.user_nm
					, A.CUSC_ORG_NO = B.cusc_org_no
					, A.AUTH_GRP_NO = B.auth_grp_no
					, A.HP_TN = B.hp_tn
					, A.EML_ADR = B.eml_adr
					, A.CNSR_TCD = B.cnsr_tcd
					, A.EE_YN = B.ee_yn
					, A.USE_YN = B.use_yn
					, A.FIN_UPDR_ID = B.fin_updr_id
					, A.FIN_MDFY_TISMP = B.fin_mdfy_tismp
					, A.ETCP_YMD = B.etcp_ymd
					, A.LVCO_YMD = B.lvco_ymd
					, A.LVCO_YN = B.lvco_yn
					, A.CTI_USE_YN = B.cti_use_yn
					, A.CTI_CNNC_USER_ID = B.cti_cnnc_user_id
					, A.AT_RCPM_YN = B.at_rcpm_yn
					, A.WKHM_YN = B.wkhm_yn
					, A.ODU_THMS_TCD = B.odu_thms_tcd
					, A.USE_XPR_PARR_YMD = B.use_xpr_parr_ymd
					, A.INLN_TN = B.inln_tn
					, A.REM_SBC = B.rem_sbc
					, A.CTI_USER_MGMT_NO = B.cti_user_mgmt_no
		WHEN NOT MATCHED THEN
			INSERT 
	    		(USER_ID
				,USER_NM
				,CUSC_ORG_NO
				,AUTH_GRP_NO
				,HP_TN
				,EML_ADR
				,CNSR_TCD
				,EE_YN
				,USE_YN
				,FIN_UPDR_ID
				,FIN_MDFY_TISMP
				,ETCP_YMD
				,LVCO_YMD
				,LVCO_YN
				,CTI_USE_YN
				,CTI_CNNC_USER_ID
				,AT_RCPM_YN
				,WKHM_YN
				,ODU_THMS_TCD
				,USE_XPR_PARR_YMD
				,INLN_TN
				,REM_SBC
				,CTI_USER_MGMT_NO)
			VALUES 
	    	(B.user_id
				,B.user_nm
				,B.cusc_org_no
				,B.auth_grp_no
				,B.hp_tn
				,B.eml_adr
				,B.cnsr_tcd
				,B.ee_yn
				,B.use_yn
				,B.fin_updr_id
				,B.fin_mdfy_tismp
				,B.etcp_ymd
				,B.lvco_ymd
				,B.lvco_yn
				,B.cti_use_yn
				,B.cti_cnnc_user_id
				,B.at_rcpm_yn
				,B.wkhm_yn
				,B.odu_thms_tcd
				,B.use_xpr_parr_ymd
				,B.inln_tn
				,B.rem_sbc
				,B.cti_user_mgmt_no
	    	);
	</update>
	
		<update id="insertCCCGroup" parameterType="java.util.List"> 
	    MERGE INTO TBL_CCC_GRP A USING (
		<foreach item="item" collection="list" open="" close="" index="index" separator="UNION" >
			SELECT #{item.CUSC_ORG_NO} as cusc_org_no
				,#{item.CUSC_ORG_NM} as cusc_org_nm
				,#{item.CUSC_ORG_EXPL_SBC} as cusc_org_expl_sbc
				,#{item.SUPI_CUSC_ORG_NO} as supi_cusc_org_no
				,#{item.SORT_SQN} as sort_sqn
				,#{item.CUSC_SYS_TYPE_CD} as cusc_sys_type_cd
				,#{item.USE_YN} as use_yn
				,#{item.VBG_RGN_ID} as vbg_rgn_id
				,dbo.FN_HD_CONV_DATETIME(#{item.VBG_RGST_TISMP}) as vbg_rgst_tismp
				,#{item.FIN_UPDR_ID} as fin_updr_id
				,dbo.FN_HD_CONV_DATETIME(#{item.FIN_MDFY_TISMP}) as fin_mdfy_tismp
				,#{item.BLNS_CO_CD} as blns_co_cd
		</foreach>
		) B ON (A.cusc_org_no = B.cusc_org_no)
		WHEN MATCHED THEN 
			UPDATE
				SET 
					A.CUSC_ORG_NM = B.cusc_org_nm
					,A.CUSC_ORG_EXPL_SBC = B.cusc_org_expl_sbc
					,A.SUPI_CUSC_ORG_NO = B.supi_cusc_org_no
					,A.SORT_SQN = B.sort_sqn
					,A.CUSC_SYS_TYPE_CD = B.cusc_sys_type_cd
					,A.USE_YN = B.use_yn
					,A.VBG_RGN_ID = B.vbg_rgn_id
					,A.VBG_RGST_TISMP = B.vbg_rgst_tismp
					,A.FIN_UPDR_ID = B.fin_updr_id
					,A.FIN_MDFY_TISMP = B.fin_mdfy_tismp
					,A.BLNS_CO_CD = B.blns_co_cd
		WHEN NOT MATCHED THEN
			INSERT 
	    		(CUSC_ORG_NO
				,CUSC_ORG_NM
				,CUSC_ORG_EXPL_SBC
				,SUPI_CUSC_ORG_NO
				,SORT_SQN
				,CUSC_SYS_TYPE_CD
				,USE_YN
				,VBG_RGN_ID
				,VBG_RGST_TISMP
				,FIN_UPDR_ID
				,FIN_MDFY_TISMP
				,BLNS_CO_CD)
			VALUES 
	    	(B.cusc_org_no
			,B.cusc_org_nm
			,B.cusc_org_expl_sbc
			,B.supi_cusc_org_no
			,B.sort_sqn
			,B.cusc_sys_type_cd
			,B.use_yn
			,B.vbg_rgn_id
			,B.vbg_rgst_tismp
			,B.fin_updr_id
			,B.fin_mdfy_tismp
			,B.blns_co_cd
	    	);
	</update>

	<update id="setRecLicenseInfo"> 
	    MERGE INTO HBRDPRD.dbo.TBL_LICENSE A USING (
		<foreach item="item" collection="list" open="" close="" index="index" separator="UNION" >
			SELECT #{item.solution_type} as solution_type
				,#{item.server_id} as server_id
				,#{item.server_name} as server_name
				,#{item.server_type} as server_type
				,#{item.server_type_name} as server_type_name
				,#{item.license_cnt} as license_cnt
				,#{item.used_cnt} as used_cnt
		</foreach>
		) B ON (A.solution_type = B.solution_type AND A.server_id = B.server_id)
		WHEN MATCHED THEN 
			UPDATE
				SET 
					server_name = B.server_name
			  		, A.server_type = B.server_type
			  		, A.server_type_name = B.server_type_name
			  		, A.license_cnt = B.license_cnt
			  		, A.used_cnt = B.used_cnt
		WHEN NOT MATCHED THEN
			INSERT 
	    		(solution_type,
	    		,server_id
	           	,server_name
	           	,server_type
	           	,server_type_name
	           	,license_cnt
	           	,used_cnt)
			VALUES 
	    	(B.solution_type
	    	, B.server_id
	    	, B.server_name
	    	, B.server_type
	    	, B.server_type_name
	    	, B.license_cnt
	    	, B.used_cnt
	    	);
	</update>

	<update id="setGenesysLicenseInfo"  parameterType="kr.co.i4way.cst.model.RecLicenseVo"> 
		UPDATE TBL_LICENSE SET 
			license_cnt = #{license_cnt}
			,used_cnt = #{used_cnt}
		WHERE solution_type = #{solution_type}
		AND server_id = #{server_id}
	</update>
	
	<select id="getDnCnt" resultType="hashmap">
		SELECT COUNT(*) CNT_DN FROM HCTIPRD.dbo.CFG_DN
		WHERE TYPE = '1' 
		and switch_dbid = '101'
	</select>
	
	<update id="setRecLicenseInfo_dev"> 
	    MERGE INTO HBRDDEV.dbo.TBL_LICENSE A USING (
		<foreach item="item" collection="list" open="" close="" index="index" separator="UNION" >
			SELECT #{item.solution_type} as solution_type
				,#{item.server_id} as server_id
				,#{item.server_name} as server_name
				,#{item.server_type} as server_type
				,#{item.server_type_name} as server_type_name
				,#{item.license_cnt} as license_cnt
				,#{item.used_cnt} as used_cnt
		</foreach>
		) B ON (A.solution_type = B.solution_type AND A.server_id = B.server_id)
		WHEN MATCHED THEN 
			UPDATE
				SET 
					server_name = B.server_name
			  		, A.server_type = B.server_type
			  		, A.server_type_name = B.server_type_name
			  		, A.license_cnt = B.license_cnt
			  		, A.used_cnt = B.used_cnt
		WHEN NOT MATCHED THEN
			INSERT 
	    		(solution_type,
	    		,server_id
	           	,server_name
	           	,server_type
	           	,server_type_name
	           	,license_cnt
	           	,used_cnt)
			VALUES 
	    	(B.solution_type
	    	, B.server_id
	    	, B.server_name
	    	, B.server_type
	    	, B.server_type_name
	    	, B.license_cnt
	    	, B.used_cnt
	    	);
	</update>

	<update id="setGenesysLicenseInfo_dev"  parameterType="kr.co.i4way.cst.model.RecLicenseVo"> 
		UPDATE HBRDDEV.dbo.TBL_LICENSE SET 
			license_cnt = #{license_cnt}
			, used_cnt = #{used_cnt}
		WHERE solution_type = #{solution_type}
		AND server_id = #{server_id}
	</update>
	
	<select id="getDnCnt_Dev" resultType="hashmap">
		SELECT COUNT(*) CNT_DN FROM HCTIDEV.dbo.CFG_DN
		WHERE TYPE = '1' 
	</select>
</mapper>