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

    <resultMap id="cbqybmMap" type="com.tofly.watermeter.entity.Cbqybm">
        <id property="qybm" column="QYBM"/>
        <result property="qymc" column="QYMC"/>
        <result property="yxbz" column="YXBZ"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="bz" column="BZ"/>
        <result property="updateFlag" column="UPDATE_FLAG"/>
        <association property="yxbzName" column="YXBZ"
                     select="getYxbzName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"></association>
    </resultMap>
    <resultMap id="qyAndQdList" type="com.tofly.watermeter.custom.entity.meterRead.QyAndQd">
        <result property="qybm" column="QYBM"/>
        <result property="qymc" column="QYMC"/>
    </resultMap>
    <resultMap id="getQydList" extends="qyAndQdList" type="com.tofly.watermeter.custom.entity.meterRead.QyAndQd">
        <collection property="cbqydbmList" column="QYBM"
                    select="getQdList"/>
    </resultMap>
    <select id="getYxbzName" resultType="string">
        select decode(#{yxbz}, '0', '启用', '1', '禁用')
        from dual
    </select>
    <select id="getQyAndQd" resultMap="getQydList">
        SELECT QYBM, QYMC
        FROM TF_YWPN_CBQYBM_W
        WHERE YXBZ = '0'
        order by QYMC
    </select>

    <select id="getQdList" resultType="map">
        SELECT a.QYDBM, a.QYBM, a.YHDQH, a.QYDMC
        FROM TF_YWPN_CBQYDBM_W a
        RIGHT JOIN TF_YWPN_YHBINGQY_W b
        ON a.QYDBM = b.QYDBM
        WHERE a.QYBM = #{qybm}
        and a.YXBZ = '0'
        GROUP BY a.QYDBM, a.QYBM, a.YHDQH, a.QYDMC
        order by a.QYDMC
    </select>
    <select id="getQyAndQdAllocation" resultMap="getQydList">
        SELECT a.QYBM, a.QYMC
        FROM TF_YWPN_CBQYBM_W a
        RIGHT JOIN TF_YWPN_YHBINGQY_W b
        ON a.QYBM = b.QYBM
        GROUP BY a.QYBM, a.QYMC
        order by a.QYMC
    </select>
    <resultMap id="getAllQyAndQyd" type="com.tofly.watermeter.custom.entity.meterRead.QyAndQd" extends="qyAndQdList">
        <collection property="cbqydbmList" column="QYBM"
                    select="getAllQdList"/>
    </resultMap>
    <select id="getAllQdList" resultType="map">
        SELECT a.QYDBM, a.QYBM, a.YHDQH, a.QYDMC
        FROM TF_YWPN_CBQYDBM_W a
        WHERE a.QYBM = #{qybm}
        GROUP BY a.QYDBM, a.QYBM, a.YHDQH, a.QYDMC
        order by a.QYDMC
    </select>
    <select id="getQyAndQdPlan" resultMap="getAllQyAndQyd">
        SELECT QYBM, QYMC
        FROM TF_YWPN_CBQYBM_W
        order by QYMC
    </select>

</mapper>
