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

    <select id="countReturnTable" resultType="map">
        select dept_id,
        (select name from TF_SMPN_DEPARTMENT_X where id = dept_id) dept_name,
        count(*) sum_num
        from (select (select DEPARTMENT_ID from TF_SMPN_USER_X where ID = BW_YJRY) dept_id
        from TF_YWPN_BWSBYJGL_W
        where BW_YJLX = 1
        and to_char(BW_YJRQ, 'yyyy-mm-dd') between #{dto.start} and #{dto.end})
        <where>
            <if test="dto.dept != null and dto.dept.size() != 0">
                dept_id in
                <foreach collection="dto.dept" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
        </where>
        group by dept_id
    </select>

    <select id="countByVendor" resultType="map">
        select dept_id,
        (select name from TF_SMPN_DEPARTMENT_X where id = dept_id) dept_name,
        BW_SBCJ,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="bw_sbcj"/>
        </include>
        ) sbcj_name,
        count(BW_SBCJ) sum_num
        from (select (select DEPARTMENT_ID from TF_SMPN_USER_X where ID = BW_YJRY) dept_id,BW_SBCJ
        from TF_YWPN_BWSBYJGL_W
        where BW_YJLX = 1 and to_char(BW_YJRQ, 'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <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>
        )
        <where>
            <if test="dto.dept != null and dto.dept.size() != 0">
                dept_id in
                <foreach collection="dto.dept" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
        </where>
        group by dept_id,BW_SBCJ
    </select>

    <select id="queryInfoReturnTable" resultType="map">
        select * from (select id,
        bw_lsbh,
        bw_sbcj,
<!--        (-->
<!--        <include refid="table_code">-->
<!--            <property name="pcid" value="2"/>-->
<!--            <property name="code" value="bw_sbcj"/>-->
<!--        </include>-->
<!--        ) sbcj_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = bw_sbcj
        and pcid = 2) sbcj_name,
        bw_sblx,
<!--        (-->
<!--        <include refid="table_code">-->
<!--            <property name="pcid" value="3"/>-->
<!--            <property name="code" value="bw_sblx"/>-->
<!--        </include>-->
<!--        ) sblx_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = bw_sblx
        and pcid = 3) sblx_name,
        bw_sbkj,
<!--        (-->
<!--        <include refid="table_code">-->
<!--            <property name="pcid" value="1"/>-->
<!--            <property name="code" value="bw_sbkj"/>-->
<!--        </include>-->
<!--        ) sbkj_name,-->

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

        bw_yjlx,
<!--        (-->
<!--        <include refid="table_code">-->
<!--            <property name="pcid" value="9"/>-->
<!--            <property name="code" value="bw_yjlx"/>-->
<!--        </include>-->
<!--        ) yjlx_name,-->

        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = bw_yjlx
        and pcid = 9) yjlx_name,

        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,
        create_user,
        (select REAL_NAME from TF_SMPN_USER_X where id = b.create_user) user_name,
        create_date,
        bw_bz,
        sbzsbh,
        bw_yjry,
        (select REAL_NAME from TF_SMPN_USER_X where id = b.bw_yjry) yjry_name,
        (select name from TF_SMPN_DEPARTMENT_X where ID = (select DEPARTMENT_ID from TF_SMPN_USER_X where id =
        b.bw_yjry)) yjbm_name,
        (select DEPARTMENT_ID from TF_SMPN_USER_X where id =
        b.bw_yjry) dept_id,
        yjsm,
        contract_note
        from TF_YWPN_BWSBYJGL_W b
        where BW_YJLX = 1
        and to_char(BW_YJRQ,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <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="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        )
        <where>
            <if test="dto.dept != null and dto.dept.size() != 0">
                dept_id in
                <foreach collection="dto.dept" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
        </where>
    </select>

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

</mapper>
