<?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>
			<if test="listCode != 102041 and listCode != null and listCode != ''">
				and c.group_full_cd like #{listCode} || '%'
			</if>
			<if test="listCode == 102041">
				and (c.group_full_cd like 102041 || '%'
					or c.group_full_cd like 102042 || '%'
					or c.group_full_cd like 102043 || '%')
			</if>
			<if test="searchWrd != null and searchWrd != ''">
				and c.group_nm like '%' || #{searchWrd} || '%'
			</if>
			and c.group_type = 'D'
			and tdi.last_yn = 'Y'
		</where>
	</sql>

 	<select id="selectStandardCodeList" parameterType="TnDocumentInfo" resultType="TnDocumentCodeList">
		select
					case
						when a.doc_level &lt; 3 then b.group_nm
						else a.group_nm
					end as main_category,
					case
						when a.doc_level &lt; 3 then c.group_nm
						else b.group_nm
					end as middle_category,
					c.group_nm,
					c.rvsn_remark ,
					c.kcsc_cd,
					tdi.doc_file_grp_id,
					c.group_seq,
					tdf.user_seq is not null as favoriteChk
		from tn_document_group a
				inner join tn_document_group b on a.group_seq = b.parent_group_seq
				inner join tn_document_group c on b.group_seq = c.parent_group_seq
				left join tn_document_info tdi on c.group_seq = tdi.group_seq
				left join tn_document_favorites tdf
		    		on c.group_seq = tdf.group_seq
						<if test='userSeq == null'>
								and 1=0
						</if>
						<if test='userSeq != null'>
							and tdf.user_seq = #{userSeq}
						</if>
		<include refid="selectStandardCodeListWhere"></include>
		order by c.kcsc_cd
 	</select>

	<select id="selectStandardCodeListCnt" parameterType="TnDocumentInfo" resultType="TnDocumentCodeList">
		select count(*) as allCnt,
					sum(case when c.rvsn_remark is not null then 1 end) as remarkCnt
		from tn_document_group a
				inner join tn_document_group b on a.group_seq = b.parent_group_seq
				inner join tn_document_group c on b.group_seq = c.parent_group_seq
				left join tn_document_info tdi on c.group_seq = tdi.group_seq
		<include refid="selectStandardCodeListWhere"></include>
	</select>

	<select id="selectStandardCodeHistoryList" parameterType="TnDocumentInfo" resultType="TnDocumentInfo">
		select a.*
		from tn_document_info a
					 inner join tn_document_group b on a.kcsc_cd = b.kcsc_cd
		<where>
			<if test="listCode != 102041 and listCode != null and listCode != ''">
				and b.group_full_cd like #{listCode} || '%'
			</if>
			<if test="listCode == 102041">
				and (b.group_full_cd like 102041 || '%'
				or b.group_full_cd like 102042 || '%'
				or b.group_full_cd like 102043 || '%')
			</if>
			and b.group_type = 'D'
		</where>
		order by doc_yr asc
	</select>

	<select id="selectStandardCodeGroupSeq" parameterType="String" resultType="Integer">
		select
			group_seq
		from tn_document_group
		where group_full_cd = #{groupFullCd}
	</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>