<?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.MeterBoundaryRelMapper">
    
    <resultMap type="com.ruoyi.ems.domain.MeterBoundaryRel" id="relResult">
        <result property="id"    column="id"    />
        <result property="objType"    column="obj_type"    />
        <result property="boundaryObj"    column="boundary_obj"    />
        <result property="meterCls"    column="meter_cls"    />
        <result property="meterDevice"    column="meter_device"    />
        <result property="meterDeviceName"    column="meter_device_name"    />
    </resultMap>

    <sql id="selectMeterBoundaryRelVo">
        select
            r.id, r.obj_type, r.boundary_obj, r.meter_cls, r.meter_device, m.device_name as meter_device_name
        from adm_meter_boundary_rel r
    </sql>

    <select id="selectMeterDevices" resultType="java.lang.String">
        SELECT DISTINCT r.meter_device
        FROM adm_meter_boundary_rel r
        <where>
            <if test="boundaryObj != null  and boundaryObj != ''"> and r.boundary_obj = #{boundaryObj}</if>
            <if test="objType != null "> and r.obj_type = #{objType}</if>
            <if test="meterCls != null "> and r.meter_cls = #{meterCls}</if>
        </where>
    </select>

    <select id="selectByObj"  resultMap="relResult">
        <include refid="selectMeterBoundaryRelVo"/>
        left join adm_meter_device m on r.meter_device = m.device_code
        <where>
            <if test="boundaryObj != null  and boundaryObj != ''"> and r.boundary_obj = #{boundaryObj}</if>
            <if test="objType != null "> and r.obj_type = #{objType}</if>
            <if test="meterCls != null "> and r.meter_cls = #{meterCls}</if>
        </where>
    </select>

    <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into adm_meter_boundary_rel (obj_type, boundary_obj, meter_cls, meter_device)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.objType}, #{item.boundaryObj}, #{item.meterCls}, #{item.meterDevice})
        </foreach>
    </insert>

    <delete id="deleteByObj" >
        delete from adm_meter_boundary_rel where obj_type = #{objType} and boundary_obj = #{boundaryObj} and meter_cls = #{meterCls}
    </delete>
</mapper>