<?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.dbnt.kcscbackend.standardCode.mapper.StandardCodeMapper">

	<sql id="selectStandardCodeListWhere">
		<where>
			a.parent_group_seq = a2.seq
			and a2.parent_seq = a3.seq
			<if test="listCode != 102041 and listCode != null and listCode != ''">
				and a.group_full_cd like #{listCode} || '%'
			</if>
			<if test="listCode == 102041">
				and (a.group_full_cd like 102041 || '%'
					or a.group_full_cd like 102042 || '%'
					or a.group_full_cd like 102043 || '%')
			</if>
			<if test="searchWrd != null and searchWrd != ''">
				and a.group_nm like '%' || #{searchWrd} || '%'
			</if>
			and a.group_type = 'D'
			and tdi.last_yn = 'Y'
		</where>
	</sql>

 	<select id="selectStandardCodeList" parameterType="TnDocumentInfo" resultType="TnDocumentCodeList">
		select
			count(*) over() as contentcount,
			case
				when a3.doc_level > 1 then a3.doc_code_name
				else a2.doc_code_name
			end as main_category,
			case
				when a3.doc_level > 1 then a2.doc_code_name
				else a.group_nm
			end as middle_category,
			a.group_nm,
			a.kcsc_cd,
			tdi.doc_file_grp_id,
			a.group_seq
		from
			tn_document_group a
			left join tn_document_info tdi on
			a.group_seq = tdi.group_seq
			left join tn_document_content tdc on
			tdi.doc_info_seq = tdc.doc_info_seq,
			sp_get_tn_document_code_by_tree() a2,
			sp_get_tn_document_code_by_tree() a3
		<include refid="selectStandardCodeListWhere"></include>
		group by main_category, middle_category, a.group_nm, a.kcsc_cd, tdi.doc_file_grp_id, a.group_seq
		order by a.kcsc_cd
 	</select>

	<select id="selectStandardCodeGroupSeq" parameterType="String" resultType="Integer">
		select
			group_seq
		from tn_document_group
		where group_full_cd = #{groupFullCd}
	</select>

	<select id="selectStandardCodeCategoryList" parameterType="Integer" resultType="TnDocumentCodeList">
		select
			group_seq,
		    group_full_cd,
		    kcsc_cd,
		    group_cur_cd,
		    group_nm,
		    parent_group_seq
		from tn_document_group
		where parent_group_seq = #{parentGroupSeq}
	</select>

	<select id="selectStandardCodeRevisionhistoryList" resultType="TnDocumentInfo">
		select tdi.aplcn_bgng_ymd,tdi.doc_file_grp_id,tdi.rvsn_file_grp_id from tn_document_info tdi, tn_document_group tdg
		where 1 = 1
		  and tdi.group_seq = tdg.group_seq
		  and tdi.kcsc_cd like 'KDS%'
		  and tdi.group_seq =#{groupseq}
		  and tdi.use_yn = 'Y'
		order by tdi.kcsc_cd;
	</select>

</mapper>