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

    <!--获取母线信息-->
    <select id="getGeneratrixInfo" resultType="com.njcn.device.pms.pojo.dto.PmsGeneratrixDTO">
        SELECT
        pm.Org_Id AS orgId,
        pm.Org_Name AS orgName,
        pm.Powerr_Id AS powerId,
        pm.Powerr_Name AS powerName,
        pm.org_name as orgName,
        pg.id AS generatrixId,
        pm.Line_Name AS generatrixName,
        pm.Id AS monitorId,
        pm.`Name` AS monitorName,
        pg.Scale AS generatrixVoltageLevel
        FROM
        pms_generatrix_wire AS pg
        LEFT JOIN pms_monitor AS pm ON pm.line_id = pg.id
        WHERE
        pm.`Status` = 1
        AND pg.`Status` =1
        AND pg.id IN
        <foreach collection="pmsGeneratrixParam.generatrixIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="pmsGeneratrixParam.generatrixVoltageLevel != null and pmsGeneratrixParam.generatrixVoltageLevel.size() != 0">
            AND pg.Scale IN
            <foreach collection="pmsGeneratrixParam.generatrixVoltageLevel" item="item" open="(" close=")"
                     separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pmsGeneratrixParam.generatrixName != null and pmsGeneratrixParam.generatrixName != ''">
            AND pm.line_nmae LIKE CONCAT('%',#{pmsGeneratrixParam.generatrixName},'%')
        </if>
    </select>

    <!--获取母线与电站关联信息-->
    <select id="getGeneratrixByCondition"
            resultType="com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationSonDTO">
        SELECT
        pss.Org_Id  AS orgid,
        pss.Org_Name AS orgName,
        pg.id AS busbarId,
        pg.name AS busbarName,
        pg.Scale AS generatrixVoltageLevel,
        pss.Power_Id AS subId,
        pss.Power_Name AS subName,
        pss.Voltage_Level AS voltageLevel

        FROM
        pms_generatrix_wire AS pg
        LEFT JOIN pms_statation_stat AS pss ON pg.station_id = pss.Power_Id
        WHERE
        pg.`Status` = 1
        AND pss.`Status` = 1
        <if test="param.powerStationId != null and param.powerStationId !=''">
                    AND pss.Power_Id = #{param.powerStationId}
        </if>
        <if test="param.powerStationVoltageLevel != null and param.powerStationVoltageLevel !=''  ">
               AND pss.Voltage_Level  = #{param.powerStationVoltageLevel}
        </if>
            AND pss.org_id  IN
            <foreach collection="orgList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>

    </select>

     <select id="getGeneratrixAndPowerStationInfo"
            resultType="com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationDTO">
        SELECT
        pg.id AS generatrixId,
        pg.name AS generatrixName,
        pg.Scale AS generatrixVoltageLevel,
        pss.Power_Id AS powerId,
        pss.Power_Name AS powerName,
        pss.org_name,
        pss.Voltage_Level AS powerVoltageLevel
        FROM
        pms_generatrix_wire AS pg
        LEFT JOIN pms_statation_stat AS pss ON pg.station_id = pss.Power_Id
        WHERE
        pg.`Status` = 1
        AND pss.`Status` = 1
        <if test="param.generatrixIds != null and param.generatrixIds.size() >0 ">
            AND pg.id IN
            <foreach collection="param.generatrixIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.powerStationIds != null and param.powerStationIds.size() >0 ">
            AND pss.Power_Id IN
            <foreach collection="param.powerStationIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.generatrixVoltageLevel != null and param.generatrixVoltageLevel.size() >0 ">
            AND pg.Scale  IN
            <foreach collection="param.generatrixVoltageLevel" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.powerStationVoltageLevel != null and param.powerStationVoltageLevel.size() >0 ">
            AND pss.Voltage_Level  IN
            <foreach collection="param.powerStationVoltageLevel" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.generatrixName != null and param.generatrixName !='' ">
            AND pg.name LIKE CONCAT('%',#{param.generatrixName},'%')
        </if>
        <if test="param.powerStationName != null and param.powerStationName !='' ">
            AND pss.stationName LIKE CONCAT('%',#{param.powerStationName},'%')
        </if>
    </select>


    <select id="getGeneratrixWireList" resultType="GeneratrixWire">
        select pms_generatrix_wire.*,pms_statation_stat.Org_Id
        from pms_generatrix_wire pms_generatrix_wire
        inner join pms_statation_stat pms_statation_stat
        on pms_generatrix_wire.Station_Id = pms_statation_stat.Power_Id
        where pms_generatrix_wire.status = 1
    </select>
    <select id="getWireAndMonitor" resultType="com.njcn.device.pms.pojo.po.GeneratrixWire">
        SELECT
            monitor.id as id,
            monitor.Org_Id as orgId
        FROM
            pms_generatrix_wire wire
        INNER JOIN pms_statation_stat stat ON wire.Station_Id = stat.Power_Id
        INNER JOIN pms_monitor monitor ON monitor.Line_Id = wire.id
        WHERE
            wire.STATUS = 1
    </select>


</mapper>
