<?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.fipTarget.mapper.FipTargetMapper">
 
  <select id="selectPartInfoList" resultType="PartInfo" parameterType="PartInfo">
	     select pi.pi_seq,
			     version_no,
				 (select item_value from code_mgt where item_cd = mgt_organ) as mgt_organ,
				 land_police,
				 (select item_value from code_mgt where item_cd = mp_work_type) as mp_work_type,
				 mp_people_cnt,
				 mp_description,
				 (select item_value from code_mgt where item_cd = pl_work_type) as pl_work_type,
				 pl_people_cnt,
				 pl_description,
				 (select user_nm from user_info where user_seq = pi_user_seq ) as pi_manager_name,
				 terminal_nm,
				 rent_type,
				 rent_price,
				 utility_type,
				 utility_price,
				 wrt_dt 
		 from part_info pi,
			 (select pi_seq,max(version_no) as lastVer 
			  from part_info group by pi_seq) b
		where pi.pi_seq = b.pi_seq
		and pi.version_no = b.lastVer
		and mgt_organ in
		 <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        	#{item}
     	 </foreach>
     	 <if test='mgtOrgan != null and mgtOrgan != ""'>
     		and mgt_organ = #{mgtOrgan}
     	 </if>
     	  <if test='landPolice != null and landPolice != ""'>
     		and land_police LIKE CONCAT('%', #{landPolice}, '%')
     	 </if>
     	 <if test='mpWorkType != null and mpWorkType != ""'>
     		and mp_work_type = #{mpWorkType}
     	 </if>
     	 <if test='plWorkType != null and plWorkType != ""'>
     		and pl_work_type = #{plWorkType}
     	 </if>
     	 <if test='terminalNm != null and terminalNm != ""'>
     		and terminal_nm LIKE CONCAT('%', #{terminalNm}, '%')
     	 </if>
     	 <if test='startDate != null and startDate != ""'>
           and to_char(wrt_dt, 'YYYY-MM-DD') = #{startDate}
     	 </if>
     	order by wrt_dt desc
     	<if test='excel != "Y"'>
		limit #{rowCnt} offset #{firstIndex}
		</if>    	
  </select>
  
  <select id="selectPartInfoListCnt" resultType="int" parameterType="PartInfo">
  	select count(*)
  	from(
	     select pi.pi_seq,
			    version_no,
			    mgt_organ,
			    land_police,
			    mp_work_type,
			    mp_people_cnt,
			    mp_description,
			    pl_work_type,
			    pl_people_cnt,
			    pl_description,
			    (select user_nm from user_info where user_seq = pi_user_seq ) as pi_manager_name,
			    terminal_nm,
			    rent_type,
			    rent_price,
			    utility_type,
			    utility_price,
			    wrt_dt 
		 from part_info pi,
			 (select pi_seq,max(version_no) as lastVer 
			  from part_info group by pi_seq) b
		where pi.pi_seq = b.pi_seq
		and pi.version_no = b.lastVer
		and mgt_organ in
		 <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        	#{item}
     	 </foreach>
     	 <if test='mgtOrgan != null and mgtOrgan != ""'>
     		and mgt_organ = #{mgtOrgan}
     	 </if>
     	  <if test='landPolice != null and landPolice != ""'>
     		and land_police LIKE CONCAT('%', #{landPolice}, '%')
     	 </if>
     	 <if test='mpWorkType != null and mpWorkType != ""'>
     		and mp_work_type = #{mpWorkType}
     	 </if>
     	 <if test='plWorkType != null and plWorkType != ""'>
     		and pl_work_type = #{plWorkType}
     	 </if>
     	 <if test='terminalNm != null and terminalNm != ""'>
     		and terminal_nm LIKE CONCAT('%', #{terminalNm}, '%')
     	 </if>
     	 <if test='startDate != null and startDate != ""'>
           and to_char(wrt_dt, 'YYYY-MM-DD') = #{startDate}
     	 </if>
     	order by wrt_dt desc
     ) a
  </select>
  
  <select id="selectWorkTypeTotal" resultType="com.dbnt.faisp.util.ParamMap" parameterType="PartInfo">
	     select sum(mp_sangju) as mp_sangju,
		        sum(mp_sangju_cnt) as mp_sangju_total,
		        sum(mp_bsangju) as mp_bsangju,
		        sum(mp_bsangju_cnt) as mp_bsangju_total,
		        sum(mp_closure) as mp_closure,
		        sum(mp_closure_cnt) as mp_closure_total,
		        sum(pl_sangju) as pl_sangju,
		        sum(pl_sangju_cnt) as pl_sangju_total,
		        sum(pl_bsangju) as pl_bsangju,
		        sum(pl_bsangju_cnt) as pl_bsangju_total,
		        sum(pl_closure) as pl_closure,
		        sum(pl_closure_cnt) as pl_closure_total
		 from(
				select case
					   	 when mp_work_type = 'PIS001' then coalesce(mp_people_cnt,0)
					   	 else 0
					   end as mp_sangju_cnt,
					   case
					   	 when mp_work_type = 'PIS002' then coalesce(mp_people_cnt,0)
					   	 else 0
					   end as mp_bsangju_cnt,
					   case
					   	 when mp_work_type = 'PIS003' then coalesce(mp_people_cnt,0)
					   	 else 0
					   end as mp_closure_cnt,
					   case
					   	 when pl_work_type = 'PIS001' then coalesce(pl_people_cnt,0)
					   	 else 0
					   end as pl_sangju_cnt,
					   case
					   	 when pl_work_type = 'PIS002' then coalesce(pl_people_cnt,0)
					   	 else 0
					   end as pl_bsangju_cnt,
					   case
					   	 when pl_work_type = 'PIS003' then coalesce(pl_people_cnt,0)
					   	 else 0
					   end as pl_closure_cnt,
					   case
					   	 when mp_work_type = 'PIS001' then 1
					   	 else 0
					   end as mp_sangju,
					   case
					   	 when mp_work_type = 'PIS002' then 1
					   	 else 0
					   end as mp_bsangju,
					   case
					   	 when mp_work_type = 'PIS003' then 1
					   	 else 0
					   end as mp_closure,
					   case
					   	 when pl_work_type = 'PIS001' then 1
					   	 else 0
					   end as pl_sangju,
					   case
					   	 when pl_work_type = 'PIS002' then 1
					   	 else 0
					   end as pl_bsangju,
					   case
					   	 when pl_work_type = 'PIS003' then 1
					   	 else 0
					   end as pl_closure
				from part_info pi,
					 (select pi_seq,max(version_no) as lastVer 
					  from part_info group by pi_seq) b
				where pi.pi_seq = b.pi_seq
				and pi.version_no = b.lastVer
				and mgt_organ in
				<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
        			#{item}
     	 		</foreach>
     	 		<if test='mgtOrgan != null and mgtOrgan != ""'>
		     		and mgt_organ = #{mgtOrgan}
		        </if>
		     	  <if test='landPolice != null and landPolice != ""'>
		     		and land_police LIKE CONCAT('%', #{landPolice}, '%')
		        </if>
		     	<if test='mpWorkType != null and mpWorkType != ""'>
		     		and mp_work_type = #{mpWorkType}
		     	</if>
		     	<if test='plWorkType != null and plWorkType != ""'>
		     		and pl_work_type = #{plWorkType}
		     	</if>
		     	<if test='terminalNm != null and terminalNm != ""'>
		     		and terminal_nm LIKE CONCAT('%', #{terminalNm}, '%')
		     	</if>
		     	<if test='startDate != null and startDate != ""'>
		           and to_char(wrt_dt, 'YYYY-MM-DD') = #{startDate}
		     	</if>
				group by pi.pi_seq, version_no
		 ) a
  </select>
  
  <select id="selectPartInfo" resultType="PartInfo" parameterType="PartInfo">
	    select pi.pi_seq,
			     version_no,
				 mgt_organ,
				 land_police,
				 mp_work_type,
				 mp_people_cnt,
				 mp_description,
				 pl_work_type,
				 pl_people_cnt,
				 pl_description,
				 pi_user_seq,
				 (select user_nm from user_info where user_seq = pi_user_seq ) as pi_manager_name,
				 terminal_nm,
				 rent_type,
				 rent_price,
				 utility_type,
				 utility_price,
				 wrt_dt 
		 from part_info pi,
			 (select pi_seq,max(version_no) as lastVer 
			  from part_info group by pi_seq) b
		where pi.pi_seq = b.pi_seq
		and pi.version_no = b.lastVer
		and pi.pi_seq = #{piSeq}
  </select>
  
  <select id="selectPartInfoFile" resultType="PartInfoFile" parameterType="PartInfo">
	    select file_seq,
	       pif.pi_seq,
	       version_no,
	       orig_nm,
	       conv_nm,
	       file_extn,
	       file_size,
	       file_path
		 from part_info_file pif,
			 (select pi_seq,max(version_no) as lastVer 
			  from part_info_file group by pi_seq) b
		where pif.pi_seq = b.pi_seq
		and pif.version_no = b.lastVer
		and pif.pi_seq = #{piSeq}
  </select>
  
  <select id="selectPartInfoSeq" resultType="PartInfo" parameterType="PartInfo">
	    select pi_seq,
			   version_no,
			   wrt_nm,
			   wrt_dt 
		 from part_info 
		where pi_seq = #{piSeq}
		order by version_no desc
  </select>
  
  <select id="selectPartInfoHistoryView" resultType="PartInfo" parameterType="PartInfo">
	    select pi_seq,
			     version_no,
				 (select item_value from code_mgt where item_cd = mgt_organ) as mgt_organ,
				 land_police,
				 (select item_value from code_mgt where item_cd = mp_work_type) as mp_work_type,
				 mp_people_cnt,
				 mp_description,
				(select item_value from code_mgt where item_cd = pl_work_type) as pl_work_type,
				 pl_people_cnt,
				 pl_description,
				 pi_user_seq,
				 (select user_nm from user_info where user_seq = pi_user_seq ) as pi_manager_name,
				 terminal_nm,
				 case 
				 	when rent_type = 'Y' then '유상'
				 	when rent_type = 'N' then '무상'
				 	else null
				 end as rent_type,
				 rent_price,
				 case 
				 	when utility_type = 'Y' then '유상'
				 	when utility_type = 'N' then '무상'
				 	else null
				 end as utility_type,
				 utility_price,
				 wrt_dt 
		from part_info
		where pi_seq = #{piSeq}
		and version_no = #{versionNo}
  </select>
  
  <select id="selectPartInfoFileHistoryView" resultType="PartInfoFile" parameterType="PartInfo">
	    select file_seq,
	       	   pi_seq,
	       	   version_no,
	       	   orig_nm,
	       	   conv_nm,
	       	   file_extn,
	       	   file_size,
	       	   file_path
		from part_info_file 
		where pi_seq = #{piSeq}
		and version_no = #{versionNo}
  </select>
  
  <select id="selectPartInfoFirstId" resultType="String" parameterType="int">
	    select wrt_nm
		from part_info 
		where pi_seq = #{piSeq}
		order by version_no asc
		limit 1
  </select>
  
  <select id="selectTerminalName" resultType="com.dbnt.faisp.util.ParamMap" parameterType="PartWork">
	    select pi.pi_seq,
		       terminal_nm
		from part_info pi,
			 (select pi_seq,max(version_no) as lastVer 
					  from part_info group by pi_seq) b
		where pi.pi_seq = b.pi_seq
		and pi.version_no = b.lastVer
		and mgt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
			 #{item}
		</foreach>
		order by terminal_nm asc    	 		
  </select>
  
  <select id="selectPartWorkList" resultType="PartWork" parameterType="PartWork">
	    select pw.pw_seq,
	   		   pw.pi_seq ,
	    	   pi.terminal_nm,
			   pw.wrt_nm,
			   pw.work_dt,
			   pw.save_yn,
			   work_type,
			   case 
			   	when pw.work_type = 'CH' then 'O'
			   end as work_type_ch,
			   case 
			   	when pw.work_type = 'POCI' then 'O'
			   end as work_type_poci,
			   case 
			   	when pw.work_type = 'SRI' then 'O'
			   end as work_type_sri,
			   case 
			   	when pw.work_type = 'JI' then 'O'
			   end as work_type_ji,
			   case 
			   	when pw.work_type = 'MT' then 'O'
			   end as work_type_mt,
			   case 
			   	when pw.work_type = 'ETC' then 'O'
			   end as work_type_etc,
			   pw.description,
			   (select count(*) from part_work_file pwf where  pw.pw_seq = pwf.pw_seq and pw.pi_seq = pwf.pi_seq) as file_cnt,
			   pw.wrt_dt 
		from part_work pw,
			 part_info pi,
			 (select pi_seq,max(version_no) as lastVer 
			  from part_info group by pi_seq) b
		where pw.pi_seq = pi.pi_seq 
		and pi.pi_seq = b.pi_seq
		and pi.version_no = b.lastVer
		and pi.mgt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
			 #{item}
		</foreach>
		order by wrt_dt desc
		<if test='excel != "Y"'>
		limit #{rowCnt} offset #{firstIndex}
		</if>    	 		
  </select>
  
  <select id="selectPartWorkListCnt" resultType="int" parameterType="PartWork">
  select count(*)
  from(
	    select pw.pw_seq,
	   		   pw.pi_seq ,
	    	   pi.terminal_nm,
			   pw.wrt_nm,
			   pw.work_dt,
			   pw.save_yn,
			   work_type,
			   case 
			   	when pw.work_type = 'CH' then 'O'
			   end as work_type_ch,
			   case 
			   	when pw.work_type = 'POCI' then 'O'
			   end as work_type_poci,
			   case 
			   	when pw.work_type = 'SRI' then 'O'
			   end as work_type_sri,
			   case 
			   	when pw.work_type = 'JI' then 'O'
			   end as work_type_ji,
			   case 
			   	when pw.work_type = 'MT' then 'O'
			   end as work_type_mt,
			   case 
			   	when pw.work_type = 'ETC' then 'O'
			   end as work_type_etc,
			   pw.description,
			   (select count(*) from part_work_file pwf where  pw.pw_seq = pwf.pw_seq and pw.pi_seq = pwf.pi_seq) as file_cnt,
			   pw.wrt_dt 
		from part_work pw,
			 part_info pi,
			 (select pi_seq,max(version_no) as lastVer 
			  from part_info group by pi_seq) b
		where pw.pi_seq = pi.pi_seq 
		and pi.pi_seq = b.pi_seq
		and pi.version_no = b.lastVer
		and pi.mgt_organ in
		<foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
			 #{item}
		</foreach>
		order by wrt_dt desc
   ) a    	 		
  </select>
  
  <select id="selectPartWorkInfo" resultType="PartWork" parameterType="PartWork">
	    select pw.pw_seq,
	   		   pw.pi_seq ,
	    	   pi.terminal_nm,
			   pw.wrt_nm,
			   pw.work_dt,
			   pw.save_yn,
			   pw.work_info,
			   work_type,
			   pw.description,
			   pw.wrt_nm,
			   pw.wrt_dt 
		from part_work pw,
			 part_info pi,
			 (select pi_seq,max(version_no) as lastVer 
			  from part_info group by pi_seq) b
		where pw.pi_seq = pi.pi_seq 
		and pi.pi_seq = b.pi_seq
		and pi.version_no = b.lastVer
		and pw.pw_seq = #{pwSeq}
		and pw.pi_seq = #{piSeq}		  	 		
  </select>
  
  <select id="selecetVulnOrganList" resultType="com.dbnt.faisp.util.ParamMap" parameterType="Vulnerable">
	     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="selectVulnerableList" resultType="com.dbnt.faisp.util.ParamMap" parameterType="Vulnerable">
	     select  gubun,
			     parent_organ,
			     item_cd,
			     item_value,
			     sum(A) as A,
			     sum(B) as B,
			     sum(C) as C
		 from(
		 	 select gubun,
			        parent_organ,
			        item_cd,
			        item_value,
			        case 
				    	when bv.vuln_type = 'VULNT001' then 1
				    	else 0
				    end as A,
				    case 
				    	when bv.vuln_type = 'VULNT002' then 1
				    	else 0
				    end as B,
				    case 
				    	when bv.vuln_type = 'VULNT003' then 1
				    	else 0
				    end as C
			 from(
				 select (select item_value from code_mgt cm2 where cm2.item_cd = oc.parent_organ) as gubun,
					   oc.parent_organ,
					   item_cd,
				       item_value
				 from code_mgt cm,
				     organ_config oc
				 where cm.item_cd = oc.organ_cd
				 and oc.organ_type not in ('OGC001')
				 and oc.organ_type = 'OGC003'
			) a left outer join board_vuln bv 
			on a.item_cd = bv.mgt_organ
		) a
		group by gubun,parent_organ,item_cd,item_value
		order by parent_organ
  </select>
  
  <select id="selectVulnInfoList" resultType="Vulnerable" parameterType="Vulnerable">
	     select vuln_key,
			    mgt_organ,
			    vuln_nm,
			    (select item_value from code_mgt where item_cd = vuln_type) as vuln_type,
			    wrt_nm,
			    wrt_dt 
		 from board_vuln
		 where mgt_organ = #{mgtOrgan}
		 and mgt_organ in
		 <foreach collection="downOrganCdList" item="item" index="index" separator="," open="(" close=")">
      	 	#{item}
   	      </foreach>
		 order by wrt_dt desc
  </select>
  
  <select id="selectOrganName" resultType="String" parameterType="String">
	     select item_value
		 from code_mgt
		 where item_cd = #{mgtOrgan}
  </select>
  
  <select id="selectVulnInfo" resultType="Vulnerable" parameterType="Vulnerable">
	     select vuln_key,
			    vuln_nm,
			    mgt_organ,
			    (select item_value from code_mgt where item_cd = vuln_type) as vuln_type,
			    description,
			    wrt_nm,
			    wrt_dt 
		 from board_vuln
		 where vuln_key = #{vulnKey}
  </select>
 
</mapper>