<?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.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,
	FORMAT(ifnull(mo.statis_value,0)/1024/1024,2) as 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.line_id
    where b.dev_model = 1
    </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,
        g.Flag 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>


</mapper>
