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

    <resultMap id="bwSbXqMap" type="com.tofly.watermeter.entity.Bwrksbdatail">
        <id property="id" column="ID"/>
        <result property="bwLsbh" column="BW_LSBH"/>
        <result property="bwSbbh" column="BW_SBBH"/>
        <result property="bwSbcj" column="BW_SBCJ"/>
        <result property="bwSblx" column="BW_SBLX"/>
        <result property="bwSbkj" column="BW_SBKJ"/>
        <result property="bwSbzt" column="BW_SBZT"/>
        <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="bwGspq" column="BW_GSPQ"/>
        <result property="bwAzrq" column="BW_AZRQ"/>
        <result property="bwAzry" column="BW_AZRY"/>
        <result property="bwSbqd" column="BW_SBQD"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="bwBz" column="BW_BZ"/>
        <result property="bwBhqz" column="BW_BHQZ"/>
        <result property="rkid" column="RKID"/>
        <result property="rkpcid" column="RKPCID"/>
        <result property="zjpdTime" column="ZJPD_TIME"/>
        <result property="ckId" column="CK_ID"/>
        <result property="azfs" column="AZFS"/>
        <result property="azbm" column="AZBM"/>
        <result property="gddh" column="GDDH"/>
        <result property="sfncys" column="SFNCYS"/>
        <result property="jyz" column="JYZ"/>
        <result property="bzbh" column="BZBH"/>
        <result property="sbazdz" column="SBAZDZ"/>
        <result property="issyc" column="ISSYS"/>
        <result property="msg" column="MSG"/>
        <result property="jyzBm" column="JYZ_BM"/>
        <result property="synTime" column="SYN_TIME"/>
        <result property="synUser" column="SYN_USER"/>
        <result property="ysxz" column="YSXZ"/>
        <result property="sbyt" column="SBYT"/>
        <result property="sbytName" column="SBYT_NAME"/>
        <result property="ysxzName" column="YSXZ_NAME"/>
        <association property="synUserName" column="SYN_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="azryName" column="BW_AZRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="x" column="BW_SBBH"
                     select="getCoordinateX"/>
        <association property="y" column="BW_SBBH"
                     select="getCoordinateY"/>
        <association property="contract" column="CK_ID"
                     select="getContract"/>
        <association property="contractCode" column="CK_ID"
                     select="getContractCode"/>
    </resultMap>

    <select id="getContract" resultType="string">
        select CONTRACT from TF_YWPN_BWCKDATAIL_W where id = ${id}
    </select>

    <select id="getContractCode" resultType="string">
        select CONTRACT_CODE from TF_YWPN_BWCKDATAIL_W where id = ${id}
    </select>

    <resultMap id="bwrksbdatailMap" type="com.tofly.watermeter.entity.Bwrksbdatail" extends="bwSbXqMap">
        <association property="sbcjName" column="BW_SBCJ" select="getVendor"/>
        <association property="sbkjName" column="BW_SBKJ" select="getCal"/>
        <association property="sblxName" column="BW_SBLX" select="getType"/>
        <association property="state" column="BW_SBZT" select="getState"/>
        <!--        <association property="bwGspqName" column="BW_GSPQ" select="getGspqName"/>-->
    </resultMap>
    <!--    <select id="getGspqName" resultType="string">-->
    <!--        select QYMC-->
    <!--        from TF_YWPN_CBQYBM_W-->
    <!--        where PQBM = #{gspq}-->
    <!--    </select>-->

    <select id="getCoordinateX" resultType="string">
        select X coordinate
        from TF_YWPN_WATERMETER_W
        where SID = #{sbbh}
          and rownum = 1
    </select>

    <select id="getCoordinateY" resultType="string">
        select Y coordinate
        from TF_YWPN_WATERMETER_W
        where SID = #{sbbh}
          and rownum = 1
    </select>

    <select id="getVendor" resultType="string">
        <choose>
            <when test="id == null">
                select null from DUAL
            </when>
            <otherwise>
                select CNAME
                from TF_YWPN_SYSCHILDCODE_D
                where CCODE = to_char(#{id}) and pcid = 2
            </otherwise>
        </choose>

    </select>
    <select id="getCal" resultType="string">
        <choose>
            <when test="id == null">
                select null from DUAL
            </when>
            <otherwise>
                select CNAME
                from TF_YWPN_SYSCHILDCODE_D
                where CCODE = to_char(#{id}) and pcid = 1
            </otherwise>
        </choose>

    </select>
    <select id="getType" resultType="string">
        <choose>
            <when test="id == null">
                select null from DUAL
            </when>
            <otherwise>
                select CNAME
                from TF_YWPN_SYSCHILDCODE_D
                where CCODE = to_char(#{id}) and pcid = 3
            </otherwise>
        </choose>

    </select>
    <select id="getState" resultType="string">
        <choose>
            <when test="id == null">
                select null from DUAL
            </when>
            <otherwise>
                select CNAME
                from TF_YWPN_SYSCHILDCODE_D
                where CCODE = to_char(#{id}) and pcid = 8
            </otherwise>
        </choose>

    </select>

    <select id="turnOver" resultType="string">
        <choose>
            <when test="id == null">
                select null from DUAL
            </when>
            <otherwise>
                select CNAME
                from TF_YWPN_SYSCHILDCODE_D
                where CCODE = to_char(#{id}) and pcid = 9
            </otherwise>
        </choose>

    </select>
    <select id="getCodeById" resultType="string">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ID = #{id}
    </select>
    <select id="getById" resultType="com.tofly.watermeter.entity.Bwrksbdatail">
        select *
        from TF_YWPN_BWRKSBDATAIL_W
        where ID = #{sbId}
    </select>

    <select id="getSbById" resultMap="bwrksbdatailMap">
        select BW_SBKJ, BW_SBCJ, BW_SBLX, BW_SBZT, ID
        from TF_YWPN_BWRKSBDATAIL_W
        where ID = #{sbId}
    </select>
    <select id="getNumAndQz" resultType="long">
        select MAX(BW_SBBH) + 1
        from TF_YWPN_BWRKSBDATAIL_W
        where RKID = #{rkId}
          and BW_SBZT = '55'
    </select>

    <select id="getSbXq" resultMap="bwrksbdatailMap">
        select *
        from TF_YWPN_BWRKSBDATAIL_W
        where BW_SBBH = #{sbbh}
    </select>

    <select id="getByYhbh" resultMap="bwrksbdatailMap">
        select *
        from TF_YWPN_BWRKSBDATAIL_W
        where BW_KHBH = #{yhbh}
          and BW_SBZT = 57
    </select>

    <resultMap id="getTjMap" type="com.tofly.watermeter.custom.entity.StatisticResult">
        <result property="month" column="MONTH"/>
        <result property="allNum" column="ALLNUM"/>
        <result property="specificNUM" column="SPECIFICNUM"/>
        <result property="num" column="NUM"/>
        <result property="cjId" column="BWSBCJ"/>
        <result property="kjId" column="BWSBKJ"/>
        <result property="lxId" column="BWSBLX"/>
        <association property="sbcj" column="BWSBCJ" select="getVendor"/>
        <association property="sbkj" column="BWSBKJ" select="getCal"/>
        <association property="sblx" column="BWSBLX" select="getType"/>
    </resultMap>
    <sql id="statisSQL">
        <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>
    </sql>
    <select id="getInstall" resultMap="getTjMap">
        SELECT COUNT(*) as NUM , BW_SBCJ as BWSBCJ , BW_SBKJ as BWSBKJ , BW_SBLX as BWSBLX FROM TF_YWPN_BWRKSBDATAIL_W
        <where>
            <include refid="statisSQL"></include>
            <if test="sta.startTime != null and sta.endTime != null">
                and BW_AZRQ BETWEEN #{sta.startTime} AND #{sta.endTime}
            </if>
            and BW_SBZT='57'
        </where>
        GROUP BY BW_SBCJ,BW_SBKJ,BW_SBLX
    </select>


    <select id="getCkSb" resultMap="getTjMap">
        SELECT SUM(BW_SL) as NUM, BW_SBCJ as BWSBCJ , BW_SBKJ as BWSBKJ , BW_SBLX as BWSBLX FROM TF_YWPN_BWCKDATAIL_W
        <where>
            <include refid="statisSQL"></include>
            <if test="sta.startTime != null and sta.endTime != null">
                and CREATE_DATE BETWEEN #{sta.startTime} AND #{sta.endTime}
            </if>
        </where>
        GROUP BY BW_SBCJ,BW_SBKJ,BW_SBLX
    </select>

    <select id="getMonthCj" resultMap="getTjMap">
        SELECT
        <if test="sta.startMonth != null and sta.endMonth != null">TO_CHAR(BW_HBRQ , 'yyyyMM') MONTH ,</if>
        COUNT(*) as ALLNUM , BW_JBCJ as BWSBCJ FROM TF_YWPN_BWHBDATAIL_W
        <where>
            <include refid="hbSQL"></include>
        </where>
        GROUP BY BW_JBCJ
        <if test="sta.startMonth != null and sta.endMonth != null">
            , TO_CHAR(BW_HBRQ , 'yyyyMM') ORDER BY TO_CHAR(BW_HBRQ , 'yyyyMM')
        </if>
    </select>
    <select id="getMonthSpecific" resultMap="getTjMap">
        SELECT
        <if test="sta.startMonth != null and sta.endMonth != null">TO_CHAR(BW_HBRQ , 'yyyyMM') MONTH ,</if>
        COUNT(*) as SPECIFICNUM , BW_JBCJ as BWSBCJ , BW_JBLX as BWSBLX , BW_JBKJ as BWSBKJ FROM TF_YWPN_BWHBDATAIL_W
        <where>
            <include refid="hbSQL"></include>
        </where>
        GROUP BY BW_JBCJ, BW_JBLX, BW_JBKJ
        <if test="sta.startMonth != null and sta.endMonth != null">
            , TO_CHAR(BW_HBRQ , 'yyyyMM') ORDER BY TO_CHAR(BW_HBRQ , 'yyyyMM')
        </if>
    </select>
    <sql id="hbSQL">
        <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="sta.startMonth != null and sta.endMonth != null">
            and TO_CHAR(BW_HBRQ , 'yyyyMM') BETWEEN #{sta.startMonth} AND #{sta.endMonth}
        </if>
        <if test="sta.startTime != null and sta.endTime != null">
            and BW_HBRQ BETWEEN #{sta.startTime} AND #{sta.endTime}
        </if>
    </sql>

    <resultMap id="hbInfoMap" type="map">
        <result property="hbrq" column="BW_HBRQ"/>
        <result property="bz" column="BW_BZ"/>
        <result property="hbyy" column="BW_HBYY"/>
        <result property="xbbh" column="BW_XBBH"/>
        <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="xbqd" column="BW_XBQD"/>
        <result property="jbzd" column="BW_JBZD"/>
        <result property="jbbh" column="BW_JBBH"/>
        <association property="xbcj" column="BW_XBCJ" select="getVendor"/>
        <association property="xbkj" column="BW_XBKJ" select="getCal"/>
        <association property="xblx" column="BW_XBLX" select="getType"/>
        <association property="jbcj" column="BW_JBCJ" select="getVendor"/>
        <association property="jblx" column="BW_JBLX" select="getType"/>
        <association property="jbkj" column="BW_JBKJ" select="getCal"/>
        <association property="hbry" column="BW_HBRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>

    <select id="getHbInfo" resultMap="hbInfoMap">
        SELECT h.BW_HBRY,
        h.BW_HBRQ,
        h.BW_BZ,
        h.BW_HBYY,
        h.BW_XBBH,
        h.BW_XBCJ,
        h.BW_XBKJ,
        h.BW_XBLX,
        h.BW_XBQD,
        h.BW_JBBH,
        h.BW_JBCJ,
        h.BW_JBLX,
        h.BW_JBKJ,
        h.BW_JBZD,
        s.BW_KHBH,
        s.BW_KHXM,
        s.BW_KHDZ,
        s.BW_KHDH
        FROM TF_YWPN_BWHBDATAIL_W h
        LEFT JOIN
        TF_YWPN_BWRKSBDATAIL_W s ON h.BW_JBBH = S.BW_SBBH
        WHERE h.BW_JBKJ = #{sta.bwSbkj}
        AND h.BW_JBCJ = #{sta.bwSbcj}
        AND h.BW_JBLX = #{sta.bwSblx}
        <if test="sta.startTime != null and sta.endTime != null">
            and h.BW_HBRQ BETWEEN #{sta.startTime} AND #{sta.endTime}
        </if>
    </select>
    <select id="getUserIdByName" resultType="long">
        select id
        from tf_smpn_user_x
        where real_name = #{userName}
    </select>


    <select id="getScrapInfo" resultType="java.util.Map">
        select from TF_YWPN_BWRKSBDATAIL_W
        <where>
            and BW_SBCJ = #{cj}
            and BW_SBLX = #{lx}
            and BW_SBKJ = #{kj}
            and BW_SBZT = #{state}
        </where>
    </select>


    <insert id="insertBatch">
        insert into
        TF_YWPN_BWRKSBDATAIL_W(ID,BW_LSBH,BW_SBBH,BW_SBCJ,BW_SBLX,BW_SBKJ,BW_SBZT,CREATE_DATE,CREATE_USER,RKID,RKPCID)
        <foreach collection="sbList" item="sb" separator="union all">
            select
            #{sb.id},
            #{sbAll.bwLsbh},
            #{sb.bwSbbh},
            #{sbAll.bwSbcj},
            #{sbAll.bwSblx},
            #{sbAll.bwSbkj},
            #{sbAll.bwSbzt},
            #{sbAll.createDate},
            #{sbAll.createUser},
            #{sb.rkid},
            #{sbAll.rkpcid}
            from dual
        </foreach>
    </insert>

    <update id="updateAzSb" parameterType="list">
        <foreach collection="list" open="begin" close=";end;" item="sb" separator=";">
            update TF_YWPN_BWRKSBDATAIL_W
            <set>
                <if test="sb.khbh != null ">
                    BW_KHBH=#{sb.khbh},
                </if>
                <if test="sb.khxm != null">
                    BW_KHXM=#{sb.khxm},
                </if>
                <if test="sb.khdz != null">
                    BW_KHDZ=#{sb.khdz},
                </if>
                <if test="sb.khdh != null">
                    BW_KHDH=#{sb.khdh},
                </if>
                BW_AZRQ=#{sb.azrq},
                ZJPD_TIME=#{sb.azrq},
                BW_AZRY=#{user},
                BW_SBQD= 0 ,
                BW_SBZT='57'
            </set>
            where BW_SBBH=#{sb.sbbh}
        </foreach>
    </update>
    <update id="updateGdZt">
        update TF_YWPN_GDGLINFO_W
        set LCJD = #{zt},
            ZZYY = '成功',
            ZJZT = 54
        where ID = #{gdId}
    </update>


</mapper>
