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

    <resultMap id="bwhbdatailMap" type="com.tofly.watermeter.entity.Bwhbdatail">
        <id property="id" column="ID"/>
        <result property="bwLsbh" column="BW_LSBH"/>
        <result property="bwHblx" column="BW_HBLX"/>
        <result property="bwXbbh" column="BW_XBBH"/>
        <result property="bwXbcj" column="BW_XBCJ"/>
        <result property="bwXblx" column="BW_XBLX"/>
        <result property="bwXbkj" column="BW_XBKJ"/>
        <result property="bwXbqd" column="BW_XBQD"/>
        <result property="bwJbbh" column="BW_JBBH"/>
        <result property="bwJbcj" column="BW_JBCJ"/>
        <result property="bwJblx" column="BW_JBLX"/>
        <result property="bwJbkj" column="BW_JBKJ"/>
        <result property="bwJbzd" column="BW_JBZD"/>
        <result property="bwHbyy" column="BW_HBYY"/>
        <result property="bwHbry" column="BW_HBRY"/>
        <result property="bwHbrq" column="BW_HBRQ"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="bwBz" column="BW_BZ"/>
        <result property="bwKhbh" column="BW_KHBH"/>
        <result property="bwKhxm" column="BW_KHXM"/>
        <result property="bwKhdz" column="BW_KHDZ"/>
        <result property="bwKhdh" column="BW_KHDH"/>
        <result property="jbAzrq" column="JB_AZRQ"/>
        <result property="syDate" column="SY_DATE"/>
        <result property="azfs" column="AZFS"/>
        <result property="hbsl" column="HBSL"/>
        <result property="jsfs" column="JSFS"/>
        <result property="qfh" column="QFH"/>
        <result property="sbxh" column="SBXH"/>
        <result property="sbm" column="SBM"/>
        <result property="sqr" column="SQR"/>
        <result property="bwHbbm" column="BW_HBBM"/>
        <result property="bwJbqd" column="BW_JBQD"/>
        <result property="isSync" column="IS_SYNC"/>
        <result property="msg" column="MSG"/>
        <result property="signature" column="SIGNATURE"/>
        <result property="image" column="IMAGE"/>
        <result property="jyz" column="JYZ"/>
        <result property="ncys" column="NCYS"/>
        <result property="auditOpinion" column="AUDIT_OPINION"/>
        <result property="remark" column="REMARK"/>
        <result property="modelCode" column="MODEL_CODE"/>
        <result property="modelName" column="MODEL_NAME"/>
        <result property="location" column="LOCATION"/>
        <result property="meterBox" column="METER_BOX"/>
        <association property="hbryName" column="BW_HBRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="signaturePath" column="SIGNATURE"
                     select="getSignaturePath"/>
        <association property="hblxName" column="BW_HBLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getType"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <collection property="imagePath" column="image"
                     select="getImagePath"/>
    </resultMap>

    <select id="getSignaturePath" resultType="string">
        select FILE_PATH
        from TF_YWPN_FILE_W
        where ID = #{signature}
    </select>

    <select id="getImagePath" resultType="map">
        select ID,FILE_PATH
        from TF_YWPN_FILE_W
        where ID in (${image})
    </select>

    <resultMap id="getExchangeMap" type="com.tofly.watermeter.custom.entity.CustomHbdetail">
        <id property="id" column="ID"/>
        <result property="bwLsbh" column="BW_LSBH"/>
        <result property="bwHblx" column="BW_HBLX"/>
        <result property="bwXbbh" column="BW_XBBH"/>
        <result property="bwXbcj" column="BW_XBCJ"/>
        <result property="bwXblx" column="BW_XBLX"/>
        <result property="bwXbkj" column="BW_XBKJ"/>
        <result property="bwXbqd" column="BW_XBQD"/>
        <result property="bwJbbh" column="BW_JBBH"/>
        <result property="bwJbcj" column="BW_JBCJ"/>
        <result property="bwJblx" column="BW_JBLX"/>
        <result property="bwJbkj" column="BW_JBKJ"/>
        <result property="bwJbzd" column="BW_JBZD"/>
        <result property="bwHbyy" column="BW_HBYY"/>
        <result property="bwHbry" column="BW_HBRY"/>
        <result property="bwHbrq" column="BW_HBRQ"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="bwBz" column="BW_BZ"/>
        <result property="bwKhbh" column="BW_KHBH"/>
        <result property="bwKhxm" column="BW_KHXM"/>
        <result property="bwKhdz" column="BW_KHDZ"/>
        <result property="bwKhdh" column="BW_KHDH"/>
        <result property="jbAzrq" column="JB_AZRQ"/>
        <result property="syDate" column="SY_DATE"/>
        <association property="hbryName" column="BW_HBRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="hblxName" column="BW_HBLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getType"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>
    <select id="getExchange" resultMap="getExchangeMap">
        SELECT *
        FROM TF_YWPN_BWHBDATAIL_W
        <where>
            <!--    and kh.KHBH || kh.KHXM || kh.SSPQ LIKE '%' || #{bwglSbjd.srk,jdbcType=VARCHAR} || '%'-->
            <if test="inputcontent != null and inputcontent != ''">
                and BW_KHBH || BW_KHXM || BW_KHDZ || BW_KHDH LIKE '%' || #{inputcontent,jdbcType=VARCHAR} || '%'
            </if>
            <if test="sbbh != null and sbbh != ''">
                and BW_XBBH || BW_JBBH LIKE '%' || #{sbbh,jdbcType=VARCHAR} || '%'
            </if>
            <if test="startTime != null and endTime != null">
                and BW_HBRQ BETWEEN #{startTime} AND #{endTime}
            </if>
        </where>
        order by BW_HBRQ DESC
    </select>

    <resultMap id="statisticHb" type="map">
        <result property="bwSbkj" column="BW_JBKJ"/>
        <result property="bwSblx" column="BW_JBLX"/>
        <result property="num" column="NUM"/>
        <result property="syDate" column="SY_DATE"/>
        <result property="bwSbcj" column="BW_JBCJ"/>
        <result property="jbAzrq" column="JB_AZRQ"/>
        <result property="syDate" column="SY_DATE"/>
        <result property="bwHbrq" column="BW_HBRQ"/>
        <result property="bwJbbh" column="BW_JBBH"/>
        <association property="sbkjName" column="BW_JBKJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getVendor"/>
        <association property="sblxName" column="BW_JBLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getType"/>
        <association property="sbcjName" column="BW_JBCJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCal"/>
    </resultMap>

    <select id="getSyDate" resultMap="statisticHb">
        SELECT BW_JBCJ , BW_JBKJ , BW_JBLX , COUNT(1) NUM
        FROM TF_YWPN_BWHBDATAIL_W
        <where>
            <if test="cj != null">
                and BW_JBCJ IN
                <foreach collection="cj" item="c" open="(" separator="," close=")">
                    #{c}
                </foreach>
            </if>
            <if test="kj != null">
                and BW_JBKJ IN
                <foreach collection="kj" item="k" open="(" separator="," close=")">
                    #{k}
                </foreach>
            </if>
            <if test="lx != null">
                and BW_JBLX IN
                <foreach collection="lx" item="l" open="(" separator="," close=")">
                    #{l}
                </foreach>
            </if>
            <if test="syDate != null">
                AND SY_DATE <![CDATA[ >= ]]> #{syDate}
            </if>
        </where>
        GROUP BY BW_JBCJ , BW_JBKJ , BW_JBLX
    </select>
    <select id="getSyDateDetails" resultMap="statisticHb">
        SELECT BW_JBBH, BW_JBCJ, BW_JBKJ, BW_JBLX, JB_AZRQ, BW_HBRQ, SY_DATE
        FROM TF_YWPN_BWHBDATAIL_W
        WHERE BW_JBCJ = #{cj}
          AND BW_JBLX = #{lx}
          AND BW_JBKJ = #{kj}
          AND SY_DATE <![CDATA[ >= ]]> #{syDate}
    </select>
    <select id="getSyDateDetailsList" resultMap="statisticHb">
        SELECT BW_JBBH, BW_JBCJ, BW_JBKJ, BW_JBLX, JB_AZRQ, BW_HBRQ, SY_DATE
        FROM TF_YWPN_BWHBDATAIL_W
        WHERE BW_JBCJ = #{cj}
          AND BW_JBLX = #{lx}
          AND BW_JBKJ = #{kj}
          AND SY_DATE <![CDATA[ >= ]]> #{syDate}
    </select>
    <select id="getYjInfo" resultMap="getExchangeMap">
        SELECT BW_JBBH , BW_JBCJ , BW_JBLX , BW_JBKJ , BW_KHBH FROM TF_YWPN_BWHBDATAIL_W
        WHERE ID IN
        (SELECT MAX(ID) FROM TF_YWPN_BWHBDATAIL_W
        WHERE BW_KHBH IN
        <foreach collection="list" open="(" close=")" item="yjsb" separator=",">
            #{yjsb.yhbh}
        </foreach>
        GROUP BY BW_KHBH)
    </select>

</mapper>
