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

    <resultMap id="bwrkdetailMap" type="com.tofly.watermeter.entity.Bwrkdetail">
        <id property="id" column="ID"/>
        <result property="bwPcmc" column="BW_PCMC"/>
        <result property="bwSbcj" column="BW_SBCJ"/>
        <result property="bwSblx" column="BW_SBLX"/>
        <result property="bwSbkj" column="BW_SBKJ"/>
        <result property="bwSl" column="BW_SL"/>
        <result property="bwBhqz" column="BW_BHQZ"/>
        <result property="bwBqh" column="BW_BQH"/>
        <result property="bwBzh" column="BW_BZH"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="bwBz" column="BW_BZ"/>
        <result property="bwLsbh" column="BW_LSBH"/>
        <result property="bwKczs" column="BW_KCZS"/>
        <result property="bwKcsl" column="BW_KCSL"/>
        <result property="bwSysl" column="BW_SYSL"/>
        <association property="sbcjName" column="BW_SBCJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <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="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"></association>

    </resultMap>
    <!--查询得到某个表的最大ID-->
    <select id="selectMaxId" resultType="long">
        select nvl(max(id), 0) + 1
        from ${tableName}
    </select>

    <resultMap id="rkRecordMap" type="com.tofly.watermeter.custom.entity.MeterInfo">
        <result property="lsbh" column="BW_LSBH"/>
        <result property="pcmc" column="BW_PCMC"/>
        <result property="createdate" column="CREATE_DATE"/>
        <result property="num" column="NUMBER1"/>
        <association property="sbcj" column="BW_SBCJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sblx" column="BW_SBLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sbkj" column="BW_SBKJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="createuser" column="CREATE1"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>
    <!--    and kh.KHBH || kh.KHXM || kh.SSPQ LIKE '%' || #{bwglSbjd.srk,jdbcType=VARCHAR} || '%'-->
    <select id="getRkRecord" resultMap="rkRecordMap">
        select BW_LSBH,BW_PCMC, CREATE_USER CREATE1 , CREATE_DATE ,BW_SL NUMBER1 ,
        BW_SBCJ , BW_SBLX , BW_SBKJ from TF_YWPN_BWRKDETAIL_W
        <where>
            <if test="sea.inputContent != null and sea.inputContent !='' ">
                and concat( BW_PCMC , BW_LSBH ) like concat( concat('%',#{sea.inputContent}) ,'%')
            </if>
            <if test="sea.time != null and sea.endTime != null">
                and TO_CHAR(CREATE_DATE,'YYYY-MM-DD') BETWEEN TO_CHAR(#{sea.time},'YYYY-MM-DD') AND
                TO_CHAR(#{sea.endTime},'YYYY-MM-DD')
            </if>
            <if test="sea.bwSbkj != null">
                and BW_SBKJ = #{sea.bwSbkj}
            </if>
            <if test="sea.bwSbcj != null">
                and BW_SBCJ = #{sea.bwSbcj}
            </if>
            <if test="sea.bwSblx != null">
                and BW_SBLX = #{sea.bwSblx}
            </if>
        </where>
        ORDER BY CREATE_DATE
    </select>

    <update id="updateNum">
        <foreach collection="map.entrySet()" open="begin" close=";end;" item="num" separator=";" index="id">
            update TF_YWPN_BWRKDETAIL_W
            <set>
                BW_KCSL=BW_KCSL - #{num},
                BW_SYSL=BW_SYSL + #{num}
            </set>
            where ID=#{id}
        </foreach>
    </update>

</mapper>
