<?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.tofly.custom.mapper.CustomDisplayMapper">

    <select id="getWaterStationInfo" resultType="com.tofly.custom.entity.vo.WaterStationVo">
        SELECT
            *
        FROM
            TF_WATER_INFO
        WHERE
            1=1
    </select>

    <select id="getScadaIndexData" resultType="com.tofly.custom.entity.vo.ScadaIndexVo">
        SELECT
        <choose>
            <when test="tableType == 2">
                <choose>
                    <when test="statisticsType == 0">
                        to_char(a.START_TIME,'yyyy-mm-dd hh24') SCADA_TIME,
                    </when>
                    <when test="statisticsType == 1">
                        to_char(a.START_TIME,'yyyy-mm-dd') SCADA_TIME,
                    </when>
                    <when test="statisticsType == 2">
                        to_char(a.START_TIME,'yyyy-mm') SCADA_TIME,
                    </when>
                    <when test="statisticsType == 3">
                        to_char(a.START_TIME,'yyyy') SCADA_TIME,
                    </when>
                </choose>
--                 ABS(decode(SUBSTR(to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),1,1),'.','0'||to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2)))))
--                 as INDEX_VALUE,
                ABS(a.MAX - a.MIN) as INDEX_VALUE,
                --a.value INDEX_VALUE,
                a.MAX INDEX_VALUE_MAX,
                a.MIN INDEX_VALUE_MIN,
                a.AVE INDEX_VALUE_AVG,
                a.FOLD INDEX_VALUE_FOLD,
                -- a.ALL_COUNT,
                a.PASS PASS_RATE,
                a.ERROR_COUNT,
            </when>
            <when test="tableType == 1 or tableType == 3">
                a.SCADA_TIME,
                -- a.VALUE INDEX_VALUE,
                a.VALUE as INDEX_VALUE,
            </when>
        </choose>
        b.VARIABLE_CODE INDEX_CODE,
        b.display_name INDEX_NAME,
        b.TYPE INDEX_TYPE,
        b.report_id report_ids,
        dic.cname  INDEX_TYPE_NAME,
        b.UNIT INDEX_UNIT,
        b.ACCURACY,
        c.id DEVICE_ID,
        c.NAME DEVICE_NAME,
        c.CODE DEVICE_CODE,
        d.ID water_Id,
        d.NAME WATER_STATION_NAME,
        dicDevice.cname device_type_name
        FROM
        <choose>
            <when test="tableType == 1">
                SCADA_MONITORING a
            </when>
            <when test="tableType == 2">
                STATISTICS_SCADA a
            </when>
            <when test="tableType == 3">
                SCADA_HISTORY a
            </when>
        </choose>
        LEFT JOIN TF_INDEX_ALLOCATION b ON b.VARIABLE_CODE = a.CODE
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE c ON c.ID = b.DEVICE_ID
        LEFT JOIN TF_WATER_INFO d ON d.ID = c.WATER_ID
        LEFT JOIN TF_YWPN_SYSCHILDCODE_D dic on dic.pcid=60000 and dic.ccode=b.type
        LEFT JOIN TF_YWPN_SYSCHILDCODE_D dicDevice on dicDevice.pcid=53400 and dicDevice.ccode=c.device_type
        WHERE
        1=1
        <if test="startDate != null and endDate != null">

            <choose>
                <when test="tableType == 2">
                    AND TO_CHAR(a.start_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startDate} AND #{endDate}
                </when>
                <when test="tableType == 1 or tableType == 3">
                    AND TO_CHAR(a.SCADA_TIME, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startDate} AND #{endDate}
                </when>
            </choose>


        </if>
        <if test="statisticsType != null and tableType == 2">
            AND a.STATISTICS_TYPE=#{statisticsType}

        </if>

        <if test="indexType != null">
            AND b.TYPE = #{indexType}
        </if>
        <if test="indexTypes != null">
            AND b.TYPE IN
            <foreach item="item" index="index" collection="indexTypes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="indexCodes != null">
            AND b.VARIABLE_CODE IN
            <foreach item="item" index="index" collection="indexCodes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="searchOption==0 and likeStr!=null and likeStr.trim() != ''">
            AND (b.VARIABLE_CODE || b.VARIABLE_NAME || b.DISPLAY_NAME like '%'||#{likeStr}||'%')

        </if>

        <if test="deviceName != null">
            AND c.NAME LIKE CONCAT ('%', #{deviceName}, '%')
        </if>
        <if test="deviceCodes != null">
            AND c.CODE in
            <foreach item="item" index="index" collection="deviceCodes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="deviceIds != null">
            AND c.id in
            <foreach item="item" index="index" collection="deviceIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="deviceTypes != null">
            AND c.DEVICE_TYPE in
            <foreach item="item" index="index" collection="deviceTypes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="types != null">
            AND c.type in
            <foreach item="item" index="index" collection="types" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>


        <if test="waterStationIds != null">
            AND d.ID in
            <foreach item="item" index="index" collection="waterStationIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <choose>
            <when test="tableType == 2">
                ORDER by  a.START_TIME DESC

            </when>
            <when test="tableType == 1 or tableType == 3">
                ORDER by    a.SCADA_TIME DESC
            </when>
        </choose>


    </select>

    <select id="getHistoryPageData" resultType="com.tofly.custom.entity.vo.ScadaIndexVo">
        select a.SCADA_TIME,
        --  a.VALUE INDEX_VALUE,
        decode(SUBSTR(to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),1,1),'.','0'||to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))))
        INDEX_VALUE,
        b.VARIABLE_CODE INDEX_CODE,
        b.display_name INDEX_NAME,
        b.TYPE INDEX_TYPE,
        b.report_id report_ids,
        dic.cname INDEX_TYPE_NAME,
        b.UNIT INDEX_UNIT,
        c.id DEVICE_ID,
        c.NAME DEVICE_NAME,
        c.CODE DEVICE_CODE,
        d.ID water_Id,
        d.NAME WATER_STATION_NAME,
        dicDevice.cname device_type_name,
        case
        when r.id is null then
        0
        else
        1
        end as is_Alarm,
        r.report_level,
        r.report_upper,
        r.report_lower
        from scada_history a
        LEFT JOIN TF_INDEX_ALLOCATION b
        ON b.VARIABLE_CODE = a.CODE
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE c
        ON c.ID = b.DEVICE_ID
        LEFT JOIN TF_WATER_INFO d
        ON d.ID = c.WATER_ID
        LEFT JOIN TF_YWPN_SYSCHILDCODE_D dic
        on dic.pcid = 60000
        and dic.ccode = b.type
        LEFT JOIN TF_YWPN_SYSCHILDCODE_D dicDevice
        on dicDevice.pcid = 53400
        LEFT JOIN TF_SCADA_REPORT_LIST r
        on a.code = r.code
        and a.scada_time = r.scada_time
        and dicDevice.ccode = c.device_type
        where 1=1
        <if test="dto.startDate != null and dto.endDate != null">
            AND TO_CHAR(a.SCADA_TIME, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dto.startDate} AND #{dto.endDate}

        </if>

        <if test="dto.indexType != null">
            AND b.TYPE=#{dto.indexType}
        </if>

        <if test="dto.indexTypes != null">
            AND b.TYPE IN
            <foreach item="item" index="index" collection="dto.indexTypes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.indexCodes != null">
            AND b.VARIABLE_CODE IN
            <foreach item="item" index="index" collection="dto.indexCodes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="dto.likeStr!=null and dto.likeStr.trim() != ''">
            AND ( c.NAME || c.code || b.VARIABLE_CODE || b.VARIABLE_NAME || b.DISPLAY_NAME like '%'||#{dto.likeStr}||'%')
        </if>

        <if test="dto.deviceName != null">
            AND c.NAME LIKE CONCAT ('%', #{dto.deviceName}, '%')
        </if>
        <if test="dto.deviceCodes != null">
            AND c.CODE in
            <foreach item="item" index="index" collection="dto.deviceCodes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.deviceIds != null">
            AND c.id in
            <foreach item="item" index="index" collection="dto.deviceIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.deviceTypes != null">
            AND c.DEVICE_TYPE in
            <foreach item="item" index="index" collection="dto.deviceTypes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="dto.types != null">
            AND c.type in
            <foreach item="item" index="index" collection="dto.types" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>


        <if test="dto.waterStationIds != null">
            AND d.ID in
            <foreach item="item" index="index" collection="dto.waterStationIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.reportLevels != null">
            AND r.report_level in
            <foreach item="item" index="index" collection="dto.reportLevels" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>


        order by a.scada_time desc
    </select>

    <select id="getWaterSourceScada" resultType="com.tofly.custom.entity.vo.WaterSourceScadaVo">
        SELECT
            TO_CHAR(a.START_TIME, 'yyyy-MM-dd HH24:mi:ss') SCADA_TIME,
            d.WATER_SOURCE,
            SUM(a.MAX - a.MIN) INDEX_VALUE
        FROM STATISTICS_SCADA a
            LEFT JOIN TF_INDEX_ALLOCATION b ON b.VARIABLE_CODE = a.CODE
            LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE c ON c.ID = b.DEVICE_ID
            LEFT JOIN TF_WATER_INFO d ON d.ID = c.WATER_ID
        WHERE
            1=1 AND a.STATISTICS_TYPE = 1
        <if test="startDate != null and endDate != null">
            AND TO_CHAR(a.START_TIME, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="indexTypes != null">
            AND b.TYPE IN
            <foreach item="item" index="index" collection="indexTypes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
            d.WATER_SOURCE, TO_CHAR(a.START_TIME, 'yyyy-MM-dd HH24:mi:ss')
        ORDER BY
            SCADA_TIME DESC
    </select>

    <select id="getWaterStationCount" resultType="Integer">
        SELECT
            COUNT( * )
        FROM
            TF_WATER_INFO
        WHERE
            1=1
        <if test="waterType != null and waterType.trim() != ''">
            AND WATER_TYPE = #{waterType}
        </if>
    </select>

    <select id="getDeviceCount" resultType="Integer">
        SELECT
        COUNT( * )
        FROM
        TF_DEVICE_ARCHIVE_MANAGE
        WHERE
        1=1
        <if test="deviceType != null and deviceType.trim() != ''">
            AND DEVICE_TYPE = #{deviceType}
        </if>
    </select>

    <select id="getTableUserNum" resultType="Integer">
        SELECT
            COUNT(1)
        FROM
            TF_YWPN_QFKHJBXX_W
    </select>

    <select id="getPipeMaterialCensus" resultType="com.tofly.custom.entity.vo.PipeMaterialCensusVo">
        SELECT
            MATERIAL,
            ROUND( SUM( NUM ), 2 ) NUM
        FROM
            (
                ( SELECT MATERIAL, ROUND( SUM( PIPELENGTH ), 2 ) NUM FROM sde_ylsw.TF_JSJS_PIPE_B GROUP BY MATERIAL ) UNION
                ( SELECT MATERIAL, ROUND( SUM( PIPELENGTH ), 2 ) NUM FROM sde_ylsw.TF_JSJS_STANDPIPE_B GROUP BY MATERIAL )
            ) t
        GROUP BY
            MATERIAL
    </select>

    <select id="getNewUserTotalMonth" resultType="com.tofly.custom.entity.vo.UserTotalVo">
        SELECT
            TO_CHAR(CREATE_TIME, 'yyyy-MM') SCADA_TIME,
            COUNT( * ) NEW_USER_TOTAL
        FROM
            TF_SMPN_USER_X
        WHERE
            1=1
        GROUP BY
            TO_CHAR(CREATE_TIME, 'yyyy-MM')
        ORDER BY
            SCADA_TIME
    </select>

    <select id="getUserTotal" resultType="Integer">
        SELECT
        COUNT( * ) NEW_USER_TOTAL
        FROM
        TF_SMPN_USER_X
        WHERE
        1=1
        <if test="censusMonth != null">
            AND TO_CHAR(CREATE_TIME, 'yyyy-MM') &lt;= #{censusMonth}
        </if>
    </select>

    <select id="getDesignProcessCapacity" resultType="BigDecimal">
        SELECT
        CASE WHEN(NVL(SUM ( DESIGN_PROCESSING_CAPACITY ), 0)) != 0 THEN round( SUM ( DESIGN_PROCESSING_CAPACITY ), 2 ) ELSE 0 END DESIGN_PROCESSING_CAPACITY
        FROM
        TF_WATER_INFO
        WHERE
        1=1 AND WATER_TYPE = '1'
        <if test="name != null and name.trim() != ''">
            AND NAME LIKE '%'||#{name}||'%'
        </if>
    </select>

    <select id="getDeviceTypeList" resultType="com.tofly.custom.entity.vo.DeviceByTypeVo">
        select t1.ccode device_Type,t1.cname device_Type_name from TF_YWPN_SYSCHILDCODE_D t1
        where exists(select 1 from TF_DEVICE_ARCHIVE_MANAGE t2 where t1.ccode=t2.device_type)
        and pcid=53400
        <if test="deviceTypeName != null and deviceTypeName.trim() != ''">
            AND t1.cname LIKE '%'||#{deviceTypeName}||'%'
        </if>
        order by t1.ccode
    </select>


    <select id="getDeviceType" resultType="com.tofly.custom.entity.vo.DeviceByTypeVo">
        select t1.ccode device_Type,t1.cname device_Type_name from TF_YWPN_SYSCHILDCODE_D t1
        where  pcid=53400

    </select>

    <select id="getTotalFlow" resultType="String">
        select sum(ROUND(to_number(a.VALUE), nvl(b.accuracy, 2))) as totalFlow
        from (select row_number() over(partition by code order by scada_time) as num,
        t.value,
        t.code
        from scada_history t
        where code in
        <foreach item="item" index="index" collection="codes" separator="," open="(" close=")">
            #{item}
        </foreach>

        and scada_time between #{startTime} and #{endTime}) a
        LEFT JOIN TF_INDEX_ALLOCATION b
        ON b.VARIABLE_CODE = a.CODE
        where a.num = 1

    </select>

    <select id="getNightTotalFlow" resultType="String">
        select sum(ROUND(to_number(a.VALUE), nvl(b.accuracy, 2))) as totalFlow
        from (select row_number() over(partition by t.code order by to_number(t.VALUE)) as num,
        t.value,
        t.code
        from scada_history t
        where code in
        <foreach item="item" index="index" collection="codes" separator="," open="(" close=")">
            #{item}
        </foreach>

        and scada_time between #{startTime} and #{endTime}) a
        LEFT JOIN TF_INDEX_ALLOCATION b
        ON b.VARIABLE_CODE = a.CODE
        where a.num = 1

    </select>


    <select id="getWaterNightFlowComm" resultType="com.tofly.custom.entity.vo.ScadaFlowVo">
        select a.*, b.scada_time, b.value
        from (select t3.water_id,
        t4.name          water_name,
        t1.sort_number,
        t1.variable_code as code,
        t1.variable_name,
        t1.unit,
        t1.type          index_type,
        t2.cname         index_type_name
        from tf_index_allocation t1
        left join tf_device_archive_manage t3
        on t1.device_id = t3.id
        left join tf_water_info t4
        on t3.water_id = t4.id
        left join TF_YWPN_SYSCHILDCODE_D t2
        on t1.type = t2.ccode
        and t2.pcid = 60000
        where t1.variable_code in
        <foreach item="item" index="index" collection="codes" separator="," open="(" close=")">
            #{item}
        </foreach>
        ) a
        left join (select code,
        scada_time,
        decode(SUBSTR(to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),1,1),'.','0'||to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))))
        as value
        from (select row_number() over(partition by code order by to_number(t.value)) as num,
        t.value,
        t.code,
        t.scada_time
        from scada_history t
        where code in
        <foreach item="item" index="index" collection="codes" separator="," open="(" close=")">
            #{item}
        </foreach>
        and scada_time between #{startTime} and #{endTime}) a
        LEFT JOIN TF_INDEX_ALLOCATION b
        ON b.VARIABLE_CODE = a.CODE
        where a.num = 1) b
        on a.code = b.code
        order by a.water_id,a.index_type,a.sort_number



    </select>


    <select id="getWaterFlowComm" resultType="com.tofly.custom.entity.vo.ScadaFlowVo">
        select a.*, b.scada_time, b.value
        from (select t3.water_id,
        t4.name          water_name,
        t1.sort_number,
        t1.variable_code as code,
        t1.variable_name,
        t1.unit,
        t1.type          index_type,
        t2.cname         index_type_name
        from tf_index_allocation t1
        left join tf_device_archive_manage t3
        on t1.device_id = t3.id
        left join tf_water_info t4
        on t3.water_id = t4.id
        left join TF_YWPN_SYSCHILDCODE_D t2
        on t1.type = t2.ccode
        and t2.pcid = 60000
        where t1.variable_code in
        <foreach item="item" index="index" collection="codes" separator="," open="(" close=")">
            #{item}
        </foreach>
        ) a
        left join (select code,
        scada_time,
        decode(SUBSTR(to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),1,1),'.','0'||to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))),to_char(ROUND(to_number(a.VALUE),nvl(b.accuracy,2))))
        as value
        from (select row_number() over(partition by code order by scada_time) as num,
        t.value,
        t.code,
        t.scada_time
        from scada_history t
        where code in
        <foreach item="item" index="index" collection="codes" separator="," open="(" close=")">
            #{item}
        </foreach>
        and scada_time between #{startTime} and #{endTime}) a
        LEFT JOIN TF_INDEX_ALLOCATION b
        ON b.VARIABLE_CODE = a.CODE
        where a.num = 1) b
        on a.code = b.code
        order by a.water_id,a.index_type,a.sort_number

    </select>

</mapper>