<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.icomsys.main_vm.db.mybatis.mapper.IntentManageMapper">
	<!-- 인텐트 목록 조회 -->
	<select id="selectIntentList" resultType="java.util.HashMap">
		/* IntentManageMapper.selectIntentList */
		SELECT A.SEQ
			 , A.OPR_MNG_CODE
			 , A.INTENT_NAME
			 , A.CATEGORY
			 , A.REGIST_ID
			 , A.REGIST_DATE
			 , A.UPDATE_ID
			 , A.UPDATE_DATE
			 , B.CODE_DESC
			 , (SELECT COUNT(*) FROM TB_INTENT_EXAMPLE WHERE INTENT_SEQ = A.SEQ AND USE_YN = 'Y') AS EXAMPLE_CNT
		  FROM TB_INTENT_MASTER A
	INNER JOIN TB_BOT_COMMON_CODE B ON A.CATEGORY = B.MINOR_CODE AND A.OPR_MNG_CODE = B.OPR_MNG_CODE
		 <where>
		 	AND A.USE_YN = 'Y'
		 	AND B.MAJOR_CODE = 'INTENT_GROUP'
		 	<if test="oprMngCode != null and oprMngCode != ''">
		 		AND A.OPR_MNG_CODE = #{oprMngCode}
		 	</if>
		 	<if test="category != null and category != ''">
		 		AND A.CATEGORY = #{category}
		 	</if>
		 	<if test="intentName != null and intentName != ''">
		 		AND A.INTENT_NAME REGEXP #{intentName}
		 	</if>
		 	
		 	
		 </where>
	  ORDER BY REGIST_DATE DESC
	</select>

	<!-- 인텐트 예문 목록 조회 -->
	<select id="selectIntentExampleList" resultType="java.util.HashMap">
		/* IntentManageMapper.selectIntentExampleList */
		SELECT SEQ
			 , INTENT_SEQ
			 , EXAMPLE_TEXT
			 , REGIST_ID
			 , REGIST_DATE
			 , UPDATE_ID
			 , UPDATE_DATE
		  FROM TB_INTENT_EXAMPLE
		 WHERE INTENT_SEQ = #{intentSeq}
		   AND USE_YN = 'Y'
	</select>

	<!-- 인텐트 갯수 조회(중복체크) -->
	<select id="selectIntentCnt" resultType="int">
		/* IntentManageMapper.selectIntentCnt */
		SELECT COUNT(*)
		  FROM TB_INTENT_MASTER
		 WHERE INTENT_NAME = #{intentName}
		   AND USE_YN = 'Y'
	</select>

	<!-- 인텐트 예문 갯수 조회(중복체크) -->
	<select id="selectIntentExampleCnt" resultType="int">
		/* IntentManageMapper.selectIntentCnt */
		SELECT COUNT(*)
		FROM TB_INTENT_EXAMPLE
		<!--<foreach collection="exampleList" item="item" separator=",">-->
		WHERE USE_YN = 'Y'
		  AND EXAMPLE_TEXT = #{item.exampleText}
		<!--</foreach>-->
	</select>

	<!-- 인텐트 마스터 등록 -->
	<insert id="insertIntentMaster" parameterType="java.util.HashMap">
		/* IntentManageMapper.insertIntentMaster */
		INSERT INTO TB_INTENT_MASTER (
			  OPR_MNG_CODE
			, INTENT_NAME
			, CATEGORY
			, REGIST_ID
			, REGIST_DATE
			, UPDATE_ID
			, UPDATE_DATE
			, USE_YN
			, IF_MAPPING_ID
		) VALUES (
			#{oprMngCode},
			#{intentName},
			#{category},
			#{registId},
			NOW(),
			#{registId},
			NOW(),
			'Y',
		    #{mappingId}
		)
		<selectKey keyProperty="seq" resultType="String">
            SELECT LAST_INSERT_ID()
        </selectKey>
	</insert>

	<!-- 인텐트 예문 등록 -->
	<insert id="insertIntentExample" >
		/* IntentManageMapper.insertIntent */
		INSERT INTO TB_INTENT_EXAMPLE (
			   INTENT_SEQ
			 , EXAMPLE_TEXT
			 , IF_MAPPING_ID
			 , REGIST_ID
			 , REGIST_DATE
			 , UPDATE_ID
			 , UPDATE_DATE
			 , USE_YN
		) VALUES
		<choose>
			<when test="exampleList != null and exampleList.size() > 0">
				<foreach collection="exampleList" item="item" separator=",">
					<if test="item.seq == null or item.seq == ''">
						(
							<choose>
								<when test="intentSeq == null or intentSeq == ''">
									(SELECT IFNULL(MAX(SEQ), 0) FROM TB_INTENT_MASTER),
								</when>
								<otherwise>
									#{intentSeq},
								</otherwise>
							</choose>
							#{item.exampleText},
						    #{mappingId},
							#{registId},
							NOW(),
							#{registId},
							NOW(),
							'Y'
						)
					</if>
				</foreach>
			</when>
			<otherwise>
				(
					<choose>
						<when test="intentSeq == null or intentSeq == ''">
							(SELECT IFNULL(MAX(SEQ), 0) FROM TB_INTENT_MASTER) A,
						</when>
						<otherwise>
							#{intentSeq},
						</otherwise>
					</choose>
					#{exampleText},
					#{registId},
					NOW(),
					#{registId},
					NOW(),
					'Y'
				)
			</otherwise>
		</choose>

		<selectKey keyProperty="seq" resultType="String">
            SELECT LAST_INSERT_ID()
        </selectKey>
	</insert>

	<!-- 인텐트 마스터 수정 -->
	<update id="updateIntentMaster">
		/* IntentManageMapper.updateIntentMaster */
		UPDATE TB_INTENT_MASTER
		<set>
			UPDATE_ID = #{updateId},
			UPDATE_DATE = NOW(),
			<if test="intentName != null and intentName != ''">
				INTENT_NAME = #{intentName},
			</if>
			<if test="category != null and category != ''">
				CATEGORY = #{category},
			</if>
			<if test="useYn != null and useYn != ''">
				USE_YN = #{useYn}
			</if>
		</set>
		<where>
			AND SEQ = #{seq}
		</where>
	</update>

	<!-- 인텐트 마스터 삭제 -->
	<delete id="deleteIntentMaster">
		/* IntentManageMapper.deleteIntentMaster */
		UPDATE TB_INTENT_MASTER
		<where>
			AND SEQ = #{seq}
		</where>
	</delete>

	<!-- 인텐트 예문 삭제 -->
	<delete id="deleteIntentExample">
		/* IntentManageMapper.deleteIntentExample */
		DELETE FROM TB_INTENT_EXAMPLE
		<where>
			AND INTENT_SEQ = #{intentSeq}
			<if test="exampleList != null and exampleList.size() > 0">
				AND SEQ NOT IN (
					<foreach collection="exampleList" item="item" separator=",">
						<!-- 기존 데이터, 신규 데이터 구분을 SEQ 값의 유무로 판단. -->
						<if test="item.seq != null and item.seq != ''">
							#{item.seq}
						</if>
					</foreach>
				)
			</if>
		</where>
	</delete>

	<!-- 인텐트 예문 단건 삭제 2022.06.28 -->
	<delete id="deleteIntentExample_one">
		/* IntentManageMapper.deleteIntentExample_one */
		DELETE FROM TB_INTENT_EXAMPLE
		WHERE SEQ = #{seq}
	</delete>

	<!-- 인텐트 삭제 시 예문 삭제 -->
	<update id="updateDeleteIntentExample">
		/* IntentManageMapper.updateDeleteIntentExample */
		UPDATE TB_INTENT_EXAMPLE
		<set>
			USE_YN = #{useYn},
		</set>
		<where>
			AND INTENT_SEQ = #{seq}
			<if test="exampleList != null and exampleList.size() > 0">
				AND SEQ NOT IN (
					<foreach collection="exampleList" item="item" separator=",">
						<!-- 기존 데이터, 신규 데이터 구분을 SEQ 값의 유무로 판단. -->
						<if test="item.seq != null and item.seq != ''">
							#{item.seq}
						</if>
					</foreach>
				)
			</if>
		</where>
	</update>

	<!-- 인텐트 분류 목록 조회 -->
	<select id="selectIntentCategoryList" resultType="java.util.HashMap">
		/* IntentManageMapper.selectIntentCategoryList */
		SELECT MAJOR_CODE
			 , MINOR_CODE
			 , OPR_MNG_CODE
			 , CODE_DESC
		  FROM TB_BOT_COMMON_CODE
		 WHERE MAJOR_CODE = 'INTENT_GROUP'
		   AND OPR_MNG_CODE = #{oprMngCode}
	  ORDER BY CODE_DESC ASC
	</select>

	<!-- 인텐트 엑셀 업로드 분류 유효성 검사 -->
	<select id="selectIntentCategoryCode" parameterType="java.util.HashMap" resultType="java.util.HashMap">
		/* IntentManageMapper.selectIntentCategoryCode */
		SELECT MINOR_CODE AS minorCode
		  FROM TB_BOT_COMMON_CODE
		 WHERE MAJOR_CODE = 'INTENT_GROUP'
		   AND CUST_CODE = #{custCode}
		   AND CODE_NAME = #{category}
	  GROUP BY MINOR_CODE
	  ORDER BY CODE_NAME ASC
	</select>

	<select id="selectIntentMappingId" resultType="long">
		/* IntentManageMapper.selectIntentMappingId */
		SELECT IF_MAPPING_ID
		FROM TB_INTENT_MASTER
		WHERE USE_YN = 'Y'
		AND OPR_MNG_CODE = #{oprMngCode}
		AND INTENT_NAME = #{intentName}
	</select>

	<select id="selectIntentSeq" resultType="int">
		/* IntentManageMapper.selectIntentSeq */
		SELECT SEQ
		FROM TB_INTENT_MASTER
		WHERE USE_YN = 'Y'
		  AND OPR_MNG_CODE = #{oprMngCode}
		  AND INTENT_NAME = #{intentName}
	</select>

</mapper>
