<?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.MeterDeviceMapper">
    
    <resultMap type="com.ruoyi.ems.domain.MeterDevice" id="meterDeviceResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"  column="device_code"    />
        <result property="areaCode"    column="area_code"    />
        <result property="meterCls"    column="meter_cls"    />
        <result property="objType"     column="obj_type"    />
        <result property="objSubType"  column="obj_sub_type"    />
        <result property="objCode"     column="obj_code"    />
        <result property="objName"     column="obj_name"    />
        <result property="objTag"      column="obj_tag"    />
        <result property="colCycle"    column="col_cycle"    />
        <result property="colMode"     column="col_mode"    />
        <result property="magnification" column="magnification" />
        <result property="specDesc"    column="spec_desc"    />
    </resultMap>

    <sql id="selectMeterDeviceVo">
        select id, device_code, area_code, meter_cls, obj_type, obj_sub_type, obj_code, obj_tag, col_cycle, col_mode, magnification, spec_desc from adm_meter_device
    </sql>

    <select id="selectMeterDeviceList" parameterType="com.ruoyi.ems.domain.MeterDevice" resultMap="meterDeviceResult">
        <include refid="selectMeterDeviceVo"/>
        <where>  
            <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="meterCls != null  and meterCls != ''"> and meter_cls = #{meterCls}</if>
            <if test="objType != null "> and obj_type = #{objType}</if>
            <if test="objSubType != null "> and obj_sub_type = #{objSubType}</if>
            <if test="objCode != null  and objCode != ''"> and obj_code = #{objCode}</if>
            <if test="objTag != null  and objTag != ''"> and obj_tag = #{objTag}</if>
            <if test="colCycle != null "> and col_cycle = #{colCycle}</if>
            <if test="colMode != null "> and col_mode = #{colMode}</if>
        </where>
        ORDER BY obj_type, obj_sub_type
    </select>
    
    <select id="selectMeterDeviceById" parameterType="Long" resultMap="meterDeviceResult">
        <include refid="selectMeterDeviceVo"/>
        where id = #{id}
    </select>

    <select id="selectMeterDeviceByCode"  resultMap="meterDeviceResult">
        <include refid="selectMeterDeviceVo"/>
        where area_code = #{areaCode} and device_code = #{deviceCode}
    </select>

    <select id="selectMeterDeviceByObj"  resultMap="meterDeviceResult">
        <include refid="selectMeterDeviceVo"/>
        where obj_type = #{objType} and obj_sub_type = #{objSubType} and obj_code = #{objCode} and meter_cls = #{meterCls}
    </select>
        
    <insert id="insertMeterDevice" parameterType="com.ruoyi.ems.domain.MeterDevice" useGeneratedKeys="true" keyProperty="id">
        insert into adm_meter_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="meterCls != null and meterCls != ''">meter_cls,</if>
            <if test="objType != null">obj_type,</if>
            <if test="objSubType != null ">obj_sub_type,</if>
            <if test="objCode != null and objCode != ''">obj_code,</if>
            <if test="objTag != null and objTag != ''">obj_tag,</if>
            <if test="colCycle != null">col_cycle,</if>
            <if test="colMode != null">col_mode,</if>
            <if test="magnification != null">magnification,</if>
            <if test="specDesc != null">spec_desc,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="meterCls != null and meterCls != ''">#{meterCls},</if>
            <if test="objType != null">#{objType},</if>
            <if test="objSubType != null">#{objSubType},</if>
            <if test="objCode != null and objCode != ''">#{objCode},</if>
            <if test="objTag != null and objTag != ''">#{objTag}</if>
            <if test="colCycle != null">#{colCycle},</if>
            <if test="colMode != null">#{colMode},</if>
            <if test="magnification != null">#{magnification},</if>
            <if test="specDesc != null">#{specDesc},</if>
         </trim>
    </insert>

    <update id="updateMeterDevice" parameterType="com.ruoyi.ems.domain.MeterDevice">
        update adm_meter_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="meterCls != null and meterCls != ''">meter_cls = #{meterCls},</if>
            <if test="objType != null">obj_type = #{objType},</if>
            <if test="objSubType != null">obj_sub_type = #{objSubType},</if>
            <if test="objCode != null and objCode != ''">obj_code = #{objCode},</if>
            <if test="objTag != null and objTag != ''">obj_tag = #{objTag},</if>
            <if test="colCycle != null">col_cycle = #{colCycle},</if>
            <if test="colMode != null">col_mode = #{colMode},</if>
            <if test="magnification != null">magnification = #{magnification},</if>
            <if test="specDesc != null">spec_desc = #{specDesc},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMeterDeviceById" parameterType="Long">
        delete from adm_meter_device where id = #{id}
    </delete>

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