<?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.ruoyi.ems.mapper.EmsObjEventLogMapper">
    
    <resultMap type="com.ruoyi.ems.domain.EmsObjEventLog" id="objLogResult">
        <result property="id"             column="id"    />
        <result property="objCode"        column="obj_code"    />
        <result property="objName"        column="obj_name"    />
        <result property="modelCode"      column="model_code"    />
        <result property="modelName"      column="model_name"    />
        <result property="eventKey"       column="event_key"    />
        <result property="eventName"       column="event_name"    />
        <result property="eventDetail"    column="event_detail"    />
        <result property="eventTime"      column="event_time"    />
    </resultMap>

    <sql id="selectList">
        select l.id, l.obj_code, m.model_name,
               CASE
                   WHEN m.obj_type = 2 THEN d.device_name
                   WHEN m.obj_type = 3 THEN s.system_name
                   END as obj_name,
               l.model_code, l.event_key, e.event_name, l.event_time
        from adm_ems_obj_event_log l
                 left join adm_ems_obj_model m on l.model_code = m.model_code
                 left join adm_ems_device d on l.obj_code = d.device_code and m.obj_type = 2
                 left join adm_ems_subsystem s on l.obj_code = s.system_code and m.obj_type = 3
                 left join adm_ems_obj_event e on l.model_code = e.model_code and l.event_key = e.event_key
    </sql>

    <sql id="selectDetail">
        select l.id, l.obj_code, m.model_name,
               CASE
                   WHEN m.obj_type = 2 THEN d.device_name
                   WHEN m.obj_type = 3 THEN s.system_name
                   END as obj_name,
               l.model_code, l.event_key, e.event_name, l.event_time, l.event_detail
        from adm_ems_obj_event_log l
                 left join adm_ems_obj_model m on l.model_code = m.model_code
                 left join adm_ems_device d on l.obj_code = d.device_code and m.obj_type = 2
                 left join adm_ems_subsystem s on l.obj_code = s.system_code and m.obj_type = 3
                 left join adm_ems_obj_event e on l.model_code = e.model_code and l.event_key = e.event_key
    </sql>

    <select id="selectLogById" parameterType="java.lang.Long" resultMap="objLogResult">
        <include refid="selectDetail"/>
        where l.id = #{id}
    </select>

    <select id="selectLog" parameterType="com.ruoyi.ems.domain.EmsObjEventLog" resultMap="objLogResult">
        <include refid="selectList"/>
        <where>  
            <if test="objCode != null  and objCode != ''"> and l.obj_code = #{objCode}</if>
            <if test="modelCode != null and modelCode != ''"> and l.model_code = #{modelCode}</if>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and l.event_time between #{startRecTime} and #{endRecTime}
            </if>
            <if test="eventKey != null and eventKey != ''"> and l.event_key = #{eventKey}</if>
            <if test="modelCodes != null">and l.model_code in
                (
                <foreach collection="modelCodes" item="item" index="index" separator=",">
                    #{item}
                </foreach>
                )
            </if>
        </where>
    </select>

    <insert id="insertLog" parameterType="com.ruoyi.ems.domain.EmsObjEventLog" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_obj_event_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objCode != null and objCode != ''">obj_code,</if>
            <if test="modelCode != null and modelCode != ''">model_code,</if>
            <if test="eventKey != null and eventKey != ''">event_key,</if>
            <if test="eventTime != null">event_time,</if>
            <if test="eventDetail != null">event_detail,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objCode != null and objCode != ''">#{objCode},</if>
            <if test="modelCode != null and modelCode != ''">#{modelCode},</if>
            <if test="eventKey != null and eventKey != ''">#{eventKey},</if>
            <if test="eventTime != null">#{eventTime},</if>
            <if test="eventDetail != null">#{eventDetail},</if>
         </trim>
    </insert>
</mapper>