<?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.custom.mapper.meterBusiness.WareHousingMapper">

    <select id="count" resultType="map">
        select
        BW_SBCJ,
        count(*) rksl,
        sum(case when BW_SBZT = 1 or BW_SBZT = 8 then 1 else 0 end) kczs,
        (
        <include refid="codeQuery">
            <property name="ccode" value="b.BW_SBCJ"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name
        from TF_YWPN_BWRKSBDATAIL_W b
        <where>
            <if test="dto.vendor != null and dto.vendor.size() != 0">
                and BW_SBCJ in
                <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.caliber != null and dto.caliber.size() != 0">
                and BW_SBKJ in
                <foreach collection="dto.caliber" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end != ''">
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </if>
        </where>
        group by BW_SBCJ
    </select>

    <select id="countByVendor" resultType="map">
        select BW_SBKJ,
        (
        <include refid="codeQuery">
            <property name="ccode" value="b.BW_SBKJ"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        sum(case when BW_SBLX = 1 then 1 else 0 end) jxb,
        sum(case when BW_SBLX = 3 then 1 else 0 end) ickb,
        sum(case when BW_SBLX = 4 then 1 else 0 end) ycb,
        sum(case when BW_SBLX = 2 then 1 else 0 end) wlwb
        from TF_YWPN_BWRKSBDATAIL_W b
        <where>
            <choose>
                <when test="dto.state == 1">
                    (bw_sbzt = 1 or BW_SBZT = 8)
                </when>
                <otherwise/>
            </choose>
            <if test="dto.vendor != null and dto.vendor.size() != 0">
                and BW_SBCJ in
                <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.caliber != null and dto.caliber.size() != 0">
                and BW_SBKJ in
                <foreach collection="dto.caliber" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end != ''">
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </if>
        </where>

        group by BW_SBKJ
    </select>

    <select id="countByCaliber" resultType="map">
        select id,
        bw_lsbh,
        bw_sbbh,
        bw_sbcj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        create_user,
        (select REAL_NAME from TF_SMPN_USER_X where ID = b.create_user) create_user_name,
        create_date
        from TF_YWPN_BWRKSBDATAIL_W b
        <where>
            <if test="dto.state == 1">
                (bw_sbzt = 1 or bw_sbzt = 8)
            </if>
            <if test="dto.vendor != null and dto.vendor.size() != 0">
                and BW_SBCJ in
                <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.caliber != null and dto.caliber.size() != 0">
                and BW_SBKJ in
                <foreach collection="dto.caliber" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.type != null">
                and BW_SBLX = #{dto.type}
            </if>
            <if test="dto.code != null and dto.code != ''">
                and bw_sbbh like '%' || #{dto.code} || '%'
            </if>
            <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end != ''">
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </if>
        </where>
    </select>

    <sql id="codeQuery">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = to_char(${ccode})
          and pcid = ${pcid}
    </sql>

    <insert id="bulkStorage">
        insert into TF_YWPN_BWRKSBDATAIL_W (ID, BW_LSBH, BW_SBBH, BW_SBCJ, BW_SBLX, BW_SBKJ, BW_SBZT, CREATE_USER,
        CREATE_DATE)
        <foreach collection="tail" item="t" separator="union all">
            select
            #{t.id},
            #{t.bwLsbh},
            #{t.bwSbbh},
            #{t.bwSbcj},
            #{t.bwSblx},
            #{t.bwSbkj},
            1,
            #{t.createUser},
            sysdate
            from DUAL
        </foreach>
    </insert>

    <select id="countInventory" resultType="com.tofly.watermeter.custom.entity.meterBusiness.dto.TableInfoDto">
        select BW_SBCJ cj, BW_SBLX lx, BW_SBKJ kj, count(*) num
        from TF_YWPN_BWRKSBDATAIL_W
        where BW_SBCJ is not null
          and BW_SBLX is not null
          and BW_SBKJ is not null
          and BW_SBZT = 1
        group by BW_SBCJ, BW_SBLX, BW_SBKJ
    </select>

    <select id="getCode" resultType="com.tofly.watermeter.custom.entity.meterBusiness.dto.CodeDto">
        select CCODE, CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where PCID = #{pcid}
    </select>

    <select id="queryInfo" parameterType="list"
            resultType="com.tofly.watermeter.custom.entity.meterBusiness.dto.RemoveTableDto">
        select bw_sbcj sbcj, bw_sblx sblx, bw_sbkj sbkj, count(*) num
        from TF_YWPN_BWRKSBDATAIL_W
        where ID in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
        group by bw_sbcj, bw_sblx, bw_sbkj
    </select>

    <insert id="insertBatch">
        insert into TF_YWPN_BWRKDETAIL_W (ID, BW_LSBH, BW_SBCJ,BW_SBKJ,BW_SBLX, BW_SL, CREATE_USER, CREATE_DATE, BW_BZ)
        <foreach collection="list" item="t" separator="union all">
            select
            #{t.id},
            #{t.bwLsbh},
            #{t.bwSbcj},
            #{t.bwSbkj},
            #{t.bwSblx},
            #{t.bwSl},
            #{t.createUser},
            sysdate,
            #{t.bwBz}
            from DUAL
        </foreach>
    </insert>

    <insert id="saveFirstInspection">
        insert into TF_YWPN_BWSJDATAIL_W (id, bw_rkid, bw_pcbh,BW_SBCJ, bw_sbkj,BW_SBLX, bw_sbsl, bw_cjsl, bw_cjbl,
        is_check,CREATE_TIME,CREATE_USER)
        <foreach collection="list" item="f" separator="union all">
            select
            #{f.id},
            #{f.bwRkid},
            #{f.bwPcbh},
            #{f.bwSbcj},
            #{f.bwSbkj},
            #{f.bwSblx},
            #{f.bwSbsl},
            #{f.bwCjsl},
            #{f.bwCjbl},
            0,
            sysdate,
            #{f.createUser}
            from DUAL
        </foreach>
    </insert>

    <select id="getAllInfo" resultType="map">
        select id,
        bw_lsbh,
        bw_sbbh,
        bw_sbcj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        create_user,
        BW_SBZT,
        (select REAL_NAME from TF_SMPN_USER_X where ID = b.create_user) create_user_name,
        create_date
        from TF_YWPN_BWRKSBDATAIL_W b
        <where>
            <if test="dto.vendor != null and dto.vendor.size() != 0">
                and BW_SBCJ in
                <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.caliber != null and dto.caliber.size() != 0">
                and BW_SBKJ in
                <foreach collection="dto.caliber" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.code != null and dto.code != ''">
                and bw_sbbh like '%' || #{dto.code} || '%'
            </if>
            <if test="dto.start != null and dto.end != null">
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </if>
        </where>

    </select>

    <select id="getAllTableCode" resultType="string" databaseId="oracle" fetchSize="10000">
        select BW_SBBH
        from TF_YWPN_BWRKSBDATAIL_W
    </select>

    <select id="getCjbl" resultType="long">
        select SJBL
        from TF_YWPN_BWSJBLGL_W
        where SBKJ = #{sbkj}
    </select>

    <select id="countBatchNo" resultType="map">
        select BW_LSBH,
        sum(case when BW_SBZT = 1 or BW_SBZT = 8 then 1 else 0 end) kc_num,
        sum(case when BW_SBZT = 1 or BW_SBZT = 8 then 0 else 1 end) sy_num,
        count(*) sum_num
        from TF_YWPN_BWRKSBDATAIL_W
        <where>
            <if test="dto.batchNo != null and dto.batchNo !=''">
                and BW_LSBH like '%' || #{dto.batchNo} || '%'
            </if>
            <if test="dto.vendor != null and dto.vendor.size() != 0">
                and BW_SBCJ in
                <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.caliber != null and dto.caliber.size() != 0">
                and BW_SBKJ in
                <foreach collection="dto.caliber" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end != ''">
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </if>
        </where>
        group by BW_LSBH
    </select>

    <select id="batchNoInfo" resultType="map" fetchSize="1000">
        select id,
        bw_lsbh,
        bw_sbbh,
        bw_sbcj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        BW_SBZT,
        (
        <include refid="codeQuery">
            <property name="ccode" value="BW_SBZT"/>
            <property name="pcid" value="8"/>
        </include>
        ) sbzt_name,
        create_user,
        (select REAL_NAME from TF_SMPN_USER_X where ID = b.create_user) create_user_name,
        create_date
        from TF_YWPN_BWRKSBDATAIL_W b
        <where>
            <if test="dto.vendor != null and dto.vendor.size() != 0">
                and BW_SBCJ in
                <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.caliber != null and dto.caliber.size() != 0">
                and BW_SBKJ in
                <foreach collection="dto.caliber" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end != ''">
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </if>
            <if test="dto.batchNo != null and dto.batchNo != ''">
                and BW_LSBH like '%' || #{dto.batchNo} || '%'
            </if>
            <choose>
                <when test="dto.state == 0">
                    and (bw_sbzt = 1 or bw_sbzt = 8)
                </when>
                <when test="dto.state == 1">
                    and bw_sbzt not in (1,8)
                </when>
            </choose>
        </where>
    </select>

</mapper>