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

    <resultMap type="DmsPlanRepair" id="DmsPlanRepairResult">
        <result property="planRepairId"    column="plan_repair_id"    />
        <result property="planRepairCode"    column="plan_repair_code"    />
        <result property="repairType"    column="repair_type"    />
        <result property="repairGroup"    column="repair_group"    />
        <result property="repairSupervisor"    column="repair_supervisor"    />
        <result property="repairTime"    column="repair_time"    />
        <result property="timeLimit"    column="time_limit"    />
<!--        <result property="repairEndTime"    column="repair_end_time"    />-->
        <result property="cyclePeriod"    column="cycle_period"    />
<!--        <result property="repairStatus"    column="repair_status"    />-->
        <result property="createMethod"    column="create_method"    />
        <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"    />
    </resultMap>

    <resultMap id="DmsPlanRepairDmsPlanRepairDetailResult" type="DmsPlanRepair" extends="DmsPlanRepairResult">
        <collection property="dmsPlanRepairDetailList" notNullColumn="sub_plan_repair_detail_id" javaType="java.util.List" resultMap="DmsPlanRepairDetailResult" />
    </resultMap>

    <resultMap type="DmsPlanRepairDetail" id="DmsPlanRepairDetailResult">
        <result property="planRepairDetailId"    column="sub_plan_repair_detail_id"    />
        <result property="planRepairId"    column="sub_plan_repair_id"    />
        <result property="deviceId"    column="sub_device_id"    />
        <result property="faultType"    column="sub_fault_type"    />
        <result property="repairDesc"    column="sub_repair_desc"    />
        <result property="repairProtocol"    column="sub_repair_protocol"    />
        <result property="operationDescription"    column="sub_operation_description"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateTime"    column="sub_update_time"    />
    </resultMap>

    <sql id="selectDmsPlanRepairVo">
        select plan_repair_id, plan_repair_code,time_limit, repair_type, repair_group, repair_supervisor, repair_time,  cycle_period,  create_method, is_flag, remark, create_by, create_time, update_by, update_time from dms_plan_repair
    </sql>

    <select id="selectDmsPlanRepairList" parameterType="DmsPlanRepair" resultMap="DmsPlanRepairResult">
        <include refid="selectDmsPlanRepairVo"/>
        <where>
            <if test="planRepairCode != null  and planRepairCode != ''"> and plan_repair_code = #{planRepairCode}</if>
            <if test="repairType != null  and repairType != ''"> and repair_type = #{repairType}</if>
            <if test="repairGroup != null  and repairGroup != ''"> and repair_group = #{repairGroup}</if>
            <if test="repairSupervisor != null  and repairSupervisor != ''"> and repair_supervisor = #{repairSupervisor}</if>
            <if test="repairTime != null "> and repair_time = #{repairTime}</if>
            <if test="cyclePeriod != null "> and cycle_period = #{cyclePeriod}</if>
            <if test="createMethod != null  and createMethod != ''"> and create_method = #{createMethod}</if>
            <if test="isFlag != null  and isFlag != ''"> and is_flag = #{isFlag}</if>
        </where>
    </select>

    <select id="selectDmsPlanRepairByPlanRepairId" parameterType="Long" resultMap="DmsPlanRepairDmsPlanRepairDetailResult">
        select a.plan_repair_id, a.plan_repair_code, a.repair_type, a.repair_group, a.repair_supervisor, a.repair_time,  a.cycle_period,  a.create_method, a.is_flag, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,a.time_limit,
 b.plan_repair_detail_id as sub_plan_repair_detail_id, b.plan_repair_id as sub_plan_repair_id, b.repair_detail_target_type as sub_repair_detail_target_type,b.repair_detail_target_id as sub_device_id, b.fault_type as sub_fault_type, b.repair_desc as sub_repair_desc, b.repair_protocol as sub_repair_protocol, b.operation_description as sub_operation_description, b.create_by as sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time
        from dms_plan_repair a
        left join dms_plan_repair_detail b on b.plan_repair_id = a.plan_repair_id
        where a.plan_repair_id = #{planRepairId}
    </select>

    <insert id="insertDmsPlanRepair" parameterType="DmsPlanRepair" useGeneratedKeys="true" keyProperty="planRepairId">
        insert into dms_plan_repair
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planRepairCode != null and planRepairCode != ''">plan_repair_code,</if>
            <if test="repairType != null">repair_type,</if>
            <if test="repairGroup != null">repair_group,</if>
            <if test="repairSupervisor != null">repair_supervisor,</if>
            <if test="repairTime != null">repair_time,</if>
            <if test="timeLimit != null">time_limit,</if>
            <if test="cyclePeriod != null">cycle_period,</if>
            <if test="createMethod != null">create_method,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planRepairCode != null and planRepairCode != ''">#{planRepairCode},</if>
            <if test="repairType != null">#{repairType},</if>
            <if test="repairGroup != null">#{repairGroup},</if>
            <if test="repairSupervisor != null">#{repairSupervisor},</if>
            <if test="repairTime != null">#{repairTime},</if>
            <if test="timeLimit != null">#{timeLimit},</if>
            <if test="repairEndTime != null">#{repairEndTime},</if>
            <if test="cyclePeriod != null">#{cyclePeriod},</if>
            <if test="createMethod != null">#{createMethod},</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>
         </trim>
    </insert>

    <update id="updateDmsPlanRepair" parameterType="DmsPlanRepair">
        update dms_plan_repair
        <trim prefix="SET" suffixOverrides=",">
            <if test="planRepairCode != null and planRepairCode != ''">plan_repair_code = #{planRepairCode},</if>
            <if test="repairType != null">repair_type = #{repairType},</if>
            <if test="timeLimit != null">time_limit = #{timeLimit},</if>
            <if test="repairGroup != null">repair_group = #{repairGroup},</if>
            <if test="repairSupervisor != null">repair_supervisor = #{repairSupervisor},</if>
            <if test="repairTime != null">repair_time = #{repairTime},</if>
            <if test="cyclePeriod != null">cycle_period = #{cyclePeriod},</if>
            <if test="createMethod != null">create_method = #{createMethod},</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>
        </trim>
        where plan_repair_id = #{planRepairId}
    </update>

    <delete id="deleteDmsPlanRepairByPlanRepairId" parameterType="Long">
        delete from dms_plan_repair where plan_repair_id = #{planRepairId}
    </delete>

    <delete id="deleteDmsPlanRepairByPlanRepairIds" parameterType="String">
        delete from dms_plan_repair where plan_repair_id in
        <foreach item="planRepairId" collection="array" open="(" separator="," close=")">
            #{planRepairId}
        </foreach>
    </delete>

    <delete id="deleteDmsPlanRepairDetailByPlanRepairIds" parameterType="String">
        delete from dms_plan_repair_detail where plan_repair_id in
        <foreach item="planRepairId" collection="array" open="(" separator="," close=")">
            #{planRepairId}
        </foreach>
    </delete>

    <delete id="deleteDmsPlanRepairDetailByPlanRepairId" parameterType="Long">
        delete from dms_plan_repair_detail where plan_repair_id = #{planRepairId}
    </delete>

    <insert id="batchDmsPlanRepairDetail">
        insert into dms_plan_repair_detail( plan_repair_detail_id, plan_repair_id, device_id, fault_type, repair_desc, repair_protocol, operation_description, create_by, create_time, update_by, update_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.planRepairDetailId}, #{item.planRepairId}, #{item.deviceId}, #{item.faultType}, #{item.repairDesc}, #{item.repairProtocol}, #{item.operationDescription}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>
</mapper>
