<?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.BaseLineMonitorMapper">

    <resultMap type="BaseLineMonitor" id="BaseLineMonitorResult">
        <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="buildId"    column="build_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="grade"    column="grade"    />
        <result property="correctValue"    column="correct_value"    />
        <result property="isAmmeter"    column="is_ammeter"    />
        <result property="lineName"    column="line_name"    />
        <result property="lineLength"    column="line_length"    />
        <result property="lineStatus"    column="line_status"    />
        <result property="powerCapacity"    column="power_capacity"    />
        <result property="waterCapacity"    column="water_capacity"    />
        <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"    />
    </resultMap>

    <sql id="selectBaseLineMonitorVo">
        select objid, parent_id, monitor_id, monitor_name, monitor_addr, monitor_type, monitor_status, collect_device_id, build_id, ancestors, grade, correct_value, is_ammeter, line_name, line_length, line_status, power_capacity, water_capacity, dept_id, user_id, create_by, create_time, update_by, update_time from base_line_monitor
    </sql>

    <select id="selectBaseLineMonitorList" parameterType="BaseLineMonitor" resultMap="BaseLineMonitorResult">
        <include refid="selectBaseLineMonitorVo"/>
        <where>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <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="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="buildId != null  and buildId != ''"> and build_id = #{buildId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="grade != null "> and grade = #{grade}</if>
            <if test="correctValue != null "> and correct_value = #{correctValue}</if>
            <if test="isAmmeter != null  and isAmmeter != ''"> and is_ammeter = #{isAmmeter}</if>
            <if test="lineName != null  and lineName != ''"> and line_name like concat('%', #{lineName}, '%')</if>
            <if test="lineLength != null "> and line_length = #{lineLength}</if>
            <if test="lineStatus != null "> and line_status = #{lineStatus}</if>
            <if test="powerCapacity != null "> and power_capacity = #{powerCapacity}</if>
            <if test="waterCapacity != null "> and water_capacity = #{waterCapacity}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
    </select>

    <select id="selectBaseLineMonitorByObjid" parameterType="Long" resultMap="BaseLineMonitorResult">
        <include refid="selectBaseLineMonitorVo"/>
        where objid = #{objid}
    </select>
    <select id="selectBaseLineMonitorByMonitorId" resultMap="BaseLineMonitorResult">
        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.build_id, t.ancestors, t.grade, t.correct_value, t.is_ammeter, t.line_name, t.line_length, t.line_status, t.power_capacity, t.water_capacity, t.dept_id, t.user_id, t.create_by, t.create_time, t.update_by, t.update_time
        from base_line_monitor t
        where t.monitor_id = #{monitor_id}
    </select>
    <insert id="insertBaseLineMonitor" parameterType="BaseLineMonitor" useGeneratedKeys="true" keyProperty="objid">
        insert into base_line_monitor
        <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="buildId != null">build_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="grade != null">grade,</if>
            <if test="correctValue != null">correct_value,</if>
            <if test="isAmmeter != null">is_ammeter,</if>
            <if test="lineName != null">line_name,</if>
            <if test="lineLength != null">line_length,</if>
            <if test="lineStatus != null">line_status,</if>
            <if test="powerCapacity != null">power_capacity,</if>
            <if test="waterCapacity != null">water_capacity,</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="buildId != null">#{buildId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="grade != null">#{grade},</if>
            <if test="correctValue != null">#{correctValue},</if>
            <if test="isAmmeter != null">#{isAmmeter},</if>
            <if test="lineName != null">#{lineName},</if>
            <if test="lineLength != null">#{lineLength},</if>
            <if test="lineStatus != null">#{lineStatus},</if>
            <if test="powerCapacity != null">#{powerCapacity},</if>
            <if test="waterCapacity != null">#{waterCapacity},</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="updateBaseLineMonitor" parameterType="BaseLineMonitor">
        update base_line_monitor
        <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="buildId != null">build_id = #{buildId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="grade != null">grade = #{grade},</if>
            <if test="correctValue != null">correct_value = #{correctValue},</if>
            <if test="isAmmeter != null">is_ammeter = #{isAmmeter},</if>
            <if test="lineName != null">line_name = #{lineName},</if>
            <if test="lineLength != null">line_length = #{lineLength},</if>
            <if test="lineStatus != null">line_status = #{lineStatus},</if>
            <if test="powerCapacity != null">power_capacity = #{powerCapacity},</if>
            <if test="waterCapacity != null">water_capacity = #{waterCapacity},</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="deleteBaseLineMonitorByObjid" parameterType="Long">
        delete from base_line_monitor where objid = #{objid}
    </delete>

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

    <select id="selectLevelLineMonitor"  parameterType="Long" resultType="com.hw.ems.report.domain.ExportReport">
        select m1.monitor_id   oneMonitorId,
               m1.monitor_name oneMonitorName,
               m2.monitor_id   twoMonitorId,
               m2.monitor_name twoMonitorName,
               m3.monitor_id threeMonitorId,
               m3.monitor_name threeMonitorName,
               m4.monitor_id fourMonitorId,
               m4.monitor_name fourMonitorName,
               m5.monitor_id fiveMonitorId,
               m5.monitor_name fiveMonitorName
        from base_line_monitor m1
                 left join base_line_monitor m2 on m2.parent_id = m1.objid
                 left join base_line_monitor m3 on m3.parent_id = m2.objid
                 left join base_line_monitor m4 on m4.parent_id = m3.objid
                 left join base_line_monitor m5 on m5.parent_id = m4.objid
        where m1.grade = 1
        <if test="monitorType != null "> and m1.monitor_type = #{monitorType}</if>
        order by oneMonitorName, twoMonitorName, threeMonitorName,fourMonitorName,fiveMonitorName
    </select>
</mapper>
