<?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="areaCode"    column="area_code"    />
        <result property="deviceCode"  column="device_code"    />
        <result property="deviceName"  column="device_name"    />
        <result property="deviceLocation"  column="device_location"    />
        <result property="meterCls"    column="meter_cls"    />
        <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, area_code, device_code, device_name, device_location, meter_cls, 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="deviceName != null and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</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="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>
    </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">
        select
            m.id, m.area_code, m.device_code, m.device_name, m.device_location, m.meter_cls, m.obj_tag, m.col_cycle, m.col_mode, m.magnification, m.spec_desc
        from adm_meter_device m
        where m.area_code = #{areaCode}
          and m.meter_cls = #{meterCls}
          and m.device_code in
              (select
                   r.meter_device from adm_meter_boundary_rel r
               where r.boundary_obj = #{boundaryObj}
                 and r.obj_type = #{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="areaCode != null and areaCode != ''">area_code,</if>
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="deviceLocation != null and deviceLocation != ''">device_location,</if>
            <if test="meterCls != null and meterCls != ''">meter_cls,</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="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="deviceLocation != null and deviceLocation != ''">#{deviceLocation},</if>
            <if test="meterCls != null and meterCls != ''">#{meterCls},</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="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="deviceLocation != null and deviceLocation != ''">device_location = #{deviceLocation},</if>
            <if test="meterCls != null and meterCls != ''">meter_cls = #{meterCls},</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>