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


    <select id="getIdByOrgId" resultType="com.njcn.device.pms.pojo.dto.PmsMonitorBaseDTO">
        SELECT
        t.Operation_Id operationId,t.Power_Station_Id powerrId,t.Line_Id lineId,t.id monitorId,b.Terminal_Id terminalId
        FROM
        (
        (
        SELECT id, Operation_Id, Power_Station_Id, Line_Id FROM
        pms_power_client WHERE org_id IN
        <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
         AND STATUS = 1 )
        UNION ALL
        (
        SELECT id, Operation_Id, Power_Station_Id, Line_Id
        FROM pms_power_generation_user WHERE org_id IN
        <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
         AND STATUS = 1 )
        UNION ALL
        ( SELECT id, Operation_Id, Power_Station_Id, Line_Id
        FROM pms_power_distributionarea WHERE org_id IN
        <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
         AND STATUS = 1 )
        UNION ALL
        ( SELECT id, Operation_Id, Powerr_Id, Line_Id FROM pms_monitor WHERE org_id IN
        <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
         AND STATUS = 1 )
        ) t
        INNER JOIN pms_distribution_monitor b ON t.id = b.Monitor_Id
    </select>


    <select id="getDoubleUserByDept" resultType="DoubleUserVO">
        (
        select yong.name,yong.id,0 as type from pms_power_client yong
        inner join pms_distribution_monitor dis on yong.id = dis.Monitor_Id and dis.If_Power_User = 0
        where yong.org_id = #{orgId}
        and dis.status = 1
        )
        UNION ALL
        (
        select fa.name,fa.id,1 as type from pms_power_generation_user fa
        inner join pms_distribution_monitor dis on fa.id = dis.Monitor_Id and dis.If_Power_User = 1
        where fa.org_id = #{orgId}
        and dis.status = 1
        )
    </select>

    <select id="getMonitorByOrg" resultType="Monitor">
        select a.* from pms_monitor a
        inner join pms_distribution_monitor b on a.id = b.Monitor_Id
        and b.Monitor_Sort = #{taiZhangParam.lineType}
        where a.org_id in
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="taiZhangParam.searchValue!='' and taiZhangParam.searchValue !=null">
            and a.name like concat('%',#{taiZhangParam.searchValue},'%')
        </if>
        <if test="taiZhangParam.monitorState!='' and taiZhangParam.monitorState !=null">
            and b.Monitor_State = #{taiZhangParam.monitorState}
        </if>
        and a.status = 1
    </select>

    <select id="getPowerDisAreaMonitorByOrg" resultType="PowerDistributionarea">
        select a.* from pms_power_distributionarea a
        inner join pms_distribution_monitor b on a.id = b.Monitor_Id
        and b.Monitor_Sort = #{taiZhangParam.lineType}
        where a.org_id in
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="taiZhangParam.searchValue!='' and taiZhangParam.searchValue !=null">
            and a.name like concat('%',#{taiZhangParam.searchValue},'%')
        </if>
        <if test="taiZhangParam.monitorState!='' and taiZhangParam.monitorState !=null">
            and b.monitor_state = #{taiZhangParam.monitorState}
        </if>
        and a.status = 1
    </select>


    <select id="getThreeMonitorByOrgIdYong" resultType="PmsPowerTreeMonitorVO">

        select yong.*,0 as type
        from pms_power_client yong
        inner join pms_distribution_monitor dis on yong.id = dis.Monitor_Id and dis.If_Power_User = 0
        where yong.org_id in
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="taiZhangParam.searchValue!='' and taiZhangParam.searchValue !=null">
            and yong.name like concat('%',#{taiZhangParam.searchValue},'%')
        </if>
        <if test="taiZhangParam.monitorState!='' and taiZhangParam.monitorState !=null">
            and dis.Monitor_State = #{taiZhangParam.monitorState}
        </if>
        and dis.status = 1
    </select>


    <select id="getThreeMonitorByOrgIdFa" resultType="PmsPowerTreeMonitorVO">
        select fa.*,1 as type
        from pms_power_generation_user fa
        inner join pms_distribution_monitor dis
        on fa.id = dis.Monitor_Id
        and dis.If_Power_User = 1
        where fa.org_id in
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="taiZhangParam.searchValue!='' and taiZhangParam.searchValue !=null">
            and fa.name like concat('%',#{taiZhangParam.searchValue},'%')
        </if>
        <if test="taiZhangParam.monitorState!='' and taiZhangParam.monitorState !=null">
            and dis.Monitor_State = #{taiZhangParam.monitorState}
        </if>
        and dis.status = 1
    </select>
    <select id="distributionMonitorListByCondition" resultType="com.njcn.device.pms.pojo.po.DistributionMonitor">
        SELECT
        *
        FROM
        pms_distribution_monitor a
        WHERE
        1 = 1
        and a.Monitor_State in (select id from sys_dict_data sdd where sdd.code='Run' )
        <if test="monitorSort!='' and monitorSort !=null">

            AND a.Monitor_Sort = #{monitorSort}
        </if>
        AND EXISTS (
        SELECT
        1
        FROM
        (
        SELECT
        id
        FROM
        pms_monitor b
        WHERE
        b.Org_Id IN
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        UNION
        SELECT
        id
        FROM
        pms_power_client c
        WHERE
        c.Org_Id IN
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        UNION
        SELECT
        id
        FROM
        pms_power_distributionarea d
        WHERE
        d.Org_Id IN
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        UNION
        SELECT
        id
        FROM
        pms_power_generation_user e
        WHERE
        e.Org_Id IN
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        ) temp
        WHERE
        a.Monitor_Id = temp.id
        )
    </select>
    <select id="statisticMonitor" resultType="com.njcn.device.pms.pojo.dto.PmsSimpleDTO">
        SELECT
        pm.Id,
        COUNT(pm.Id) AS statistics,
        pm.Voltage_Level AS name
        FROM pms_monitor pm
        WHERE pm.Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY
        pm.Id,
        pm.Voltage_Level
    </select>
    <select id="statisticTerminal" resultType="com.njcn.device.pms.pojo.dto.PmsSimpleDTO">
        SELECT
        COUNT(t.Terminal_Id) statistics,
        t.Voltage_Level name
        FROM
        ( SELECT
        DISTINCT pm.Terminal_Id,
        pss.Voltage_Level
        FROM pms_monitor pm
        INNER JOIN pms_terminal pt on pt.Id=pm.Terminal_Id
        INNER JOIN pms_statation_stat pss on pss.Power_Id=pt.Power_Station_Id
        WHERE pm.Terminal_Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND
        pm.Terminal_Id != ''
        ) t
        GROUP BY t.Voltage_Level
    </select>
    <select id="getOnlineRate" resultType="com.njcn.device.pms.pojo.dto.PmsSimpleDTO">
        SELECT
        pm.Voltage_Level name,
        IFNULL(SUM(rmded.dev_effective_rate),0) doubleValue
        FROM pms_monitor pm
        INNER JOIN r_mp_dev_evaluate_detail rmded ON pm.Terminal_Id = rmded.device_id
        <where>
            pm.Terminal_Id IN
            <foreach item="item" collection="list" separator="," open="(" close=")">
                #{item}
            </foreach>
            <if test="param.startTime != null and param.startTime != ''">
                AND DATE_FORMAT(data_date, '%Y-%m-%d') &gt;= DATE_FORMAT(#{param.startTime}, '%Y-%m-%d')
            </if>
            <if test="param.endTime != null and param.endTime != ''">
                AND DATE_FORMAT(data_date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{param.endTime}, '%Y-%m-%d')
            </if>
        </where>
        GROUP BY pm.Voltage_Level
    </select>
    <select id="getIntegrity" resultType="com.njcn.device.pms.pojo.dto.PmsSimpleDTO">
        SELECT
        pm.Voltage_Level name,
        IFNULL(SUM(romd.data_integrity_rate),0) doubleValue
        FROM pms_monitor pm
        INNER JOIN r_operating_monitor_d romd ON pm.Id = romd.measurement_point_id
        <where>
         pm.Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        <if test="param.startTime != null and param.startTime != ''">
            AND DATE_FORMAT(data_date, '%Y-%m-%d') &gt;= DATE_FORMAT(#{param.startTime}, '%Y-%m-%d')
        </if>
        <if test="param.endTime != null and param.endTime != ''">
            AND DATE_FORMAT(data_date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{param.endTime}, '%Y-%m-%d')
        </if>
        </where>
        GROUP BY pm.Voltage_Level
    </select>


    <select id="getDisMonitorAllList" resultType="com.njcn.device.pms.pojo.dto.PmsMonitorBaseDTO">
        SELECT
        t.Update_Time,t.org_id orgId,t.id monitorId,b.Terminal_Id terminalId,t.Voltage_Level voltageLevel,t.powerrId,t.Line_Id,t.lineType
        FROM
        (
        (
        SELECT Update_Time,id, org_id, 3 as lineType,Power_Station_Id as powerrId,Line_Id,Voltage_Level
        FROM pms_power_client
        where STATUS = 1 )
        UNION ALL
        (
        SELECT Update_Time,id, org_id, 3 as lineType,Power_Station_Id as powerrId,Line_Id,Voltage_Level
        FROM pms_power_generation_user
        WHERE
        STATUS = 1 )
        UNION ALL
        ( SELECT Update_Time,id, org_id, 2 as lineType,Power_Station_Id as powerrId,Line_Id,Voltage_Level
        FROM pms_power_distributionarea
        WHERE STATUS = 1 )
        UNION ALL
        ( SELECT Update_Time,id, org_id, 1 as lineType,Powerr_Id as powerrId,Line_Id,Voltage_Level
        FROM pms_monitor
        WHERE STATUS = 1 )
        ) t
        INNER JOIN pms_distribution_monitor b ON t.id = b.Monitor_Id
        left join pms_terminal ter on b.Terminal_Id = ter.id
        <where>
            <if test="type == 1">
                powerrId = #{subId}
            </if>
            <if test="type == 2">
                Line_Id = #{subId}
            </if>
        </where>
    </select>


    <select id="pwMonitorAreaDetailInfo" resultType="com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO">
        SELECT
        t.org_id orgId,
        t.id monitorId,
        t.NAME monitorName,
        b.Terminal_Id terminalId,
        t.Voltage_Level voltageLevel,
        t.powerrId,
        t.Line_Id,
        t.powerName
        FROM
        (
        (
        SELECT
        a.id,
        a.NAME,
        a.org_id,
        3 AS lineType,
        a.Power_Station_Id AS powerrId,
        a.Line_Id,
        a.Voltage_Level,
        b.Power_Name powerName
        FROM
        pms_power_client a
        LEFT JOIN pms_statation_stat b ON a.Power_Station_Id = b.Power_Id
        WHERE
        a.STATUS = 1
        ) UNION ALL
        (
        SELECT
        a.id,
        a.NAME,
        a.org_id,
        3 AS lineType,
        a.Power_Station_Id AS powerrId,
        a.Line_Id,
        a.Voltage_Level,
        b.Power_Name powerName
        FROM
        pms_power_generation_user a
        LEFT JOIN pms_statation_stat b ON a.Power_Station_Id = b.Power_Id
        WHERE
        a.STATUS = 1
        ) UNION ALL
        (
        SELECT
        id,
        NAME,
        org_id,
        2 AS lineType,
        Power_Station_Id AS powerrId,
        Line_Id,
        Voltage_Level,
        Powerr_Name powerName
        FROM
        pms_power_distributionarea
        WHERE
        STATUS = 1
        ) UNION ALL
        (
        SELECT
        id,
        NAME,
        org_id,
        1 AS lineType,
        Powerr_Id AS powerrId,
        Line_Id,
        Voltage_Level,
        Powerr_Name powerName
        FROM
        pms_monitor
        WHERE
        STATUS = 1
        )
        ) t
        INNER JOIN pms_distribution_monitor b ON t.id = b.Monitor_Id
        where b.Monitor_Id in
        <foreach collection="monitorIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

    </select>


</mapper>

