<?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.hw.ems.base.mapper.BaseMonitorInfoMapper">

    <resultMap type="BaseMonitorInfo" id="BaseMonitorInfoResult">
        <result property="objid"    column="objid"    />
        <result property="parentId"    column="parent_id"    />
        <result property="monitorId"    column="monitor_id"    />
        <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="buildId"    column="build_id"    />
        <result property="subentryId"    column="subentry_id"    />
        <result property="businessId"    column="business_id"    />
        <result property="meterTypeId"    column="meter_type_id"    />
        <result property="meterId"    column="meter_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="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <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="parentName" column="parent_name" />
    </resultMap>

    <sql id="selectBaseMonitorInfoVo">
        select objid, parent_id, monitor_id, monitor_name, monitor_addr, monitor_type,
               monitor_status, collect_device_id, ancestors, grade, build_id, subentry_id,
               business_id, meter_type_id, meter_id, correct_value, pt, ct, is_ammeter,
               is_key_monitor, is_circuit, dept_id, user_id, create_by, create_time, update_by, update_time from base_monitor_info
    </sql>

    <select id="selectBaseMonitorInfoList" parameterType="BaseMonitorInfo" resultMap="BaseMonitorInfoResult">
        <include refid="selectBaseMonitorInfoVo"/>
        <where>
            <if test="monitorName != null  and monitorName != ''"> and monitor_name like concat('%', #{monitorName}, '%')</if>
            <if test="monitorAddr != null  and monitorAddr != ''"> and monitor_addr = #{monitorAddr}</if>
            <if test="monitorId != null  and monitorId != ''"> and monitor_id = #{monitorId}</if>
            <if test="parentId != null  and parentId != ''"> and parent_id = #{parentId}</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="buildId != null  and buildId != ''"> and build_id = #{buildId}</if>
            <if test="subentryId != null  and subentryId != ''"> and subentry_id = #{subentryId}</if>
            <if test="businessId != null  and businessId != ''"> and business_id = #{businessId}</if>
            <if test="meterTypeId != null  and meterTypeId != ''"> and meter_type_id = #{meterTypeId}</if>
            <if test="publicShareType != null "> and public_share_type = #{publicShareType}</if>
            <if test="meterId != null  and meterId != ''"> and meter_id = #{meterId}</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="isCircuit != null "> and is_circuit = #{isCircuit}</if>
        </where>
        order by parent_id,monitor_name
    </select>

    <select id="selectBaseMonitorInfoByObjid" parameterType="Long" resultMap="BaseMonitorInfoResult">
        select t.objid, t.parent_id, t.monitor_id, t.monitor_name, t.monitor_addr, t.monitor_type, t.monitor_status, t.collect_device_id, t.ancestors, t.grade, t.build_id, t.subentry_id, t.business_id, t.meter_type_id, t.meter_id, t.correct_value, t.pt, t.ct, t.is_ammeter, t.is_key_monitor, t.is_circuit, t.dept_id, t.user_id, t.create_by, t.create_time, t.update_by, t.update_time, p.monitor_name as parent_name
        from base_monitor_info t
                 left join base_monitor_info p on p.objid = t.parent_id
        where t.objid = #{objid}
    </select>

    <select id="checkMonitorIdLegal" parameterType="BaseMonitorInfo" resultMap="BaseMonitorInfoResult">
        select t.objid, t.parent_id, t.monitor_id, t.monitor_name, t.monitor_addr, t.monitor_type
        from base_monitor_info t
        <where>
            <if test="monitorId != null  and monitorId != ''"> and t.monitor_id like concat('%', #{monitorId}, '%')</if>
        </where>
    </select>

    <select id="selectChildrenMonitorById" parameterType="String"  resultMap="BaseMonitorInfoResult">
        select * from base_monitor_info where find_in_set(#{monitorId}, ancestors)
    </select>
    <select id="selectBaseMonitorInfoByMonitorId" parameterType="String"  resultMap="BaseMonitorInfoResult">
        select * from base_monitor_info where monitor_id = #{monitorId}
    </select>
    <update id="updateMonitorChildren" parameterType="java.util.List">
        update base_monitor_info set ancestors =
        <foreach collection="monitor" item="item" index="index"
                 separator=" " open="case monitor_id" close="end">
            when #{item.monitorId} then #{item.ancestors}
        </foreach>
        where monitor_id in
        <foreach collection="monitor" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.monitorId}
        </foreach>
    </update>
    <insert id="insertBaseMonitorInfo" parameterType="BaseMonitorInfo" useGeneratedKeys="true" keyProperty="objid">
        insert into base_monitor_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="monitorId != null">monitor_id,</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="buildId != null">build_id,</if>
            <if test="subentryId != null">subentry_id,</if>
            <if test="businessId != null">business_id,</if>
            <if test="meterTypeId != null">meter_type_id,</if>
            <if test="meterId != null">meter_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="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="monitorId != null">#{monitorId},</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="buildId != null">#{buildId},</if>
            <if test="subentryId != null">#{subentryId},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="meterTypeId != null">#{meterTypeId},</if>
            <if test="meterId != null">#{meterId},</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="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</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>
        </trim>
    </insert>

    <update id="updateBaseMonitorInfo" parameterType="BaseMonitorInfo">
        update base_monitor_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="monitorId != null">monitor_id = #{monitorId},</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="buildId != null">build_id = #{buildId},</if>
            <if test="subentryId != null">subentry_id = #{subentryId},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="meterTypeId != null">meter_type_id = #{meterTypeId},</if>
            <if test="meterId != null">meter_id = #{meterId},</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="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</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>
        </trim>
        where objid = #{objid}
    </update>

    <delete id="deleteBaseMonitorInfoByObjid" parameterType="Long">
        delete from base_monitor_info where objid = #{objid}
    </delete>

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

    <select id="selectMonitorList" resultType="BaseMonitorInfo">
        select objid,
        parent_id parentId,
        monitor_id monitorId,
        monitor_name monitorName
        from base_monitor_info
        where 1=1
        <if test="deptId != null and deptId != 0">
            AND dept_id = #{deptId}
        </if>
        <if test="parentId != null and parentId != 0">
            AND parent_id = #{parentId}
        </if>
        <if test="monitorName != null and monitorName != ''">
            AND monitor_name like concat('%', #{monitorName}, '%')
        </if>
        <if test="monitorType != null and monitorType != ''">
           and monitor_type = #{monitorType}
        </if>
        <!-- 数据范围过滤
        ${params.dataScope}-->
        order by parent_id<!--, d.order_num-->
    </select>

    <select id="monitorLedgerList" parameterType="BaseMonitorInfo" resultMap="BaseMonitorInfoResult">
        select objid,is_key_monitor parent_id, monitor_id, monitor_name, monitor_addr, monitor_type,
        monitor_status, collect_device_id, ancestors, grade, build_id, subentry_id,
        business_id, meter_type_id, meter_id, correct_value, pt, ct, is_ammeter,
        is_key_monitor, is_circuit, dept_id, user_id, create_by, create_time, update_by, update_time from base_monitor_info
        <where>
            <if test="monitorName != null  and monitorName != ''"> and monitor_name like concat('%', #{monitorName}, '%')</if>
            <if test="monitorAddr != null  and monitorAddr != ''"> and monitor_addr = #{monitorAddr}</if>
            <if test="monitorId != null  and monitorId != ''"> and monitor_id = #{monitorId}</if>
            <if test="parentId != null  and parentId != ''"> and parent_id = #{parentId}</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="buildId != null  and buildId != ''"> and build_id = #{buildId}</if>
            <if test="subentryId != null  and subentryId != ''"> and subentry_id = #{subentryId}</if>
            <if test="businessId != null  and businessId != ''"> and business_id = #{businessId}</if>
            <if test="meterTypeId != null  and meterTypeId != ''"> and meter_type_id = #{meterTypeId}</if>
            <if test="meterId != null  and meterId != ''"> and meter_id = #{meterId}</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="isCircuit != null "> and is_circuit = #{isCircuit}</if>
        </where>
        order by parent_id,monitor_name
    </select>
</mapper>
