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

    <resultMap id="cbqydbmMap" type="com.tofly.watermeter.entity.Cbqydbm">
        <result property="qydbm" column="QYDBM"/>
        <result property="qybm" column="QYBM"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="yxbz" column="YXBZ"/>
        <result property="bz" column="BZ"/>
        <result property="yhdqh" column="YHDQH"/>
        <result property="qydmc" column="QYDMC"/>
        <result property="updateFlag" column="UPDATE_FLAG"/>
        <association property="yxbzName" column="YXBZ"
                     select="getYxbzName"/>
        <association property="qymc" column="QYBM"
                     select="getQymcName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>


    <select id="getYxbzName" resultType="string">
        select decode(#{yxbz}, '0', '启用', '1', '禁用')
        from dual
    </select>
    <select id="getQymcName" resultType="string">
        SELECT QYMC
        FROM TF_YWPN_CBQYBM_W
        WHERE QYBM = #{qybm}
    </select>


    <select id="allocationParagraph" resultType="map">
        SELECT b.QYDBM , b.QYDMC ,b.YHDQH ,NVL(a.NUM, 0) NUM , a.cbb_mc
        FROM
        (SELECT QYDBM , wm_concat(TO_CHAR(cbb_mc)) as cbb_mc , SUM(NUM) as NUM  FROM
        (SELECT QYDBM , COUNT(0) NUM , cc.cbb_mc FROM TF_YWPN_YHBINGQY_W aa LEFT JOIN TF_YWPN_QFKHJBXX_W bb on aa.yhbh = bb.yhbh
        LEFT JOIN TF_YWPN_DMACISCBB_W cc on bb.cbb = cc.cbb_bm
        GROUP BY aa.QYDBM , cc.cbb_mc ORDER BY QYDBM) GROUP BY QYDBM) a
        RIGHT JOIN TF_YWPN_CBQYDBM_W b ON a.QYDBM = b.QYDBM
        <where>
            b.YXBZ = '0'
            <if test="qybm != null and qybm != '' ">
                AND b.QYBM = #{qybm}
            </if>
            <if test="qydbm != null and qydbm != '' ">
                AND b.QYDBM = #{qydbm}
            </if>
        </where>
        ORDER BY b.QYDMC
    </select>
    <select id="getPeriod" resultType="java.lang.Integer">
        SELECT CBZQ
        FROM TF_YWPN_CBQYDBM_W
        WHERE YXBZ = '0'
        GROUP BY CBZQ
        ORDER BY CBZQ
    </select>

    <select id="getPlanIsCb" resultType="java.util.Map">
        SELECT b.QYDBM, COUNT(1) num
        FROM TF_YWPN_QFCBQFMX_W a,
             TF_YWPN_YHBINGQY_W b
        WHERE a.YHBH = b.YHBH
          AND b.QYDBM = #{qyd}
          AND a.XZBS = 1
          AND CBBZ = 0
        GROUP BY b.QYDBM
    </select>
    <select id="getDownloadCb" resultType="java.util.Map">
        SELECT a.YHBH, a.YHXM, a.XZBS, a.CBBZ, b.QYDBM
        FROM TF_YWPN_QFCBQFMX_W a,
             TF_YWPN_YHBINGQY_W b
        WHERE a.YHBH = b.YHBH
          AND b.QYDBM = #{qyd}
          AND a.XZBS = 0
          AND JHRQ IS NOT NULL
    </select>
    <select id="getQydCollect" resultType="java.util.Map">
        SELECT a.QYBM , a.QYMC , COUNT(b.QYDBM) qydNum
        FROM TF_YWPN_CBQYBM_W a left join TF_YWPN_CBQYDBM_W b
        on a.QYBM = b.qybm
        <where>
            a.yxbz = '0'
            <if test="inputcontent != null">
                and(a.QYBM like '%' || #{inputcontent} || '%'
                or
                a.QYMC like '%' || #{inputcontent} || '%')
            </if>
        </where>
        GROUP BY a.QYBM , a.QYMC
        order by a.QYMC
    </select>
    <select id="getMaxId" resultType="java.lang.Long">
        select nvl(max(QYDBM), 0) + 1
        from TF_YWPN_CBQYDBM_W
    </select>

    <insert id="saveCbqyList">
        insert into TF_YWPN_CBQYDBM_W (QYDBM , YXBZ , QYDMC , BZ , QYBM , YHDQH , CREATE_USER , CREATE_DATE , UPDATE_FLAG)
        <foreach collection="list" item="info" separator="union all">
            select
            #{info.qydbm},
            #{info.yxbz},
            #{info.qydmc},
            #{info.bz},
            #{info.qybm},
            #{info.yhdqh},
            #{info.createUser},
            #{info.createDate},
            #{info.updateFlag}
            from dual
        </foreach>
    </insert>

</mapper>
