<?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.steerinfo.ems.tpmcase.mapper.TpmCaseMapper">
    <resultMap id="TpmCaseMap" type="com.steerinfo.ems.tpmcase.model.TPmCase">
        <id column="CASEID" jdbcType="VARCHAR" property="caseId"/>
        <result column="CASEDESC" jdbcType="VARCHAR" property="caseDesc"/>
        <result column="ISAP" jdbcType="VARCHAR" property="isAp"/>
        <result column="SEQNO" jdbcType="DOUBLE" property="seqNo"/>
        <result column="CASECONFIG" jdbcType="VARCHAR" property="caseConfig"/>
    </resultMap>

    <!--增加-->
    <insert id="insert" parameterType="com.steerinfo.ems.tpmcase.model.TPmCase">
        INSERT  INTO T_PM_CASE(
        CASEID,CASEDESC,ISAP,SEQNO,CASECONFIG
        )
        VALUES (#{caseId,jdbcType=VARCHAR},#{caseDesc,jdbcType=VARCHAR},#{isAp,jdbcType=VARCHAR},#{seqNo,jdbcType=DOUBLE},#{caseConfig,jdbcType=VARCHAR})
    </insert>

    <!--根据caseId来删除-->
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        DELETE FROM T_PM_CASE
        WHERE CASEID = #{caseId,jdbcType=VARCHAR}
    </delete>
    <!--根据caseId查找-->
    <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="TpmCaseMap">
    select CASEID, CASEDESC, ISAP, SEQNO, CASECONFIG,CRMAN, CRTIME, UPMAN, UPTIME
    from T_PM_CASE
    where CASEID = #{caseId,jdbcType=VARCHAR}
  </select>

    <update id="updateByPrimaryKeySelective" parameterType="com.steerinfo.ems.tpmcase.model.TPmCase">
        UPDATE T_PM_CASE
        <set>
            <if test="caseDesc!=null">
                CASEDESC = #{caseDesc,jdbcType=VARCHAR},
            </if>
            <if test="isAp!=null">
                ISAP = #{isAp,jdbcType=VARCHAR},
            </if>
            <if test="seqNo!=null">
                SEQNO = #{seqNo,jdbcType=DOUBLE},
            </if>
            <if test="caseConfig!=null">
                CASECONFIG = #{caseConfig,jdbcType=VARCHAR},
            </if>
            <if test="crman != null">
                CRMAN = #{crman,jdbcType=VARCHAR},
            </if>
            <if test="crtime != null">
                CRTIME = #{crtime,jdbcType=TIMESTAMP},
            </if>
            <if test="upman != null">
                UPMAN = #{upman,jdbcType=VARCHAR},
            </if>
            <if test="uptime != null">
                UPTIME = #{uptime,jdbcType=TIMESTAMP},
            </if>
        </set>
        WHERE CASEID = #{caseId,jdbcType=VARCHAR}
    </update>

    <!--高级模糊查-->
    <select id="selectLikeByParameters" parameterType="java.util.HashMap" resultMap="TpmCaseMap">
     SELECT CASEID,CASEDESC,ISAP,SEQNO,CASECONFIG,CRMAN, CRTIME, UPMAN, UPTIME
     FROM T_PM_CASE
     <where>
         <if test="caseId != null and caseId != ''">
             AND CASEID LIKE '%${caseId}%'
         </if>
         <if test="caseDesc!=null and  caseDesc!= ''">
             AND CASEDESC LIKE '%${caseDesc}%'
         </if>
         <if test="isAp!=null and isAp!='' ">
             AND ISAP LIKE '%${isAp}%'
         </if>
         <if test="seqNo!=null and seqNo !='' ">
            AND SEQNO LIKE ${seqNo}
         </if>
         <if test="caseConfig!=null and caseConfig!= ''">
            AND CASECONFIG LIKE '%${caseConfig}%'
         </if>
     </where>
    </select>

    <select id="getIdAndName" resultMap="TpmCaseMap">
        SELECT CASEDESC,CASEID FROM T_PM_CASE
    </select>

    <!-- 获取默认计划方案 -->
    <select id="getTPmCaseDef" resultMap="TpmCaseMap">
        select CASEID,CASEDESC,ISAP,SEQNO,CASECONFIG from
        (select CASEID,CASEDESC,ISAP,SEQNO,CASECONFIG,row_number() over (order by SEQNO) rn from T_PM_CASE)
        where rn = 1
    </select>
    
    <!-- 获取默认计划方案 -->
    <select id="getTPmCaseByItemId" resultMap="TpmCaseMap">
        select CASEID,CASEDESC,ISAP,SEQNO,CASECONFIG from T_PM_CASE
        where caseid = (select caseid from T_PM_CASE_ACT where timegranid=#{timegranid} and itemid =#{itemid})
    </select>
</mapper>