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

    <resultMap id="tfPublicLeakageInfoMap" type="com.tofly.xrtygis.entity.TfPublicLeakageInfo">
        <id property="id" column="ID"/>
        <result property="openId" column="OPEN_ID"/>
        <result property="estimateLeakageWater" column="ESTIMATE_LEAKAGE_WATER"/>
        <result property="leakName" column="LEAK_NAME"/>
        <result property="leakType" column="LEAK_TYPE"/>
        <result property="leakPlace" column="LEAK_PLACE"/>
        <result property="leakLevel" column="LEAK_LEVEL"/>
        <result property="leakAddress" column="LEAK_ADDRESS"/>
        <result property="leakLongitude" column="LEAK_LONGITUDE"/>
        <result property="leakLatitude" column="LEAK_LATITUDE"/>
        <result property="leakReason" column="LEAK_REASON"/>
        <result property="dmaId" column="DMA_ID"/>
        <result property="orderId" column="ORDER_ID"/>
        <result property="disposeSuggest" column="DISPOSE_SUGGEST"/>
        <result property="annex" column="ANNEX"/>
        <result property="remark" column="REMARK"/>
        <result property="isRewardWater" column="IS_REWARD_WATER"/>
        <result property="verifyUser" column="VERIFY_USER"/>
        <result property="verifyTime" column="VERIFY_TIME"/>
        <result property="verifyStatus" column="VERIFY_STATUS"/>
        <result property="rewardWaterVolume" column="REWARD_WATER_VOLUME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="rewardWaterId" column="REWARD_WATER_ID"/>
        <result property="yxSynState" column="YX_SYN_STATE"/>
        <association property="dmaName" column="DMA_ID" select="getDmaName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.xrtygis.mapper.IFileMapper.getUserName"/>
        <association property="verifyUserName" column="VERIFY_USER"
                     select="com.tofly.xrtygis.mapper.IFileMapper.getUserName"/>
        <association property="leakLevelName" column="LEAK_LEVEL" select="getLeakLevelName"/>
        <association property="isRewardWaterName" column="IS_REWARD_WATER" select="getIsRewardWaterName"/>
        <association property="leakTypeName" column="LEAK_TYPE" select="getLeakTypeName"/>
        <association property="verifyStatusName" column="VERIFY_STATUS" select="getVerifyStatusName"/>
        <association property="auditList" column="ID" select="getAuditList"/>
        <association property="handleList" column="ORDER_ID" select="getHandleList"/>
    </resultMap>

    <select id="getDmaName" resultType="string">
        select DMA_NAME
        from TF_YWPN_DMAREGION_W
        where ID = #{dmaId}
    </select>

    <select id="getLeakLevelName" resultType="string">
        SELECT decode(#{leakLevel}, '1', '严重', '2', '一般') as leakLevelName
        FROM dual
    </select>

    <select id="getIsRewardWaterName" resultType="string">
        SELECT decode(#{isRewardWater}, '0', '不进行奖励', '1', '进行奖励') as isRewardWaterName
        FROM dual
    </select>

    <select id="getLeakTypeName" resultType="string">
        SELECT decode(#{leakType}, '1', '明漏', '2', '暗漏') as leakTypeName
        FROM dual
    </select>

    <select id="getVerifyStatusName" resultType="string">
        SELECT decode(#{verifyStatus}, '1', '真实且处置中', '2', '核实有误', '3', '真实且处置完成') as verifyStatusName
        FROM dual
    </select>

    <select id="getTfPublicLeakageInfoPage" resultMap="tfPublicLeakageInfoMap">
        SELECT *
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <if test="tfPublicLeakageInfoDto.startDate != null and tfPublicLeakageInfoDto.startDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy-MM-dd hh:mm:ss') >= #{tfPublicLeakageInfoDto.startDate}
        </if>
        <if test="tfPublicLeakageInfoDto.endDate != null and tfPublicLeakageInfoDto.endDate.trim() != ''">
            <![CDATA[ and to_char(CREATE_TIME,'yyyy-MM-dd hh:mm:ss') <= #{tfPublicLeakageInfoDto.endDate}   ]]>
        </if>
        <if test="tfPublicLeakageInfoDto.leakName != null and tfPublicLeakageInfoDto.leakName.trim() != ''">
            and LEAK_NAME like '%' || #{tfPublicLeakageInfoDto.leakName} || '%'
        </if>
        <if test="tfPublicLeakageInfoDto.leakAddress != null and tfPublicLeakageInfoDto.leakAddress.trim() != ''">
            and LEAK_ADDRESS like '%' || #{tfPublicLeakageInfoDto.leakAddress} || '%'
        </if>
        <if test="tfPublicLeakageInfoDto.createUser != null and tfPublicLeakageInfoDto.createUser != ''">
            and CREATE_USER = #{tfPublicLeakageInfoDto.createUser}
        </if>
        <if test="tfPublicLeakageInfoDto.verifyStatus != null and tfPublicLeakageInfoDto.verifyStatus.trim() != ''">
            and VERIFY_STATUS = #{tfPublicLeakageInfoDto.verifyStatus}
        </if>
    </select>

    <select id="getPublicPeopleByYear" resultType="int">
        SELECT count (distinct CREATE_USER)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
    </select>

    <select id="getLeakagePassByYear" resultType="int">
        SELECT count (*)
        FROM
        TF_YMPN_AUDIT_W t1
        LEFT JOIN TF_PUBLIC_LEAKAGE_INFO t2 ON t2.ID = t1.AUDIT_PLAN_ID
        WHERE
        t1.AUDIT_TYPE = '021'
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(t2.CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
    </select>

    <select id="getEstimateWaterByYear" resultType="integer">
        SELECT SUM (ESTIMATE_LEAKAGE_WATER)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
    </select>

    <select id="getRewardWaterByYear" resultType="integer">
        SELECT SUM (REWARD_WATER_VOLUME)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
    </select>

    <select id="getLeakageInfoByYear" resultType="int">
        SELECT count (*)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
    </select>

    <select id="getPublicPeopleByQuarter" resultType="int">
        SELECT count (distinct CREATE_USER)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
        <if test="currentQuarterDate != null and currentQuarterDate.trim() != ''">
            and to_char(CREATE_TIME,'q') = #{currentQuarterDate}
        </if>
    </select>

    <select id="getLeakagePassByQuarter" resultType="int">
        SELECT count (*)
        FROM
        TF_YMPN_AUDIT_W t1
        LEFT JOIN TF_PUBLIC_LEAKAGE_INFO t2 ON t2.ID = t1.AUDIT_PLAN_ID
        WHERE
        t1.AUDIT_TYPE = '021'
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(t2.CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
        <if test="currentQuarterDate != null and currentQuarterDate.trim() != ''">
            and to_char(t2.CREATE_TIME,'q') = #{currentQuarterDate}
        </if>
    </select>

    <select id="getEstimateWaterByQuarter" resultType="integer">
        SELECT SUM (ESTIMATE_LEAKAGE_WATER)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
        <if test="currentQuarterDate != null and currentQuarterDate.trim() != ''">
            and to_char(CREATE_TIME,'q') = #{currentQuarterDate}
        </if>
    </select>

    <select id="getRewardWaterByQuarter" resultType="integer">
        SELECT SUM (REWARD_WATER_VOLUME)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
        <if test="currentQuarterDate != null and currentQuarterDate.trim() != ''">
            and to_char(CREATE_TIME,'q') = #{currentQuarterDate}
        </if>
    </select>

    <select id="getLeakageInfoByQuarter" resultType="int">
        SELECT count (*)
        FROM
        TF_PUBLIC_LEAKAGE_INFO
        WHERE
        1=1
        <!--        <if test="dmaId != null and dmaId != ''">-->
        <!--            and DMA_ID = #{dmaId}-->
        <!--        </if>-->
        <if test="currentYearDate != null and currentYearDate.trim() != ''">
            and to_char(CREATE_TIME,'yyyy') = #{currentYearDate}
        </if>
        <if test="currentQuarterDate != null and currentQuarterDate.trim() != ''">
            and to_char(CREATE_TIME,'q') = #{currentQuarterDate}
        </if>
    </select>


    <resultMap id="auditMap" type="com.tofly.pojo.comm.Audit">
        <id property="id" column="ID"/>
        <result property="auditType" column="AUDIT_TYPE"/>
        <result property="auditPlanId" column="AUDIT_PLAN_ID"/>
        <result property="auditState" column="AUDIT_STATE"/>
        <result property="auditTime" column="AUDIT_TIME"/>
        <result property="auditNote" column="AUDIT_NOTE"/>
        <result property="auditUser" column="AUDIT_USER"/>
        <result property="auditLevel" column="AUDIT_LEVEL"/>
        <association property="auditStateName" column="AUDIT_STATE" select="getAuditStateName"/>
        <association property="auditUserName" column="AUDIT_USER"
                     select="com.tofly.xrtygis.mapper.IFileMapper.getUserName"/>
    </resultMap>

    <select id="getAuditStateName" resultType="string">
        select decode(#{auditState}, '0', '未审核', '1', '通过', '2', '未通过', '3', '不可审批') as auditStateName
        from dual
    </select>

    <select id="getAuditList" resultMap="auditMap">
        select *
        from TF_YMPN_AUDIT_W
        where AUDIT_TYPE = '021'
          and AUDIT_PLAN_ID = #{id}
    </select>

    <resultMap id="handleMap" type="com.tofly.xrtygis.entity.vo.TfPublicLeakageHandleVo">
        <id property="id" column="ID"/>
        <result property="slbm" column="SLBM"/>
        <result property="slry" column="SLRY"/>
        <association property="slbmName" column="SLBM" select="slbmName"/>
        <association property="slryName" column="SLRY" select="com.tofly.xrtymis.mapper.GdglinfoMapper.getSlryName"/>
    </resultMap>

    <select id="slbmName" resultType="string">
        select NAME
        FROM TF_SMPN_DEPARTMENT_X
        WHERE ID = #{slbm}
    </select>

    <select id="getHandleList" resultMap="handleMap">
        select *
        from TF_YWPN_GDINFO_W
        where ID = #{orderId}
    </select>

</mapper>
