<?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.equip.mapper.EquipMapper">
	
	<select id="selectEquipStatus" resultType="com.dbnt.faisp.util.ParamMap" parameterType="Equip">
	    select (ROW_NUMBER() OVER()) AS rownum,
	    	   category_cd,
			   equ_type,
			   item_value,
			   item_cd,
			     sum( cnt_bon+ 
					  cnt_center+
					  cnt_incheon+
					  cnt_pyeongtaek+
					  cnt_taean+
					  cnt_boryeong+
					  cnt_west+
					  cnt_mokpo+
					  cnt_buan+
					  cnt_gunsan+
					  cnt_yusu+
					  cnt_wando+
					  cnt_south+
					  cnt_ulsan+
					  cnt_busan+
					  cnt_changwon+
					  cnt_tongyong+
					  cnt_sacheon+
					  cnt_east+
					  cnt_sokcho+
					  cnt_donghe+
					  cnt_ulgin+
					  cnt_pohang+
					  cnt_jeju+
					  cnt_jejuseo+
					  cnt_seoguipo) as total,
			   sum(cnt_bon) as cnt_bon,
			   sum(cnt_center) as cnt_center,
			   sum(cnt_incheon) as cnt_incheon,
			   sum(cnt_pyeongtaek) as cnt_pyeongtaek,
			   sum(cnt_taean) as cnt_taean,
			   sum(cnt_boryeong) as cnt_boryeong,
			   sum(cnt_west) as cnt_west,
			   sum(cnt_mokpo) as cnt_mokpo,
			   sum(cnt_buan) as cnt_buan,
			   sum(cnt_gunsan) as cnt_gunsan,
			   sum(cnt_yusu) as cnt_yusu,
			   sum(cnt_wando) as cnt_wando,
			   sum(cnt_south) as cnt_south,
			   sum(cnt_ulsan) as cnt_ulsan,
			   sum(cnt_busan) as cnt_busan,
			   sum(cnt_changwon) as cnt_changwon,
			   sum(cnt_tongyong) as cnt_tongyong,
			   sum(cnt_sacheon) as cnt_sacheon,
			   sum(cnt_east) as cnt_east,
			   sum(cnt_sokcho) as cnt_sokcho,
			   sum(cnt_donghe) as cnt_donghe,
			   sum(cnt_ulgin) as cnt_ulgin,
			   sum(cnt_pohang) as cnt_pohang,
			   sum(cnt_jeju) as cnt_jeju,
			   sum(cnt_jejuseo) as cnt_jejuseo,
			   sum(cnt_seoguipo) as cnt_seoguipo
		from(
				select a.category_cd,
					   a.item_cd,
							    	   (select item_value from code_mgt cmt where a.category_cd = cmt.item_cd) as equ_type,
									   a.item_value,
									   case 
									   	  when b.mgt_organ = 'OG001' then b.item_qty
									   	  else 0
									   end as cnt_bon,
									   case 
									   	  when b.mgt_organ = 'OG002' then b.item_qty
									   	  else 0
									   end as cnt_center,
									   case 
									   	  when b.mgt_organ = 'OG007' then b.item_qty
									   	  else 0
									   end as cnt_incheon,
									   case 
									   	  when b.mgt_organ = 'OG008' then b.item_qty
									   	  else 0
									   end as cnt_pyeongtaek,
									   case 
									   	  when b.mgt_organ = 'OG009' then b.item_qty
									   	  else 0
									   end as cnt_taean,
									   case 
									   	  when b.mgt_organ = 'OG010' then b.item_qty
									   	  else 0
									   end as cnt_boryeong,
									   case 
									   	  when b.mgt_organ = 'OG003' then b.item_qty
									   	  else 0
									   end as cnt_west,
									   case 
									   	  when b.mgt_organ = 'OG011' then b.item_qty
									   	  else 0
									   end as cnt_mokpo,
									   case 
									   	  when b.mgt_organ = 'OG012' then b.item_qty
									   	  else 0
									   end as cnt_buan,
									   case 
									   	  when b.mgt_organ = 'OG013' then b.item_qty
									   	  else 0
									   end as cnt_gunsan,
									   case 
									   	  when b.mgt_organ = 'OG014' then b.item_qty
									   	  else 0
									   end as cnt_yusu,
									   case 
									   	  when b.mgt_organ = 'OG015' then b.item_qty
									   	  else 0
									   end as cnt_wando,
									   case 
									   	  when b.mgt_organ = 'OG004' then b.item_qty
									   	  else 0
									   end as cnt_south,
									   case 
									   	  when b.mgt_organ = 'OG016' then b.item_qty
									   	  else 0
									   end as cnt_ulsan,
									   case 
									   	  when b.mgt_organ = 'OG017' then b.item_qty
									   	  else 0
									   end as cnt_busan,
									   case 
									   	  when b.mgt_organ = 'OG018' then b.item_qty
									   	  else 0
									   end as cnt_changwon,
									   case 
									   	  when b.mgt_organ = 'OG019' then b.item_qty
									   	  else 0
									   end as cnt_tongyong,
									   case 
									   	  when b.mgt_organ = 'OG020' then b.item_qty
									   	  else 0
									   end as cnt_sacheon,
									   case 
									   	  when b.mgt_organ = 'OG005' then b.item_qty
									   	  else 0
									   end as cnt_east,
									   case 
									   	  when b.mgt_organ = 'OG021' then b.item_qty
									   	  else 0
									   end as cnt_sokcho,
									   case 
									   	  when b.mgt_organ = 'OG022' then b.item_qty
									   	  else 0
									   end as cnt_donghe,
									   case 
									   	  when b.mgt_organ = 'OG023' then b.item_qty
									   	  else 0
									   end as cnt_ulgin,
									   case 
									   	  when b.mgt_organ = 'OG024' then b.item_qty
									   	  else 0
									   end as cnt_pohang,
									   case 
									   	  when b.mgt_organ = 'OG006' then b.item_qty
									   	  else 0
									   end as cnt_jeju,
									    case 
									   	  when b.mgt_organ = 'OG025' then b.item_qty
									   	  else 0
									   end as cnt_jejuseo,
									    case 
									   	  when b.mgt_organ = 'OG026' then b.item_qty
									   	  else 0
									   end as cnt_seoguipo
								from (select category_cd,
											   item_cd,
											   item_value
										from code_mgt
										where category_cd in('IT1','IT2','IT3','IT4')
										order by category_cd,item_cd asc) a left outer join
										(select a.equ_key,
												   a.version_no,
												   mgt_organ,
												   equ_type,
												   detail_type,
												   item_qty
											from equ_mgt a,
												 (select equ_key,max(version_no) as lastVer 
												  from equ_mgt group by equ_key) b,
												  code_mgt cm
											where a.equ_key = b.equ_key
											and a.version_no = b.lastVer
											group by a.equ_key,a.version_no) b on a.item_cd = b.detail_type
								order by a.category_cd,a.item_cd asc
					) c
		group by c.category_cd,c.equ_type,c.item_value,item_cd
		order by category_cd,item_cd asc
  </select>
  
  <select id="selectEquipList" resultType="Equip" parameterType="Equip">
	    select em.equ_key,
			   em.version_no,
			   mgt_organ,
			   case 
				 when oc.organ_type = 'OGC001' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
				 when oc.organ_type = 'OGC002' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
				 else (select item_value from code_mgt cm where cm.item_cd = oc.parent_organ )
				end as sosok,
			   case
			     when oc.organ_type = 'OGC003' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
			     else null
			   end as detail_sosok,
			   equ_type,
			   detail_type,
			   stored_year,
			   item_qty,
			   (select item_value from code_mgt cm where cm.item_cd = item_condition) as item_condition,
			   note,
			   ef.orig_nm,
			   ef.conv_nm,
			   ef.file_path
		from equ_mgt em,
		    (select equ_key,max(version_no) as lastVer 
			  from equ_mgt group by equ_key) b,
			  equ_file ef,
			  organ_config oc
		where em.equ_key = b.equ_key
		and em.version_no = b.lastVer
		and em.equ_key = ef.equ_key
		and em.version_no = ef.version_no
		and em.mgt_organ = oc.organ_cd
		and detail_type = #{detailType}
		and mgt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
        </foreach>
		order by equ_key desc
		limit #{rowCnt} offset #{firstIndex}
  </select>
  
  <select id="selectEquipListCnt" resultType="int" parameterType="Equip">
	    select count(*)
		from(
				select em.equ_key,
							   em.version_no,
							   mgt_organ,
							   case 
								 when oc.organ_type = 'OGC001' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
								 when oc.organ_type = 'OGC002' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
								 else (select item_value from code_mgt cm where cm.item_cd = oc.parent_organ )
								end as sosok,
							   case
							     when oc.organ_type = 'OGC003' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
							     else null
							   end as detail_sosok,
							   equ_type,
							   detail_type,
							   stored_year,
							   item_qty,
							   (select item_value from code_mgt cm where cm.item_cd = item_condition) as item_condition,
							   note,
							   ef.orig_nm,
							   ef.conv_nm,
							   ef.file_path
						from equ_mgt em,
						    (select equ_key,max(version_no) as lastVer 
							  from equ_mgt group by equ_key) b,
							  equ_file ef,
							  organ_config oc
						where em.equ_key = b.equ_key
						and em.version_no = b.lastVer
						and em.equ_key = ef.equ_key
						and em.version_no = ef.version_no
						and em.mgt_organ = oc.organ_cd
						and detail_type = #{detailType}
						and mgt_organ in
						<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
				        #{item}
				        </foreach>
						order by equ_key asc
				) a
  </select>
  
  <select id="selectEquipInfo" resultType="Equip" parameterType="Equip">
	    select em.equ_key,
			   em.version_no,
			   mgt_organ,
			   equ_type,
			   (select item_value from code_mgt cm where cm.item_cd = detail_type ) as detail_Name,
		   	   detail_type,
			   stored_year,
			   item_qty,
			   item_condition,
			   note,
			   wrt_nm,
			   ef.orig_nm,
			   ef.conv_nm,
			   ef.file_path
		from equ_mgt em,
			  (select equ_key,max(version_no) as lastVer 
			   from equ_mgt group by equ_key) b,
			   equ_file ef
		where em.equ_key = b.equ_key
		  	  and em.version_no = b.lastVer
			  and em.equ_key = ef.equ_key
			  and em.version_no = ef.version_no
			  and em.equ_key = #{equKey}
  </select>
  
  <select id="selectEduType" resultType="com.dbnt.faisp.util.ParamMap" parameterType="Equip">
	    select (select item_value from code_mgt cm where cm.item_cd = a.category_cd) as equ_type,
			   (select item_value from code_mgt cm where cm.item_cd = a.item_cd) as detail_type
		from code_mgt a
		where item_cd = #{detailType}
	    
  </select>
  
  <select id="selectHistoryDetail" resultType="Equip" parameterType="Equip">
	    select em.equ_key,
			   em.version_no,
			   mgt_organ,
			   case 
				 when oc.organ_type = 'OGC001' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
				 when oc.organ_type = 'OGC002' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
				 else CONCAT((select item_value from code_mgt cm where cm.item_cd = oc.parent_organ ),'-',(select item_value from code_mgt cm where cm.item_cd = oc.organ_cd ))
				end as sosok,
			   (select item_value from code_mgt cm where cm.item_cd = equ_type ) as equ_type,
			   (select item_value from code_mgt cm where cm.item_cd = detail_type ) as detail_type,
			   stored_year,
			   item_qty,
			   (select item_value from code_mgt cm where cm.item_cd = item_condition ) as item_condition,
			   note,
			   ef.orig_nm,
			   ef.conv_nm,
			   ef.file_path
		from equ_mgt em,
			  equ_file ef,
			  organ_config oc
		where em.equ_key = ef.equ_key
			  and em.version_no = ef.version_no
			  and em.mgt_organ = oc.organ_cd
			  and em.equ_key = #{equKey}
			  and em.version_no = #{versionNo}
  </select>
  
  <select id="selectEquipLogList" resultType="EquipLog" parameterType="EquipLog">
	    select (select item_value from code_mgt cm where cm.item_cd = el.equ_type) as equ_type,
			   (select item_value from code_mgt cm where cm.item_cd = el.detail_type) as detail_name,
			   detail_type,
			   (select item_value from code_mgt where item_cd = wrt_organ) as sosok,
			   (select item_value from code_mgt where item_cd = ofc_cd ) as detail_sosok,
			   el_contents,
			   wrt_nm,
			   el.wrt_dt
		from equ_log el,
			 user_info ui
		where el.wrt_nm = ui.user_id
		and wrt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
        </foreach>
		order by el.wrt_dt desc
		limit #{rowCnt} offset #{firstIndex}
  </select>
  
   <select id="selectEquipLogListCnt" resultType="int" parameterType="EquipLog">
   select count(*)
   from(
	    select (select item_value from code_mgt cm where cm.item_cd = el.equ_type) as equ_type,
			   (select item_value from code_mgt cm where cm.item_cd = el.detail_type) as detail_name,
			   detail_type,
			   (select item_value from code_mgt where item_cd = wrt_organ) as sosok,
			   (select item_value from code_mgt where item_cd = ofc_cd ) as detail_sosok,
			   el_contents,
			   wrt_nm,
			   el.wrt_dt
		from equ_log el,
			 user_info ui
		where el.wrt_nm = ui.user_id
		and wrt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
        </foreach>
		order by el.wrt_dt desc
	) a
  </select>
  
  <select id="selectEquipFirstId" resultType="String" parameterType="Equip">
	     select wrt_nm  
		 from equ_mgt
		 where equ_key = #{equKey}
		 order by version_no asc
		 limit 1
  </select>
  
  <select id="selectCellPhoneList" resultType="CellPhone" parameterType="CellPhone">
		select phone_key,
			   case 
				 when oc.organ_type = 'OGC001' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
				 when oc.organ_type = 'OGC002' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
				 else concat((select item_value from code_mgt cm where cm.item_cd = oc.parent_organ ),'-', (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd ))
			    end as sosok,
			    tel_no,
			    p_user_seq,
			    (select user_nm from user_info ui where ui.user_seq = cm.p_user_seq) as user_nm,
			    ext_mail,
			    webex_no,
			    katalk_id
		from cellphone_mgt cm,
			 organ_config oc
		where cm.mgt_organ = oc.organ_cd
		and cm.mgt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
        </foreach>
		order by cm.wrt_dt desc
		<if test='excel != "Y"'>
		limit #{rowCnt} offset #{firstIndex}
		</if>
  </select>
  
  <select id="selectCellPhoneListCnt" resultType="int" parameterType="CellPhone">
		select count(*)
		from(
			select phone_key,
				   case 
					 when oc.organ_type = 'OGC001' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
					 when oc.organ_type = 'OGC002' then (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd )
					 else concat((select item_value from code_mgt cm where cm.item_cd = oc.parent_organ ),'-', (select item_value from code_mgt cm where cm.item_cd = oc.organ_cd ))
				    end as sosok,
				    tel_no,
				    p_user_seq,
				    (select user_nm from user_info ui where ui.user_seq = cm.p_user_seq) as user_nm,
				    ext_mail,
				    webex_no,
				    katalk_id
			from cellphone_mgt cm,
				 organ_config oc
			where cm.mgt_organ = oc.organ_cd
			and cm.mgt_organ in
			<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
	        #{item}
	        </foreach>
			order by cm.wrt_dt desc
		) a
  </select>
  
  <select id="selectOrganList" resultType="com.dbnt.faisp.util.ParamMap" parameterType="UseList">
	     select item_cd,
		        item_value
		 from code_mgt cm,
		      organ_config oc
		  where cm.item_cd = oc.organ_cd
		  and oc.organ_type = 'OGC003'
		  and cm.use_chk = 'T'
		  and item_cd in
		  <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
      	 	#{item}
   	      </foreach>
     	  order by item_cd asc
  </select>
  
  <select id="selectLastUseNo" resultType="Integer" parameterType="UseList">
	     select a.useNo
		 from(
			 select substring(use_no, 6, 3)::int as useNo  
			 from use_list
			 where mgt_organ = #{mgtOrgan}
			 and use_no  like #{year}||'%'
			 and use_type = #{useType}
			 group by use_no 
			 ) a
		 order by useNo desc
		 limit 1
  </select>
  
  <sql id="searchUseList">
      <if test='mgtOrgan != null and mgtOrgan != ""'>
        and ul.mgt_organ = #{mgtOrgan}
      </if>
      <if test='useNo != null and useNo != ""'>
        and ul.use_no like '%'||#{useNo}||'%'
      </if>
      <if test='detailType != null and detailType != ""'>
        and ul.detail_type = #{detailType}
      </if>
      <if test='dateSelector == "useDt"'>
        <if test='startDate != null and startDate != ""'>
          and ul.use_dt >= #{startDate}::date
        </if>
        <if test='endDate != null and endDate != ""'>
          and ul.use_dt &lt;= #{endDate}::date+1
        </if>
      </if>
      <if test='dateSelector == "wrtDt"'>
        <if test='startDate != null and startDate != ""'>
          and ul.wrt_dt >= #{startDate}::date
        </if>
        <if test='endDate != null and endDate != ""'>
          and ul.wrt_dt &lt;= #{endDate}::date+1
        </if>
      </if>
  </sql>
  
  <select id="selectUseList" resultType="UseList" parameterType="UseList">
	     select (select item_value from code_mgt where item_cd = ul.mgt_organ) as sosok,
	           ul.mgt_organ,
	           ul.use_type,
			   ul.use_no,
			   ul.version_no,
			   use_dt,
			   (select item_value from code_mgt where item_cd = detail_type) as detail_type_name,
			   detail_type,
			   detail_self,
			   people_cnt,
			   description,
			   wrt_dt 
		from use_list ul,
			 (select use_no,max(version_no) as lastVer,mgt_organ,use_type from use_list
			 where use_type = #{useType}
			 and mgt_organ in
			 <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
	         #{item}
	         </foreach>
			 group by use_no,use_type,mgt_organ) b
		where ul.use_no = b.use_no
		and ul.version_no = b.lastVer
		and ul.mgt_organ = b.mgt_organ
		and ul.use_type = b.use_type
		and ul.use_type = #{useType}
		and ul.mgt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
        </foreach>
        <include refid="searchUseList"></include>
		order by ul.use_no desc
		<if test='excel != "Y"'>
		limit #{rowCnt} offset #{firstIndex}
		</if>
  </select>
  
  <select id="selectUseListCnt" resultType="Integer" parameterType="UseList">
  select count(*)
  from(
	     select (select item_value from code_mgt where item_cd = ul.mgt_organ) as sosok,
	           ul.mgt_organ,
	           ul.use_type,
			   ul.use_no,
			   ul.version_no,
			   use_dt,
			   (select item_value from code_mgt where item_cd = detail_type) as detail_type_name,
			   detail_type,
			   detail_self,
			   people_cnt,
			   description,
			   wrt_dt 
		from use_list ul,
			 (select use_no,max(version_no) as lastVer,mgt_organ,use_type from use_list
			 where use_type = #{useType}
			 and mgt_organ in
			 <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
	         #{item}
	         </foreach>	         
			 group by use_no,use_type,mgt_organ) b
		where ul.use_no = b.use_no
		and ul.version_no = b.lastVer
		and ul.mgt_organ = b.mgt_organ
		and ul.use_type = b.use_type
		and ul.use_type = #{useType}
		and ul.mgt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        #{item}
        </foreach>
        <include refid="searchUseList"></include>
		order by ul.use_no desc
	) a
  </select>
  
  <select id="selectUseInfo" resultType="UseList" parameterType="UseList">
  select (select item_value from code_mgt where item_cd = mgt_organ) as sosok,
	       use_type,
	       mgt_organ,
		   ul.use_no,
		   ul.version_no,
		   use_dt,
		   (select item_value from code_mgt where item_cd = detail_type) as detail_type_name,
		   detail_type,
		   detail_self,
		   people_cnt,
		   description,
		   wrt_dt 
	from use_list ul,
		 (select use_no,max(version_no) as lastVer from use_list
		 where use_no = #{useNo}
		 <if test='versionNo != null and versionNo != ""'>
		 and version_no = #{versionNo}
		 </if> 
		 and use_type = #{useType}
		 and mgt_organ  = #{mgtOrgan}
		 group by use_no) b
	where ul.use_no = b.use_no
	and ul.version_no = b.lastVer
	and ul.use_no = #{useNo}
	<if test='versionNo != null and versionNo != ""'>
	and version_no = #{versionNo}
	</if> 
	and use_type = #{useType}
	and mgt_organ  = #{mgtOrgan}
  </select>

 
</mapper>