<?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.njcn.device.pms.mapper.majornetwork.MonitorMapper">

    <!--根据组织ID以及其查询条件筛选出台账信息-->
    <select id="getMonitorByCondition" resultType="com.njcn.device.pms.pojo.dto.PmsMonitorBaseDTO">
        SELECT
            pms_monitor.id monitorId,
            pms_monitor.Operation_Id,
            pms_monitor.Powerr_id,
            pms_monitor.Terminal_Id,
            pms_monitor.line_Name,
            pms_monitor.line_Id
        FROM
            pms_monitor pms_monitor LEFT JOIN pms_terminal pms_terminal ON pms_monitor.Terminal_Id = pms_terminal.id
        WHERE
            pms_monitor.Org_Id IN
        <foreach collection="orgIdList" item="orgId" open="(" close=")" separator=",">
            #{orgId}
        </foreach>
        <if test="pmsDeviceInfoParam.voltageLevel!=null and pmsDeviceInfoParam.voltageLevel.size()!=0">
            AND pms_monitor.Voltage_Level IN
            <foreach collection="pmsDeviceInfoParam.voltageLevel" item="item" open="(" close=")" separator=",">
                #{item.id}
            </foreach>
        </if>
        <if test="pmsDeviceInfoParam.monitorObjectType!=null and pmsDeviceInfoParam.monitorObjectType.size()!=0">
            AND pms_monitor.Monitor_Object_Type IN
            <foreach collection="pmsDeviceInfoParam.monitorObjectType" item="item" open="(" close=")" separator=",">
                #{item.id}
            </foreach>
        </if>
        <if test="pmsDeviceInfoParam.monitorState!=null and pmsDeviceInfoParam.monitorState.size()!=0">
            AND pms_monitor.Monitor_State IN
            <foreach collection="pmsDeviceInfoParam.monitorState" item="item" open="(" close=")" separator=",">
                #{item.id}
            </foreach>
        </if>
        <if test="pmsDeviceInfoParam.manufacturer!=null and pmsDeviceInfoParam.manufacturer.size()!=0">
            AND pms_terminal.Manufacture IN
            <foreach collection="pmsDeviceInfoParam.manufacturer" item="item" open="(" close=")" separator=",">
                #{item.id}
            </foreach>
        </if>
    </select>


    <!--根据组织ID以及其查询条件筛选出变电站信息-->
    <select id="getPmsPowerrInfoWithInOrg" resultType="com.njcn.common.pojo.dto.SimpleDTO">
        SELECT
            pms_monitor.Powerr_id id,
            pms_monitor.Powerr_Name name
        FROM
            pms_monitor pms_monitor LEFT JOIN pms_terminal pms_terminal ON pms_monitor.Terminal_Id = pms_terminal.id
        WHERE
            pms_monitor.Org_Id IN
            <foreach collection="orgIdList" item="orgId" open="(" close=")" separator=",">
                #{orgId}
            </foreach>
            <if test="pmsDeviceInfoParam.voltageLevel!=null and pmsDeviceInfoParam.voltageLevel.size()!=0">
                AND pms_monitor.Voltage_Level IN
                <foreach collection="pmsDeviceInfoParam.voltageLevel" item="item" open="(" close=")" separator=",">
                    #{item.id}
                </foreach>
            </if>
            <if test="pmsDeviceInfoParam.monitorObjectType!=null and pmsDeviceInfoParam.monitorObjectType.size()!=0">
                AND pms_monitor.Monitor_Object_Type IN
                <foreach collection="pmsDeviceInfoParam.monitorObjectType" item="item" open="(" close=")" separator=",">
                    #{item.id}
                </foreach>
            </if>
            <if test="pmsDeviceInfoParam.monitorState!=null and pmsDeviceInfoParam.monitorState.size()!=0">
                AND pms_monitor.Monitor_State IN
                <foreach collection="pmsDeviceInfoParam.monitorState" item="item" open="(" close=")" separator=",">
                    #{item.id}
                </foreach>
            </if>
            <if test="pmsDeviceInfoParam.manufacturer!=null and pmsDeviceInfoParam.manufacturer.size()!=0">
                AND pms_terminal.Manufacture IN
                <foreach collection="pmsDeviceInfoParam.manufacturer" item="item" open="(" close=")" separator=",">
                    #{item.id}
                </foreach>
            </if>
    </select>


    <!--获取指定的监测点信息-->
    <select id="getMonitorInfo" resultType="com.njcn.device.pms.pojo.dto.PmsMonitorInfoDTO">
        SELECT
        pm.Org_Id AS orgId,
        pm.Org_Name AS orgName,
        pm.Powerr_Id AS powerId,
        pm.Powerr_Name AS powerName,
        pm.Id AS monitorId,
        pm.`Name` AS monitorName,
        pm.Voltage_Level AS monitorVoltageLevel,
        pm.Monitor_Type AS monitorType,
        pm.Monitor_State AS monitorState
        FROM
        pms_monitor AS pm
        WHERE
        pm.`Status` = 1
        AND pm.Id IN
        <foreach collection="pmsMonitorInfoParam.monitorIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="pmsMonitorInfoParam.monitorVoltageLevel != null and pmsMonitorInfoParam.monitorVoltageLevel.size() != 0">
            AND pm.Voltage_Level IN
            <foreach collection="pmsMonitorInfoParam.monitorVoltageLevel" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pmsMonitorInfoParam.monitorType != null and pmsMonitorInfoParam.monitorType.size() != 0">
            AND pm.Monitor_Type IN
            <foreach collection="pmsMonitorInfoParam.monitorType" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pmsMonitorInfoParam.monitorState != null and pmsMonitorInfoParam.monitorState.size() != 0">
            AND pm.Monitor_State IN
            <foreach collection="pmsMonitorInfoParam.monitorState" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pmsMonitorInfoParam.monitorName != null and pmsMonitorInfoParam.monitorName != ''">
            AND pm.`Name` LIKE CONCAT('%',#{pmsMonitorInfoParam.monitorName},'%')
        </if>
    </select>
    <select id="getMonitorTerminal" resultType="com.njcn.device.pms.pojo.vo.MonitorVO">
        SELECT
            pm.Id,
            pm.NAME,
            pm.Terminal_Wiring_Method,
            pt.Ip,
            pt.Port,
            pm.Pt1,
            pm.Pt2,
            pm.Ct1,
            pm.Ct2
        FROM
            pms_monitor pm
        INNER JOIN pms_terminal pt on pt.Id=pm.Terminal_Id
        where pm.id = #{id}

    </select>
    <select id="getPwMonitorTerminal" resultType="com.njcn.device.pms.pojo.vo.MonitorVO">
       SELECT
            pm.Monitor_Id,
            pm.Terminal_Wiring_Method,
            pt.Ip,
            pt.PORT,
            pm.Pt1,
            pm.Pt2,
            pm.Ct1,
            pm.Ct2
       FROM
            pms_distribution_monitor pm
	   INNER JOIN pms_terminal pt ON pt.Id = pm.Terminal_Id
       where pm.Monitor_Id = #{id}
    </select>
    <select id="selectLineDetail" resultType="com.njcn.device.biz.pojo.dto.LineDTO">
        SELECT
            pm.Id as lineId,
            pm.Terminal_Id as devId,
            pm.Statistical_Interval as timeInterval,
            pm.Voltage_Level as voltageLevel,
            pm.Monitor_State as runFlag,
            pm.Terminal_Wiring_Method as ptType,
            pm.PT_Phase_Type as ptPhaseType
        FROM
            pms_monitor pm
        WHERE
            pm.id = #{id}
    </select>
    <select id="selectLineDetailBatch" resultType="com.njcn.device.biz.pojo.dto.LineDTO">
        SELECT
            pm.Id as lineId,
            pm.Terminal_Id as devId,
            pm.Statistical_Interval as timeInterval,
            pm.Voltage_Level as voltageLevel,
            pm.Monitor_State as runFlag,
            pm.Terminal_Wiring_Method as ptType,
            pm.PT_Phase_Type as ptPhaseType
        FROM
            pms_monitor pm
        WHERE
            pm.id in
        <foreach collection="ids" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
    </select>
    <select id="selectMonitorList" resultType="com.njcn.device.pms.pojo.po.Monitor">
        SELECT
            pm.Id,
            pm.Org_Id,
            pm.Line_Id,
            pm.Voltage_Level,
            pm.Monitor_Tag,
            IFNULL(ps.capacity,0) AS standShortCapacity
        FROM
            pms_monitor pm
        LEFT JOIN pms_special_monitor ps ON ps.id = pm.id
        <where>
            and Status=1
            <if test="param.startTime != null and param.startTime != ''">
                AND DATE_FORMAT(Create_Time, '%Y-%m-%d') &gt;= DATE_FORMAT(#{param.startTime}, '%Y-%m-%d')
            </if>
            <if test="param.endTime != null and param.endTime != ''">
                AND DATE_FORMAT(Create_Time, '%Y-%m-%d') &lt;= DATE_FORMAT(#{param.endTime}, '%Y-%m-%d')
            </if>
        </where>

    </select>


    <select id="getCustomDetailByLineId" resultType="map">
        SELECT
            monitor.id AS lineId,
            concat(statation.Power_Name,"_",wire.name,"_",monitor.name) as lineName,
            concat(monitor.pt1,concat(':',monitor.pt2)) as pt,
            concat(monitor.ct1,concat(':',monitor.ct2)) as ct,
            monitor.Power_Supply_Eq_Capacity as Dev_Capacity,
            monitor.Min_Short_Circuit_Capacity as Short_Capacity,
            monitor.User_Agreement_Capacity as Standard_Capacity,
            monitor.User_Agreement_Capacity as Deal_Capacity,
            terminal.Verification_Date AS This_Time_Check,
            dic.name AS voltageLevel
        FROM
            pms_monitor monitor
                left join pms_terminal terminal on monitor.Terminal_Id = terminal.id
                left join pms_statation_stat statation on monitor.Powerr_Id = statation.Power_Id
                left join pms_generatrix_wire wire on monitor.Line_Id = wire.id
                left join sys_dict_data dic on monitor.Voltage_Level = dic.id
        WHERE monitor.id = #{lineId}
    </select>

</mapper>
