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

    <resultMap id="gjglMap" type="com.tofly.watermeter.entity.Gjgl">
        <id property="id" column="ID"/>
        <result property="collectionType" column="COLLECTION_TYPE"/>
        <result property="collectionStatus" column="COLLECTION_STATUS"/>
        <result property="isDeleted" column="IS_DELETED"/>
        <result property="collectionNumber" column="COLLECTION_NUMBER"/>
        <result property="collectionName" column="COLLECTION_NAME"/>
    </resultMap>
    <select id="collectAnalyzeOne" resultType="java.util.Map">
        SELECT a.COLLECTION_NUMBER, a.COLLECTION_NAME, a.COLLECTION_TYPE, NVL(SUM(c.CBSL), 0) as ALL_SL
        FROM TF_YWPN_GJGL_W a
        LEFT JOIN TF_YWPN_GJGLXQ_W b ON a.COLLECTION_NUMBER = b.COLLECTION_NUMBER
        LEFT JOIN TF_YWPN_READMETERHIS_W c ON b.CLIENT_NUMBER = c.YHBH
        <where>
            a.COLLECTION_STATUS = '1'
            and a.IS_DELETED != '0'
            <if test="inputcontent != null and inputcontent != ''">
                and (a.COLLECTION_NUMBER like '%'||#{inputcontent}||'%'
                or a.COLLECTION_NAME like '%'||#{inputcontent}||'%' )
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                and c.MONTH_PLAN BETWEEN #{startDate} and #{endDate}
            </if>
            <if test="collectionType != null and collectionType != ''">
                and COLLECTION_TYPE = #{collectionType}
            </if>
        </where>
        GROUP BY a.COLLECTION_NUMBER, a.COLLECTION_NAME, a.COLLECTION_TYPE
    </select>

    <select id="collectAnalyzeTwo" resultType="java.util.Map">
        SELECT c.MONTH_PLAN, NVL(SUM(c.CBSL), 0) as ALL_SL
        FROM TF_YWPN_GJGLXQ_W b
        LEFT JOIN TF_YWPN_READMETERHIS_W c ON b.CLIENT_NUMBER = c.YHBH
        where b.COLLECTION_NUMBER = #{collectionNumber}
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            and c.MONTH_PLAN BETWEEN #{startDate} and #{endDate}
        </if>
        GROUP BY c.MONTH_PLAN
        order by c.MONTH_PLAN
    </select>
    <select id="collectAnalyzeThree" resultType="java.util.Map">
        select aaa.* , nvl(bbb.ljsl , 0) ljsl from
        <!--根据条件查询筛选用户信息-->
        (SELECT aa.YHBH , bb.QYMC , cc.QYDMC, dd.YHXM , dd.LXDH , dd.YHDZ, ee.YSXZ_MC as ysxz_name, ff.CBB_MC as
        cbb_name, gg.PQMC as GSPQ_NAME , dd.sbbh , dd.sbkj as sbkj_name
        FROM TF_YWPN_GJGLXQ_W zz JOIN TF_YWPN_QFKHJBXX_W dd ON zz.CLIENT_NUMBER = dd.YHBH
        LEFT JOIN TF_YWPN_YHBINGQY_W aa ON zz.CLIENT_NUMBER = aa.YHBH
        LEFT JOIN TF_YWPN_CBQYBM_W bb ON aa.QYBM = bb.QYBM
        LEFT JOIN TF_YWPN_CBQYDBM_W cc ON aa.QYDBM = cc.QYDBM
        left join TF_YWPN_QFMIDYSXZBM_E ee ON dd.ysxz = ee.YSXZ_BM
        left join TF_YWPN_DMACISCBB_W ff on dd.cbb = ff.CBB_BM
        left join TF_YWPN_QFCBCBPQ_W gg on dd.CBPQ = gg.PQBM
        <where>
            <if test="collectionNumber != null and collectionNumber != ''">
                zz.COLLECTION_NUMBER = #{collectionNumber}
            </if>
        </where>
        ) aaa
        join
        <!--查询历史水量-->
        (SELECT aa.YHBH , nvl(SUM(aa.CBSL) , 0) ljsl
        FROM TF_YWPN_READMETERHIS_W aa join TF_YWPN_GJGLXQ_W bb
        on aa.yhbh = bb.CLIENT_NUMBER
        where COLLECTION_NUMBER = #{collectionNumber}
        <if test="startDate != null and endDate != null and startDate != '' and endDate != ''">
            AND aa.CBYF BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY aa.YHBH) bbb ON aaa.yhbh = bbb.yhbh
        order by aaa.QYMC , aaa.QYDMC
    </select>


</mapper>
