<?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.DmsBillsInspectInstanceActivityMapper">

    <resultMap type="DmsBillsInspectInstanceActivity" id="DmsBillsInspectInstanceActivityResult">
        <result property="instanceActivityId"    column="instance_activity_id"    />
        <result property="inspectInstanceId"    column="inspect_instance_id"    />
        <result property="processActivityId"    column="process_activity_id"    />
        <result property="inspectRouteId"    column="inspect_route_id"    />
        <result property="routeName"    column="route_name"    />
        <result property="deviceAmount"    column="device_amount"    />
        <result property="performer"    column="performer"    />
        <result property="processHandleResolution"    column="process_handle_resolution"    />
        <result property="remark"    column="remark"    />
        <result property="processHandleStatus"    column="process_handle_status"    />
        <result property="processStepOrder"    column="process_step_order"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="handleUserId"    column="handle_user_id"    />
        <result property="handleBy"    column="handle_by"    />
        <result property="handleTime"    column="handle_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="transferUserId"    column="transfer_user_id"    />
        <result property="attr1"    column="attr1"    />
    </resultMap>

    <sql id="selectDmsBillsInspectInstanceActivityVo">
        select instance_activity_id, inspect_instance_id, process_activity_id, inspect_route_id, device_amount, performer, process_handle_resolution, remark, process_handle_status, process_step_order, start_time, end_time, handle_user_id, handle_by, handle_time, create_by, create_time, transfer_user_id, attr1 from dms_bills_inspect_instance_activity
    </sql>

    <select id="selectDmsBillsInspectInstanceActivityList" parameterType="DmsBillsInspectInstanceActivity" resultMap="DmsBillsInspectInstanceActivityResult">
        select a.instance_activity_id, a.inspect_instance_id, a.process_activity_id,
        a.inspect_route_id,b.route_name, a.device_amount, a.performer, a.process_handle_resolution, a.remark, a.process_handle_status, a.process_step_order, a.start_time, a.end_time, a.handle_user_id, a.handle_by, a.handle_time, a.create_by, a.create_time, a.transfer_user_id, a.attr1
        from dms_bills_inspect_instance_activity as a
        left join dms_base_inspect_route as b on a.inspect_route_id = b.inspect_route_id
        <where>
            <if test="inspectInstanceId != null "> and inspect_instance_id = #{inspectInstanceId}</if>
            <if test="processActivityId != null "> and process_activity_id = #{processActivityId}</if>
            <if test="inspectRouteId != null "> and inspect_route_id = #{inspectRouteId}</if>
            <if test="deviceAmount != null "> and device_amount = #{deviceAmount}</if>
            <if test="performer != null  and performer != ''"> and performer = #{performer}</if>
            <if test="processHandleResolution != null  and processHandleResolution != ''"> and process_handle_resolution = #{processHandleResolution}</if>
            <if test="processHandleStatus != null  and processHandleStatus != ''"> and process_handle_status = #{processHandleStatus}</if>
            <if test="processStepOrder != null "> and process_step_order = #{processStepOrder}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="handleUserId != null "> and handle_user_id = #{handleUserId}</if>
            <if test="handleBy != null  and handleBy != ''"> and handle_by = #{handleBy}</if>
            <if test="handleTime != null "> and handle_time = #{handleTime}</if>
            <if test="transferUserId != null "> and transfer_user_id = #{transferUserId}</if>
            <if test="attr1 != null  and attr1 != ''"> and attr1 = #{attr1}</if>
        </where>
    </select>

    <select id="selectDmsBillsInspectInstanceActivityByInstanceActivityId" parameterType="Long" resultMap="DmsBillsInspectInstanceActivityResult">
        <include refid="selectDmsBillsInspectInstanceActivityVo"/>
        where instance_activity_id = #{instanceActivityId}
    </select>

    <insert id="insertDmsBillsInspectInstanceActivity" parameterType="DmsBillsInspectInstanceActivity" useGeneratedKeys="true" keyProperty="instanceActivityId">
        insert into dms_bills_inspect_instance_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inspectInstanceId != null">inspect_instance_id,</if>
            <if test="processActivityId != null">process_activity_id,</if>
            <if test="inspectRouteId != null">inspect_route_id,</if>
            <if test="deviceAmount != null">device_amount,</if>
            <if test="performer != null">performer,</if>
            <if test="processHandleResolution != null and processHandleResolution != ''">process_handle_resolution,</if>
            <if test="remark != null">remark,</if>
            <if test="processHandleStatus != null">process_handle_status,</if>
            <if test="processStepOrder != null">process_step_order,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="handleUserId != null">handle_user_id,</if>
            <if test="handleBy != null">handle_by,</if>
            <if test="handleTime != null">handle_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="transferUserId != null">transfer_user_id,</if>
            <if test="attr1 != null">attr1,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inspectInstanceId != null">#{inspectInstanceId},</if>
            <if test="processActivityId != null">#{processActivityId},</if>
            <if test="inspectRouteId != null">#{inspectRouteId},</if>
            <if test="deviceAmount != null">#{deviceAmount},</if>
            <if test="performer != null">#{performer},</if>
            <if test="processHandleResolution != null and processHandleResolution != ''">#{processHandleResolution},</if>
            <if test="remark != null">#{remark},</if>
            <if test="processHandleStatus != null">#{processHandleStatus},</if>
            <if test="processStepOrder != null">#{processStepOrder},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="handleUserId != null">#{handleUserId},</if>
            <if test="handleBy != null">#{handleBy},</if>
            <if test="handleTime != null">#{handleTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="transferUserId != null">#{transferUserId},</if>
            <if test="attr1 != null">#{attr1},</if>
         </trim>
    </insert>

    <update id="updateDmsBillsInspectInstanceActivity" parameterType="DmsBillsInspectInstanceActivity">
        update dms_bills_inspect_instance_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="inspectInstanceId != null">inspect_instance_id = #{inspectInstanceId},</if>
            <if test="processActivityId != null">process_activity_id = #{processActivityId},</if>
            <if test="inspectRouteId != null">inspect_route_id = #{inspectRouteId},</if>
            <if test="deviceAmount != null">device_amount = #{deviceAmount},</if>
            <if test="performer != null">performer = #{performer},</if>
            <if test="processHandleResolution != null and processHandleResolution != ''">process_handle_resolution = #{processHandleResolution},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="processHandleStatus != null">process_handle_status = #{processHandleStatus},</if>
            <if test="processStepOrder != null">process_step_order = #{processStepOrder},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="handleUserId != null">handle_user_id = #{handleUserId},</if>
            <if test="handleBy != null">handle_by = #{handleBy},</if>
            <if test="handleTime != null">handle_time = #{handleTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="transferUserId != null">transfer_user_id = #{transferUserId},</if>
            <if test="attr1 != null">attr1 = #{attr1},</if>
        </trim>
        where instance_activity_id = #{instanceActivityId}
    </update>

    <delete id="deleteDmsBillsInspectInstanceActivityByInstanceActivityId" parameterType="Long">
        delete from dms_bills_inspect_instance_activity where instance_activity_id = #{instanceActivityId}
    </delete>

    <delete id="deleteDmsBillsInspectInstanceActivityByInstanceActivityIds" parameterType="String">
        delete from dms_bills_inspect_instance_activity where instance_activity_id in
        <foreach item="instanceActivityId" collection="array" open="(" separator="," close=")">
            #{instanceActivityId}
        </foreach>
    </delete>
    <!--   先查询出该工单审批到了第几步，然后根据步骤查询拥有权限的对象id集合 -->
    <select id="selectUserIdByDmsBillsInspectInstanceActivityId" parameterType="Long" resultType="java.lang.Long">
        	select auth_target_id from wf_process_activity_auth where process_activity_id =(
					select process_activity_id from wf_process_activity
					where wf_process_id = 105
					and process_activity_order = (1+(select max(process_step_order)
						from dms_bills_inspect_instance_activity
						where inspect_instance_id=#{instanceActivityId})))
    </select>
</mapper>
