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

    <resultMap id="bwrktotalMap" type="com.tofly.watermeter.entity.Bwrktotal">
        <id property="id" column="ID"/>
        <result property="bwLsbh" column="BW_LSBH"/>
        <result property="bwSbcj" column="BW_SBCJ"/>
        <result property="bwSblx" column="BW_SBLX"/>
        <result property="bwSbkj" column="BW_SBKJ"/>
        <result property="bwKczs" column="BW_KCZS"/>
        <result property="bwSysl" column="BW_SYSL"/>
        <result property="bwKcsl" column="BW_KCSL"/>
    </resultMap>
    <resultMap id="bwrksbdatailMap" type="com.tofly.watermeter.entity.Bwrktotal" extends="bwrktotalMap">

    </resultMap>


    <resultMap id="sbKcMap" type="map">
        <result property="bwSbkj" column="BW_SBKJ"></result>
        <result property="bwSblx" column="BW_SBLX"></result>
        <result property="allNum" column="ALLNUM"></result>
        <result property="kcNum" column="KCNUM"></result>
        <result property="syNum" column="SYNUM"></result>
        <result property="bwSbcj" column="BW_SBCJ"></result>
        <association property="sbkjName" column="BW_SBKJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sblxName" column="BW_SBLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sbcjName" column="BW_SBCJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
    </resultMap>

    <select id="getSbKc" resultMap="sbKcMap">
        SELECT BW_SBLX ,BW_SBKJ , BW_SBCJ , SUM(BW_KCZS) as ALLNUM,SUM(BW_KCSL) as KCNUM,SUM(BW_SYSL) as SYNUM
        FROM TF_YWPN_BWRKTOTAL_W
        <where>
            <if test="cj != null">
                and BW_SBCJ IN
                <foreach collection="cj" item="c" open="(" separator="," close=")">
                    #{c}
                </foreach>
            </if>
            <if test="kj != null">
                and BW_SBKJ IN
                <foreach collection="kj" item="k" open="(" separator="," close=")">
                    #{k}
                </foreach>
            </if>
            <if test="lx != null">
                and BW_SBLX IN
                <foreach collection="lx" item="l" open="(" separator="," close=")">
                    #{l}
                </foreach>
            </if>
        </where>
        GROUP BY BW_SBLX , BW_SBKJ , BW_SBCJ
    </select>

    <resultMap id="getCjMap" type="map">
        <result property="num" column="NUM"/>
        <association property="cjName" column="CJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
    </resultMap>
    <select id="getkcSta" resultMap="getCjMap">
        SELECT BW_SBCJ CJ, SUM(BW_KCSL) NUM
        FROM TF_YWPN_BWRKTOTAL_W
        GROUP BY BW_SBCJ
    </select>
    <select id="getXzSta" resultMap="getCjMap">
        SELECT BW_SBCJ CJ, COUNT(1) NUM
        FROM TF_YWPN_BWRKSBDATAIL_W
        WHERE BW_SBZT = '57'
        GROUP BY BW_SBCJ
    </select>
    <select id="getCkSta" resultMap="getCjMap">
        SELECT BW_SBCJ CJ, SUM(BW_SL) NUM
        FROM TF_YWPN_BWCKDATAIL_W
        GROUP BY BW_SBCJ
    </select>
    <select id="getBfSta" resultMap="getCjMap">
        SELECT BW_SBCJ CJ, COUNT(1) NUM
        FROM TF_YWPN_BWSBYJGL_W
        WHERE BW_YJLX <![CDATA[ <> ]]> 63
        GROUP BY BW_SBCJ
    </select>
    <select id="getHbSta" resultMap="getCjMap">
        SELECT BW_JBCJ CJ, COUNT(1) NUM
        FROM TF_YWPN_BWHBDATAIL_W
        GROUP BY BW_JBCJ
    </select>
    <select id="getZySta" resultMap="getCjMap">
        SELECT BW_SBCJ CJ, COUNT(1) NUM
        FROM TF_YWPN_BWZYSBJDDATAIL_W
        GROUP BY BW_SBCJ
    </select>

    <select id="queryMeterInfo" resultType="map">
        select b.bw_sbbh,
        b.bw_sbcj,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where pcid = 1 and CCODE = TO_CHAR( b.BW_SBKJ)) sbkj_name,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where pcid = 2 and CCODE = TO_CHAR(b.BW_SBCJ)) sbcj_name,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where pcid = 3 and CCODE = TO_CHAR(b.BW_SBLX)) sblx_name,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where pcid = 8 and CCODE = TO_CHAR(b.BW_SBZT)) state,
        b.bw_sblx,
        b.BW_SBZT,
        q.YHXM bw_khxm,
        q.YHBH bw_khbh,
        q.YHDZ bw_khdz,
        q.AZRQ bw_azrq,
        BW_AZRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_AZRY) azry_name,
        w.X,
        w.Y,
        b.bw_sbkj,
        q.YSXZ,
        SBAZDZ,
        BZBH,
        (select YSXZ_MC from TF_YWPN_QFMIDYSXZBM_E where YSXZ_BM = q.YSXZ) ysxz_name
        from TF_YWPN_BWRKSBDATAIL_W b
        left join TF_YWPN_WATERMETER_W w on w.SID = b.BW_SBBH
        left join TF_YWPN_QFKHJBXX_W q on b.BW_SBBH = q.SBBH
        <where>
            <if test="statistic.sbbh != null and statistic.sbbh != ''">
                and q.yhbh || q.YHXM || b.BW_SBBH || q.YHDZ like concat('%',concat(#{statistic.sbbh},'%'))
            </if>
            <if test="statistic.sbzt != null and statistic.sbzt != ''">
                and b.BW_SBZT = #{statistic.sbzt}
            </if>
            <if test="statistic.bwSbkj != null and statistic.bwSbkj != ''">
                and b.BW_SBKJ = #{statistic.bwSbkj}
            </if>
            <if test="statistic.bwSbcj != null and statistic.bwSbcj != ''">
                and b.BW_SBCJ = #{statistic.bwSbcj}
            </if>
            <if test="statistic.bwSblx != null and statistic.bwSblx != ''">
                and b.bw_sblx = #{statistic.bwSblx}
            </if>
            <if test="statistic.startTime != null and statistic.endTime != null">
                and b.bw_azrq between #{statistic.startTime} and #{statistic.endTime}
            </if>
            <if test="statistic.ysxz != null and statistic.ysxz != ''">
                and q.ysxz in
                <foreach collection="statistic.ysxz.split(',')" item="y" open="(" separator="," close=")">
                    #{y}
                </foreach>
            </if>
        </where>
    </select>


</mapper>
