<?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="deviceModel" column="device_model"/>
        <result property="deviceCode" column="device_code"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceEnable" column="device_enable"/>
        <result property="location" column="location"/>
        <result property="locationRef" column="location_ref"/>
        <result property="meterCls" column="meter_cls"/>
        <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_model,
               device_code,
               device_name,
               device_enable,
               location,
               location_ref,
               meter_cls,
               col_cycle,
               col_mode,
               magnification,
               spec_desc
        from adm_meter_device
    </sql>

    <select id="selectMeterDeviceList" parameterType="com.ruoyi.ems.model.QueryDevice" 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="deviceModel != null and deviceModel != ''">and device_model = #{deviceModel}</if>
            <if test="meterCls != null and meterCls != ''">and meter_cls = #{meterCls}</if>
            <if test="deviceEnable != null">and device_enable = #{deviceEnable}</if>
            <if test="colMode != null ">and col_mode = #{colMode}</if>
            <if test="locationRef != null and locationRef != '' and areaCodes != null">
                and `location_ref` in
                <foreach item="areaCode" collection="areaCodes" open="(" separator="," close=")">
                    #{areaCode}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectByModel" parameterType="com.ruoyi.ems.model.QueryDevice" resultMap="meterDeviceResult">
        select
        m.id, m.area_code, m.device_model, m.device_code, m.device_name, m.location, m.location_ref, m.meter_cls,
        m.col_cycle, m.col_mode, m.magnification, m.spec_desc
        from adm_meter_device m
        where m.device_model = #{deviceModel}
    </select>

    <select id="selectMeterDeviceById" parameterType="Long" resultMap="meterDeviceResult">
        <include refid="selectMeterDeviceVo"/>
        where id = #{id}
    </select>

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

    <select id="selectMeterDeviceByObj" resultMap="meterDeviceResult">
        select m.id,
               m.area_code,
               m.device_model,
               m.device_code,
               m.device_name,
               m.location,
               m.location_ref,
               m.meter_cls,
               m.col_cycle,
               m.col_mode,
               m.magnification,
               m.spec_desc
        from adm_meter_device m
        <where>
            <if test="areaCode != null and areaCode != ''">
                AND m.area_code = #{areaCode}
            </if>
            <if test="meterCls != null">
                AND m.meter_cls = #{meterCls}
            </if>
            <if test="colMode != null">
                AND m.col_mode = #{colMode}
            </if>
            <if test="objType != null and objCode != null and objCode != ''">
                AND m.device_code in
                 (select r.meter_device
                     from adm_meter_boundary_rel r
                  where r.boundary_obj = #{objCode}
                  and r.obj_type = #{objType})
            </if>
        </where>
    </select>

    <select id="selectMeterDevByTag" resultMap="meterDeviceResult">
        select m.id,
        m.area_code,
        m.device_model,
        m.device_code,
        m.device_name,
        m.location,
        m.location_ref,
        m.meter_cls,
        m.col_cycle,
        m.col_mode,
        m.magnification,
        m.spec_desc
        from adm_meter_device m
          left join adm_obj_tag_rel r on r.obj_code = m.device_code and r.obj_type = 4
        <where>
            <if test="tagCode != null and tagCode != ''">and r.tag_code = #{tagCode}</if>
            <if test="queryDevice.deviceCode != null and queryDevice.deviceCode != ''">and m.device_code = #{queryDevice.deviceCode}</if>
            <if test="queryDevice.areaCode != null and queryDevice.areaCode != ''">and m.area_code = #{queryDevice.areaCode}</if>
            <if test="queryDevice.deviceModel != null and queryDevice.deviceModel != ''">and m.device_model = #{queryDevice.deviceModel}</if>
            <if test="queryDevice.meterCls != null and queryDevice.meterCls != ''">and m.meter_cls = #{queryDevice.meterCls}</if>
            <if test="queryDevice.deviceEnable != null">and m.device_enable = #{queryDevice.deviceEnable}</if>
            <if test="queryDevice.colMode != null ">and m.col_mode = #{queryDevice.colMode}</if>
        </where>
    </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="deviceModel != null and deviceModel != ''">device_model,</if>
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="deviceEnable != null">device_enable,</if>
            <if test="locationRef != null and locationRef != ''">location_ref,</if>
            <if test="location != null and location != ''">location,</if>
            <if test="meterCls != null and meterCls != ''">meter_cls,</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="deviceModel != null and deviceModel != ''">#{deviceModel},</if>
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="deviceEnable != null">#{deviceEnable},</if>
            <if test="locationRef != null and locationRef != ''">#{locationRef},</if>
            <if test="location != null and location != ''">#{location},</if>
            <if test="meterCls != null and meterCls != ''">#{meterCls},</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="deviceModel != null and deviceModel != ''">device_model = #{deviceModel},</if>
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="deviceEnable != null">device_enable = #{deviceEnable},</if>
            <if test="locationRef != null and locationRef != ''">location_ref = #{locationRef},</if>
            <if test="location != null and location != ''">location = #{location},</if>
            <if test="meterCls != null and meterCls != ''">meter_cls = #{meterCls},</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>

    <select id="countMeterDeviceByObj" parameterType="MeterDevice" resultType="java.lang.Integer">
        select
        count(*)
        from adm_meter_device m
        where
        col_mode = 0
        <if test="areaCode != null and areaCode != ''">and m.area_code = #{areaCode}</if>
    </select>

    <select id="countOnlineMeterDeviceByObj" parameterType="ElecMeterH" resultType="java.lang.Integer">
        SELECT
        count(DISTINCT aemh.device_code)
        from
        adm_elec_meter_h aemh
        inner join adm_meter_device aed on aed.device_code = aemh.device_code and aed.col_mode = 0
        <where>
            <if test="areaCode != null and areaCode != ''">and aemh.area_code = #{areaCode}</if>
            <if test="startRecTime != null and startRecTime != ''">and aemh.date = #{startRecTime}</if>
        </where>
    </select>


</mapper>