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

    <resultMap type="DmsBillsFaultInstance" id="DmsBillsFaultInstanceResult">
        <result property="repairInstanceId"    column="repair_instance_id"    />
        <result property="faultSourceType"    column="fault_source_type"    />
        <result property="faultSourceId"    column="fault_source_id"    />
        <result property="wfProcessId"    column="wf_process_id"    />
        <result property="billsFaultCode"    column="bills_fault_code"    />
        <result property="billsStatus"    column="bills_status"    />
        <result property="applyUser"    column="apply_user"    />
        <result property="applyTime"    column="apply_time"    />
        <result property="realBeginTime"    column="real_begin_time"    />
        <result property="realEndTime"    column="real_end_time"    />
        <result property="requireEndTime"    column="require_end_time"    />
        <result property="instanceType"    column="instance_type"    />
        <result property="isFlag"    column="is_flag"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deviceId"    column="device_id"    />

        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceLocation"    column="device_location"    />
        <result property="deviceSpec"    column="device_spec"    />
        <result property="instanceActivityId"    column="instance_activity_id"    />
        <result property="faultType"    column="fault_type"    />
        <result property="faultDescription"    column="fault_description"    />
        <result property="designOperations"    column="design_operations"    />
        <result property="repairType"    column="repair_type"    />
        <result property="outsrcId"    column="outsrc_id"    />
        <result property="processHandleResolution"    column="process_handle_resolution"    />

    </resultMap>


    <resultMap id="DmsBillsFaultInstanceDmsFaultInstanceActivityResult" type="DmsBillsFaultInstance" extends="DmsBillsFaultInstanceResult">
        <collection property="dmsFaultInstanceActivityList" notNullColumn="sub_instance_activity_id" javaType="java.util.List" resultMap="DmsFaultInstanceActivityResult" />
    </resultMap>

    <resultMap type="DmsFaultInstanceActivity" id="DmsFaultInstanceActivityResult">
        <result property="instanceActivityId"    column="sub_instance_activity_id"    />
        <result property="repairInstanceId"    column="sub_repair_instance_id"    />
        <result property="processActivityId"    column="sub_process_activity_id"    />
        <result property="faultType"    column="sub_fault_type"    />
        <result property="faultDescription"    column="sub_fault_description"    />
        <result property="designOperations"    column="sub_design_operations"    />
        <result property="deviceId"    column="sub_device_id"    />
        <result property="repairType"    column="sub_repair_type"    />
        <result property="outsrcId"    column="sub_outsrc_id"    />
        <result property="processHandleResolution"    column="sub_process_handle_resolution"    />
        <result property="remark"    column="sub_remark"    />
        <result property="processHandleStatus"    column="sub_process_handle_status"    />
        <result property="processStepOrder"    column="sub_process_step_order"    />
        <result property="startTime"    column="sub_start_time"    />
        <result property="endTime"    column="sub_end_time"    />
        <result property="handleUserId"    column="sub_handle_user_id"    />
        <result property="handleBy"    column="sub_handle_by"    />
        <result property="handleTime"    column="sub_handle_time"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="transferUserId"    column="sub_transfer_user_id"    />
        <result property="attr1"    column="sub_attr1"    />
    </resultMap>


    <sql id="selectDmsBillsFaultInstanceVo">
        select repair_instance_id, fault_source_type, fault_source_id, wf_process_id, bills_fault_code, bills_status, apply_user, apply_time, real_begin_time, real_end_time, require_end_time, instance_type, is_flag, remark, create_by, create_time, update_by, update_time,device_id from dms_bills_fault_instance
    </sql>

    <select id="selectDmsBillsFaultInstanceList" parameterType="DmsBillsFaultInstance" resultMap="DmsBillsFaultInstanceResult">
        <include refid="selectDmsBillsFaultInstanceVo"/>
        <where>
            <if test="faultSourceType != null  and faultSourceType != ''"> and fault_source_type = #{faultSourceType}</if>
            <if test="faultSourceId != null "> and fault_source_id = #{faultSourceId}</if>
            <if test="repairInstanceId != null "> and repair_instance_id = #{repairInstanceId}</if>
            <if test="wfProcessId != null "> and wf_process_id = #{wfProcessId}</if>
            <if test="billsFaultCode != null  and billsFaultCode != ''"> and bills_fault_code = #{billsFaultCode}</if>
            <if test="billsStatus != null  and billsStatus != ''"> and bills_status = #{billsStatus}</if>
            <if test="applyUser != null  and applyUser != ''"> and apply_user = #{applyUser}</if>
            <if test="applyTime != null "> and apply_time = #{applyTime}</if>
            <if test="realBeginTime != null "> and real_begin_time = #{realBeginTime}</if>
            <if test="realEndTime != null "> and real_end_time = #{realEndTime}</if>
            <if test="requireEndTime != null "> and require_end_time = #{requireEndTime}</if>
            <if test="instanceType != null  and instanceType != ''"> and instance_type = #{instanceType}</if>
            <if test="isFlag != null  and isFlag != ''"> and is_flag = #{isFlag}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
        </where>
    </select>

    <select id="selectDmsBillsFaultInstanceByRepairInstanceId" parameterType="Long" resultMap="DmsBillsFaultInstanceResult">
        <include refid="selectDmsBillsFaultInstanceVo"/>
        where repair_instance_id = #{repairInstanceId}
    </select>

    <insert id="insertDmsBillsFaultInstance" parameterType="DmsBillsFaultInstance" useGeneratedKeys="true" keyProperty="repairInstanceId">
        insert into dms_bills_fault_instance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="faultSourceType != null">fault_source_type,</if>
            <if test="faultSourceId != null">fault_source_id,</if>
            <if test="wfProcessId != null">wf_process_id,</if>
            <if test="billsFaultCode != null and billsFaultCode != ''">bills_fault_code,</if>
            <if test="billsStatus != null and billsStatus != ''">bills_status,</if>
            <if test="applyUser != null">apply_user,</if>
            <if test="applyTime != null">apply_time,</if>
            <if test="realBeginTime != null">real_begin_time,</if>
            <if test="realEndTime != null">real_end_time,</if>
            <if test="requireEndTime != null">require_end_time,</if>
            <if test="instanceType != null and instanceType != ''">instance_type,</if>
            <if test="isFlag != null and isFlag != ''">is_flag,</if>
            <if test="remark != null">remark,</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>
            <if test="deviceId != null">device_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="faultSourceType != null">#{faultSourceType},</if>
            <if test="faultSourceId != null">#{faultSourceId},</if>
            <if test="wfProcessId != null">#{wfProcessId},</if>
            <if test="billsFaultCode != null and billsFaultCode != ''">#{billsFaultCode},</if>
            <if test="billsStatus != null and billsStatus != ''">#{billsStatus},</if>
            <if test="applyUser != null">#{applyUser},</if>
            <if test="applyTime != null">#{applyTime},</if>
            <if test="realBeginTime != null">#{realBeginTime},</if>
            <if test="realEndTime != null">#{realEndTime},</if>
            <if test="requireEndTime != null">#{requireEndTime},</if>
            <if test="instanceType != null and instanceType != ''">#{instanceType},</if>
            <if test="isFlag != null and isFlag != ''">#{isFlag},</if>
            <if test="remark != null">#{remark},</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>
            <if test="deviceId != null">#{deviceId},</if>
         </trim>
    </insert>

    <update id="updateDmsBillsFaultInstance" parameterType="DmsBillsFaultInstance">
        update dms_bills_fault_instance
        <trim prefix="SET" suffixOverrides=",">
            <if test="faultSourceType != null">fault_source_type = #{faultSourceType},</if>
            <if test="faultSourceId != null">fault_source_id = #{faultSourceId},</if>
            <if test="wfProcessId != null">wf_process_id = #{wfProcessId},</if>
            <if test="billsFaultCode != null and billsFaultCode != ''">bills_fault_code = #{billsFaultCode},</if>
            <if test="billsStatus != null and billsStatus != ''">bills_status = #{billsStatus},</if>
            <if test="applyUser != null">apply_user = #{applyUser},</if>
            <if test="applyTime != null">apply_time = #{applyTime},</if>
            <if test="realBeginTime != null">real_begin_time = #{realBeginTime},</if>
            <if test="realEndTime != null">real_end_time = #{realEndTime},</if>
            <if test="requireEndTime != null">require_end_time = #{requireEndTime},</if>
            <if test="instanceType != null and instanceType != ''">instance_type = #{instanceType},</if>
            <if test="isFlag != null and isFlag != ''">is_flag = #{isFlag},</if>
            <if test="remark != null">remark = #{remark},</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>
            <if test="deviceId != null">device_id = #{deviceId},</if>
        </trim>
        where repair_instance_id = #{repairInstanceId}
    </update>

    <delete id="deleteDmsBillsFaultInstanceByRepairInstanceId" parameterType="Long">
        delete from dms_bills_fault_instance where repair_instance_id = #{repairInstanceId}
    </delete>

    <delete id="deleteDmsBillsFaultInstanceByRepairInstanceIds" parameterType="String">
        delete from dms_bills_fault_instance where repair_instance_id in
        <foreach item="repairInstanceId" collection="array" open="(" separator="," close=")">
            #{repairInstanceId}
        </foreach>
    </delete>
    <select id="selectProcessActivityId" parameterType="Long" resultType="Long">
        select min(process_activity_id) from wf_process_activity where wf_process_id = #{wfProcessId}
    </select>







    <sql id="selectDmsBillsFaultInstanceJoinVo">
        select dbfi.repair_instance_id, dbfi.fault_source_type, dbfi.fault_source_id, dbfi.wf_process_id, dbfi.bills_fault_code, dbfi.bills_status,
               dbfi.apply_user, dbfi.apply_time, dbfi.real_begin_time, dbfi.real_end_time, dbfi.require_end_time, dbfi.device_id,
               dbdl.device_code,dbdl.device_name,dbdl.device_location,dbdl.device_spec
        from dms_bills_fault_instance dbfi left join dms_base_device_ledger dbdl on dbfi.device_id=dbdl.device_id
    </sql>

    <select id="selectDmsBillsFaultInstanceJoinList" parameterType="DmsBillsFaultInstance" resultMap="DmsBillsFaultInstanceResult">
        <include refid="selectDmsBillsFaultInstanceJoinVo"/>
        <where>
            <if test="faultSourceType != null  and faultSourceType != ''"> and fault_source_type = #{faultSourceType}</if>
            <if test="faultSourceId != null "> and fault_source_id = #{faultSourceId}</if>
            <if test="repairInstanceId != null "> and repair_instance_id = #{repairInstanceId}</if>
            <if test="wfProcessId != null "> and wf_process_id = #{wfProcessId}</if>
            <if test="billsFaultCode != null  and billsFaultCode != ''"> and bills_fault_code = #{billsFaultCode}</if>
            <if test="billsStatus != null  and billsStatus != ''"> and bills_status = #{billsStatus}</if>
            <if test="applyUser != null  and applyUser != ''"> and apply_user = #{applyUser}</if>
            <if test="applyTime != null "> and apply_time = #{applyTime}</if>
            <if test="instanceType != null  and instanceType != ''"> and instance_type = #{instanceType}</if>
            <if test="isFlag != null  and isFlag != ''"> and is_flag = #{isFlag}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
        </where>
    </select>




    <select id="selectFaultInstanceJoinFirstAndDeviceById" parameterType="Long" resultMap="DmsBillsFaultInstanceResult">
        select dbfi.repair_instance_id, dbfi.fault_source_type, dbfi.fault_source_id, dbfi.wf_process_id, dbfi.bills_fault_code, dbfi.bills_status,
        dbfi.apply_user, dbfi.apply_time, dbfi.real_begin_time, dbfi.real_end_time, dbfi.require_end_time, dbfi.device_id,
        dbdl.device_code,dbdl.device_name,dbdl.device_location,dbdl.device_spec,
        dfia.instance_activity_id,dfia.fault_type,dfia.fault_description,dfia.design_operations,dfia.repair_type,dfia.outsrc_id,dfia.process_handle_resolution
        from dms_bills_fault_instance dbfi left join dms_base_device_ledger dbdl on dbfi.device_id=dbdl.device_id
            left join dms_fault_instance_activity dfia on dbfi.repair_instance_id=dfia.repair_instance_id
        where dbfi.repair_instance_id = #{repairInstanceId} and dfia.process_step_order=1
    </select>


    <select id="selectFaultInstanceJoinActivityByRepairInstanceId" parameterType="Long" resultMap="DmsBillsFaultInstanceDmsFaultInstanceActivityResult">
        select a.repair_instance_id, a.fault_source_type, a.fault_source_id, a.wf_process_id, a.bills_fault_code, a.bills_status, a.apply_user, a.apply_time, a.real_begin_time, a.real_end_time, a.require_end_time, a.instance_type, a.is_flag, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,dbdl.device_code,dbdl.device_name,dbdl.device_location,dbdl.device_spec,
               b.instance_activity_id as sub_instance_activity_id, b.repair_instance_id as sub_repair_instance_id, b.process_activity_id as sub_process_activity_id, b.fault_type as sub_fault_type, b.fault_description as sub_fault_description, b.design_operations as sub_design_operations, b.device_id as sub_device_id, b.repair_type as sub_repair_type, b.outsrc_id as sub_outsrc_id, b.process_handle_resolution as sub_process_handle_resolution, b.remark as sub_remark, b.process_handle_status as sub_process_handle_status, b.process_step_order as sub_process_step_order, b.start_time as sub_start_time, b.end_time as sub_end_time, b.handle_user_id as sub_handle_user_id, b.handle_by as sub_handle_by, b.handle_time as sub_handle_time
        from dms_bills_fault_instance a
                 left join dms_base_device_ledger dbdl on a.device_id=dbdl.device_id
                 left join dms_fault_instance_activity b on b.repair_instance_id = a.repair_instance_id
        where a.repair_instance_id = #{repairInstanceId} order by b.process_step_order
    </select>



    <select id="selectCountByRepairInstanceIdAndDeviceCode" resultType="Long">
        select count(1) from dms_bills_fault_instance dbfi where dbfi.repair_instance_id=#{repairInstanceId}
                                                      and exists (select 1 from dms_base_device_ledger dbdl where dbfi.device_id=dbdl.device_id and dbdl.device_code=#{deviceCode})
    </select>



    <select id="selectFaultInstanceJoinFirstById" parameterType="Long" resultMap="DmsBillsFaultInstanceResult">
        select dbfi.repair_instance_id, dbfi.fault_source_type, dbfi.fault_source_id, dbfi.wf_process_id, dbfi.bills_fault_code, dbfi.bills_status,
               dbfi.apply_user, dbfi.apply_time, dbfi.real_begin_time, dbfi.real_end_time, dbfi.require_end_time, dbfi.device_id,
               dfia.instance_activity_id,dfia.fault_type,dfia.fault_description,dfia.design_operations,dfia.repair_type,dfia.outsrc_id,dfia.process_handle_resolution
        from dms_bills_fault_instance dbfi
                                           left join dms_fault_instance_activity dfia on dbfi.repair_instance_id=dfia.repair_instance_id
        where dbfi.repair_instance_id = #{repairInstanceId} and dfia.process_step_order=1
    </select>


</mapper>
