<?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>
                <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="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>		    
    
    <update id="update"  parameterType="com.elt.ems.vo.CsVo">
      update t_cs set 
        procStatus=#{procStatus},
        procMemo=#{procMemo},
        <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>     
		
		
    
	<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>	
			
			
	<select id="selectListHistory" 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_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>
				
	</mapper>