<?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.faisp.translator.mapper.TranslatorMapper">
 
 <select id="selectTranslatorList" resultType="Translator" parameterType="Translator">
	    select a.translator_key,
		 version_no,
		 c.item_value  as ogdp1,
		 d.item_value  as tr_lang,
		 tr_career,
		 tr_name,
		 e.item_value  as tr_sex,
		 tr_age,
		 f.item_value  as tr_nny,
		 g.item_value  as tr_edu,
		 tr_cft,
		 h.item_value  as tr_visa,
		 dml_yn,
		 apt_dt,
		 tr_phone
	from translator_info a 
		inner join (select translator_key, max(version_no) as lastVer
					from translator_info
					group by translator_key) b 
				on a.translator_key =b.translator_key and a.version_no = b.lastVer
		left outer join code_mgt c on a.ogdp1 = c.item_cd
		left outer join code_mgt d on a.tr_lang = d.item_cd 
		left outer join code_mgt e on a.tr_sex = e.item_cd 
		left outer join code_mgt f on a.tr_nny = f.item_cd 
		left outer join code_mgt g on a.tr_edu = g.item_cd 
		left outer join code_mgt h on a.tr_visa = h.item_cd 
	where ogdp1 in 
     <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
     </foreach>
     <if test='trLang != null and trLang != ""'>
     	and tr_lang = #{trLang}
     </if>
     <if test='ogdp1 != null and ogdp1 != ""'>
     	and ogdp1 = #{ogdp1}
     </if>
     <if test='trName != null and trName != ""'>
     	and tr_name = #{trName}
     </if>
     <if test='trNny != null and trNny != ""'>
     	and tr_nny = #{trNny}
     </if>
     <if test='trVisa != null and trVisa != ""'>
     	and tr_visa = #{trVisa}
     </if>
     <if test='aptDt != null'>
     	and to_char(apt_dt,'YYYY-MM-DD') = #{aptDt}::VARCHAR
     </if>
     <if test='dmlYn != null and dmlYn != ""'>
     	and dml_yn = #{dmlYn}
     </if>
	order by translator_key desc
    limit #{rowCnt} offset #{firstIndex}
  </select>
  
  <select id="selectTranslatorListCnt" resultType="int" parameterType="Translator">
    select count(*)
	from(
	select a.translator_key,
		 version_no,
		 c.item_value  as ogdp1,
		 d.item_value  as tr_lang,
		 tr_career,
		 tr_name,
		 e.item_value  as tr_sex,
		 tr_age,
		 f.item_value  as tr_nny,
		 g.item_value  as tr_edu,
		 tr_cft,
		 h.item_value  as tr_visa,
		 dml_yn,
		 apt_dt,
		 tr_phone
	from translator_info a 
		inner join (select translator_key, max(version_no) as lastVer
					from translator_info
					group by translator_key) b 
				on a.translator_key =b.translator_key and a.version_no = b.lastVer
		left outer join code_mgt c on a.ogdp1 = c.item_cd
		left outer join code_mgt d on a.tr_lang = d.item_cd 
		left outer join code_mgt e on a.tr_sex = e.item_cd 
		left outer join code_mgt f on a.tr_nny = f.item_cd 
		left outer join code_mgt g on a.tr_edu = g.item_cd 
		left outer join code_mgt h on a.tr_visa = h.item_cd
	where ogdp1 in
	   <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
     </foreach>
     <if test='trLang != null and trLang != ""'>
     	and tr_lang = #{trLang}
     </if>
     <if test='ogdp1 != null and ogdp1 != ""'>
     	and ogdp1 = #{ogdp1}
     </if>
     <if test='trName != null and trName != ""'>
     	and tr_name = #{trName}
     </if>
     <if test='trNny != null and trNny != ""'>
     	and tr_nny = #{trNny}
     </if>
     <if test='trVisa != null and trVisa != ""'>
     	and tr_visa = #{trVisa}
     </if>
     <if test='aptDt != null'>
     	and to_char(apt_dt,'YYYY-MM-DD') = #{aptDt}::VARCHAR
     </if>
     <if test='dmlYn != null and dmlYn != ""'>
     	and dml_yn = #{dmlYn}
     </if>
     ) a
  </select>
  
  <select id="selectHistoryList" resultType="Translator" parameterType="Translator">
	    select translator_key,
	    	   version_no,
	    	   wrt_nm,
	    	   wrt_dt
	    from translator_info
	    where translator_key = #{translatorKey}
	    order by wrt_dt desc
  </select>
  
  <select id="HistoryView" resultType="Translator" parameterType="Translator">
	    select a.translator_key,
		 version_no,
		 c.item_value  as ogdp1,
		 d.item_value  as tr_lang,
		 tr_career,
		 tr_name,
		 e.item_value  as tr_sex,
		 tr_age,
		 f.item_value  as tr_nny,
		 g.item_value  as tr_edu,
		 tr_cft,
		 h.item_value  as tr_visa,
		 dml_yn,
		 apt_dt,
		 tr_phone,
		 remark
	from translator_info a
		left outer join code_mgt c on a.ogdp1 = c.item_cd
		left outer join code_mgt d on a.tr_lang = d.item_cd 
		left outer join code_mgt e on a.tr_sex = e.item_cd 
		left outer join code_mgt f on a.tr_nny = f.item_cd 
		left outer join code_mgt g on a.tr_edu = g.item_cd 
		left outer join code_mgt h on a.tr_visa = h.item_cd
	where translator_key = #{translatorKey}
		  and version_no = #{versionNo}
  </select>
  
  <select id="selectCareerList" resultType="TranslatorCrr" parameterType="TranslatorCrr">
	select career_seq,
		   translator_key,
		   his_gubun,
		   contents,
		   remark,
		   tc_dt
	from translator_career
	where translator_key = #{translatorKey}
	and his_gubun = #{hisGubun}
	order by career_seq desc
  </select>
  
  <select id="selectStatisticsLangCnt" resultType="com.dbnt.faisp.util.ParamMap">
	select (ROW_NUMBER() OVER()) AS rownum,
		    cm.item_value as lang ,
			sum(center+west+south+east+jeju) as cnt_total,
			sum(center) as cnt_center,
			sum(west) as cnt_west,
			sum(south) as cnt_south,
			sum(east) as cnt_east,
			sum(jeju) as cnt_jeju
	from (select 
		    a.tr_lang as lang,
		    case 
		    	when c.organ_cd = 'OG002' or c.parent_organ  = 'OG002' then 1
		    	else 0
		    end as center,
		    case 
		    	when c.organ_cd = 'OG003' or c.parent_organ  = 'OG003' then 1
		    	else 0
		    end as west,
		    case 
		    	when c.organ_cd = 'OG004' or c.parent_organ  = 'OG004' then 1
		    	else 0
		    end as south,
		    case 
		    	when c.organ_cd = 'OG005' or c.parent_organ  = 'OG005' then 1
		    	else 0
		    end as east,
		    case 
		    	when c.organ_cd = 'OG006' or c.parent_organ  = 'OG006' then 1
		    	else 0
		    end as jeju
		from translator_info a 
				inner join (select translator_key, max(version_no) as lastVer 
		  				from translator_info 
						group by translator_key) b 
					on a.translator_key =b.translator_key and a.version_no = b.lastVer
				inner join organ_config c on a.ogdp1 = c.organ_cd) d
				inner join code_mgt cm on d.lang=cm.item_cd 
	group by cm.item_value
  </select>
  
  <select id="selectStatisticsLangTotal" resultType="com.dbnt.faisp.util.ParamMap">
	select sum(cnt_total) as total_total,
		   sum(cnt_center) as total_center,
		   sum(cnt_west) as total_west,
		   sum(cnt_south) as total_south,
		   sum(cnt_east) as total_east,
		   sum(jeju) as total_jeju
	from(
		select cm.item_value ,
				sum(center+west+south+east+jeju) as cnt_total,
				sum(center) as cnt_center,
				sum(west) as cnt_west,
				sum(south) as cnt_south,
				sum(east) as cnt_east,
				sum(jeju) as jeju
		from (select 
			    a.tr_lang as lang,
			    case 
			    	when c.organ_cd = 'OG002' or c.parent_organ  = 'OG002' then 1
			    	else 0
			    end as center,
			    case 
			    	when c.organ_cd = 'OG003' or c.parent_organ  = 'OG003' then 1
			    	else 0
			    end as west,
			    case 
			    	when c.organ_cd = 'OG004' or c.parent_organ  = 'OG004' then 1
			    	else 0
			    end as south,
			    case 
			    	when c.organ_cd = 'OG005' or c.parent_organ  = 'OG005' then 1
			    	else 0
			    end as east,
			    case 
			    	when c.organ_cd = 'OG006' or c.parent_organ  = 'OG006' then 1
			    	else 0
			    end as jeju
			from translator_info a 
					inner join (select translator_key, max(version_no) as lastVer 
			  				from translator_info 
							group by translator_key) b 
						on a.translator_key =b.translator_key and a.version_no = b.lastVer
					inner join organ_config c on a.ogdp1 = c.organ_cd) d
					inner join code_mgt cm on d.lang=cm.item_cd 
		group by cm.item_value
		) a
  </select>
  
  <select id="selectTranslatorListEx" resultType="com.dbnt.faisp.util.ParamMap" parameterType="Translator">
	    select a.translator_key,
		 version_no,
		 c.item_value  as ogdp1,
		 d.item_value  as tr_lang,
		 tr_career,
		 tr_name,
		 e.item_value  as tr_sex,
		 tr_age,
		 f.item_value  as tr_nny,
		 g.item_value  as tr_edu,
		 tr_cft,
		 h.item_value  as tr_visa,
		 dml_yn,
		 apt_dt,
		 tr_phone
	from translator_info a 
		inner join (select translator_key, max(version_no) as lastVer
					from translator_info
					group by translator_key) b 
				on a.translator_key =b.translator_key and a.version_no = b.lastVer
		left outer join code_mgt c on a.ogdp1 = c.item_cd
		left outer join code_mgt d on a.tr_lang = d.item_cd 
		left outer join code_mgt e on a.tr_sex = e.item_cd 
		left outer join code_mgt f on a.tr_nny = f.item_cd 
		left outer join code_mgt g on a.tr_edu = g.item_cd 
		left outer join code_mgt h on a.tr_visa = h.item_cd 
	where wrt_organ in 
	   <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
     </foreach>
     <if test='trLang != null and trLang != ""'>
     	and tr_lang = #{trLang}
     </if>
     <if test='ogdp1 != null and ogdp1 != ""'>
     	and ogdp1 = #{ogdp1}
     </if>
     <if test='trName != null and trName != ""'>
     	and tr_name = #{trName}
     </if>
     <if test='trNny != null and trNny != ""'>
     	and tr_nny = #{trNny}
     </if>
     <if test='trVisa != null and trVisa != ""'>
     	and tr_visa = #{trVisa}
     </if>
     <if test='aptDt != null'>
     	and to_char(apt_dt,'YYYY-MM-DD') = #{aptDt}::VARCHAR
     </if>
     <if test='dmlYn != null and dmlYn != ""'>
     	and dml_yn = #{dmlYn}
     </if>
	order by translator_key desc
  </select>
  
  <select id="selectTrFristId" resultType="String" parameterType="int">
	    select wrt_nm 
		from translator_info
		where translator_key = #{translatorKey}
		order by version_no asc
		limit 1
  </select>

</mapper>