<?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">
	 
	<!-- 해당 부분의 namespace는 project package + Mapper Package + Mapper Interface 이름입니다. -->
	<mapper namespace="com.elt.ems.mapper.CsMapper">
	
    <sql id="commonPagingHeader"  >
      SELECT R1.* FROM (
    </sql>
    
    <sql id="commonPagingFooter"  >
      ) R1 LIMIT #{pgtl.startNo}, #{pgtl.listPerPage}
    </sql>   
    	
	<select id="selectListCount" parameterType="com.elt.ems.vo.CsVo"  resultType="int">
		select count(c.seq) as count
		from t_cs c, t_plant p where c.plantSeq = p.seq		
		<if test="csType != null and csType != '-1' ">and c.csType = #{csType} </if>
		<if test="procStatus != null and procStatus != '-1' ">
			<choose>
                <when test="procStatus == 10 or procStatus == 90">
                    and c.procStatus = #{procStatus}
                </when>
                <otherwise>
                    and c.procStatus &gt; 10 and c.procStatus &lt; 90
                </otherwise>
            </choose>
		</if>
		<if test="procType != null and procType != '-1' ">and c.procType = #{procType} </if>
		<if test="plantSeq > 0 ">and p.seq = #{plantSeq} </if>  
		<if test="str1 != null"> and c.inputDate between #{str1} and #{str2} </if>   
	</select>
		
	<!-- 해당 부분의 id는 MapperClass의 함수 이름과 유사하여야 합니다. -->
	<select id="selectList" parameterType="com.elt.ems.vo.CsVo"  resultType="com.elt.ems.vo.CsVo">
      <if test="pgtl != null">
        <include refid="commonPagingHeader" /> 
      </if>
      		
		select c.*, p.name as plantName
		from t_cs c, t_plant p where c.plantSeq = p.seq		
		<if test="csType != null and csType != '-1' ">and c.csType = #{csType} </if>
		<if test="procStatus != null and procStatus != '-1' ">
			<choose>
                <when test="procStatus == 10 or procStatus == 90">
                    and c.procStatus = #{procStatus}
                </when>
                <when test="procStatus == 80">
                    and c.procStatus &lt; 90
                </when>
                <otherwise>
                    and c.procStatus &gt; 10 and c.procStatus &lt; 90
                </otherwise>
            </choose>
		</if>
		<if test="procType != null and procType != '-1' ">and c.procType = #{procType} </if>
		<if test="plantSeq > 0 ">and p.seq = #{plantSeq} </if> 
		<if test="str1 != null"> and c.inputDate between #{str1} and #{str2} </if>     
     		order by c.inputDate desc, c.seq desc   
     		
      <if test="pgtl != null">
        <include refid="commonPagingFooter" />
      </if>
	</select>
	

	<select id="selectListMapCount" parameterType="java.util.HashMap"  resultType="int">
		select count(c.seq) as count
		from t_cs c, t_plant p where c.plantSeq = p.seq		
		<if test="csType != null and csType != '-1' ">and c.csType = #{csType} </if>
		<if test="procStatus != null and procStatus != '-1' ">
			<choose>
                <when test="procStatus == 10 or procStatus == 90">
                    and c.procStatus = #{procStatus}
                </when>
                <otherwise>
                    and c.procStatus &gt; 10 and c.procStatus &lt; 90
                </otherwise>
            </choose>
		</if>
		<if test="procType != null and procType != '-1' ">and c.procType = #{procType} </if>
		<if test="plantSeq > 0 ">and p.seq = #{plantSeq} </if>
		<if test="clientOrderSeq > 0 ">and p.clientOrderSeq = #{clientOrderSeq} </if> 
		<if test="plantType != null and plantType != '-1' ">and p.plantType = #{plantType} </if> 
		<if test="str1 != null"> and c.inputDate between #{str1} and #{str2} </if> 
		<if test="searchMemo != null and searchMemo != ''"> and c.procMemo like concat('%', #{searchMemo}, '%')  </if>  
	</select>
		
	<!-- 해당 부분의 id는 MapperClass의 함수 이름과 유사하여야 합니다. -->
	<select id="selectListMap" parameterType="java.util.HashMap"  resultType="com.elt.ems.vo.CsVo">
      <if test="pgtl != null">
        <include refid="commonPagingHeader" /> 
      </if>
      		
		select c.*, p.name as plantName
		from t_cs c, t_plant p where c.plantSeq = p.seq		
		<if test="csType != null and csType != '-1' ">and c.csType = #{csType} </if>
		<if test="procStatus != null and procStatus != '-1' ">
			<choose>
                <when test="procStatus == 10 or procStatus == 90">
                    and c.procStatus = #{procStatus}
                </when>
                <otherwise>
                    and c.procStatus &gt; 10 and c.procStatus &lt; 90
                </otherwise>
            </choose>
		</if>
		<if test="procType != null and procType != '-1' ">and c.procType = #{procType} </if>
		<if test="plantSeq > 0 ">and p.seq = #{plantSeq} </if> 
		<if test="clientOrderSeq > 0 ">and p.clientOrderSeq = #{clientOrderSeq} </if>
		<if test="plantType != null and plantType != '-1' ">and p.plantType = #{plantType} </if>
		<if test="str1 != null"> and c.inputDate between #{str1} and #{str2} </if> 
		<if test="searchMemo != null and searchMemo != ''"> and c.procMemo like concat('%', #{searchMemo}, '%')  </if>    
     	order by c.inputDate desc, c.seq desc   
     		
      <if test="pgtl != null">
        <include refid="commonPagingFooter" />
      </if>
	</select>

	
    <select id="select" parameterType="int"  resultType="com.elt.ems.vo.CsVo">
		select c.*, p.name as plantName
		from t_cs c, t_plant p where c.plantSeq = p.seq	and c.seq = #{seq}       
    </select>		    
    
    <select id="selectMaxSeq" parameterType="com.elt.ems.vo.CsVo"  resultType="int">
		select max(seq) as seq
		from t_cs c where plantSeq = #{plantSeq} and inputDate = #{inputDate}
    </select>	
        
	
    <select id="selectUpload" parameterType="int"  resultType="com.elt.ems.vo.FileVo">
		select u.*
		from t_cs_upload u where u.seq = #{seq}       
    </select>
    	
    <select id="selectListUpload" parameterType="int"  resultType="com.elt.ems.vo.FileVo">
		select u.*
		from t_cs_upload u where u.csSeq = #{csSeq}       
    </select>
            
    <update id="update"  parameterType="com.elt.ems.vo.CsVo">
      update t_cs set 
        procStatus=#{procStatus},
        <if test="procMemo != null and procMemo != '' ">procMemo=#{procMemo},</if>        
        <if test="procType != null and procType != '' ">procType=#{procType},</if>
        <if test="csType != null and csType != '' ">csType=#{csType},</if>
        updateId=#{updateId},
        updateDatetime=now()
      where seq=#{seq}
    </update>     
		
    <update id="updateUpload"  parameterType="com.elt.ems.vo.FileVo">
      update t_cs_upload set 
        csSeq=#{csSeq},
        fileType=#{fileType},
        fileName=#{fileName}
      where seq=#{seq}
    </update> 	
    
     <update id="deleteUpload"  parameterType="com.elt.ems.vo.FileVo">
      delete from t_cs_upload where seq=#{seq}
    </update> 
    
	<insert id="insert"  parameterType="com.elt.ems.vo.CsVo">
		insert into t_cs (
			plantSeq, pcsIdx, timetableSeq, inputDate, inputHms, csType, procType, procStatus, procMemo,
			pcsFaultStr, batteryFaultStr, updateId, updateDatetime, createId, createDatetime
		) values (
			#{plantSeq}, #{pcsIdx}, #{timetableSeq}, #{inputDate}, #{inputHms}, #{csType}, #{procType}, #{procStatus}, #{procMemo},
			#{pcsFaultStr}, #{batteryFaultStr}, #{updateId}, now(), #{createId}, now()
		)
	</insert>	
			
			
	<insert id="insertUpload"  parameterType="com.elt.ems.vo.FileVo">
		insert into t_cs_upload (
			csSeq, fileType, fileName
		) values (
			#{csSeq}, #{fileType}, #{fileName}
		)
	</insert>
			
			
	<select id="selectUploadListCount" parameterType="com.elt.ems.vo.CsVo"  resultType="int">
		select count(u.seq) as count
		from t_cs c, t_plant p, t_cs_upload u
		where c.plantSeq = p.seq and u.csSeq = c.seq
		<if test="plantSeq > 0 ">and p.seq = #{plantSeq} </if> 
		<if test="str1 != null"> and c.inputDate between #{str1} and #{str2} </if>
        order by c.seq desc		 
	</select>
		
	<!-- 해당 부분의 id는 MapperClass의 함수 이름과 유사하여야 합니다. -->
	<select id="selectUploadList" parameterType="com.elt.ems.vo.CsUploadFileVo"  resultType="com.elt.ems.vo.CsUploadFileVo">
        <if test="pgtl != null">
          <include refid="commonPagingHeader" /> 
        </if>
      		
		select u.*, c.plantSeq, c.pcsIdx, c.procMemo, c.inputDate, c.updateId, c.updateDatetime, c.createId, c.createDatetime, p.name as plantName
		from t_cs c, t_plant p, t_cs_upload u
		where c.plantSeq = p.seq and u.csSeq = c.seq
		<if test="plantSeq > 0 ">and p.seq = #{plantSeq} </if> 
		<if test="str1 != null"> and c.inputDate between #{str1} and #{str2} </if>
        order by c.seq desc		

     		
     	<if test="pgtl != null">
          <include refid="commonPagingFooter" />
        </if>
	</select>
					
	<select id="selectListHistory" parameterType="com.elt.ems.vo.CsUploadFileVo"  resultType="com.elt.ems.vo.CsUploadFileVo">
      <if test="pgtl != null">
        <include refid="commonPagingHeader" /> 
      </if>
      		
		select c.*, p.name as plantName
		from t_cs_history c, t_plant p where c.plantSeq = p.seq		
		<if test="csType != null and csType != '-1' ">and c.csType = #{csType} </if>
		<if test="procStatus != null and procStatus != '-1' ">
			<choose>
                <when test="procStatus == 10 or procStatus == 90">
                    and c.procStatus = #{procStatus}
                </when>
                <otherwise>
                    and c.procStatus &gt; 10 and c.procStatus &lt; 90
                </otherwise>
            </choose>
		</if>
		<if test="csSeq != null and csSeq > 0 ">and c.csSeq = #{csSeq} </if>
		<if test="procType != null and procType != '-1' ">and c.procType = #{procType} </if>
		<if test="plantSeq > 0 ">and p.seq = #{plantSeq} </if> 
		<if test="str1 != null"> and c.inputDate &gt;= #{str1} and c.inputDate &lt;= #{str2} </if>     
     		order by c.seq desc   
     		
     <if test="pgtl != null">
        <include refid="commonPagingFooter" />
      </if>
	</select>
	
	
	<insert id="insertSms"  parameterType="com.elt.ems.vo.SmsVo">
		insert into t_sms ( plantSeq, title, receiver, sender, result, createId, createDatetime) 
		values ( #{plantSeq}, #{title}, #{receiver}, #{sender}, #{result}, #{createId}, now() )
	</insert>	
		
	
    <select id="selectSmsList" parameterType="com.elt.ems.vo.SmsVo"  resultType="com.elt.ems.vo.SmsVo">
      <if test="pgtl != null">
        <include refid="commonPagingHeader" /> 
      </if>
          
		select s.*
		from t_sms s
		where s.plantSeq = #{plantSeq}  
		order by seq desc
		
     <if test="pgtl != null">
        <include refid="commonPagingFooter" />
      </if>		     
    </select>
    
  
    <select id="selectListByPlant" parameterType="com.elt.ems.common.CaseSensibleHashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap">
		select c.*, p.name as plantName, count(c.seq) cnt
		from t_cs c, t_plant p where c.plantSeq = p.seq		
		and p.seq = #{plantSeq} 		
		and c.inputDate between #{startDate} and #{endDate}     
     	group by c.inputDate     		 
    </select>  
	
    <select id="selectListCountByDate" parameterType="com.elt.ems.common.CaseSensibleHashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap">
		select inputDate, count(distinct(plantSeq)) as plantCnt, count(distinct(plantSeq+":"+pcsIdx)) as pcsCnt, count(seq) as cnt
		from t_cs where inputDate between #{startDate} and #{endDate} 	
		<if test="plantSeq > 0 ">and plantSeq = #{plantSeq} </if> 		
		group by inputDate  
    </select>
        
    <select id="selectListCountByCsType" parameterType="com.elt.ems.common.CaseSensibleHashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap">
		select  csType, count(seq) as cnt from t_cs
		where inputDate between #{startDate} and #{endDate} 	
		<if test="plantSeq > 0 ">and plantSeq = #{plantSeq} </if> 		
		group by csType 
    </select>        	
    		
    <select id="selectListCountByProcType" parameterType="com.elt.ems.common.CaseSensibleHashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap">
		select  procType, count(seq) as cnt from t_cs
		where inputDate between #{startDate} and #{endDate} 	
		<if test="plantSeq > 0 ">and plantSeq = #{plantSeq} </if> 		
		group by procType
    </select>      				
	</mapper>