<?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.EmsObjAbilityCallLogMapper">
    
    <resultMap type="com.ruoyi.ems.domain.EmsObjAbilityCallLog" id="objAbilityCallLogResult">
        <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="abilityKey"    column="ability_key"    />
        <result property="abilityName"    column="ability_name"    />
        <result property="callTime"    column="call_time"    />
        <result property="callPayload"    column="call_payload"    />
        <result property="callStatus"    column="call_status"    />
        <result property="resTime"    column="res_time"    />
        <result property="resPayload"    column="res_payload"    />
    </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, m.model_name, l.ability_key, a.ability_name, l.call_time, l.call_status from adm_ems_obj_ability_call_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
        left join adm_ems_obj_ability a on l.ability_key = a.ability_key
    </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, m.model_name, l.ability_key, a.ability_name, l.call_time, l.call_status, l.call_payload, l.res_time, l.res_payload from adm_ems_obj_ability_call_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
        left join adm_ems_obj_ability a on l.ability_key = a.ability_key
    </sql>

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

    <select id="selectObjAbilityCallLog" parameterType="com.ruoyi.ems.domain.EmsObjAbilityCallLog" resultMap="objAbilityCallLogResult">
        <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="abilityKey != null  and abilityKey != ''"> and l.ability_key = #{abilityKey}</if>
            <if test="callStatus != null "> and l.call_status = #{callStatus}</if>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and l.call_time between #{startRecTime} and #{endRecTime}
            </if>
        </where>
    </select>

    <insert id="insertObjAbilityCallLog" parameterType="com.ruoyi.ems.domain.EmsObjAbilityCallLog" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_obj_ability_call_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="abilityKey != null and abilityKey != ''">ability_key,</if>
            <if test="callTime != null">call_time,</if>
            <if test="callPayload != null">call_payload,</if>
            <if test="callStatus != null">call_status,</if>
            <if test="resTime != null">res_time,</if>
            <if test="resPayload != null">res_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="abilityKey != null and abilityKey != ''">#{abilityKey},</if>
            <if test="callTime != null">#{callTime},</if>
            <if test="callPayload != null">#{callPayload},</if>
            <if test="callStatus != null">#{callStatus},</if>
            <if test="resTime != null">#{resTime},</if>
            <if test="resPayload != null">#{resPayload},</if>
         </trim>
    </insert>

    <update id="updateObjAbilityCallLog" parameterType="com.ruoyi.ems.domain.EmsObjAbilityCallLog">
        update adm_ems_obj_ability_call_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="objCode != null and objCode != ''">obj_code = #{objCode},</if>
            <if test="modelCode != null and modelCode != ''">model_code = #{modelCode},</if>
            <if test="abilityKey != null and abilityKey != ''">ability_key = #{abilityKey},</if>
            <if test="callTime != null">call_time = #{callTime},</if>
            <if test="callPayload != null">call_payload = #{callPayload},</if>
            <if test="callStatus != null">call_status = #{callStatus},</if>
            <if test="resTime != null">res_time = #{resTime},</if>
            <if test="resPayload != null">res_payload = #{resPayload},</if>
        </trim>
        where id = #{id}
    </update>
</mapper>