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

    <resultMap type="DmsRealtimeStatus" id="DmsRealtimeStatusResult">
        <result property="statusId" column="status_id"/>
        <result property="statusCode" column="status_code"/>
        <result property="statusName" column="status_name"/>
        <result property="statusValue" column="status_value"/>
        <result property="deviceId" column="device_id"/>
        <result property="creatTime" column="creat_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="deviceName" column="device_name"/>
    </resultMap>

    <sql id="selectDmsRealtimeStatusVo">
        select drs.status_id,
               drs.status_code,
               drs.status_name,
               drs.status_value,
               drs.device_id,
               drs.creat_time,
               drs.update_time,
               dbdl.device_name
        from dms_realtime_status drs
                 left join base_deviceledger dbdl on drs.device_id = dbdl.OBJ_ID
    </sql>

    <select id="selectDmsRealtimeStatusList" parameterType="DmsRealtimeStatus" resultMap="DmsRealtimeStatusResult">
        <include refid="selectDmsRealtimeStatusVo"/>
        <where>
            <if test="statusCode != null  and statusCode != ''">and drs.status_code = #{statusCode}</if>
            <if test="statusName != null  and statusName != ''">and drs.status_name like concat('%', #{statusName}, '%')
            </if>
            <if test="statusValue != null  and statusValue != ''">and drs.status_value = #{statusValue}</if>
            <if test="deviceId != null ">and drs.device_id = #{deviceId}</if>
            <if test="creatTime != null ">and drs.creat_time = #{creatTime}</if>
        </where>
    </select>

    <select id="selectDmsRealtimeStatusByStatusId" parameterType="Long" resultMap="DmsRealtimeStatusResult">
        <include refid="selectDmsRealtimeStatusVo"/>
        where drs.status_id = #{statusId}
    </select>
    <select id="history" resultType="com.aucma.dms.domain.DmsRealtimeStatusHistory"
            parameterType="java.lang.Long">
        select
        a.*,
        b.device_name
        from
        dms_realtime_status_history a
        left join base_deviceledger b on
        a.device_id = b.OBJ_ID
        <where>
            <if test="realBeginTime != null">and a.sync_time > #{realBeginTime}</if>
            <if test="statusId != null">and a.status_id = #{statusId}</if>
            <if test="realEndTime != null ">and a.#{realEndTime} > sync_time</if>
        </where>
    </select>
    <select id="historyTimeCount" resultType="java.lang.Long"
            parameterType="com.aucma.dms.domain.DmsRealtimeStatusHistory">
        select
       count(1)  from
        dms_realtime_status_history
        <where>
            status_value = 'True'
            <if test="realBeginTime != null">and sync_time > #{realBeginTime}</if>
            <if test="statusId != null">and status_id = #{statusId}</if>
            <if test="realEndTime != null ">and #{realEndTime} > sync_time</if>
        </where>
    </select>

    <insert id="insertDmsRealtimeStatus" parameterType="DmsRealtimeStatus" useGeneratedKeys="true"
            keyProperty="statusId">
        insert into dms_realtime_status
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="statusCode != null and statusCode != ''">status_code,</if>
            <if test="statusName != null and statusName != ''">status_name,</if>
            <if test="statusValue != null">status_value,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="creatTime != null">creat_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="statusCode != null and statusCode != ''">#{statusCode},</if>
            <if test="statusName != null and statusName != ''">#{statusName},</if>
            <if test="statusValue != null">#{statusValue},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="creatTime != null">#{creatTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateDmsRealtimeStatus" parameterType="DmsRealtimeStatus">
        update dms_realtime_status
        <trim prefix="SET" suffixOverrides=",">
            <if test="statusCode != null and statusCode != ''">status_code = #{statusCode},</if>
            <if test="statusName != null and statusName != ''">status_name = #{statusName},</if>
            <if test="statusValue != null">status_value = #{statusValue},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="creatTime != null">creat_time = #{creatTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where status_id = #{statusId}
    </update>

    <delete id="deleteDmsRealtimeStatusByStatusId" parameterType="Long">
        delete
        from dms_realtime_status
        where status_id = #{statusId}
    </delete>

    <delete id="deleteDmsRealtimeStatusByStatusIds" parameterType="String">
        delete from dms_realtime_status where status_id in
        <foreach item="statusId" collection="array" open="(" separator="," close=")">
            #{statusId}
        </foreach>
    </delete>
</mapper>