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

    <resultMap id="bwsbyjglMap" type="com.tofly.watermeter.entity.Bwsbyjgl">
        <id property="id" column="ID"/>
        <result property="bwLsbh" column="BW_LSBH"/>
        <result property="bwSbcj" column="BW_SBCJ"/>
        <result property="bwSblx" column="BW_SBLX"/>
        <result property="bwSbkj" column="BW_SBKJ"/>
        <result property="sbcj" column="SBCJ"/>
        <result property="sblx" column="SBLX"/>
        <result property="sbkj" column="SBKJ"/>
        <result property="sbbh" column="SBBH"/>
        <result property="bwYjlx" column="BW_YJLX"/>
        <result property="bwBhqh" column="BW_BHQH"/>
        <result property="bwBhzh" column="BW_BHZH"/>
        <result property="bwYjsl" column="BW_YJSL"/>
        <result property="bwSbzd" column="BW_SBZD"/>
        <result property="bwYjyy" column="BW_YJYY"/>
        <result property="bwJsbm" column="BW_JSBM"/>
        <result property="bwJsry" column="BW_JSRY"/>
        <result property="bwYjrq" column="BW_YJRQ"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="bwRkbs" column="BW_RKBS"/>
        <result property="bwBz" column="BW_BZ"/>
        <result property="sbqz" column="SBQZ"/>
        <result property="sbzsbh" column="SBZSBH"/>
        <result property="bwYjry" column="BW_YJRY"/>
        <result property="checkFlag" column="CHECK_FLAG"/>
        <result property="yjxx" column="YJXX"/>
        <result property="yjsm" column="YJSM"/>
        <result property="userName" column="USER_NAME"/>
        <result property="address" column="ADDRESS"/>
        <result property="bwYjbm" column="BW_YJBM"/>
        <result property="contractNote" column="CONTRACT_NOTE"/>
        <association property="yjxxName" column="YJXX" select="getYjxxName"/>
        <association property="bwYjryName" column="BW_YJRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="yjlxName" column="BW_YJLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.turnOver"/>
        <association property="jsryName" column="BW_JSRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="sbcjName" column="BW_SBCJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getVendor"/>
        <association property="sbkjName" column="BW_SBKJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCal"/>
        <association property="sblxName" column="BW_SBLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getType"/>
        <association property="cjName" column="BW_SBCJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getVendor"/>
        <association property="kjName" column="BW_SBKJ"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCal"/>
        <association property="lxName" column="BW_SBLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getType"/>
    </resultMap>

    <select id="getYjxxName" resultType="string">
        select decode(#{yjxx} , '1' , '正常移交' , '2' , '无表号移交' , '3' , '表号重复移交')
        from dual
    </select>

    <insert id="insertBatch">
        insert into TF_YWPN_BWSBYJGL_W (ID , BW_SBCJ , BW_SBLX , BW_SBKJ , BW_YJLX , BW_YJSL , BW_JSBM ,
        BW_JSRY , BW_YJRQ , CREATE_USER , CREATE_DATE , SBZSBH , CHECK_FLAG ,BW_LSBH , YJXX , YJSM)
        <foreach collection="list" item="value" separator="union all">
            select
            #{value.id},
            #{value.bwSbcj},
            #{value.bwSblx},
            #{value.bwSbkj},
            #{yjgl.bwYjlx},
            1 ,
            #{yjgl.bwJsbm},
            #{yjgl.bwJsry},
            #{yjgl.bwYjrq},
            #{yjgl.createUser},
            #{yjgl.createDate},
            #{value.sbbh},
            <if test="value.checkFlag != null ">
            #{value.checkFlag} ,
            </if>
            <if test="value.checkFlag == null ">
            '2' ,
            </if>
            #{yjgl.bwLsbh} ,
            #{value.yjxx} ,
            <if test="value.yjsm != null and value.yjsm != ''">
                #{value.yjsm}
            </if>
            <if test="value.yjsm == null or value.yjsm == ''">
                ''
            </if>
            from dual
        </foreach>
    </insert>

    <select id="select" resultMap="bwsbyjglMap">
        select ID,
               BW_LSBH,
               BW_YJLX,
               BW_BHQH,
               BW_BHZH,
               BW_YJSL,
               BW_SBZD,
               BW_YJYY,
               BW_JSBM,
               BW_JSRY,
               BW_YJRQ,
               CREATE_USER,
               CREATE_DATE,
               BW_RKBS,
               BW_BZ,
               SBQZ,
               SBZSBH
        from TF_YWPN_BWSBYJGL_W
        where ID = #{id}
    </select>
    <resultMap id="scrapInfoMap" type="map">
        <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="bwSbbh" column="BW_SBBH"/>
        <result property="bwSbqd" column="BW_SBQD"/>
        <result property="bwYjyy" column="BW_YJYY"/>
        <association property="sbcjName" column="BW_SBCJ" select="codeById"/>
        <association property="sbkjName" column="BW_SBKJ" select="codeById"/>
        <association property="sblxName" column="BW_SBLX" select="codeById"/>
        <association property="yjlxName" column="BW_YJLX"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.turnOver"/>
        <association property="bwYjryName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>
    <select id="getScrapInfo" resultMap="scrapInfoMap">
        SELECT b.BW_KHBH , b.BW_KHXM , b.BW_KHDZ , b.BW_KHDH , B.BW_SBBH , b.BW_SBCJ , b.BW_SBLX , b.BW_SBKJ ,
        b.BW_SBQD, y.BW_YJLX , y.BW_YJYY , y.CREATE_USER
        FROM TF_YWPN_BWSBYJGL_W y LEFT JOIN TF_YWPN_BWRKSBDATAIL_W b ON y.SBZSBH = b.BW_SBBH
        WHERE y.BW_YJLX <![CDATA[ <> ]]> 63 and y.BW_SBCJ = #{cj} and y.BW_SBKJ = #{kj} and y.BW_SBLX = #{lx}
        <if test="start != null and end != null">
            and BW_YJRQ BETWEEN #{start} AND #{end}
        </if>
    </select>


    <resultMap id="getTjMap" type="map">
        <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="codeById"/>
        <association property="sbkj" column="BWSBKJ" select="codeById"/>
        <association property="sblx" column="BWSBLX" select="codeById"/>
    </resultMap>
    <select id="codeById" resultType="string">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ID = #{id}
    </select>
    <select id="getScrap" resultMap="getTjMap">
        SELECT COUNT(*) as NUM , BW_SBCJ as BWSBCJ , BW_SBKJ as BWSBKJ , BW_SBLX as BWSBLX FROM TF_YWPN_BWSBYJGL_W
        <where>
            and BW_YJLX <![CDATA[ <> ]]> 63
            <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>
            <if test="sta.startTime != null and sta.endTime != null">
                and BW_YJRQ BETWEEN #{sta.startTime} AND #{sta.endTime}
            </if>
        </where>
        GROUP BY BW_SBCJ,BW_SBKJ,BW_SBLX
    </select>


</mapper>
