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

    <select id="getAubotUser" resultType="long">
        select distinct USER_ID
        from TF_SMPN_USERROLE_X
        where ROLE_ID in (select distinct ROLE_ID
                          from TF_SMPN_ROLEMENU_X
                          where MENU_ID = #{menuId})

    </select>

    <insert id="insertMsg">
        insert into TF_YWPN_MESSAGEPUSH_W (id, topic, message, type, route_ip,
                                           create_time, create_user)
        values (#{msg.id}, #{msg.topic}, #{msg.message}, '0',
                #{msg.routeIp}, sysdate, #{msg.createUser})
    </insert>

    <insert id="insertMsgStateClient">
        insert into TF_YWPN_MESSAGERECIPIENT_W (ID, MESSAGE_ID, RECIPIENT, FLAG, RECIPIENT_TIME)
        VALUES ((select nvl(max(id), 0) + 1 from TF_YWPN_MESSAGERECIPIENT_W), #{msg.messageId}, #{msg.recipient},
                '0', sysdate)
    </insert>

    <insert id="insertMsgStateClientBatch">
        insert into TF_YWPN_MESSAGERECIPIENT_W (ID, MESSAGE_ID, RECIPIENT, FLAG, RECIPIENT_TIME)
        VALUES
        <foreach collection="idList" item="userId" separator=",">
            (#{msg.id}, #{msg.messageId}, #{userId},'0', sysdate)
        </foreach>

    </insert>

    <select id="warnNeedInfo" resultType="string">
        select (case
                    when ITNUM_VAL &lt; DOWN_LIMIT or ITNUM_VAL &gt; UP_LIMIT then STNM || ITNM || '【' ||
                                                                                   decode(substr(ITNUM_VAL, 1, 1), '.', '0' || ITNUM_VAL, ITNUM_VAL) ||
                                                                                   '】,' || '阈值【' ||
                                                                                   decode(substr(DOWN_LIMIT, 1, 1), '.', '0' || DOWN_LIMIT, DOWN_LIMIT) ||
                                                                                   '-' ||
                                                                                   decode(substr(UP_LIMIT, 1, 1), '.', '0' || UP_LIMIT, UP_LIMIT) ||
                                                                                   '】，超出阈值报警，请及时查看' end) msg
        from TF_YWPN_SCADASTAITEM_W i
                 left join TF_YWPN_SCADASTATION_B s on i.STATION_ID = s.ID
                 left join TF_YWPN_SCADAREALTIME_R r on i.ITCD = r.ITCD
        where i.ITCD = #{itcd}
          and ITNUM_VAL != 99999
    </select>

    <select id="getMsgAndSMSUser" resultType="map">
        select w.ITCD, SMS_USER, MSG_USER, SMS_WARN, MSG_WARN
        from TF_YWPN_SCADASTAITEM_W s
                 right join TF_YWPN_WARN_W w on s.ITCD = w.ITCD
        where (MSG_USER is not null or SMS_USER is not null)
          and (MSG_WARN != 1 or SMS_WARN != 1 or MSG_WARN is null or SMS_WARN is null)

    </select>

    <select id="thresholdValue" resultType="map">
        select r.ITCD,
               MSG_USER,
               SMS_USER,
               UP_WARN_TIME,
               UP_WARN_VAL,
               OPEN_UP_WARN,
               OPEN_DOWN_WARN,
               DOWN_WARN_TIME,
               DOWN_WARN_VAL,
               UP_WARN,
               DOWN_WARN,
               UP_SMS,
               DOWN_SMS
        from TF_YWPN_SCADASTAITEM_W s
                 right join TF_YWPN_SCADAREALTIME_R r on r.ITCD = s.ITCD
        where (SMS_USER is not null or MSG_USER is not null)
          and ((UP_WARN != 1 or UP_WARN is null or DOWN_WARN != 1 or DOWN_WARN is null) or
               (UP_SMS != 1 or UP_SMS is null or DOWN_SMS is null or DOWN_SMS != 1))
          and round((sysdate - SCADA_TIME) * 24 * 60, 0) > #{minWarnTime}
          and (OPEN_DOWN_WARN = 1 or OPEN_UP_WARN = 1)
    </select>

    <select id="getUserTel" resultType="string">
        select PHONE from TF_SMPN_USER_X where ID in
        <foreach collection="userId" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </select>

    <update id="updateWarnTable">
        update TF_YWPN_WARN_W
        set ${str} = 1
        where ITCD = #{itcd}
    </update>

    <update id="updateScadaTable">
        update TF_YWPN_SCADAREALTIME_R
        set ${str} = 1
        where ITCD = #{itcd}
    </update>

    <select id="getMaxWarnValAndTime" resultType="map">
        select *
        from (select ITNUM_VAL ITVAL, SCADA_TIME
              from TF_YWPN_SCADAHISTORY_R_VIEW
              where SCADA_TIME between sysdate - #{time} / 1440 and sysdate
                and ITCD = #{itcd}
                and ITNUM_VAL != '99999'
              order by ITNUM_VAL desc, scada_time desc)
        where rownum = 1
    </select>

    <select id="getMinWarnValAndTime" resultType="map">
        select *
        from (select ITNUM_VAL ITVAL, SCADA_TIME
              from TF_YWPN_SCADAHISTORY_R_VIEW
              where SCADA_TIME between sysdate - #{time} / 1440 and sysdate
                and ITCD = #{itcd}
                and ITNUM_VAL != '99999'
              order by ITNUM_VAL, scada_time)
        where rownum = 1
    </select>

    <select id="upWarnInfo" resultType="string">
        select STNM || ITNM || '【' || #{min} || '】,' || '在间隔时间【' || #{time} || '】分钟，从【' || #{min} || '~' || #{max} ||
               '】突升量【' || #{val} || '】已超出阈值'
        from TF_YWPN_SCADASTAITEM_W i
                 left join TF_YWPN_SCADASTATION_B s on i.STATION_ID = s.ID
                 left join TF_YWPN_SCADAREALTIME_R r on i.ITCD = r.ITCD
        where i.ITCD = #{itcd}
    </select>

    <select id="downWarnInfo" resultType="string">
        select STNM || ITNM || '【' || #{min} || '】,' || '在间隔时间【' || #{time} || '】分钟，从【' || #{min} || '~' || #{max} ||
               '】突降量【' || #{val} || '】已超出阈值'
        from TF_YWPN_SCADASTAITEM_W i
                 left join TF_YWPN_SCADASTATION_B s on i.STATION_ID = s.ID
                 left join TF_YWPN_SCADAREALTIME_R r on i.ITCD = r.ITCD
        where i.ITCD = #{itcd}
    </select>

    <select id="getWarnHandle" resultType="string">
        select h.id
        from TF_YWPN_DMAWARNHANDLE_W h
                 left join TF_YWPN_GDINFO_W g on h.ORDER_ID = g.ID
        where LCJD = 45
          and FINISH_TIME is not null
          and (sysdate - FINISH_TIME) * 24 > #{hour}
          and EVA_TIME is null
          and ORDER_ID is not null
    </select>

    <select id="warnHandleInfo" resultType="com.tofly.watermeter.custom.dto.DmawarnhandleDto">
        select id, warn_id, warn_value,
        order_id, hand_time, min_flow_after, evaluate, eva_time
        from TF_YWPN_DMAWARNHANDLE_W
        where ID in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="getNightMinFlow" resultType="double">
        select MINVAL
        from TF_YWPN_DMANIGHTFLOW_W
        where to_char(UDAY, 'yyyy-mm-dd') = to_char(sysdate, 'yyyy-mm-dd')
          and TABLE_ID = #{dmaId}
          and mintime is not null
    </select>

    <update id="updateWarnHandle">
        update TF_YWPN_DMAWARNHANDLE_W
        set EVA_TIME       = sysdate,
            HAND_TIME      = (select FINISH_TIME from TF_YWPN_GDINFO_W where ID = #{dto.orderId,jdbcType=NUMERIC}),
            min_flow_after = #{dto.minFlowAfter,jdbcType=NUMERIC},
            evaluate       = #{dto.evaluate,jdbcType=NUMERIC}
        where ID = #{dto.id,jdbcType=VARCHAR}
    </update>

    <select id="getNowMonthUserCode" resultType="string">
        select YHBH
        from TF_YWPN_READMETERHIS_W
        where IS_GENERAL = 0
          and CBBZ = 1
          and ISSYN = 0
          and SXJMESSAGE is null
          and MONTH_PLAN = to_char(sysdate, 'yyyymm')
          and ROWNUM &lt;= 20
    </select>
</mapper>