<?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.distribution.PwMonitorMapper">

    <!--获取配网监测点信息详情-->
    <select id="getPwMonitorDataList" resultType="com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO">
        SELECT DISTINCT
        monitor.Org_Id AS orgId,
        monitor.Org_Name AS orgName,
        monitor.`Powerr_Id` AS powerId,
        monitor.`Powerr_Name` AS powerName,
        monitor.id AS monitorId,
        monitor.`Name` AS monitorName,
        monitor.Line_Id,
        monitor.Line_Name,
        pdm.Monitor_Sort AS monitorSort,
        pdm.Voltage_Level AS voltageLevel,
        pdm.If_Power_User AS if_powerUser,
        pdm.Monitor_State AS monitorState,
        pdm.Created_Date AS createdDate,
        pdm.STATUS AS STATUS,
        pdm.Terminal_Id AS terminalId,
        pdm.Terminal_Wiring_Method AS terminalWiringMethod,
        pdm.Create_By AS createBy,
        pdm.Create_Time AS createTime,
        pdm.Update_By AS updateBy,
        pdm.Update_Time AS updateTime
        FROM
        ((
        SELECT
        pm.id,
        pm.`Name`,
        pm.`Status`,
        pm.Org_Id,
        pm.Org_Name,
        pm.Powerr_Id,
        pm.Powerr_Name,
        pm.Line_Id,
        pm.Line_Name
        FROM
        pms_monitor AS pm
        <where>
            <if test="monitorTag!=null and monitorTag.size()!=0">
                AND pm.Monitor_Tag IN
                <foreach collection="monitorTag" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>

        ) UNION ALL
        (
        SELECT
        ppd.id,
        ppd.`Name`,
        ppd.`Status`,
        ppd.Org_Id,
        ppd.Org_Name,
        ppd.Power_Station_Id,
        ppd.Powerr_Name,
        ppd.Line_Id,
        ppd.Line_Name
        FROM
        pms_power_distributionarea AS ppd
        ) UNION ALL
        (
        SELECT
        ppc.id,
        ppc.`Name`,
        ppc.`Status`,
        ppc.Org_Id,
        ppc.Org_Name,
        ppc.Power_Station_Id,
        ps.Power_Name,
        ppc.Line_Id,
        0
        FROM
        pms_power_client AS ppc
        INNER JOIN pms_statation_stat ps ON ps.Power_Id = ppc.Power_Station_Id
        AND ppc.Org_Id = ps.Org_Id
        ) UNION ALL
        (
        SELECT
        ppgu.id,
        ppgu.`Name`,
        ppgu.`Status`,
        ppgu.Org_Id,
        ppgu.Org_Name,
        ppgu.Power_Station_Id,
        pss.Power_Name,
        ppgu.Line_Id,
        0
        FROM
        pms_power_generation_user AS ppgu
        INNER JOIN pms_statation_stat AS pss ON pss.Power_Id = ppgu.Power_Station_Id
        AND ppgu.Org_Id = pss.Org_Id
        )) AS monitor
        INNER JOIN pms_distribution_monitor AS pdm ON monitor.id = pdm.Monitor_Id
        WHERE
        monitor.`Status` = 1
        AND pdm.`Status` = 1
        AND monitor.Org_Id IN
        <foreach collection="deptIdList" item="orgId" open="(" close=")" separator=",">
            #{orgId}
        </foreach>
        <if test="pwPmsMonitorParam.voltageLevels!=null and pwPmsMonitorParam.voltageLevels.size()!=0">
            AND pdm.Voltage_Level IN
            <foreach collection="pwPmsMonitorParam.voltageLevels" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.powerId!=null and pwPmsMonitorParam.powerId.size()!=0">
            AND monitor.Powerr_Id IN
            <foreach collection="pwPmsMonitorParam.powerId" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.monitorSort!=null and pwPmsMonitorParam.monitorSort.size()!=0">
            AND pdm.Monitor_Sort IN
            <foreach collection="pwPmsMonitorParam.monitorSort" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.monitorState!=null and pwPmsMonitorParam.monitorState.size()!=0">
            AND pdm.Monitor_State IN
            <foreach collection="pwPmsMonitorParam.monitorState" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.ifPowerUser != null and pwPmsMonitorParam.ifPowerUser != ''">
            AND pdm.If_Power_User = #{pwPmsMonitorParam.ifPowerUser}
        </if>
        <if test="pwPmsMonitorParam.monitorName !=null and pwPmsMonitorParam.monitorName != ''">
            AND monitor.`Name` LIKE CONCAT('%',#{pwPmsMonitorParam.monitorName},'%')
        </if>
    </select>
    <select id="getPwPhotovoltaicMonitorList" resultType="com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO">
        SELECT DISTINCT
        monitor.Org_Id AS orgId,
        monitor.Org_Name AS orgName,
        monitor.`Powerr_Id` AS powerId,
        monitor.`Powerr_Name` AS powerName,
        monitor.id AS monitorId,
        monitor.`Name` AS monitorName,
        monitor.Line_Id,
        monitor.Line_Name,
        pdm.Monitor_Sort AS monitorSort,
        pdm.Voltage_Level AS voltageLevel,
        pdm.If_Power_User AS if_powerUser,
        pdm.Monitor_State AS monitorState,
        pdm.Created_Date AS createdDate,
        pdm.STATUS AS STATUS,
        pdm.Create_By AS createBy,
        pdm.Create_Time AS createTime,
        pdm.Update_By AS updateBy,
        pdm.Update_Time AS updateTime
        FROM
        ((
        SELECT
        pm.id,
        pm.`Name`,
        pm.`Status`,
        pm.Org_Id,
        pm.Org_Name,
        pm.Powerr_Id,
        pm.Powerr_Name,
        pm.Line_Id,
        pm.Line_Name
        FROM
        pms_monitor AS pm
        ) UNION ALL
        (
        SELECT
        ppd.id,
        ppd.`Name`,
        ppd.`Status`,
        ppd.Org_Id,
        ppd.Org_Name,
        ppd.Power_Station_Id,
        ppd.Powerr_Name,
        ppd.Line_Id,
        ppd.Line_Name
        FROM
        pms_power_distributionarea AS ppd
        )
        ) AS monitor
        INNER JOIN pms_distribution_monitor AS pdm ON monitor.id = pdm.Monitor_Id
        WHERE
        monitor.`Status` = 1
        AND pdm.`Status` = 1
        AND monitor.Org_Id IN
        <foreach collection="deptIdList" item="orgId" open="(" close=")" separator=",">
            #{orgId}
        </foreach>
        <if test="pwPmsMonitorParam.voltageLevels!=null and pwPmsMonitorParam.voltageLevels.size()!=0">
            AND pdm.Voltage_Level IN
            <foreach collection="pwPmsMonitorParam.voltageLevels" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.powerId!=null and pwPmsMonitorParam.powerId.size()!=0">
            AND monitor.Powerr_Id IN
            <foreach collection="pwPmsMonitorParam.powerId" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.monitorSort!=null and pwPmsMonitorParam.monitorSort.size()!=0">
            AND pdm.Monitor_Sort IN
            <foreach collection="pwPmsMonitorParam.monitorSort" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.monitorState!=null and pwPmsMonitorParam.monitorState.size()!=0">
            AND pdm.Monitor_State IN
            <foreach collection="pwPmsMonitorParam.monitorState" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pwPmsMonitorParam.ifPowerUser != null and pwPmsMonitorParam.ifPowerUser != ''">
            AND pdm.If_Power_User = #{pwPmsMonitorParam.ifPowerUser}
        </if>
        <if test="pwPmsMonitorParam.monitorName !=null and pwPmsMonitorParam.monitorName != ''">
            AND monitor.`Name` LIKE CONCAT('%',#{pwPmsMonitorParam.monitorName},'%')
        </if>
    </select>

</mapper>
