<?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.base.mapper.EmsBaseMonitorInfoMapper">

    <resultMap type="EmsBaseMonitorInfo" id="EmsBaseMonitorInfoResult">
        <result property="objId"    column="obj_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="monitorCode"    column="monitor_code"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="monitorAddr"    column="monitor_addr"    />
        <result property="monitorType"    column="monitor_type"    />
        <result property="monitorStatus"    column="monitor_status"    />
        <result property="collectDeviceId"    column="collect_device_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="grade"    column="grade"    />
        <result property="meterTypeId"    column="meter_type_id"    />
        <result property="correctValue"    column="correct_value"    />
        <result property="pt"    column="pt"    />
        <result property="ct"    column="ct"    />
        <result property="isAmmeter"    column="is_ammeter"    />
        <result property="isKeyMonitor"    column="is_key_monitor"    />
        <result property="isCircuit"    column="is_circuit"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="publicShareType"    column="public_share_type"    />
        <result property="monitorHierarchy"    column="monitor_hierarchy"    />
    </resultMap>

    <sql id="selectEmsBaseMonitorInfoVo">
        select obj_id, parent_id, monitor_code, monitor_name, monitor_addr, monitor_type, monitor_status, collect_device_id, ancestors, grade, meter_type_id, correct_value, pt, ct, is_ammeter, is_key_monitor, is_circuit, create_by, create_time, update_by, update_time, public_share_type, monitor_hierarchy from ems_base_monitor_info
    </sql>

    <select id="selectEmsBaseMonitorInfoList" parameterType="EmsBaseMonitorInfo" resultMap="EmsBaseMonitorInfoResult">
        <include refid="selectEmsBaseMonitorInfoVo"/>
        <where>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="monitorCode != null  and monitorCode != ''"> and monitor_code = #{monitorCode}</if>
            <if test="monitorName != null  and monitorName != ''"> and monitor_name like concat('%', #{monitorName}, '%')</if>
            <if test="monitorTypeList != null  and monitorTypeList.size > 0">
                and monitor_type IN
                <foreach item="monitorId" collection="monitorTypeList" open="(" separator="," close=")">
                    #{monitorId}
                </foreach>
            </if>
            <if test="monitorAddr != null  and monitorAddr != ''"> and monitor_addr = #{monitorAddr}</if>
            <if test="monitorType != null "> and monitor_type = #{monitorType}</if>
            <if test="monitorStatus != null "> and monitor_status = #{monitorStatus}</if>
            <if test="collectDeviceId != null  and collectDeviceId != ''"> and collect_device_id = #{collectDeviceId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="grade != null "> and grade = #{grade}</if>
            <if test="meterTypeId != null  and meterTypeId != ''"> and meter_type_id = #{meterTypeId}</if>
            <if test="correctValue != null "> and correct_value = #{correctValue}</if>
            <if test="pt != null "> and pt = #{pt}</if>
            <if test="ct != null "> and ct = #{ct}</if>
            <if test="isAmmeter != null  and isAmmeter != ''"> and is_ammeter = #{isAmmeter}</if>
            <if test="isKeyMonitor != null "> and is_key_monitor = #{isKeyMonitor}</if>
            <if test="isCircuit != null "> and is_circuit = #{isCircuit}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="updateBy != null  and updateBy != ''"> and update_by = #{updateBy}</if>
            <if test="updateTime != null "> and update_time = #{updateTime}</if>
            <if test="publicShareType != null "> and public_share_type = #{publicShareType}</if>
            <if test="monitorHierarchy != null "> and monitor_hierarchy = #{monitorHierarchy}</if>
        </where>
    </select>

    <select id="selectEmsBaseMonitorInfoByObjId" parameterType="Long" resultMap="EmsBaseMonitorInfoResult">
        <include refid="selectEmsBaseMonitorInfoVo"/>
        where obj_id = #{objId}
    </select>

    <select id="selectEmsBaseMonitorTypeByMonitorCode" parameterType="String">
        select monitor_type
        from ems_base_monitor_info
        where monitor_code = #{monitorType}
    </select>


    <insert id="insertEmsBaseMonitorInfo" parameterType="EmsBaseMonitorInfo" useGeneratedKeys="true" keyProperty="objId">
        insert into ems_base_monitor_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="monitorCode != null and monitorCode != ''">monitor_code,</if>
            <if test="monitorName != null">monitor_name,</if>
            <if test="monitorAddr != null">monitor_addr,</if>
            <if test="monitorType != null">monitor_type,</if>
            <if test="monitorStatus != null">monitor_status,</if>
            <if test="collectDeviceId != null">collect_device_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="grade != null">grade,</if>
            <if test="meterTypeId != null">meter_type_id,</if>
            <if test="correctValue != null">correct_value,</if>
            <if test="pt != null">pt,</if>
            <if test="ct != null">ct,</if>
            <if test="isAmmeter != null">is_ammeter,</if>
            <if test="isKeyMonitor != null">is_key_monitor,</if>
            <if test="isCircuit != null">is_circuit,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="publicShareType != null">public_share_type,</if>
            <if test="monitorHierarchy != null">monitor_hierarchy,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="monitorCode != null and monitorCode != ''">#{monitorCode},</if>
            <if test="monitorName != null">#{monitorName},</if>
            <if test="monitorAddr != null">#{monitorAddr},</if>
            <if test="monitorType != null">#{monitorType},</if>
            <if test="monitorStatus != null">#{monitorStatus},</if>
            <if test="collectDeviceId != null">#{collectDeviceId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="grade != null">#{grade},</if>
            <if test="meterTypeId != null">#{meterTypeId},</if>
            <if test="correctValue != null">#{correctValue},</if>
            <if test="pt != null">#{pt},</if>
            <if test="ct != null">#{ct},</if>
            <if test="isAmmeter != null">#{isAmmeter},</if>
            <if test="isKeyMonitor != null">#{isKeyMonitor},</if>
            <if test="isCircuit != null">#{isCircuit},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="publicShareType != null">#{publicShareType},</if>
            <if test="monitorHierarchy != null">#{monitorHierarchy},</if>
         </trim>
    </insert>

    <update id="updateEmsBaseMonitorInfo" parameterType="EmsBaseMonitorInfo">
        update ems_base_monitor_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="monitorCode != null and monitorCode != ''">monitor_code = #{monitorCode},</if>
            <if test="monitorName != null">monitor_name = #{monitorName},</if>
            <if test="monitorAddr != null">monitor_addr = #{monitorAddr},</if>
            <if test="monitorType != null">monitor_type = #{monitorType},</if>
            <if test="monitorStatus != null">monitor_status = #{monitorStatus},</if>
            <if test="collectDeviceId != null">collect_device_id = #{collectDeviceId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="grade != null">grade = #{grade},</if>
            <if test="meterTypeId != null">meter_type_id = #{meterTypeId},</if>
            <if test="correctValue != null">correct_value = #{correctValue},</if>
            <if test="pt != null">pt = #{pt},</if>
            <if test="ct != null">ct = #{ct},</if>
            <if test="isAmmeter != null">is_ammeter = #{isAmmeter},</if>
            <if test="isKeyMonitor != null">is_key_monitor = #{isKeyMonitor},</if>
            <if test="isCircuit != null">is_circuit = #{isCircuit},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="publicShareType != null">public_share_type = #{publicShareType},</if>
            <if test="monitorHierarchy != null">monitor_hierarchy = #{monitorHierarchy},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteEmsBaseMonitorInfoByObjId" parameterType="Long">
        delete from ems_base_monitor_info where obj_id = #{objId}
    </delete>

    <delete id="deleteEmsBaseMonitorInfoByObjIds" parameterType="String">
        delete from ems_base_monitor_info where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>

    <select id="getMonitorCountByEnergyType" parameterType="String" resultType="java.lang.Integer">
            select count(1) from ems_base_monitor_info where monitor_type = #{energyType}
        </select>

</mapper>
