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

    <resultMap type="DmsBillsLubeInstanceActivity" id="DmsBillsLubeInstanceActivityResult">
        <result property="instanceActivityId"    column="instance_activity_id"    />
        <result property="lubeInstanceId"    column="lube_instance_id"    />
        <result property="processActivityId"    column="process_activity_id"    />
        <result property="lubeGroup"    column="lube_group"    />
        <result property="lubeSupervisor"    column="lube_supervisor"    />
        <result property="lubeLevel"    column="lube_level"    />
        <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="selectDmsBillsLubeInstanceActivityVo">
        select instance_activity_id, lube_instance_id, process_activity_id, lube_group, lube_supervisor, lube_level, 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_lube_instance_activity
    </sql>

    <select id="selectDmsBillsLubeInstanceActivityList" parameterType="DmsBillsLubeInstanceActivity" resultMap="DmsBillsLubeInstanceActivityResult">
        <include refid="selectDmsBillsLubeInstanceActivityVo"/>
        <where>
            <if test="lubeInstanceId != null "> and lube_instance_id = #{lubeInstanceId}</if>
            <if test="processActivityId != null "> and process_activity_id = #{processActivityId}</if>
            <if test="lubeGroup != null  and lubeGroup != ''"> and lube_group = #{lubeGroup}</if>
            <if test="lubeSupervisor != null  and lubeSupervisor != ''"> and lube_supervisor = #{lubeSupervisor}</if>
            <if test="lubeLevel != null  and lubeLevel != ''"> and lube_level = #{lubeLevel}</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="selectDmsBillsLubeInstanceActivityByInstanceActivityId" parameterType="Long" resultMap="DmsBillsLubeInstanceActivityResult">
        <include refid="selectDmsBillsLubeInstanceActivityVo"/>
        where instance_activity_id = #{instanceActivityId}
    </select>

    <insert id="insertDmsBillsLubeInstanceActivity" parameterType="DmsBillsLubeInstanceActivity" useGeneratedKeys="true" keyProperty="instanceActivityId">
        insert into dms_bills_lube_instance_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lubeInstanceId != null">lube_instance_id,</if>
            <if test="processActivityId != null">process_activity_id,</if>
            <if test="lubeGroup != null">lube_group,</if>
            <if test="lubeSupervisor != null">lube_supervisor,</if>
            <if test="lubeLevel != null">lube_level,</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="lubeInstanceId != null">#{lubeInstanceId},</if>
            <if test="processActivityId != null">#{processActivityId},</if>
            <if test="lubeGroup != null">#{lubeGroup},</if>
            <if test="lubeSupervisor != null">#{lubeSupervisor},</if>
            <if test="lubeLevel != null">#{lubeLevel},</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="updateDmsBillsLubeInstanceActivity" parameterType="DmsBillsLubeInstanceActivity">
        update dms_bills_lube_instance_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="lubeInstanceId != null">lube_instance_id = #{lubeInstanceId},</if>
            <if test="processActivityId != null">process_activity_id = #{processActivityId},</if>
            <if test="lubeGroup != null">lube_group = #{lubeGroup},</if>
            <if test="lubeSupervisor != null">lube_supervisor = #{lubeSupervisor},</if>
            <if test="lubeLevel != null">lube_level = #{lubeLevel},</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="deleteDmsBillsLubeInstanceActivityByInstanceActivityId" parameterType="Long">
        delete from dms_bills_lube_instance_activity where instance_activity_id = #{instanceActivityId}
    </delete>

    <delete id="deleteDmsBillsLubeInstanceActivityByInstanceActivityIds" parameterType="String">
        delete from dms_bills_lube_instance_activity where instance_activity_id in
        <foreach item="instanceActivityId" collection="array" open="(" separator="," close=")">
            #{instanceActivityId}
        </foreach>
    </delete>

    <!--   先查询出该工单审批到了第几步，然后根据步骤查询拥有权限的对象id集合 -->
    <select id="selectUserIdByDmsBillsLubeInstanceActivityId" 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 = 106
					and process_activity_order = (1+(select max(process_step_order)
						from dms_bills_lube_instance_activity
						where lube_instance_id=#{lubeInstanceId})))
    </select>
</mapper>
