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

    <!--    <select id="putInStorage" resultType="map">
            select 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) user_name,
            BW_LSBH,
            BW_SBBH,
            CREATE_USER,
            CREATE_DATE,
            BW_AZRQ,
            BW_AZRY,
            (select REAL_NAME from TF_SMPN_USER_X where ID = BW_AZRY) azry_name
            from TF_YWPN_BWRKSBDATAIL_W b
            where BW_SBBH = #{code}
            and bw_sbzt = #{state}
        </select>

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

        <select id="firstInspection" resultType="map">
            select
            BW_PCBH,
            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,
            IS_CHECK,
            BW_JYRQ
            from TF_YWPN_BWSJDATAIL_W where BW_PCBH = #{code}
        </select>

        <select id="outbound" resultType="map">
            select
            BW_LSBH,
            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_LQBM,
            (select NAME from TF_SMPN_DEPARTMENT_X where ID = BW_LQBM) lqbm_name,
            BW_LQRY,
            (select REAL_NAME from TF_SMPN_USER_X where id = BW_LQRY) lqry_name,
            CREATE_USER,
            (select REAL_NAME from TF_SMPN_USER_X where id = TF_YWPN_BWCKDATAIL_W.CREATE_USER) user_name,
            CREATE_DATE
            from TF_YWPN_BWCKDATAIL_W
            where ID in
            <foreach collection="idList" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </select>

        <select id="changeTable" resultType="map">
            select id,
            bw_lsbh,
            bw_hblx,
            bw_xbbh,
            bw_xbcj,
            bw_xblx,
            bw_xbkj,
            (
            <include refid="codeQuery">
                <property name="ccode" value="bw_xbcj"/>
                <property name="pcid" value="2"/>
            </include>
            ) xbcj_name,
            (
            <include refid="codeQuery">
                <property name="ccode" value="bw_xblx"/>
                <property name="pcid" value="3"/>
            </include>
            ) xblx_name,
            (
            <include refid="codeQuery">
                <property name="ccode" value="bw_xbkj"/>
                <property name="pcid" value="1"/>
            </include>
            ) xbkj_name,
            bw_jbbh,
            bw_jbcj,
            bw_jblx,
            bw_jbkj,
            (
            <include refid="codeQuery">
                <property name="ccode" value="bw_jbcj"/>
                <property name="pcid" value="2"/>
            </include>
            ) jbcj_name,

            (
            <include refid="codeQuery">
                <property name="ccode" value="bw_jblx"/>
                <property name="pcid" value="3"/>
            </include>
            ) jblx_name,

            (
            <include refid="codeQuery">
                <property name="ccode" value="bw_jbkj"/>
                <property name="pcid" value="1"/>
            </include>
            ) jbkj_name,
            bw_hbyy,
            bw_hbry,
            (select REAL_NAME from TF_SMPN_USER_X where id = bw_hbry) hbry_name,
            bw_hbrq
            from TF_YWPN_BWHBDATAIL_W
            where BW_XBBH = #{code}
        </select>

        <select id="turnOver" resultType="map">
            select id,
            bw_lsbh,
            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_yjlx,
            (select CNAME from TF_YWPN_SYSCHILDCODE_D where PCID = 9 and CCODE = bw_yjlx) yjlx_name,
            bw_yjyy,
            bw_jsbm,
            (select NAME from TF_SMPN_DEPARTMENT_X where id = bw_jsbm)    jsbm_name,
            bw_jsry,
                   (select REAL_NAME from TF_SMPN_USER_X where id = bw_jsry) jsry_name,
            bw_yjrq,
            sbzsbh,
            bw_yjry,
            decode(check_flag,'0','未检查','已检查') check_flag,
            decode(yjxx,'1','正常移交','2','无表号移交','表号重复移交') yjxx,
            yjsm
            from TF_YWPN_BWSBYJGL_W
            where SBZSBH = #{code}
        </select>-->

    <select id="countDay" resultType="map">
        select BW_SBCJ,
        BW_SBLX,
        BW_SBKJ,
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBCJ
        and pcid = 2) sbcj_name,
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBLX
        and pcid = 3) sblx_name,
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBKJ
        and pcid = 1) sbkj_name,
        round(sum(day), 0) sum_day,
        count(*) sum_num,
        round(round(sum(day), 0) / nullif(count(*), 0)) avg_day
        from (select r.BW_SBBH,
        r.BW_SBCJ,
        r.BW_SBKJ,
        r.BW_SBLX,
        decode(BW_HBRQ, null, sysdate, BW_HBRQ) - BW_AZRQ day
        from TF_YWPN_BWRKSBDATAIL_W r
        left join
        TF_YWPN_BWHBDATAIL_W h on r.BW_SBBH = h.BW_JBBH
        where BW_SBZT in (3, 9) and r.BW_AZRQ is not null
        <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.cal != null and dto.cal.size() != 0">
            and bw_sbkj in
            <foreach collection="dto.cal" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and bw_sblx in
            <foreach collection="dto.type" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        )
        group by BW_SBCJ, BW_SBKJ, BW_SBLX
    </select>

    <select id="queryInfo" resultType="map">
        select BW_SBCJ, BW_SBLX, BW_SBKJ,
<!--        (<include refid="codeQuery">-->
<!--            <property name="ccode" value="BW_SBCJ"/>-->
<!--            <property name="pcid" value="2"/>-->
<!--        </include>-->
<!--        ) sbcj_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBCJ
        and pcid = 2) sbcj_name,


<!--        (-->
<!--        <include refid="codeQuery">-->
<!--            <property name="ccode" value="BW_SBLX"/>-->
<!--            <property name="pcid" value="3"/>-->
<!--        </include>-->
<!--        ) sblx_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBLX
        and pcid = 3) sblx_name,



<!--        (-->
<!--        <include refid="codeQuery">-->
<!--            <property name="ccode" value="BW_SBKJ"/>-->
<!--            <property name="pcid" value="1"/>-->
<!--        </include>-->
<!--        ) sbkj_name,-->

        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBKJ
        and pcid = 1) sbkj_name,


        BW_HBRQ hbrq,
        r.BW_AZRQ,
        BW_SBBH,
        round(decode(BW_HBRQ, null, sysdate, BW_HBRQ) - BW_AZRQ,0) day
        from TF_YWPN_BWRKSBDATAIL_W r
        left join
        TF_YWPN_BWHBDATAIL_W h on r.BW_SBBH = h.BW_JBBH
        where BW_SBZT in (3, 9) and r.BW_AZRQ is not null
        <if test="vendor != null">
            and BW_SBCJ = #{vendor}
        </if>
        <if test="cal != null">
            and bw_sbkj = #{cal}
        </if>
        <if test="type != null">
            and bw_sblx = #{type}
        </if>
    </select>

    <sql id="codeQuery">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = '${ccode}'
          and pcid = ${pcid}
    </sql>
</mapper>