<?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.xrtygis.mapper.AbnormalWaterUseMapper">

    <resultMap id="abnormalWaterUseMap" type="com.tofly.xrtygis.entity.AbnormalWaterUse">
        <id property="id" column="ID"/>
        <result property="yhbh" column="YHBH"/>
        <result property="sevenAvg" column="SEVEN_AVG"/>
        <result property="nowSl" column="NOW_SL"/>
        <result property="nowTime" column="NOW_TIME"/>
        <result property="startStopValue" column="START_STOP_VALUE"/>
        <result property="state" column="STATE"/>
        <result property="monthDay" column="MONTH_DAY"/>
        <association property="stateName" column="STATE" select="getState"/>
    </resultMap>

    <select id="getState" resultType="java.lang.String">
        select decode(#{state},'0','正常','1','异常') as stateName from dual
    </select>


    <select id="getAbnormalWaterUserPage" resultType="map">
        select t1.YHBH yhbh,t1.SEVEN_AVG sevenAvg,t1.NOW_SL nowSl,t1.NOW_TIME nowTime,t1.START_STOP_VALUE startStopValue,
               t1.STATE state,t1.MONTH_DAY monthDay,t2.YHDZ yhdz,t2.LXDH lxdh,t2.YHXM yhxm
        from tf_abnormal_water_use t1 left join yh_khjbxx@YXDBLINK t2
        on t1.yhbh = t2.yhbh
        where 1=1
        <if test="startTime != null and endTime != null ">
           and t1.NOW_TIME between #{startTime} and #{endTime}
        </if>
        <if test="state != null and state.trim() != ''">
            and t1.STATE = #{state}
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and t1.YHBH || t2.YHDZ || t2.LXDH || t2.YHXM like '%' || #{likeStr} || '%'
        </if>
    </select>

    <select id="getUserMeterReading" resultType="map">
        select yhbh,cbrq,sl
        from znb_cb_day@yxdblink
        where cljg = '1'
        and cbrq between #{startTime} and #{endTime}
        and yhbh = #{yhbh}
    </select>

    <select id="getAbnormalWaterUserList" resultMap="abnormalWaterUseMap">
        select m.yhbh YHBH,m.cbrq NOW_TIME,(m.bcds||'-'||m.chzs) START_STOP_VALUE,m.sl NOW_SL,nvl(m.avgSl,0) SEVEN_AVG,
               (case when (m.sl*rateValue/100) > (m.avgSl)
                     then 1
                     when (m.avgSl) = 0
                     then 0
                     else 0 end) STATE,to_char(cbrq,'yyyy-MM-dd') MONTH_DAY
        from(
        select q1.yhbh,q1.cbrq,q1.bcds,q1.chzs,q1.sl,q2.avgSl,(select  trend_threshold from tf_abnormal_water_config)rateValue
        from
        (select yhbh,cbrq,bcds,chzs,sl
        from znb_cb_day@yxdblink
        where cljg = '1'
        and to_char(cbrq,'yyyy-MM-dd') =  #{monthDay}) q1 left join

        (select t1.yhbh, nvl(avg(sl),0) avgSl
        from
        (select yhbh,cbrq,bcds,chzs,sl
        from znb_cb_day@yxdblink
        where cljg = '1'
        and sl !=0
        and cbrq >= #{startTime}
        and cbrq &lt; #{endTime}
        ) t1
        where not exists
        (select t2.yhbh,t2.NOW_TIME
        from tf_abnormal_water_use t2
        where t2.state = '1'
        and t1.yhbh = t2.yhbh
        and t1.cbrq = t2.NOW_TIME
        )
        group by t1.yhbh) q2
        on q1.yhbh  = q2.yhbh) m
        order by cbrq desc
    </select>


    <select id="getPlanTempMaxId" parameterType="java.lang.String" resultType="java.lang.Long">
        select nvl(max(id), 0) + 1
        from ${tableName}
    </select>


    <insert id="addMessageList" parameterType="java.util.List" >
        INSERT ALL
        <foreach item="item" index="index" collection="list">
            INTO TF_YWPN_MESSAGEPUSH_W
            (
            ID,
            TOPIC,
            MESSAGE,
            TYPE,
            TABLE_NAME,
            ROUTE_IP,
            TABLE_ID,
            CREATE_TIME,
            CREATE_USER,
            ROUTE_IP_APP
            ) VALUES
            (
            #{item.id},
            #{item.topic},
            #{item.message},
            #{item.type},
            #{item.tableName},
            #{item.routeIp},
            #{item.tableId},
            #{item.createTime},
            #{item.createUser},
            #{item.routeIpApp}
            )
        </foreach>
        SELECT 1 FROM DUAL
    </insert>

    <insert id="addMessageRecipientList" parameterType="java.util.List" >
        INSERT ALL
        <foreach item="item" index="index" collection="list">
            INTO TF_YWPN_MESSAGERECIPIENT_W
            (
            ID,
            MESSAGE_ID,
            RECIPIENT,
            FLAG
            ) VALUES
            (
            #{item.id},
            #{item.messageId},
            #{item.recipient},
            #{item.flag}
            )
        </foreach>
        SELECT 1 FROM DUAL
    </insert>
    <insert id="addUserTaskList" parameterType="java.util.List">
        INSERT ALL
        <foreach item="item" index="index" collection="list">
            INTO TF_YWPN_USERTASK_W
            (
            ID,
            TKNM,
            TKTY,
            TABLE_NAME,
            TABLE_ID,
            ROUTE_IP,
            ROUTE_APP_IP,
            ISHANDLE,
            HANDLE_USER,
            START_TIME,
            END_TIME,
            CREATE_USER,
            CREATE_TIME
            ) VALUES
            (
            #{item.id},
            #{item.tknm},
            #{item.tkty},
            #{item.tableName},
            #{item.tableId},
            #{item.routeIp},
            #{item.routeAppIp},
            #{item.ishandle},
            #{item.handleUser},
            #{item.startTime},
            #{item.endTime},
            #{item.createUser},
            #{item.createTime}
            )
        </foreach>
        SELECT 1 FROM DUAL
    </insert>
</mapper>
