<?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.EmsObjReportLogMapper">
    
    <resultMap type="com.ruoyi.ems.domain.EmsObjReportLog" 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="msgDesc"        column="msg_desc"    />
        <result property="reportPayload"  column="report_payload"    />
        <result property="reportTime"     column="report_time"    />
    </resultMap>

    <sql id="selectList">
        select l.id, l.obj_code,
               CASE
                   WHEN m.obj_type = 1 THEN f.facs_name
                   WHEN m.obj_type = 2 THEN d.device_name
                   END as obj_name,
               l.model_code, l.msg_desc, l.report_time
        from adm_ems_obj_report_log l
                 left join adm_ems_obj_model m on l.model_code = m.model_code
                 left join adm_ems_facs f on l.obj_code = f.facs_code and m.obj_type = 1
                 left join adm_ems_device d on l.obj_code = d.device_code and m.obj_type = 2
    </sql>

    <sql id="selectDetail">
        select l.id, l.obj_code,
               CASE
                   WHEN m.obj_type = 1 THEN f.facs_name
                   WHEN m.obj_type = 2 THEN d.device_name
                   END as obj_name,
               l.model_code, l.msg_desc, l.report_time, l.report_payload
        from adm_ems_obj_report_log l
                 left join adm_ems_obj_model m on l.model_code = m.model_code
                 left join adm_ems_facs f on l.obj_code = f.facs_code and m.obj_type = 1
                 left join adm_ems_device d on l.obj_code = d.device_code and m.obj_type = 2
    </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.EmsObjReportLog" 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.report_time between #{startRecTime} and #{endRecTime}
            </if>
        </where>
    </select>

    <insert id="insertLog" parameterType="com.ruoyi.ems.domain.EmsObjReportLog" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_obj_report_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="msgDesc != null and msgDesc != ''">msg_desc,</if>
            <if test="reportTime != null">report_time,</if>
            <if test="reportPayload != null">report_payload,</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="msgDesc != null and msgDesc != ''">#{msgDesc},</if>
            <if test="reportTime != null">#{reportTime},</if>
            <if test="reportPayload != null">#{reportPayload},</if>
         </trim>
    </insert>
</mapper>