<?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.gyee.wisdom.alarm.sharding.mapper.DeviceMetricsMapper">
    <resultMap type="com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics" id="deviceModelMetricsResult">
        <result property="id"    column="id"    />
        <result property="deviceid"    column="deviceid"    />
        <result property="structurecode"    column="structurecode"    />
        <result property="metriccode"    column="metriccode"    />
        <collection  property="deviceModelMetrics"  column="metriccode"      javaType="java.util.List"  select="com.gyee.wisdom.alarm.sharding.mapper.DeviceModelMetricsMapper.getDeviceModelMetricsById"/>
    </resultMap>
    <update id="updateByMetricsId" parameterType="com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics">
        update device_metrics
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceid != null">deviceid = #{deviceid},</if>
            <if test="structurecode != null">structurecode = #{structurecode},</if>
            <if test="metriccode != null">metriccode = #{metriccode},</if>
            <if test="name != null">name = #{name},</if>
            <if test="enname != null">enname = #{enname},</if>
            <if test="unitname != null">unitname = #{unitname},</if>
            <if test="unitnamecn != null">unitnamecn = #{unitnamecn},</if>
            <if test="categorydata != null">categorydata = #{categorydata},</if>
            <if test="categorysci != null">categorysci = #{categorysci},</if>
            <if test="categorysys != null">categorysys = #{categorysys},</if>
            <if test="categoryres1 != null">categoryres1 = #{categoryres1},</if>
            <if test="categoryres2 != null">categoryres2 = #{categoryres2},</if>
            <if test="categoryres3 != null">categoryres3 = #{categoryres3},</if>
            <if test="categoryres4 != null">categoryres4 = #{categoryres4},</if>
            <if test="categoryres5 != null">categoryres5 = #{categoryres5},</if>
            <if test="description != null">description = #{description},</if>
        </trim>
        where id = #{id}
    </update>

    <insert id="insertByMetrics" parameterType="com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics" useGeneratedKeys="true">
        insert into device_metrics(id, deviceid, structurecode, metriccode, name, enname,
                                   unitname,unitnamecn,categorydata,categorysci,categorysys,categoryres1,categoryres2,categoryres3,categoryres4,categoryres5,description)
        values (#{id}, #{deviceid}, #{structurecode},
                #{metriccode}, #{name}, #{enname},#{unitname}, #{unitnamecn}, #{categorydata},#{categorysci},#{categorysys}, #{categoryres1}, #{categoryres2},#{categoryres3}, #{categoryres4}, #{categoryres5},#{description})
    </insert>



    <select id="getDeviceMetricsByDeviceId" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics">
        select * from device_metrics a
        <where>
            1=1
            <if test="deviceId !=null and deviceId !=''">
                and a.deviceId=#{deviceId}
            </if>
        </where>
        order by metriccode
    </select>

    <select id="getDeviceMetricsByStructureCode" parameterType="java.util.Map" resultMap="deviceModelMetricsResult">
        select * from device_metrics a
        <where>
            1=1
            <if test="deviceId !=null and deviceId !=''">
                and a.deviceId=#{deviceId}
                <if test="structureCode !=null and structureCode !=''">
                    and a.structurecode like '${structureCode}%'
                </if>
            </if>

        </where>
    </select>

    <select id="pageQueryAll" parameterType="java.util.Map" resultMap="deviceModelMetricsResult">
        select m.* from  device_metrics m
        <if test="model !=null and model !=''">
            inner join device_model_metrics mm ON mm.METRICCODE = m.METRICCODE
        </if>
        <where>
            1=1
            <if test="deviceId !=null and deviceId !=''">
                and m.deviceId=#{deviceId}
            </if>
            <if test="categorydata !=null and categorydata !=''">
                and m.categorydata=#{categorydata}
            </if>
            <if test="model !=null and model !=''">
                and mm.devicemodel=#{model}
            </if>
            <if test="keyword !=null and keyword !=''">
                and m.name like '%${keyword}%'
            </if>
            <if test="structureCode !=null and structureCode !=''">
                and m.structurecode like '${structureCode}%'
            </if>
        </where>

        order by m.metriccode  asc
    </select>

    <select id="getDeviceMetricsByCode" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics">
        select * from device_metrics a
        <where>
            1=1
            <if test="deviceId !=null and deviceId !=''">
                and a.deviceId=#{deviceId}
                <if test="metricCode !=null and metricCode !=''">
                    and a.metricCode=#{metricCode}
                </if>
            </if>

        </where>
    </select>
    <select id="selectByMetricsId" resultType="com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics">
        select id,deviceid,structurecode,metriccode,name,enname,unitname,unitnamecn,categorydata,categorysci,categorysys,categoryres1,categoryres2,
               categoryres3,categoryres4,categoryres5,description from device_metrics where id=#{id}
    </select>

    <select id="selectByMetriccode" resultType="com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics">
        select * from device_metrics where metriccode=#{metriccode}
    </select>

</mapper>
