<?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.pq.mapper.TerminalMaintainMapper">

    <select id="getTerminalDevInfo" resultType="TerminalMaintainVO">
            SELECT
                a.id,
                a.pid,
                a.NAME,
                a.level,
                b.run_flag,
                b.com_flag,
                e.NAME devType,
                ifnull(g.name,'/') version,
                ifnull(d.flow,(select flow from cld_flow_meal where type = 0 and flag = 1)) baseFlowMeal,
                ifnull(d1.flow,0) reamFlowMeal,
                ROUND(ifnull(mo.Actual_Value,0)/1024/1024,2) statisValue
        FROM
            pq_line a
            INNER JOIN pq_device b ON a.id = b.id
            LEFT JOIN cld_dev_meal c ON b.id = c.line_id
            LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
            LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
            LEFT JOIN sys_dict_data e ON b.dev_type = e.id
            LEFT JOIN cld_dev_version f on a.id = f.line_id and f.flag = 1 and f.state = 1
            LEFT JOIN cld_version g on f.version_id = g.id
            LEFT JOIN
            (select * from cld_month_flow where date_format(time_id,'%Y-%m' ) = date_format(CURRENT_TIME(),'%Y-%m')) mo on a.id = mo.Dev_Id
            where a.id in
                <foreach collection="devIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                <if test="comFlag != null">
                    AND b.Com_Flag=#{comFlag}
                </if>
                <if test="devType !=null and devType != ''">
                    AND b.Dev_Type =#{devType}
                </if>
    </select>

    <select id="getGdAndSubList" resultType="TerminalMaintainVO">
        select id,name,level,pid from pq_line where level = #{level} and state = 1 order by sort
    </select>

    <select id="getProvList" resultType="TerminalMaintainVO">
        select a.id,b.name,a.level,a.pid from pq_line a
        inner join sys_area b on a.name = b.id
        where a.level = 1
        and a.state = 1
        order by a.sort
    </select>

    <select id="getPqLineProvList" resultType="TerminalMaintainVO">
        select a.id,b.name,a.level,a.pid from pq_line a
        inner join sys_area b on a.name = b.id
        where a.level = 1
        and a.id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by a.sort
    </select>

    <select id="getPqLineGdAndSubList" resultType="TerminalMaintainVO">
        select a.id,
        a.name,
        a.level,
        a.pid
        from pq_line a
        where a.id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by a.sort
    </select>


    <select id="getRunFlagList" resultType="TerminalMaintainVO">
        SELECT
        a.id,
        a.pid,
        a.NAME,
        b.ip,
        a.level,
        b.run_flag
        FROM
        pq_line a
        INNER JOIN pq_device b ON a.id = b.id
        WHERE a.id in
        <foreach collection="ids" item="item" separator="," index="index" open="(" close=")">
            #{item}
        </foreach>
        order by a.sort
    </select>

    <select id="getFlowMealList" resultType="TerminalMaintainVO">
        SELECT
        a.id,
        a.pid,
        a.name,
        a.level,
        h.ip,
        ifnull(f.Flow,(select flow from cld_flow_meal where type = 0 and flag = 1)) baseFlowMeal,
        ifnull(g.Flow,0) reamFlowMeal
        FROM
        pq_line a
        inner join pq_device h on a.id = h.id
        Left join cld_dev_meal e on a.id = e.Line_Id
        left join cld_flow_meal f on e.Base_Meal_Id = f.Id
        left join cld_flow_meal g on e.Ream_Meal_Id = g.Id
        WHERE a.id in
        <foreach collection="ids" item="item" separator="," index="index" open="(" close=")">
            #{item}
        </foreach>
        order by a.sort
    </select>

    <select id="getFlowStrategyList" resultType="TerminalMaintainVO">
        SELECT
        a.id,
        a.pid,
        a.name,
        a.level,
        ifnull(f.name,(select name from cld_flow_strategy where flag = 1 and state = 1)) flowStrategy,
        ifnull(f.flag,1) as strategyFlag
        FROM
        pq_line a
        Left join cld_dev_strategy e on a.id = e.Line_Id
        left join cld_flow_strategy f on e.Strategy_Id = f.Id
        WHERE a.id in
        <foreach collection="ids" item="item" separator="," index="index" open="(" close=")">
            #{item}
        </foreach>
        order by a.sort
    </select>
    <select id="getFlowManageDetail" resultType="com.njcn.device.pq.pojo.vo.TerminalDaliyFlowVO">
        SELECT
        c.Line_Id devId,
        IFNULL(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) baseFlow,
        IFNULL(d1.flow, 0) extendFlow,
        IFNULL(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) +  IFNULL(d1.flow, 0) flowMeal
        FROM  pq_line device
        LEFT JOIN cld_dev_meal c ON device.id = c.Line_Id
        LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
        LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
        WHERE device.Id = #{devId}
    </select>

    <select id="getMonthFlow" resultType="com.njcn.device.pq.pojo.vo.LineFlowMealDetailVO">
        select t.*,
        (t.statisValue)/t.flowMeal flowProportion
        from (
                SELECT
                        a.id id,
                        a.name deviceName,
                        a.level,
                        sub.name substation,
                        gd.name electricPowerCompany,
                        b.IP DeviceIP,
                        b.id deviceId,
                        ifnull(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) + ifnull(d1.flow, 0) flowMeal,
                        ifnull(convert(m.Actual_Value/1024/1024,decimal(7,2)),0) statisValue
                FROM pq_line a
                INNER JOIN pq_line sub ON sub.id = a.pid
                INNER JOIN pq_line gd ON gd.id = sub.pid
                INNER JOIN pq_device b ON a.id = b.id
                LEFT JOIN  cld_month_flow m ON b.id = m.Dev_Id
                LEFT JOIN cld_dev_meal c ON b.id = c.line_id
                LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
                LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
                WHERE a.id  IN
                    <foreach item="item" collection="devs" separator="," open="(" close=")">
                        #{item}
                    </foreach>
                AND
                    m.Time_Id between #{startTime} and #{endTime}
                AND
                    b.Run_Flag != 2
        ) t
        ORDER BY flowProportion DESC
    </select>

    <select id="getMonthFlowNew" resultType="com.njcn.device.pq.pojo.vo.LineFlowMealDetailVO">
        select t.*,
        (t.statisValue)/t.flowMeal flowProportion
        from (
        SELECT
        a.id id,
        a.name deviceName,
        a.level,
        sub.name substation,
        gd.name electricPowerCompany,
        b.IP DeviceIP,
        b.id deviceId,
        ifnull(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) + ifnull(d1.flow, 0) flowMeal,
        ifnull(convert(m.Actual_Value/1024/1024,decimal(7,2)),0) statisValue
        FROM pq_line a
        INNER JOIN pq_line sub ON sub.id = a.pid
        INNER JOIN pq_line gd ON gd.id = sub.pid
        INNER JOIN pq_device b ON a.id = b.id
        LEFT JOIN  cld_month_flow m ON b.id = m.Dev_Id
        LEFT JOIN cld_dev_meal c ON b.id = c.line_id
        LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
        LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
        where a.id in
        <foreach item="item" collection="devs" separator="," open="(" close=")">
            #{item}
        </foreach>
        and
        m.Time_Id between #{startTime} and #{endTime}
        and
        b.Run_Flag != 2
        ) t
        ORDER BY flowProportion DESC
    </select>


</mapper>
