<?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.hw.dms.mapper.DmsInspectInstanceDetailProjectMapper">

    <resultMap type="DmsInspectInstanceDetailProject" id="DmsInspectInstanceDetailProjectResult">
        <result property="instanceDetailProjectId"    column="instance_detail_project_id"    />
        <result property="instanceDetailId"    column="instance_detail_id"    />
        <result property="inspectProjectId"    column="inspect_project_id"    />
        <result property="inspectProjectStatus"    column="inspect_project_status"    />
        <result property="inspectProjectResult"    column="inspect_project_result"    />
        <result property="projectStepOrder"    column="project_step_order"    />
        <result property="defValue"    column="def_value"    />
        <result property="inspectProjectName"    column="inspect_project_name"    />
        <result property="inspectProjectProperty"    column="inspect_project_property"    />
        <result property="upLimit"    column="up_limit"    />
        <result property="lowLimit"    column="low_limit"    />
        <result property="inspectProjectDesc"    column="inspect_project_desc"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDmsInspectInstanceDetailProjectVo">
        select instance_detail_project_id, instance_detail_id, inspect_project_id, inspect_project_status, inspect_project_result, project_step_order, def_value, inspect_project_name, inspect_project_property, up_limit, low_limit, create_by, create_time, update_by, update_time from dms_inspect_instance_detail_project
    </sql>

    <select id="selectDmsInspectInstanceDetailProjectList" parameterType="DmsInspectInstanceDetailProject" resultMap="DmsInspectInstanceDetailProjectResult">
        <include refid="selectDmsInspectInstanceDetailProjectVo"/>
        <where>
            <if test="instanceDetailId != null "> and instance_detail_id = #{instanceDetailId}</if>
            <if test="inspectProjectId != null "> and inspect_project_id = #{inspectProjectId}</if>
            <if test="inspectProjectStatus != null  and inspectProjectStatus != ''"> and inspect_project_status = #{inspectProjectStatus}</if>
            <if test="inspectProjectResult != null "> and inspect_project_result = #{inspectProjectResult}</if>
            <if test="projectStepOrder != null "> and project_step_order = #{projectStepOrder}</if>
            <if test="defValue != null "> and def_value = #{defValue}</if>
            <if test="inspectProjectName != null  and inspectProjectName != ''"> and inspect_project_name like concat('%', #{inspectProjectName}, '%')</if>
            <if test="inspectProjectProperty != null  and inspectProjectProperty != ''"> and inspect_project_property = #{inspectProjectProperty}</if>
            <if test="upLimit != null "> and up_limit = #{upLimit}</if>
            <if test="lowLimit != null "> and low_limit = #{lowLimit}</if>
        </where>
    </select>

    <select id="selectDmsInspectInstanceDetailProjectByInstanceDetailProjectId" parameterType="Long" resultMap="DmsInspectInstanceDetailProjectResult">
        <include refid="selectDmsInspectInstanceDetailProjectVo"/>
        where instance_detail_project_id = #{instanceDetailProjectId}
    </select>

    <insert id="insertDmsInspectInstanceDetailProject" parameterType="DmsInspectInstanceDetailProject" useGeneratedKeys="true" keyProperty="instanceDetailProjectId">
        insert into dms_inspect_instance_detail_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="instanceDetailId != null">instance_detail_id,</if>
            <if test="inspectProjectId != null">inspect_project_id,</if>
            <if test="inspectProjectStatus != null">inspect_project_status,</if>
            <if test="inspectProjectResult != null">inspect_project_result,</if>
            <if test="projectStepOrder != null">project_step_order,</if>
            <if test="defValue != null">def_value,</if>
            <if test="inspectProjectName != null">inspect_project_name,</if>
            <if test="inspectProjectProperty != null">inspect_project_property,</if>
            <if test="upLimit != null">up_limit,</if>
            <if test="lowLimit != null">low_limit,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="instanceDetailId != null">#{instanceDetailId},</if>
            <if test="inspectProjectId != null">#{inspectProjectId},</if>
            <if test="inspectProjectStatus != null">#{inspectProjectStatus},</if>
            <if test="inspectProjectResult != null">#{inspectProjectResult},</if>
            <if test="projectStepOrder != null">#{projectStepOrder},</if>
            <if test="defValue != null">#{defValue},</if>
            <if test="inspectProjectName != null">#{inspectProjectName},</if>
            <if test="inspectProjectProperty != null">#{inspectProjectProperty},</if>
            <if test="upLimit != null">#{upLimit},</if>
            <if test="lowLimit != null">#{lowLimit},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDmsInspectInstanceDetailProject" parameterType="DmsInspectInstanceDetailProject">
        update dms_inspect_instance_detail_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="instanceDetailId != null">instance_detail_id = #{instanceDetailId},</if>
            <if test="inspectProjectId != null">inspect_project_id = #{inspectProjectId},</if>
            <if test="inspectProjectStatus != null">inspect_project_status = #{inspectProjectStatus},</if>
            <if test="inspectProjectResult != null">inspect_project_result = #{inspectProjectResult},</if>
            <if test="projectStepOrder != null">project_step_order = #{projectStepOrder},</if>
            <if test="defValue != null">def_value = #{defValue},</if>
            <if test="inspectProjectName != null">inspect_project_name = #{inspectProjectName},</if>
            <if test="inspectProjectProperty != null">inspect_project_property = #{inspectProjectProperty},</if>
            <if test="upLimit != null">up_limit = #{upLimit},</if>
            <if test="lowLimit != null">low_limit = #{lowLimit},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where instance_detail_project_id = #{instanceDetailProjectId}
    </update>

    <delete id="deleteDmsInspectInstanceDetailProjectByInstanceDetailProjectId" parameterType="Long">
        delete from dms_inspect_instance_detail_project where instance_detail_project_id = #{instanceDetailProjectId}
    </delete>

    <delete id="deleteDmsInspectInstanceDetailProjectByInstanceDetailProjectIds" parameterType="String">
        delete from dms_inspect_instance_detail_project where instance_detail_project_id in
        <foreach item="instanceDetailProjectId" collection="array" open="(" separator="," close=")">
            #{instanceDetailProjectId}
        </foreach>
    </delete>





    <sql id="selectDmsInspectInstanceDetailProjectJoinVo">
        select diidp.instance_detail_project_id, diidp.instance_detail_id, diidp.inspect_project_id, diidp.inspect_project_status,
               diidp.inspect_project_result, diidp.inspect_project_desc,
               dbip.inspect_project as inspect_project_name,dbip.inspect_project_property, dbip.up_limit, dbip.low_limit,dbip.def_value
        from  dms_inspect_instance_detail_project diidp right join dms_base_inspect_project dbip  on dbip.inspect_project_id=diidp.inspect_project_id
    </sql>

    <select id="selectDmsInspectInstanceDetailProjectJoinList" parameterType="DmsInspectInstanceDetailProject" resultMap="DmsInspectInstanceDetailProjectResult">
        <include refid="selectDmsInspectInstanceDetailProjectJoinVo"/>
        <where>
            <if test="deviceId != null "> and exists (select 1 from dms_inspect_project_device dipd where dipd.target_type=1
             and dipd.target_id = (select device_type_id from dms_base_device_ledger where device_id=#{deviceId})
             and dipd.inspect_project_id = dbip.inspect_project_id)</if>
            <if test="instanceDetailId != null "> and instance_detail_id = #{instanceDetailId}</if>
            <if test="inspectProjectId != null "> and inspect_project_id = #{inspectProjectId}</if>
            <if test="inspectProjectStatus != null  and inspectProjectStatus != ''"> and inspect_project_status = #{inspectProjectStatus}</if>
            <if test="inspectProjectResult != null "> and inspect_project_result = #{inspectProjectResult}</if>
            <if test="projectStepOrder != null "> and project_step_order = #{projectStepOrder}</if>
            <if test="defValue != null "> and def_value = #{defValue}</if>
            <if test="inspectProjectName != null  and inspectProjectName != ''"> and inspect_project_name like concat('%', #{inspectProjectName}, '%')</if>
            <if test="inspectProjectProperty != null  and inspectProjectProperty != ''"> and inspect_project_property = #{inspectProjectProperty}</if>
            <if test="upLimit != null "> and up_limit = #{upLimit}</if>
            <if test="lowLimit != null "> and low_limit = #{lowLimit}</if>
        </where>
    </select>

    <select id="selectDmsInspectInstanceDetailProjectByInspectInstanceId" parameterType="Long" resultMap="DmsInspectInstanceDetailProjectResult">
        select instance_detail_project_id, instance_detail_id, inspect_project_id, inspect_project_status, inspect_project_result, project_step_order, def_value, inspect_project_name, inspect_project_property, up_limit, low_limit, create_by, create_time, update_by, update_time from dms_inspect_instance_detail_project
     where instance_detail_id  =(
select instance_detail_id from dms_inspect_instance_detail where inspect_instance_id = #{inspectInstanceId} and line_step = #{lineStep})
    </select>

</mapper>
