<?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.DmawarnhandleMapper">

    <resultMap id="dmawarnhandleMap" type="com.tofly.xrtygis.entity.Dmawarnhandle">
        <id property="id" column="ID"/>
        <result property="warnType" column="WARN_TYPE"/>
        <result property="warnId" column="WARN_ID"/>
        <result property="warnName" column="WARN_NAME"/>
        <result property="warnValue" column="WARN_VALUE"/>
        <result property="warnNotes" column="WARN_NOTES"/>
        <result property="warnTime" column="WARN_TIME"/>
        <result property="warnMonth" column="WARN_MONTH"/>
        <result property="lid" column="LID"/>
        <result property="ishandle" column="ISHANDLE"/>
        <result property="orderId" column="ORDER_ID"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="orderTime" column="ORDER_TIME"/>
        <result property="handTime" column="HAND_TIME"/>
        <result property="minFlowBefore" column="MIN_FLOW_BEFORE"/>
        <result property="minFlowAfter" column="MIN_FLOW_AFTER"/>
        <result property="evaluate" column="EVALUATE"/>
        <result property="evaTime" column="EVA_TIME"/>
        <result property="nosendOrderBz" column="NOSEND_ORDER_BZ"/>
        <result property="nosendOrderReason" column="NOSEND_ORDER_REASON"/>
        <association property="ishandleName" column="ISHANDLE"
                     select="getHandleName"/>
        <association property="warnTypeName" column="WARN_TYPE"
                     select="getWarnTypeName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.xrtygis.mapper.IFileMapper.getUserName"/>
        <association property="nosendOrderReasonName" column="NOSEND_ORDER_REASON"
                     select="getCodeById"/>
    </resultMap>

    <select id="getCodeById" resultType="string">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ID = #{id}
    </select>
    <select id="getHandleName" resultType="string">
        select decode(#{handle}, '0', '待派单', '1', '不派单', '2', '已派单')
        from dual
    </select>
    <select id="getWarnTypeName" resultType="string">
        select decode(#{warnType}, '1', '夜间最小流量异常处理', '2', '产销差水量异常处理')
        from dual
    </select>
    <select id="analyze" resultType="java.util.Map">
        SELECT sum(case when ISHANDLE = '0' then 1 else 0 end) WAITSEND,
               sum(case when ISHANDLE = '1' then 1 else 0 end) NOSEND,
               sum(case when ISHANDLE = '2' then 1 else 0 end) SEND
        FROM TF_YWPN_DMAWARNHANDLE_W
        WHERE WARN_TYPE = #{type}
          AND WARN_TIME <![CDATA[ <= ]]> #{end}
          AND WARN_TIME <![CDATA[ >= ]]> #{start}
    </select>
<!--    orderStartTime orderEndTime handStartTime  handEndTime evaStartTime evaEndTime-->
    <select id="getResultEval" resultType="map">
        select TO_CHAR(ORDER_TIME,'yyyy-MM-dd hh:mm:ss') ORDER_TIME ,
        TO_CHAR(HAND_TIME,'yyyy-MM-dd hh:mm:ss') HAND_TIME , MIN_FLOW_BEFORE , MIN_FLOW_AFTER ,
        EVALUATE, TO_CHAR(EVA_TIME,'yyyy-MM-dd hh:mm:ss') EVA_TIME , WARN_NAME
        from TF_YWPN_DMAWARNHANDLE_W
        <where>
            and WARN_TYPE = '1'

            and ISHANDLE = '2'
            <if test="eval.id != null">
                and WARN_ID = #{eval.id}
            </if>
            <if test="eval.orderStartTime != null and eval.orderEndTime != null">
                and ORDER_TIME <![CDATA[ <= ]]> #{eval.orderEndTime}
                and ORDER_TIME <![CDATA[ >= ]]> #{eval.orderStartTime}
            </if>
            <if test="eval.handStartTime != null and eval.handEndTime != null">
                and HAND_TIME <![CDATA[ <= ]]> #{eval.handEndTime}
                and HAND_TIME <![CDATA[ >= ]]> #{eval.handStartTime}
            </if>
            <if test="eval.evaStartTime != null and eval.evaEndTime != null">
                and EVA_TIME <![CDATA[ <= ]]> #{eval.evaEndTime}
                and EVA_TIME <![CDATA[ >= ]]> #{eval.evaStartTime}
            </if>
        </where>
        ORDER BY ORDER_TIME ASC
    </select>
    <select id="getResultEvalNotPage" resultType="java.util.Map">
        select TO_CHAR(ORDER_TIME,'yyyy-MM-dd hh:mm:ss') ORDER_TIME ,
        TO_CHAR(HAND_TIME,'yyyy-MM-dd hh:mm:ss') HAND_TIME , MIN_FLOW_BEFORE , MIN_FLOW_AFTER ,
        EVALUATE, TO_CHAR(EVA_TIME,'yyyy-MM-dd hh:mm:ss') EVA_TIME , WARN_NAME
        from TF_YWPN_DMAWARNHANDLE_W
        <where>
            and WARN_TYPE = '1'

            and ISHANDLE = '2'
            <if test="eval.id != null">
                and WARN_ID = #{eval.id}
            </if>
            <if test="eval.orderStartTime != null and eval.orderEndTime != null">
                and ORDER_TIME <![CDATA[ <= ]]> #{eval.orderEndTime}
                and ORDER_TIME <![CDATA[ >= ]]> #{eval.orderStartTime}
            </if>
            <if test="eval.handStartTime != null and eval.handEndTime != null">
                and HAND_TIME <![CDATA[ <= ]]> #{eval.handEndTime}
                and HAND_TIME <![CDATA[ >= ]]> #{eval.handStartTime}
            </if>
            <if test="eval.evaStartTime != null and eval.evaEndTime != null">
                and EVA_TIME <![CDATA[ <= ]]> #{eval.evaEndTime}
                and EVA_TIME <![CDATA[ >= ]]> #{eval.evaStartTime}
            </if>
        </where>
        ORDER BY ORDER_TIME ASC
    </select>

</mapper>
