<?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="com.style24.admin.biz.dao.TsaSystemDao">

	<!-- 사용자 목록 -->
	<select id="getUserList" parameterType="User" resultType="User">
		/* TsaSystem.getUserList */
		SELECT A.USER_NO                                                     /*사용자번호*/
		     , A.USER_ID                                                     /*사용자ID*/
		     , A.USER_NM                                                     /*사용자명*/
		     , A.ROLE_CD                                                     /*권한코드*/
		     , A.ROLE_REF_VAL                                                /*권한관련값*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) IN ('A','B','E') THEN
		                A.ROLE_REF_VAL
		            END                                    AS SUPPLY_COMP_CD /*공급업체코드*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) IN ('A','B','E') THEN
		                (SELECT SUPPLY_COMP_NM
		                 FROM   TB_SUPPLY_COMPANY
		                 WHERE  SUPPLY_COMP_CD = A.ROLE_REF_VAL
		                )
		       END                                         AS SUPPLY_COMP_NM /*공급업체명*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) = 'C' THEN
		                A.ROLE_REF_VAL
		       END                                         AS VENDOR_ID      /*벤더ID*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) = 'C' THEN
		                FN_GET_CODE_NM('G003',A.ROLE_REF_VAL)
		       END                                         AS VENDOR_NM      /*벤더명*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                A.ROLE_REF_VAL
		       END                                         AS AF_LINK_CD     /*제휴링크코드*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                (SELECT AF_LINK_NM
		                 FROM   TB_AF_LINK
		                 WHERE  AF_LINK_CD = A.ROLE_REF_VAL
		                )
		       END                                         AS AF_LINK_NM     /*제휴링크명*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                (SELECT AF_CHANNEL
		                 FROM   TB_AF_LINK
		                 WHERE  AF_LINK_CD = A.ROLE_REF_VAL
		                )
		       END                                         AS AF_CHANNEL     /*제휴채널*/
		     , CASE WHEN SUBSTRING(REPLACE(A.ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                (SELECT FN_GET_CODE_NM('G053',AL.AF_CHANNEL)
		                 FROM   TB_AF_LINK AL
		                 WHERE  AL.AF_LINK_CD = A.ROLE_REF_VAL
		                )
		       END                                         AS AF_CHANNEL_NM  /*제휴채널명*/
		     , A.EMAIL                                                       /*이메일*/
		     , A.CELL_PHNNO                                                  /*휴대전화번호*/
		     , A.PNT_ASSIGN_AMT                                              /*포인트부여가능금액*/
		     , A.IP_ADDR                                                     /*IP주소*/
		     , A.IP_CHK_YN                                                   /*IP체크여부*/
		     , DATE_FORMAT(A.LOGIN_LDT,'%Y%m%d%H%i%S')     AS LOGIN_LDT      /*최종로그인일시*/
		     , DATE_FORMAT(A.PASSWD_CHG_DT,'%Y%m%d%H%i%S') AS PASSWD_CHG_DT  /*비밀번호변경일시*/
		     , A.USE_YN                                                      /*사용여부*/
		     , A.MASKING_YN                                                  /*마스킹여부*/
		FROM   TB_USER A
		WHERE  1 = 1
		<choose>
		    <when test="searchGb == 'userId'">
		AND    LOWER(A.USER_ID) LIKE CONCAT('%',LOWER(#{searchTxt}),'%')
		    </when>
		    <when test="searchGb == 'userNm'">
		AND    LOWER(A.USER_NM) LIKE CONCAT('%',LOWER(#{searchTxt}),'%')
		    </when>
		</choose>
		<if test="useYn != null and useYn != ''">
		AND    A.USE_YN  = #{useYn}
		</if>
		<if test="supplyCompCd != null and supplyCompCd != ''">
		AND    A.ROLE_REF_VAL = #{supplyCompCd}
		</if>
		<if test="roleCd != null and roleCd != ''">
		AND    A.ROLE_CD  = #{roleCd}
		</if>
		ORDER  BY A.USER_NM
	</select>

	<!-- 사용자 조회 -->
	<select id="getUser" parameterType="Integer" resultType="User">
		/* TsaSystem.getUser */
		SELECT USER_NO                                                         /*사용자번호*/
		     , USER_ID                                                         /*사용자ID*/
		     , USER_NM                                                         /*사용자명*/
		     , ROLE_CD                                                         /*권한코드*/
		     , (SELECT CD_NM
		        FROM   TB_COMMON_CODE
		        WHERE  CD_GB = 'G001'
		        AND    CD = A.ROLE_CD
		       )                                              AS ROLE_NM       /*권한명*/
		     , ROLE_REF_VAL                                                    /*권한관련값*/
		     , EMAIL                                                           /*이메일*/
		     , CELL_PHNNO                                                      /*휴대전화번호*/
		     , PNT_ASSIGN_AMT                                                  /*포인트부여가능금액*/
		     , IP_ADDR                                                         /*IP주소*/
		     , IP_CHK_YN                                                       /*IP체크여부*/
		     , DATE_FORMAT(LOGIN_LDT,'%Y-%m-%d %H:%i:%S')     AS LOGIN_LDT     /*최종로그인일시*/
		     , DATE_FORMAT(PASSWD_CHG_DT,'%Y-%m-%d %H:%i:%S') AS PASSWD_CHG_DT /*비밀번호변경일시*/
		     , USE_YN                                                          /*사용여부*/
		     , MASKING_YN                                                      /*마스킹여부*/
		FROM   TB_USER A
		WHERE  USER_NO = #{userNo}
	</select>

	<!-- 사용자 삭제 -->
	<update id="deleteUser" parameterType="User">
		/* TsaSystem.deleteUser */
		UPDATE TB_USER
		SET    USE_YN = 'N'
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  USER_NO = #{userNo}
	</update>

	<!-- 사용자ID 건수 -->
	<select id="getUserIdCount" parameterType="String" resultType="int">
		/* TsaSystem.getUserIdCount */
		SELECT COUNT(*) AS CNT
		FROM   TB_USER
		WHERE  USER_ID = #{userId}
	</select>

	<!-- 사용자 등록/수정 -->
	<insert id="saveUser" parameterType="User" keyProperty="userNo">
		/* TsaSystem.saveUser */
		INSERT INTO TB_USER (
		       USER_NO
		     , USER_ID
		     , USER_NM
		     , PASSWD
		     , ROLE_CD
		     , ROLE_REF_VAL
		     , EMAIL
		     , CELL_PHNNO
		     , PNT_ASSIGN_AMT
		     , IP_ADDR
		     , IP_CHK_YN
		     , USE_YN
		     , MASKING_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{userNo}
		     , #{userId}
		     , #{userNm}
		     , #{passwd}
		     , #{roleCd}
		     , #{roleRefVal}
		     , #{email}
		     , #{cellPhnno}
		     , IFNULL(#{pntAssignAmt},0)
		     , #{ipAddr}
		     , #{ipChkYn}
		     , #{useYn}
		     , #{maskingYn}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       USER_NM = #{userNm}
		     , ROLE_CD = #{roleCd}
		     , ROLE_REF_VAL = #{roleRefVal}
		     , EMAIL = #{email}
		     , CELL_PHNNO = #{cellPhnno}
		     , PNT_ASSIGN_AMT = IFNULL(#{pntAssignAmt},0)
		     , IP_ADDR = #{ipAddr}
		     , IP_CHK_YN = #{ipChkYn}
		     , USE_YN = #{useYn}
		     , MASKING_YN = #{maskingYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>

	<!-- 임시비밀번호 조회 -->
	<select id="getTemporaryPassword" parameterType="int" resultType="String">
		/* TsaSystem.getTemporaryPassword */
		SELECT CONVERT(TRUNCATE(RAND() * CAST(CONCAT(1,LPAD(0,(#{length} - 1),'0')) AS UNSIGNED),0),CHAR) AS PASSWD FROM DUAL
	</select>

	<!-- 사용자 비밀번호 수정 -->
	<update id="updateUserPassword" parameterType="User">
		/* TsaSystem.updateUserPassword */
		UPDATE TB_USER
		SET    PASSWD = #{passwd}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  USER_NO = #{userNo}
	</update>

	<!-- 사용자 메뉴 목록 -->
	<select id="getUserMenuList" parameterType="Integer" resultType="UserMenu">
		/* TsaSystem.getUserMenuList */
		WITH RECURSIVE CONNECTBY AS (
		    SELECT 1                                    AS LVL
		         , PMENU_ID
		         , MENU_ID
		         , MENU_NM
		         , MENU_GB
		         , CONCAT(MENU_ID,LPAD(DISP_ORD,4,'0')) AS ORDBY
		    FROM   TB_MENU
		    WHERE  PMENU_ID = 'ROOT'
		    AND    USE_YN = 'Y'
		    
		    UNION ALL
		    
		    SELECT A.LVL + 1                              AS LVL
		         , B.PMENU_ID
		         , B.MENU_ID
		         , B.MENU_NM
		         , B.MENU_GB
		         , CONCAT(A.ORDBY,LPAD(B.DISP_ORD,4,'0')) AS ORDBY
		    FROM   CONNECTBY A
		    INNER JOIN TB_MENU B ON A.MENU_ID = B.PMENU_ID
		)
		SELECT #{userNo}                       AS USER_NO  /*사용자번호*/
		     , A.PMENU_ID                                  /*상위메뉴ID*/
		     , A.MENU_ID                                   /*메뉴ID*/
		     , A.MENU_NM                                   /*메뉴명*/
		     , A.MENU_GB                                   /*메뉴구분*/
		     , CASE WHEN B.USER_NO IS NULL OR B.USE_ROLE = 'NNNN' THEN
		                'N'
		            ELSE
		                'Y'
		       END                             AS USE_YN   /*사용여부*/
		     , A.LVL                           AS MENU_LVL /*메뉴레벨*/
		     , IFNULL(B.USE_ROLE,'NNNN')       AS USE_ROLE /*사용권한*/
		FROM   CONNECTBY A
		LEFT OUTER JOIN TB_USER_MENU B ON A.MENU_ID = B.MENU_ID
		                              AND B.USER_NO = #{userNo}
		ORDER  BY A.ORDBY
	</select>
	
	<!-- 사용자정보변경이력 생성 -->
	<insert id="createUserInfoChangeHistory" parameterType="UserHst">
		/* TsaSystem.createUserInfoChangeHistory */
		INSERT INTO TB_USER_HST (
		       CHG_HST_SQ
		     , USER_NO
		     , CHG_GB
		     , CHG_CONTENT
		     , REG_NO
		     , REG_DT
		)
		VALUES (
		       NULL
		     , #{userNo}
		     , #{chgGb}
		     , CASE WHEN #{chgGb} = 'G049_20' THEN
		                CONCAT(FN_GET_CODE_NM('G001',#{orgRoleCd}),' -> ',FN_GET_CODE_NM('G001',#{roleCd}))
		            ELSE
		                NULL
		       END
		     , #{regNo}
		     , NOW()
		)
	</insert>

	<!-- 메뉴 목록 -->
	<select id="getMenuList" parameterType="Menu" resultType="Menu">
		/* TsaSystem.getMenuList */
		WITH RECURSIVE CONNECTBY1 AS (
		    SELECT 1                                    AS MENU_LVL  /*메뉴레벨*/
		         , MENU_ID                                           /*메뉴ID*/
		         , MENU_NM                                           /*메뉴명*/
		         , MENU_GB                                           /*메뉴구분*/
		         , MENU_DESC                                         /*메뉴상세명*/
		         , MENU_URL                                          /*메뉴URL*/
		         , PMENU_ID                                          /*상위메뉴ID*/
		         , DISP_ORD                                          /*표시순서*/
		         , USE_YN                                            /*사용여부*/
		         , MENU_ID                              AS TREE_PATH /*AG-GRID트리패스(ag-Grid미사용시 필요 없음)*/
		         , CONCAT(MENU_ID,LPAD(DISP_ORD,4,'0')) AS ORDBY
		    FROM   TB_MENU A
		    WHERE  1 = 1
		    <choose>
		        <when test="pmenuId != null and pmenuId != ''">
		    AND    PMENU_ID = #{pmenuId}
		        </when>
		        <otherwise>
		    AND    PMENU_ID = 'ROOT'
		        </otherwise>
		    </choose>
		    
		    UNION ALL
		    
		    SELECT A.MENU_LVL + 1                         AS MENU_LVL  /*메뉴레벨*/
		         , B.MENU_ID                                           /*메뉴ID*/
		         , B.MENU_NM                                           /*메뉴명*/
		         , B.MENU_GB                                           /*메뉴구분*/
		         , B.MENU_DESC                                         /*메뉴상세명*/
		         , B.MENU_URL                                          /*메뉴URL*/
		         , B.PMENU_ID                                          /*상위메뉴ID*/
		         , B.DISP_ORD                                          /*표시순서*/
		         , B.USE_YN                                            /*사용여부*/
		         , CONCAT(A.TREE_PATH,'/',B.MENU_ID)      AS TREE_PATH /*AG-GRID트리패스(ag-Grid미사용시 필요 없음) */
		         , CONCAT(A.ORDBY,LPAD(B.DISP_ORD,4,'0')) AS ORDBY
		    FROM   CONNECTBY1 A
		    INNER JOIN TB_MENU B ON A.MENU_ID = B.PMENU_ID
		), TAB_MENU_ROLE AS (
		    SELECT A.MENU_ID
		         , GROUP_CONCAT(B.ROLE_CD ORDER BY B.ROLE_CD SEPARATOR ',')                        AS ROLE_CDS /*다중메뉴권한코드*/
		         , GROUP_CONCAT(FN_GET_CODE_NM('G001',B.ROLE_CD) ORDER BY B.ROLE_CD SEPARATOR ',') AS ROLE_NMS /*다중메뉴권한명*/
		    FROM   TB_MENU A
		    INNER JOIN TB_MENU_ROLE B ON A.MENU_ID = B.MENU_ID
		    GROUP  BY A.MENU_ID
		)
		SELECT MENU_LVL
		     , MENU_ID
		     , MENU_NM
		     , MENU_GB
		     , MENU_DESC
		     , MENU_URL
		     , PMENU_ID
		     , DISP_ORD
		     , USE_YN
		     , TREE_PATH
		     , (SELECT ROLE_CDS
		        FROM   TAB_MENU_ROLE
		        WHERE  MENU_ID = A.MENU_ID) AS ROLE_CDS  /*다중메뉴권한코드*/
		     , (SELECT ROLE_NMS
		        FROM   TAB_MENU_ROLE
		        WHERE  MENU_ID = A.MENU_ID) AS ROLE_NMS  /*다중메뉴권한명*/
		FROM   CONNECTBY1 A
		ORDER  BY ORDBY
	</select>

	<!-- 메뉴 등록/수정 -->
	<insert id="saveMenu" parameterType="Menu">
		/* TsaSystem.saveMenu */
		INSERT INTO TB_MENU (
		       MENU_ID
		     , MENU_NM
		     , MENU_GB
		     , MENU_DESC
		     , MENU_URL
		     , DISP_ORD
		     , USE_YN
		     , PMENU_ID
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{menuId}
		     , #{menuNm}
		     , #{menuGb}
		     , #{menuDesc}
		     , #{menuUrl}
		     , #{dispOrd}
		     , #{useYn}
		     , IFNULL(NULLIF(#{pmenuId},''),'ROOT')
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       MENU_NM = #{menuNm}
		     , MENU_GB = #{menuGb}
		     , MENU_DESC = #{menuDesc}
		     , MENU_URL = #{menuUrl}
		     , DISP_ORD = #{dispOrd}
		     , USE_YN = #{useYn}
		     , PMENU_ID = IFNULL(NULLIF(#{pmenuId},''),'ROOT')
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>

	<!-- 메뉴권한 삭제 -->
	<delete id="deleteMenuRole" parameterType="String">
		/* TsaSystem.deleteMenuRole */
		DELETE FROM TB_MENU_ROLE
		WHERE  MENU_ID = #{menuId}
	</delete>

	<!-- 메뉴권한 생성 -->
	<insert id="createMenuRole" parameterType="MenuRole">
		/* TsaSystem.createMenuRole */
		INSERT INTO TB_MENU_ROLE (
		       ROLE_CD
		     , MENU_ID
		     , USE_ROLE
		     , REG_NO
		     , REG_DT
		)
		VALUES (
		       #{roleCd}
		     , #{menuId}
		     , IFNULL(NULLIF(#{useRole},''),'RCUD')
		     , #{regNo}
		     , NOW()
		)
	</insert>

	<!-- 전체 사용자 메뉴 삭제 -->
	<delete id="deleteAllUserMenu" parameterType="MenuRole">
		/* TsaSystem.deleteAllUserMenu */
		DELETE
		FROM   TB_USER_MENU
		WHERE  USER_NO IN (SELECT USER_NO
		                   FROM   TB_USER
		                   WHERE  ROLE_CD = #{roleCd}
		                  )
		AND    MENU_ID = #{menuId}
	</delete>

	<!-- 전체 사용자 메뉴 생성-->
	<insert id="createAllUserMenu" parameterType="MenuRole">
		/* TsaSystem.createAllUserMenu */
		INSERT INTO TB_USER_MENU (
		       USER_NO
		     , MENU_ID
		     , USE_ROLE
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT B.USER_NO
		     , A.MENU_ID
		     , A.USE_ROLE
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		FROM   TB_MENU_ROLE A
		INNER JOIN TB_USER B ON A.ROLE_CD = B.ROLE_CD
		WHERE  A.ROLE_CD = #{roleCd}
		AND    A.MENU_ID = #{menuId}
		AND    B.USE_YN = 'Y'
		ON DUPLICATE KEY UPDATE
		       USE_ROLE = A.USE_ROLE
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>

	<!-- 사용자 전체 메뉴 삭제 -->
	<delete id="deleteUserAllMenu" parameterType="Integer">
		/* TsaSystem.deleteUserAllMenu */
		DELETE
		FROM   TB_USER_MENU
		WHERE  USER_NO = #{userNo}
	</delete>

	<!-- 사용자 전체 메뉴 생성 -->
	<insert id="createUserAllMenu" parameterType="User">
		/* TsaSystem.createUserAllMenu */
		INSERT INTO TB_USER_MENU (
		       USER_NO
		     , MENU_ID
		     , USE_ROLE
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT A.USER_NO
		     , B.MENU_ID
		     , B.USE_ROLE
		     , #{regNo}     AS REG_NO
		     , NOW()        AS REG_DT
		     , #{updNo}     AS UPD_NO
		     , NOW()        AS UPD_DT
		FROM   TB_USER A
		INNER JOIN TB_MENU_ROLE B ON A.ROLE_CD = B.ROLE_CD
		WHERE  A.USER_NO = #{userNo}
		AND    A.ROLE_CD = #{roleCd}
	</insert>

	<!-- 사용자 메뉴 삭제 -->
	<delete id="deleteUserMenu" parameterType="UserMenu">
		/* TsaSystem.deleteUserMenu */
		DELETE
		FROM   TB_USER_MENU
		WHERE  USER_NO = #{userNo}
		AND    MENU_ID = #{menuId}
	</delete>

	<!-- 사용자 메뉴 Insert/Update -->
	<insert id="createUserMenu" parameterType="UserMenu">
		/* TsaSystem.createUserMenu */
		INSERT INTO TB_USER_MENU (
		       USER_NO
		     , MENU_ID
		     , USE_ROLE
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{userNo}
		     , #{menuId}
		     , #{useRole}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       USE_ROLE = #{useRole}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>
	
	<!-- 메뉴접속이력 생성 -->
	<insert id="createMenuAccessHistory" parameterType="MenuAccessHst">
		/* TsaSystem.createMenuAccessHistory */
		INSERT INTO TB_MENU_ACCESS_HST (
		       MENU_ACCESS_SQ
		     , USER_NO
		     , MENU_ID
		     , REQ_URL
		     , PARAMS
		     , REG_NO
		     , REG_DT
		)
		VALUES (
		       NULL
		     , #{userNo}
		     , #{menuId}
		     , #{reqUrl}
		     , #{params}
		     , #{userNo}
		     , NOW()
		)
	</insert>

	<!-- 공통코드 목록 -->
	<select id="getCommonCodeList" parameterType="CommonCode" resultType="CommonCode">
		/* TsaSystem.getCommonCodeList */
		SELECT CD_GB
		     , CD
		     , CD_NM
		     , CD_DESC
		     , DISP_ORD
		     , USE_YN
		     , DISP_ORD
		FROM   TB_COMMON_CODE
		WHERE  CD_GB = #{cdGb}
		ORDER  BY DISP_ORD
	</select>

	<!-- 공통코드 등록/수정 -->
	<insert id="saveCommonCode" parameterType="CommonCode">
		/* TsaSystem.saveCommonCode */
		INSERT INTO TB_COMMON_CODE (
		       CD_GB
		     , CD
		     , CD_NM
		     , CD_DESC
		     , DISP_ORD
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{cdGb}
		     , #{cd}
		     , #{cdNm}
		     , #{cdDesc}
		     , #{dispOrd}
		     , #{useYn}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       CD_NM = #{cdNm}
		     , CD_DESC = #{cdDesc}
		     , DISP_ORD = #{dispOrd}
		     , USE_YN = #{useYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>

	<!-- 공통코드 삭제 -->
	<delete id="deleteCommonCode" parameterType="CommonCode">
		/* TsaSystem.deleteCode */
		DELETE FROM TB_COMMON_CODE
		WHERE  CD_GB = #{cdGb}
		AND    CD = #{cd}
	</delete>
	
	<!-- 샘플파일 목록 -->
	<select id="getSampleFileList" resultType="SampleFile">
		/* TsaSystem.getSampleFileList */
		SELECT SAMPLE_FILE_ID
		     , SAMPLE_FILE_NM
		     , ORG_FILE_NM
		     , SYS_FILE_NM
		     , USE_YN
		FROM   TB_SAMPLE_FILE
		ORDER  BY SAMPLE_FILE_NM
	</select>

	<!-- 샘플파일 등록/수정 -->
	<insert id="createSampleFile" parameterType="SampleFile">
		/* TsaSystem.createSampleFile */
		INSERT INTO TB_SAMPLE_FILE (
		       SAMPLE_FILE_ID
		     , SAMPLE_FILE_NM
		     , ORG_FILE_NM
		     , SYS_FILE_NM
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       (SELECT CONCAT('SF',IFNULL(LPAD(CAST(SUBSTRING(MAX(SAMPLE_FILE_ID),3) AS UNSIGNED) + 1,3,'0'),'000'))
		        FROM   TB_SAMPLE_FILE Z
		       )
		     , #{sampleFileNm}
		     , #{orgFileNm}
		     , #{sysFileNm}
		     , #{useYn}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
	</insert>
	
	<!-- 샘플파일 등록/수정 -->
	<update id="updateSampleFile" parameterType="SampleFile">
		/* TsaSystem.updateSampleFile */
		UPDATE TB_SAMPLE_FILE
		SET    SAMPLE_FILE_NM = #{sampleFileNm}
		     , ORG_FILE_NM = #{orgFileNm}
		     , SYS_FILE_NM = #{sysFileNm}
		     , USE_YN = #{useYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  SAMPLE_FILE_ID = #{sampleFileId}
	</update>
	
	<!-- 알람 목록 -->
	<select id="getAlarmList" parameterType="Alarm" resultType="Alarm">
		/* TsaSystem.getAlarmList */
		SELECT ALARM_ID
		     , ALARM_TITLE
		     , ALARM_MSG
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		FROM   TB_ALARM
		WHERE  1 = 1
		<if test="alarmId != null and alarmId != ''">
		AND    ALARM_ID = #{alarmId}
		</if>
		<if test="alarmTitle != null and alarmTitle != ''">
		AND    ALARM_TITLE LIKE CONCAT('%',#{alarmTitle},'%')
		</if>
		ORDER  BY ALARM_ID
	</select>

	<!-- 알람 저장 -->
	<insert id="createAlarm" parameterType="Alarm">
		/* TsaSystem.createAlarm */
		INSERT INTO TB_ALARM (
		       ALARM_ID
		     , ALARM_TITLE
		     , ALARM_MSG
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		        (SELECT CONCAT('A',IFNULL(LPAD(SUBSTRING(MAX(ALARM_ID),3) + 1,3,'0'),'000'))
		         FROM   TB_ALARM Z
		        )
		      , #{alarmTitle}
		      , #{alarmMsg}
		      , #{useYn}
		      , #{regNo}
		      , NOW()
		      , #{updNo}
		      , NOW()
		)
	</insert>
	
	<!-- 알람 수정 -->
	<update id="updateAlarm" parameterType="Alarm">
		/* TsaSystem.updateAlarm */
		UPDATE TB_ALARM
		SET    ALARM_TITLE = #{alarmTitle}
		     , ALARM_MSG = #{alarmMsg}
		     , USE_YN = #{useYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  ALARM_ID = #{alarmId}
	</update>

	<!-- 알람 삭제 -->
	<delete id="deleteAlarm" parameterType="Alarm">
		/* TsaSystem.deleteAlarm */
		UPDATE TB_ALARM
		SET    USE_YN = 'N'
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  ALARM_ID = #{alarmId}
	</delete>

	<!-- 알람수신자 목록 -->
	<select id="getAlarmReceiverList" parameterType="String" resultType="Alarm">
		/* TsaSystem.getAlarmReceiverList */
		SELECT ALARM_ID
		     , CELL_PHNNO
		     , RECEIVER_NM
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		FROM   TB_ALARM_RECEIVER
		WHERE  ALARM_ID = #{alarmId}
	</select>

	<!-- 알람수신자 저장 -->
	<insert id="saveAlarmReceiver" parameterType="AlarmReceiver">
		/* TsaSystem.saveAlarmReceiver */
		INSERT INTO TB_ALARM_RECEIVER (
		       ALARM_ID
		     , CELL_PHNNO
		     , RECEIVER_NM
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{alarmId}
		     , #{cellPhnno}
		     , #{receiverNm}
		     , #{useYn}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       RECEIVER_NM = #{receiverNm}
		     , USE_YN = #{useYn}
		     , UPD_ID = #{updNo}
		     , UPD_DT = NOW()
	</insert>

	<!-- 시스템알람 수신자 삭제 -->
	<delete id="deleteAlarmReceiver" parameterType="AlarmReceiver">
		/* TsaSystem.deleteAlarmReceiver */
		UPDATE TB_ALARM_RECEIVER
		SET    USE_YN = 'N'
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  ALARM_ID = #{alarmId}
		AND    CELL_PHNNO = #{cellPhnno}
	</delete>
	
	<!-- 배치 목록 -->
	<select id="getBatchList" parameterType="Batch" resultType="Batch">
		/* TsaSystem.getBatchList */
		SELECT BATCH_ID
		     , BATCH_NM
		     , BIZ_GB
		     , BATCH_FRQ
		     , BATCH_DESC
		     , USE_YN
		FROM   TB_BATCH
		WHERE  1 = 1
		<if test="bizGb != null and bizGb != ''">
		AND    BIZ_GB = #{bizGb}
		</if>
		<if test="batchFrq != null and batchFrq != ''">
		AND    BATCH_FRQ = #{batchFrq}
		</if>
		<choose>
		    <when test="searchGb == 'batchId'">
		AND    LOWER(BATCH_ID) LIKE CONCAT('%',LOWER(#{searchTxt}),'%')
		    </when>
		    <when test="searchGb == 'batchNm'">
		AND    LOWER(BATCH_NM) LIKE CONCAT('%',LOWER(#{searchTxt}),'%')
		    </when>
		</choose>
	</select>
	
	<!-- 배치 생성 -->
	<insert id="saveBatch" parameterType="Batch">
		/* TsaSystem.saveBatch */
		INSERT INTO TB_BATCH (
		       BATCH_ID
		     , BATCH_NM
		     , BIZ_GB
		     , BATCH_FRQ
		     , BATCH_DESC
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{batchId}
		     , #{batchNm}
		     , #{bizGb}
		     , #{batchFrq}
		     , #{batchDesc}
		     , #{useYn}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       BATCH_NM = #{batchNm}
		     , BIZ_GB = #{bizGb}
		     , BATCH_FRQ = #{batchFrq}
		     , BATCH_DESC = #{batchDesc}
		     , USE_YN = #{useYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>
	
	<!-- 배치 로그 목록 -->
	<select id="getBatchLogList" parameterType="Batch" resultType="BatchLog">
		/* TsaSystem.getBatchLogList */
		SELECT B.BATCH_ID
		     , B.BATCH_NM
		     , B.BIZ_GB
		     , B.BATCH_FRQ
		     , BL.BATCH_STAT
		     , DATE_FORMAT(BL.BATCH_STDT,'%Y-%m-%d %H:%i:%s.%f') AS BATCH_STDT
		     , DATE_FORMAT(BL.BATCH_EDDT,'%Y-%m-%d %H:%i:%s.%f') AS BATCH_EDDT
		     , TIMESTAMPDIFF(SECOND,BL.BATCH_STDT,BL.BATCH_EDDT) AS RUN_TIME
		FROM   TB_BATCH B
		     , TB_BATCH_LOG BL
		WHERE  B.BATCH_ID = BL.BATCH_ID
		<if test="bizGb != null and bizGb != ''">
		AND    B.BIZ_GB = #{bizGb}
		</if>
		<if test="batchFrq != null and batchFrq != ''">
		AND    B.BATCH_FRQ = #{batchFrq}
		</if>
		<choose>
		    <when test="searchGb == 'batchId'">
		AND    LOWER(B.BATCH_ID) LIKE CONCAT('%',LOWER(#{searchTxt}),'%')
		    </when>
		    <when test="searchGb == 'batchNm'">
		AND    LOWER(B.BATCH_NM) LIKE CONCAT('%',LOWER(#{searchTxt}),'%')
		    </when>
		</choose>
		<if test="batchStdt != null and batchStdt != ''">
		AND    BL.REG_DT <![CDATA[>=]]> STR_TO_DATE(#{batchStdt},'%Y-%m-%d')
		</if>
		<if test="batchEddt != null and batchEddt != ''">
		AND    BL.REG_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{batchEddt},'%Y-%m-%d'), INTERVAL 1 DAY)
		</if>
	</select>

</mapper>