<?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.SynonymManageMapper">

	<select id="selectSynonymList" resultType="java.util.HashMap">
	/* SynonymManageMapper.selectSynonymList */
		SELECT 
		    a.SEQ,
		    a.IDIV_WORD,
		    a.REP_WORD,
		    GROUP_CONCAT(b.SEQ
		        SEPARATOR ',') AS SYNONYM_SEQS,
		    GROUP_CONCAT(b.SYNONYM_WORD
		        SEPARATOR ',') AS SYNONYM_WORDS
		FROM
		    TB_SYNONYM_MASTER a
		        LEFT JOIN
		    TB_SYNONYM_WORD b ON a.SEQ = b.MASTER_SEQ
				and b.USE_YN = 'Y'
		WHERE
			a.USE_YN='Y'
			AND b.USE_YN='Y'
		    AND a.OPR_MNG_CODE = #{serviceCode}
		<if test="repWord != '' and repWord != null">
			AND a.REP_WORD REGEXP #{repWord}
		</if>
		GROUP BY a.SEQ, a.IDIV_WORD, a.REP_WORD
		ORDER BY a.UPDATE_DATE DESC
	</select>
	
	<update id="deleteSynonym">
	/* SynonymManageMapper.deleteSynonym */
		UPDATE TB_SYNONYM_MASTER SET
			USE_YN='N'
		WHERE SEQ=#{seq}
	</update>
	
	<select id="dupIndivWord" resultType="int">
	/* SynonymManageMapper.dupIndivWord */
		SELECT 
			COUNT(*)
		FROM 
			TB_SYNONYM_MASTER
		WHERE
			IDIV_WORD=#{idivWord}
			AND USE_YN='Y'
			AND OPR_MNG_CODE=#{serviceCode}
	</select>
	
	<select id="dupRepWord" resultType="int">
	/* SynonymManageMapper.dupRepWord */
		SELECT 
			COUNT(*)
		FROM
			TB_SYNONYM_MASTER
		WHERE 
			REP_WORD=#{repWord}
		    AND USE_YN='Y'
			AND OPR_MNG_CODE=#{serviceCode}
	</select>

	<select id="dupRepWordN" resultType="int">
		/* SynonymManageMapper.dupRepWord */
		SELECT
			COUNT(*)
		FROM
			TB_SYNONYM_MASTER
		WHERE
			REP_WORD=#{repWord}
		  AND OPR_MNG_CODE=#{serviceCode}
		  AND NOT SEQ=#{seq}
	</select>
	
	<select id="dupSynonymY" resultType="int">
	/* SynonymManageMapper.dupSynonymY */
		SELECT 
			COUNT(*)
		FROM 
			TB_SYNONYM_WORD
		WHERE 
			SYNONYM_WORD=#{synonymWord}
			AND OPR_MNG_CODE=#{serviceCode}
			AND USE_YN='Y'
		  AND NOT MASTER_SEQ=#{seq}
	</select>
	
	<select id="dupSynonymN" resultType="int">
	/* SynonymManageMapper.dupSynonymN */
		SELECT 
			COUNT(*)
		FROM 
			TB_SYNONYM_WORD
		WHERE 
			SYNONYM_WORD=#{synonymWord}
			AND OPR_MNG_CODE=#{serviceCode}
	</select>
	
	<insert id="insertSynonymMaster" useGeneratedKeys="true" keyProperty="seq">
	/* SynonymManageMapper.insertSynonymMaster */
		<if test="seq == '' or seq == null">
		INSERT INTO TB_SYNONYM_MASTER 
		(OPR_MNG_CODE, IDIV_WORD, REP_WORD, IF_MAPPING_ID, USE_YN, REGIST_ID, REGIST_DATE, UPDATE_ID, UPDATE_DATE)
		VALUES
		(#{serviceCode}, #{idivWord}, #{repWord}, #{mappingId},'Y', #{userId}, NOW(), #{userId}, NOW())
		</if>
		<if test="seq != '' and seq != null">
		UPDATE TB_SYNONYM_MASTER 
		SET
			IDIV_WORD=#{idivWord}, 
			REP_WORD=#{repWord},
			USE_YN = 'Y',
			UPDATE_ID=#{userId}, 
			UPDATE_DATE=NOW()
		WHERE 
			SEQ=#{seq}
		</if>
	</insert>
	
	<insert id="insertSynonymWord">
	/* SynonymManageMapper.insertSynonymWord */
		INSERT INTO TB_SYNONYM_WORD
		(MASTER_SEQ, OPR_MNG_CODE, SYNONYM_WORD, USE_YN, REGIST_ID, REGIST_DATE, UPDATE_ID, UPDATE_DATE)
		SELECT
			a.SEQ, #{serviceCode}, #{synonymWord}, 'Y',#{userId}, NOW() ,#{userId}, NOW()
		FROM 
			TB_SYNONYM_MASTER a
		WHERE 
			a.IDIV_WORD=#{idivWord}
			AND a.OPR_MNG_CODE=#{serviceCode}
	</insert>
	
	<update id="updateSynonymWord">
	/* SynonymManageMapper.updateSynonymWord */
		UPDATE TB_SYNONYM_WORD a,
			(SELECT SEQ FROM TB_SYNONYM_MASTER WHERE IDIV_WORD=#{idivWord} AND OPR_MNG_CODE = #{serviceCode} AND USE_YN = 'Y') b
		SET
			a.MASTER_SEQ=b.SEQ,
			a.USE_YN='Y',
			a.UPDATE_ID=#{userId}, 
			a.UPDATE_DATE=NOW()
		WHERE SYNONYM_WORD = #{synonymWord}
		  AND OPR_MNG_CODE = #{serviceCode}
	</update>
	
	<update id="saveSynonymList">
	/* SynonymManageMapper.saveSynonymList */
		UPDATE TB_SYNONYM_MASTER a,
			TB_SYNONYM_WORD b
		SET
			a.IDIV_WORD = #{idivWord},
			a.REP_WORD = #{repWord},
			a.UPDATE_ID = #{userId},
			a.UPDATE_DATE = NOW()
			<if test="list != null and list.size() > 0">
			, b.USE_YN = IF(b.SYNONYM_WORD IN(
			<foreach collection="list" item="item" separator=",">
			#{item}
			</foreach>
			), 'Y', 'N')
			</if>
		WHERE 
			a.IDIV_WORD = #{idivWord}
			AND b.MASTER_SEQ = a.SEQ
			AND a.USE_YN = 'Y'
			AND b.OPR_MNG_CODE = #{serviceCode};
	</update>
	
	<select id="selectSynonymMaster" resultType="integer">
	/* SynonymManageMapper.selectSynonymMaster */
		SELECT
			SEQ
		FROM
			TB_SYNONYM_MASTER
		WHERE
			OPR_MNG_CODE=#{serviceCode}
			AND IDIV_WORD=#{idivWord}
	</select>

	<update id="deleteSynonymWord">
		/* SynonymManageMapper.deleteSynonymWord */
		UPDATE TB_SYNONYM_WORD SET
			USE_YN='N'
		WHERE MASTER_SEQ=#{seq}
	</update>

	<select id="countSynonymWord" resultType="int">
		/* SynonymManageMapper.countSynonymWord */
		SELECT
			COUNT(*)
		FROM
			TB_SYNONYM_WORD
		WHERE
			MASTER_SEQ=#{seq}
		  AND USE_YN='Y'
		  AND OPR_MNG_CODE=#{serviceCode}
	</select>

</mapper>
