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

    <resultMap type="DmsRecordShutDown" id="DmsRecordShutDownResult">
        <result property="recordShutDownId" column="record_shut_down_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="shutType" column="shut_type"/>
        <result property="shutReasonId" column="shut_reason_id"/>
        <result property="shutReason" column="shut_reason"/>
        <result property="shutBeginTime" column="shut_begin_time"/>
        <result property="shutEndTime" column="shut_end_time"/>
        <result property="shutTime" column="shut_time"/>
        <result property="downtimeFlag" column="downtime_flag"/>
        <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="deviceCode" column="device_code"/>
        <result property="deviceName" column="device_name"/>
    </resultMap>

    <!-- 关联base_deviceledger查询设备编码和名称，使用resultMap映射物理列名 -->
    <sql id="selectDmsRecordShutDownVo">
        select r.record_shut_down_id,
               r.machine_id as device_id,
               r.shut_type_id as shut_type,
               r.shut_reason_id,
               nvl(s.shut_reason, r.shut_reason) as shut_reason,
               r.shut_begin_time,
               r.shut_end_time,
               r.shut_time,
               r.downtime_flag,
               r.active_flag as is_flag,
               r.remark,
               r.create_by,
               r.create_time,
               r.update_by,
               r.update_time,
               d.device_code as device_code,
               d.device_name as device_name
        from dms_record_shut_down r
        left join base_deviceledger d on r.machine_id = d.OBJ_ID
        left join dms_base_shut_reason s on r.shut_reason_id = s.shut_reason_id
    </sql>

    <select id="selectDmsRecordShutDownList" parameterType="DmsRecordShutDown" resultMap="DmsRecordShutDownResult">
        <include refid="selectDmsRecordShutDownVo"/>
        <where>
            <if test="shutType != null ">and r.shut_type_id = #{shutType}</if>
            <if test="shutReason != null  and shutReason != ''">and r.shut_reason = #{shutReason}</if>
            <if test="shutReasonId != null">and r.shut_reason_id = #{shutReasonId}</if>
            <if test="beginTime != null and endTime != null">
                and r.shut_begin_time between #{beginTime} and #{endTime}
            </if>
            <if test="params != null and params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
                and r.shut_begin_time between to_date(#{params.beginTime}, 'yyyy-MM-dd HH24:mi:ss')
                and to_date(#{params.endTime}, 'yyyy-MM-dd HH24:mi:ss')
            </if>
            <if test="shutBeginTime != null ">and r.shut_begin_time &gt;= #{shutBeginTime}</if>
            <if test="shutEndTime != null ">and #{shutEndTime} &gt;= r.shut_end_time</if>
            <if test="shutTime != null ">and r.shut_time = #{shutTime}</if>
            <if test="deviceCode != null and deviceCode != ''">and d.device_code = #{deviceCode}</if>
            <if test="deviceName != null and deviceName != ''">and d.device_name like concat(concat('%', #{deviceName}), '%')</if>
        </where>
    </select>

    <select id="selectDmsRecordShutDownByRecordShutDownId" parameterType="Long" resultMap="DmsRecordShutDownResult">
        <include refid="selectDmsRecordShutDownVo"/>
        where r.record_shut_down_id = #{recordShutDownId}
    </select>

    <insert id="insertDmsRecordShutDown" parameterType="DmsRecordShutDown">
        <selectKey keyProperty="recordShutDownId" resultType="long" order="BEFORE">
            SELECT SEQ_DMS_REC_SHUT_DOWN.NEXTVAL FROM DUAL
        </selectKey>
        insert into dms_record_shut_down
        <trim prefix="(" suffix=")" suffixOverrides=",">
            record_shut_down_id,
            <if test="deviceId != null">machine_id,</if>
            <if test="shutType != null">shut_type_id,</if>
            <if test="shutReasonId != null">shut_reason_id,</if>
            <if test="shutReason != null">shut_reason,</if>
            <if test="shutBeginTime != null">shut_begin_time,</if>
            <if test="shutEndTime != null">shut_end_time,</if>
            <if test="shutTime != null">shut_time,</if>
            <if test="downtimeFlag != null">downtime_flag,</if>
            <if test="isFlag != null">active_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=",">
            #{recordShutDownId},
            <if test="deviceId != null">#{deviceId},</if>
            <if test="shutType != null">#{shutType},</if>
            <if test="shutReasonId != null">#{shutReasonId},</if>
            <if test="shutReason != null">#{shutReason},</if>
            <if test="shutBeginTime != null">#{shutBeginTime},</if>
            <if test="shutEndTime != null">#{shutEndTime},</if>
            <if test="shutTime != null">#{shutTime},</if>
            <if test="downtimeFlag != null">#{downtimeFlag},</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="updateDmsRecordShutDown" parameterType="DmsRecordShutDown">
        update dms_record_shut_down
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">machine_id = #{deviceId},</if>
            <if test="shutType != null">shut_type_id = #{shutType},</if>
            <if test="shutReasonId != null">shut_reason_id = #{shutReasonId},</if>
            <if test="shutReason != null">shut_reason = #{shutReason},</if>
            <if test="shutBeginTime != null">shut_begin_time = #{shutBeginTime},</if>
            <if test="shutEndTime != null">shut_end_time = #{shutEndTime},</if>
            <if test="shutTime != null">shut_time = #{shutTime},</if>
            <if test="downtimeFlag != null">downtime_flag = #{downtimeFlag},</if>
            <if test="isFlag != null">active_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 record_shut_down_id = #{recordShutDownId}
    </update>

    <select id="selectPdaShutDownList" resultMap="DmsRecordShutDownResult">
        <include refid="selectDmsRecordShutDownVo"/>
        where (r.shut_end_time is null or r.shut_reason_id = 1)
        order by r.shut_begin_time desc
    </select>

    <update id="batchUpdateShutReason">
        update dms_record_shut_down
        set shut_reason_id = #{shutReasonId},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where record_shut_down_id in
        <foreach collection="recordShutDownIds" item="recordShutDownId" open="(" separator="," close=")">
            #{recordShutDownId}
        </foreach>
    </update>

    <delete id="deleteDmsRecordShutDownByRecordShutDownId" parameterType="Long">
        delete
        from dms_record_shut_down
        where record_shut_down_id = #{recordShutDownId}
    </delete>

    <delete id="deleteDmsRecordShutDownByRecordShutDownIds" parameterType="String">
        delete from dms_record_shut_down where record_shut_down_id in
        <foreach item="recordShutDownId" collection="array" open="(" separator="," close=")">
            #{recordShutDownId}
        </foreach>
    </delete>
</mapper>
