<?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.aucma.dms.mapper.DmsPlanMaintDetailMapper">

    <resultMap type="DmsPlanMaintDetail" id="DmsPlanMaintDetailResult">
        <result property="planMaintDetailId"    column="plan_maint_detail_id"    />
        <result property="planMaintId"    column="plan_maint_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="maintStationId"    column="maint_station_id"    />
        <result property="maintStandardId"    column="maint_standard_id"    />
        <result property="operationDescription"    column="operation_description"    />
        <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="maintProtocol"    column="maint_protocol"    />
        <result property="maintStationName"    column="maint_station_name"    />
        <result property="maintStationCode"    column="maint_station_code"    />
        <result property="deviceName"    column="device_name"    />
    </resultMap>

    <sql id="selectDmsPlanMaintDetailVo">
        select plan_maint_detail_id, plan_maint_id, device_id, maint_station_id, maint_standard_id, operation_description, is_flag, remark, create_by, create_time, update_by, update_time from dms_plan_maint_detail
    </sql>

    <select id="selectDmsPlanMaintDetailList" parameterType="DmsPlanMaintDetail" resultMap="DmsPlanMaintDetailResult">
        <include refid="selectDmsPlanMaintDetailVo"/>
        <where>
            <if test="planMaintId != null "> and plan_maint_id = #{planMaintId}</if>
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="maintStationId != null "> and maint_station_id = #{maintStationId}</if>
            <if test="maintStandardId != null  and maintStandardId != ''"> and maint_standard_id = #{maintStandardId}</if>
            <if test="operationDescription != null  and operationDescription != ''"> and operation_description = #{operationDescription}</if>
            <if test="isFlag != null "> and is_flag = #{isFlag}</if>
        </where>
    </select>

    <select id="selectDmsPlanMaintDetailByPlanMaintDetailId" parameterType="Long" resultMap="DmsPlanMaintDetailResult">
<!--        <include refid="selectDmsPlanMaintDetailVo"/>-->
        select a.plan_maint_detail_id, a.plan_maint_id, a.device_id, a.maint_station_id, a.maint_standard_id, a.operation_description, a.is_flag, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,b.maint_station_code
        from dms_plan_maint_detail a left join dms_base_maint_station b on a.maint_station_id = b.maint_station_id
        where a.plan_maint_detail_id = #{planMaintDetailId}
    </select>

    <insert id="insertDmsPlanMaintDetail" parameterType="DmsPlanMaintDetail">
        <selectKey keyProperty="planMaintDetailId" resultType="long" order="BEFORE">
            SELECT HAIWEI.SEQ_DMS_PLAN_MAINT_DETAIL.NEXTVAL FROM DUAL
        </selectKey>
        insert into dms_plan_maint_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planMaintDetailId != null">plan_maint_detail_id,</if>
            <if test="planMaintId != null">plan_maint_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="maintStationId != null">maint_station_id,</if>
            <if test="maintStandardId != null">maint_standard_id,</if>
            <if test="operationDescription != null">operation_description,</if>
            <if test="isFlag != null">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="planMaintDetailId != null">#{planMaintDetailId},</if>
            <if test="planMaintId != null">#{planMaintId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="maintStationId != null">#{maintStationId},</if>
            <if test="maintStandardId != null">#{maintStandardId},</if>
            <if test="operationDescription != null">#{operationDescription},</if>
            <if test="isFlag != null">#{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="updateDmsPlanMaintDetail" parameterType="DmsPlanMaintDetail">
        update dms_plan_maint_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="planMaintId != null">plan_maint_id = #{planMaintId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="maintStationId != null">maint_station_id = #{maintStationId},</if>
            <if test="maintStandardId != null">maint_standard_id = #{maintStandardId},</if>
            <if test="operationDescription != null">operation_description = #{operationDescription},</if>
            <if test="isFlag != null">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_maint_detail_id = #{planMaintDetailId}
    </update>

    <delete id="deleteDmsPlanMaintDetailByPlanMaintDetailId" parameterType="Long">
        delete from dms_plan_maint_detail where plan_maint_detail_id = #{planMaintDetailId}
    </delete>

    <delete id="deleteDmsPlanMaintDetailByPlanMaintDetailIds" parameterType="String">
        delete from dms_plan_maint_detail where plan_maint_detail_id in
        <foreach item="planMaintDetailId" collection="array" open="(" separator="," close=")">
            #{planMaintDetailId}
        </foreach>
    </delete>










    <sql id="selectDmsPlanMaintDetailJoinVo">
        select dpmd.plan_maint_detail_id, dpmd.plan_maint_id, dpmd.device_id, dpmd.maint_station_id, dpmd.maint_standard_id,
               dpmd.operation_description, dpmd.is_flag, dpmd.remark,
               dbmsd.maint_protocol,
               dbms.maint_station_name,
               dbms.maint_station_code
               from dms_plan_maint_detail dpmd
                   left join dms_base_maint_standard dbmsd on dpmd.maint_standard_id=dbmsd.maint_standard_id
                   left join dms_base_maint_station dbms on dpmd.maint_station_id=dbms.maint_station_id
    </sql>

    <select id="selectDmsPlanMaintDetailJoinList" parameterType="DmsPlanMaintDetail" resultMap="DmsPlanMaintDetailResult">
        <include refid="selectDmsPlanMaintDetailJoinVo"/>
        <where>
            <if test="planMaintId != null "> and plan_maint_id = #{planMaintId}</if>
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="maintStationId != null "> and maint_station_id = #{maintStationId}</if>
            <if test="maintStandardId != null  and maintStandardId != ''"> and maint_standard_id = #{maintStandardId}</if>
            <if test="operationDescription != null  and operationDescription != ''"> and operation_description = #{operationDescription}</if>
            <if test="isFlag != null "> and is_flag = #{isFlag}</if>
        </where>
    </select>
    <select id="selectDmsPlanMaintDetailListByPlanMaintId" parameterType="Long" resultMap="DmsPlanMaintDetailResult">
        select
            a.plan_maint_detail_id,
            a.plan_maint_id,
            a.device_id,
            a.maint_station_id,
            a.maint_standard_id,
            a.operation_description,
            b.device_name,
            c.maint_station_code,
            d.maint_protocol
        from
            dms_plan_maint_detail a
                left join base_deviceledger b on
                a.device_id = b.OBJ_ID
                left join dms_base_maint_station c on
                a.maint_station_id = c.maint_station_id
                left join dms_base_maint_standard d on
                a.maint_standard_id = d.maint_standard_id
        where
            a.plan_maint_id = #{planMaintId}
    </select>
    <select id="selectStationIdByCode" resultType="java.lang.Long" parameterType="java.lang.String">
        SELECT x.maint_station_id FROM dms_base_maint_station x where x.maint_station_code = #{maintStationCode}
    </select>
    <select id="selectStandardId" resultType="java.lang.Long" parameterType="java.lang.Long">
        select
            dbms.maint_standard_id
        from
            dms_base_maint_station x left join dms_base_maint_standard dbms on x.protocol_code = dbms.maint_standard_code
        where
            x.maint_station_id = #{stationId}
    </select>

    <!-- 查询保养计划下的所有设备ID列表 -->
    <select id="selectDeviceIdsByPlanId" resultType="java.lang.Long">
        SELECT device_id
        FROM dms_plan_maint_detail
        WHERE plan_maint_id = #{planMaintId}
    </select>

</mapper>
