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

    <resultMap id="cbqydwaterMap" type="com.tofly.watermeter.entity.Cbqydwater">
        <result property="cbqyMc" column="CBQY_MC"/>
        <result property="cbqyBm" column="CBQY_BM"/>
        <result property="cbqydBm" column="CBQYD_BM"/>
        <result property="qydmc" column="QYDMC"/>
        <result property="yearmonth" column="YEARMONTH"/>
        <result property="waterValue" column="WATER_VALUE"/>
        <result property="addDay" column="ADD_DAY"/>
    </resultMap>
    <select id="getTrendCollectOne" resultMap="cbqydwaterMap">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, d.MONTH_PLAN as YEARMONTH , SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_READMETERHIS_W d ON a.YHBH = d.YHBH
        <where>
            d.CBBZ = '1' AND d.IS_GENERAL = #{isGeneral}
            <if test="yearMonth != null and yearMonth != ''">
                and d.MONTH_PLAN = #{yearMonth}
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND d.CBYF BETWEEN #{startDate} AND #{endDate}
            </if>
            <if test="list != null">
                AND a.QYBM IN
                <foreach collection="list" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
        </where>
        GROUP BY a.QYBM , b.QYMC , d.MONTH_PLAN
        ORDER BY b.QYMC ,d.MONTH_PLAN
    </select>

    <select id="trendCollectOne" resultMap="cbqydwaterMap">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, d.CBYF as YEARMONTH , SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_READMETEREASHIS_W d ON a.YHBH = d.YHBH
        where d.CBYF is not null
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND d.CBYF BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="list != null">
            AND a.QYBM IN
            <foreach collection="list" open="(" item="qybm" separator="," close=")">
                #{qybm}
            </foreach>
        </if>
        GROUP BY a.QYBM , b.QYMC , d.cbyf
        ORDER BY b.QYMC ,d.cbyf
    </select>

    <select id="getTrendCollectOneHistory" resultMap="cbqydwaterMap">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, d.CBYF as YEARMONTH , SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_QFCBQFMXLS_W d ON a.YHBH = d.YHBH
        <where>
            d.SBLX != '3'
            <if test="quaryType == 1">
                AND d.CBYF != #{yearMonth}
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND d.CBYF BETWEEN #{startDate} AND #{endDate}
            </if>
            <if test="list != null">
                AND a.QYBM IN
                <foreach collection="list" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
        </where>
        GROUP BY a.QYBM , b.QYMC , d.CBYF
        ORDER BY b.QYMC , d.CBYF
    </select>

    <select id="getTrendCollectTwo" resultMap="cbqydwaterMap">
        SELECT a.QYDBM as CBQYD_BM, c.QYDMC as QYDMC , SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_READMETERHIS_W d ON a.YHBH = d.YHBH
        <where>
            d.CBBZ = '1' AND d.IS_GENERAL = #{isGeneral}
            <if test="yearMonth != null and yearMonth != ''">
                and d.MONTH_PLAN = #{yearMonth}
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND d.CBYF BETWEEN #{startDate} AND #{endDate}
            </if>
            <if test="list != null">
                AND a.QYDBM IN
                <foreach collection="list" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybm != null and qybm != ''">
                AND a.QYBM = #{qybm}
            </if>
        </where>
        GROUP BY a.QYDBM , c.QYDMC
        ORDER BY c.QYDMC
    </select>

    <select id="trendCollectTwo" resultMap="cbqydwaterMap">
        SELECT a.QYDBM as CBQYD_BM, c.QYDMC as QYDMC , SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_READMETEREASHIS_W d ON a.YHBH = d.YHBH
        <where>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND d.CBYF BETWEEN #{startDate} AND #{endDate}
            </if>
            <if test="list != null">
                AND a.QYDBM IN
                <foreach collection="list" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybm != null and qybm != ''">
                AND a.QYBM = #{qybm}
            </if>
        </where>
        GROUP BY a.QYDBM , c.QYDMC
        ORDER BY c.QYDMC
    </select>

    <select id="getTrendCollectTwoHistory" resultMap="cbqydwaterMap">
        SELECT a.QYDBM as CBQYD_BM, c.QYDMC as QYDMC , SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_QFCBQFMXLS_W d ON a.YHBH = d.YHBH
        <where>
            <if test="quaryType == 1">
                AND d.CBYF != #{yearMonth}
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND d.CBYF BETWEEN #{startDate} AND #{endDate}
            </if>
            <if test="list != null">
                AND a.QYDBM IN
                <foreach collection="list" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybm != null and qybm != ''">
                AND a.QYBM = #{qybm}
            </if>
        </where>
        GROUP BY a.QYDBM , c.QYDMC
        ORDER BY c.QYDMC
    </select>
    <select id="getTrendCollectThreeHistory" resultMap="cbqydwaterMap">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, c.QYDMC as QYDMC, a.QYDBM as CBQYD_BM, d.CBYF as YEARMONTH,
        SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a
        LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_QFCBQFMXLS_W d ON a.YHBH = d.YHBH
        <where>
            <if test="quaryType == 1">
                AND d.CBYF != #{yearMonth}
            </if>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND d.CBYF BETWEEN #{startDate} AND #{endDate}
            </if>
            <if test="qydbmList != null">
                AND a.QYDBM IN
                <foreach collection="qydbmList" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybmList != null">
                AND a.QYBM IN
                <foreach collection="qybmList" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
        </where>
        GROUP BY a.QYBM , b.QYMC , c.QYDMC , a.QYDBM , d.CBYF
        ORDER BY b.QYMC , c.QYDMC
    </select>
    <select id="getTrendCollectThree" resultType="com.tofly.watermeter.entity.Cbqydwater">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, c.QYDMC as QYDMC, a.QYDBM as CBQYD_BM, d.MONTH_PLAN as YEARMONTH,
        SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a
        LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_READMETERHIS_W d ON a.YHBH = d.YHBH
        <where>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                AND d.CBYF BETWEEN #{startDate} AND #{endDate}
            </if>
            <if test="qydbmList != null">
                AND a.QYDBM IN
                <foreach collection="qydbmList" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybmList != null">
                AND a.QYBM IN
                <foreach collection="qybmList" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
        </where>
        GROUP BY a.QYBM , b.QYMC , c.QYDMC , a.QYDBM , d.MONTH_PLAN
        ORDER BY b.QYMC , c.QYDMC , d.MONTH_PLAN
    </select>

    <select id="trendCollectThree" resultType="com.tofly.watermeter.entity.Cbqydwater">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, c.QYDMC as QYDMC, a.QYDBM as CBQYD_BM, d.CBYF as YEARMONTH,
        SUM(d.CBSL) as WATER_VALUE
        FROM TF_YWPN_YHBINGQY_W a
        LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_READMETEREASHIS_W d ON a.YHBH = d.YHBH
        where CBYF is not null
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND d.CBYF BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="qydbmList != null">
            AND a.QYDBM IN
            <foreach collection="qydbmList" open="(" item="qydbm" separator="," close=")">
                #{qydbm}
            </foreach>
        </if>
        <if test="qybmList != null">
            AND a.QYBM IN
            <foreach collection="qybmList" open="(" item="qybm" separator="," close=")">
                #{qybm}
            </foreach>
        </if>
        GROUP BY a.QYBM , b.QYMC , c.QYDMC , a.QYDBM , d.CBYF
        ORDER BY b.QYMC , c.QYDMC , d.CBYF
    </select>

    <select id="getTrendCollectFourHistory" resultType="com.tofly.watermeter.entity.Readmeterhis">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, c.QYDMC as QYDMC, a.QYDBM as CBQYD_BM , a.yhbh as YHBH , d.YHXM ,
        d.YHDZ , d.CBYF , d.CBSL
        FROM TF_YWPN_YHBINGQY_W a
        LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_QFCBQFMXLS_W d ON a.YHBH = d.YHBH
        <where>
            <if test="yearMonth != null and yearMonth != ''">
                AND d.CBYF = #{yearMonth}
            </if>
            <if test="qydbmList != null">
                AND a.QYDBM IN
                <foreach collection="qydbmList" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybmList != null">
                AND a.QYBM IN
                <foreach collection="qybmList" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
        </where>
        ORDER BY b.QYMC , c.QYDMC
    </select>
    <select id="getTrendCollectFour" resultType="com.tofly.watermeter.entity.Readmeterhis">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, c.QYDMC as QYDMC, a.QYDBM as CBQYD_BM , a.yhbh as YHBH , d.YHXM ,
        d.YHDZ , d.CBYF , d.CBSL , d.MONTH_PLAN
        FROM TF_YWPN_YHBINGQY_W a
        LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_READMETERHIS_W d ON a.YHBH = d.YHBH
        <where>
            d.CBBZ = '1' AND d.IS_GENERAL = #{isGeneral}
            <if test="yearMonth != null and yearMonth != ''">
                AND d.MONTH_PLAN = #{yearMonth}
            </if>
            <if test="qydbmList != null">
                AND a.QYDBM IN
                <foreach collection="qydbmList" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybmList != null">
                AND a.QYBM IN
                <foreach collection="qybmList" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
        </where>
        ORDER BY b.QYMC , c.QYDMC
    </select>

    <select id="trendCollectFour" resultType="com.tofly.watermeter.entity.Readmeterhis">
        SELECT a.QYBM as CBQY_BM, b.QYMC as CBQY_MC, c.QYDMC as QYDMC, a.QYDBM as CBQYD_BM , a.yhbh as YHBH , q.YHXM ,
        q.YHDZ , d.CBYF , d.CBSL , d.MONTH_PLAN
        FROM TF_YWPN_YHBINGQY_W a
        LEFT JOIN TF_YWPN_CBQYBM_W b ON a.QYBM = b.QYBM
        LEFT JOIN TF_YWPN_CBQYDBM_W c ON a.QYDBM = c.QYDBM
        LEFT JOIN TF_YWPN_READMETEREASHIS_W d ON a.YHBH = d.YHBH
        left join TF_YWPN_QFKHJBXX_W q on q.YHBH = d.YHBH
        <where>
            <if test="qydbmList != null">
                AND a.QYDBM IN
                <foreach collection="qydbmList" open="(" item="qydbm" separator="," close=")">
                    #{qydbm}
                </foreach>
            </if>
            <if test="qybmList != null">
                AND a.QYBM IN
                <foreach collection="qybmList" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
        </where>
        ORDER BY b.QYMC , c.QYDMC
    </select>

</mapper>
