<?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.watermeter.mapper.WateryieldMapper">

    <resultMap id="wateryieldMap" type="com.tofly.watermeter.entity.Wateryield">
        <id property="id" column="ID"/>
        <result property="factoryId" column="FACTORY_ID"/>
        <result property="hourGetWater" column="HOUR_GET_WATER"/>
        <result property="hourWaterSupply" column="HOUR_WATER_SUPPLY"/>
        <result property="sumGetWater" column="SUM_GET_WATER"/>
        <result property="sumWaterSupply" column="SUM_WATER_SUPPLY"/>
        <result property="uhour" column="UHOUR"/>
        <result property="uday" column="UDAY"/>
    </resultMap>

    <select id="queryHistoryInfo" resultType="string">
        select val
        from (select ITNUM_VAL val
        from TF_YWPN_SCADAHISTORY_R_VIEW
        where ITCD = #{itcd}
        and itnum_val not like '%9999%'
        and to_char(scada_time, #{format}) = to_char(#{time},#{format})
        order by scada_time
        <if test="sort != null and sort != ''">
            ${sort}
        </if>
        )
        where rownum = 1
    </select>

    <select id="getScadaItVal" resultType="string">
        select val
        from (select ITNUM_VAL val
        from TF_YWPN_SCADAHISTORY_R_VIEW
        where ITCD = #{itcd}
        and to_char(scada_time, 'yyyy-mm-dd hh24') = #{str}
        order by scada_time
        <if test="sort != null and sort != ''">
            ${sort}
        </if>
        )
        where rownum = 1
    </select>

    <select id="groupTimeList" resultType="string" fetchSize="1000">
        select to_char(SCADA_TIME, 'yyyy-mm-dd hh24') || ':00:00' time
        from TF_YWPN_SCADAHISTORY_R_VIEW
        where to_char(SCADA_TIME, 'yyyy-mm-dd') = '2022-05-31'
        group by to_char(SCADA_TIME, 'yyyy-mm-dd hh24')
        order by time
    </select>

    <insert id="batchInsertWaterYield">
        insert into TF_YWPN_WATERYIELD_W (ID, FACTORY_ID, HOUR_GET_WATER, HOUR_WATER_SUPPLY, SUM_GET_WATER,
        SUM_WATER_SUPPLY, UHOUR, UDAY)
        <foreach collection="wateryields" item="wy" separator="union all ">
            select
            #{wy.id ,jdbcType = NUMERIC},
            #{wy.factoryId ,jdbcType = NUMERIC},
            #{wy.hourGetWater ,jdbcType = NUMERIC},
            #{wy.hourWaterSupply ,jdbcType = NUMERIC},
            #{wy.sumGetWater ,jdbcType = NUMERIC},
            #{wy.sumWaterSupply ,jdbcType = NUMERIC},
            #{wy.uhour ,jdbcType = VARCHAR},
            #{wy.uday ,jdbcType = VARCHAR}
            from DUAL
        </foreach>
    </insert>

    <select id="getMeterInfo" resultType="map" fetchSize="1000">
        select r.BCDS, r.YHBH, temp.BCDS sl
        from TF_YWPN_READMETERHIS_W r
                 left join (select BCDS, YHBH
                            from (select BCDS,
                                         YHBH,
                                         row_number() over (partition by YHBH order by CBYF desc) row_id
                                  from TF_YWPN_READMETERHIS_W
                                  where IS_GENERAL = 1
                                    and CBBZ = 1
                                    and MONTH_PLAN &lt; #{month})
                            where row_id = 1) temp on r.YHBH = temp.YHBH
        where MONTH_PLAN = #{month}
          and SCDS = 0
          and IS_GENERAL = 1
          and temp.BCDS is not null
    </select>

</mapper>
