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

    <select id="outboundCount" resultType="map">
        select r.BW_SBCJ,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="r.bw_sbcj"/>
        </include>
        ) sbcj_name,
        sum(case when METER_USE is not null and METER_USE = 0 then 1 else 0 end) install_num,
        sum(case when METER_USE is not null and METER_USE = 1 then 1 else 0 end) change_num,
        sum(case when METER_USE is null then 1 else 0 end) not_num,
        count(*) num
        from TF_YWPN_BWRKSBDATAIL_W r
        left join TF_YWPN_BWCKDATAIL_W c on r.CK_ID = c.ID
        where
        to_char(c.CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.meterUse != null and dto.meterUse != ''">
            and METER_USE = #{dto.meterUse}
        </if>
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and r.BW_SBCJ in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <if test="dto.dept != null and dto.dept.size() != 0">
            and BW_LQBM in
            <foreach collection="dto.dept" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        group by r.BW_SBCJ
    </select>

    <select id="outboundByCal" resultType="map">
        select r.BW_SBKJ,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="r.BW_SBKJ"/>
        </include>
        ) sbkj_name,
        sum(case when r.BW_SBLX = 1 then 1 else 0 end) ptb_num,
        sum(case when r.BW_SBLX = 2 then 1 else 0 end) wlb_num,
        sum(case when r.BW_SBLX = 3 then 1 else 0 end) kb_num,
        sum(case when r.BW_SBLX = 4 then 1 else 0 end) ycb_num
        from TF_YWPN_BWCKDATAIL_W c left join TF_YWPN_BWRKSBDATAIL_W r on c.ID = r.CK_ID
        where to_char(c.CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end} and ck_id is not null
        <choose>
            <when test="dto.meterUse == 0">
                and meter_use = 0
            </when>
            <when test="dto.meterUse == 1">
                and meter_use = 1
            </when>
            <when test="dto.meterUse == 2">
                and meter_use is null
            </when>
        </choose>
        <if test="dto.dept != null and dto.dept.size() != 0">
            and BW_LQBM in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>

        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and r.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 r.bw_sbkj in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and r.bw_sblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
        group by r.BW_SBKJ
    </select>

    <select id="outboundInfo" resultType="map">
        select r.BW_SBCJ,
        r.BW_SBKJ,
        r.BW_SBLX,
        <include refid="table_code_query"/>
        r.BW_SBBH,
        METER_USE,
        (
        <include refid="table_code">
            <property name="pcid" value="40043"/>
            <property name="code" value="METER_USE"/>
        </include>
        ) use_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,
        c.CREATE_USER,
        (select REAL_NAME from TF_SMPN_USER_X where id = c.CREATE_USER) ckry_name,
        c.CREATE_DATE,
        c.BW_BZ
        from TF_YWPN_BWRKSBDATAIL_W r left join TF_YWPN_BWCKDATAIL_W c on c.id = r.CK_ID
        where
        to_char(c.CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end} and ck_id is not null
        <if test="dto.dept != null and dto.dept.size() != 0">
            and bw_lqbm in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        <choose>
            <when test="dto.meterUse == 0">
                and meter_use = 0
            </when>
            <when test="dto.meterUse == 1">
                and meter_use = 1
            </when>
            <when test="dto.meterUse == 2">
                and meter_use is null
            </when>
        </choose>
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and c.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 c.bw_sbkj in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and c.bw_sblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
    </select>

    <select id="changeTableCount" resultType="map">
        select BW_XBCJ,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="BW_XBCJ"/>
        </include>
        ) sbcj_name,
        count(*) num
        from TF_YWPN_BWHBDATAIL_W h
        where
        <include refid="changeTable_common"/>
        group by bw_xbcj
    </select>

    <select id="changeTableByCal" resultType="map">
        select BW_XBKJ,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="BW_XBKJ"/>
        </include>
        ) sbkj_name,
        sum(case when BW_XBLX = 1 then 1 else 0 end) ptb_num,
        sum(case when BW_XBLX = 3 then 1 else 0 end) wlb_num,
        sum(case when BW_XBLX = 4 then 1 else 0 end) kb_num,
        sum(case when BW_XBLX = 2 then 1 else 0 end) ycb_num
        from TF_YWPN_BWHBDATAIL_W h
        where
        <include refid="changeTable_common"/>
        group by BW_XBKJ
    </select>

    <select id="changeTableInfo" resultType="map">
        select YHBH,
        YHXM,
        YHDZ,
        LXDH,
        YSXZ,
        (select YSXZ_MC from TF_YWPN_QFMIDYSXZBM_E where YSXZ_BM = ysxz ) ysxz_name,
        BW_JBCJ,
        BW_JBLX,
        BW_JBKJ,
        (
        <include refid="table_code">
            <property name="code" value="BW_JBCJ"/>
            <property name="pcid" value="2"/>
        </include>
        ) jbcj_name,
        (
        <include refid="table_code">
            <property name="code" value="BW_JBLX"/>
            <property name="pcid" value="3"/>
        </include>
        ) jblx_name,
        (
        <include refid="table_code">
            <property name="code" value="BW_JBKJ"/>
            <property name="pcid" value="1"/>
        </include>
        ) jbkj_name,
        BW_JBZD,
        BW_XBCJ,
        BW_XBBH,
        BW_XBKJ,
        BW_XBLX,
        (
        <include refid="table_code">
            <property name="code" value="BW_XBCJ"/>
            <property name="pcid" value="2"/>
        </include>
        ) xbcj_name,
        (
        <include refid="table_code">
            <property name="code" value="BW_XBLX"/>
            <property name="pcid" value="3"/>
        </include>
        ) xblx_name,
        (
        <include refid="table_code">
            <property name="code" value="BW_XBKJ"/>
            <property name="pcid" value="1"/>
        </include>
        ) xbkj_name,
        BW_XBQD,
        BW_HBLX,
        (
        <include refid="table_code">
            <property name="code" value="BW_HBLX"/>
            <property name="pcid" value="9"/>
        </include>
        ) hblx_name,
        BW_HBBM,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = BW_HBBM) hbbm_name,
        BW_HBRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_HBRY) hbry_name,
        BW_HBRQ,
        BW_HBYY
        from TF_YWPN_BWHBDATAIL_W h
        left join TF_YWPN_QFKHJBXX_W k on h.BW_KHBH = k.YHBH
        where
        <include refid="changeTable_common"/>
    </select>

    <select id="scrapCount" resultType="map">
        select BW_SBCJ,
        (
        <include refid="table_code">
            <property name="code" value="BW_SBCJ"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        count(*) num
        from TF_YWPN_BWSBYJGL_W
        <where>
            <include refid="outBound_common">
                <property name="dept" value="BW_JSBM"/>
            </include>
            and BW_YJLX != 1
        </where>
        group by BW_SBCJ
    </select>

    <select id="scrapByCal" resultType="map">
        select BW_SBKJ,
        (
        <include refid="table_code">
            <property name="code" value="BW_SBKJ"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        <include refid="table_type_count_common"/>
        from TF_YWPN_BWSBYJGL_W
        where BW_YJLX != 1
        and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.dept != null and dto.dept.size() != 0">
            and BW_JSBM in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </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.cal != null and dto.cal.size() != 0">
            and bw_sbkj in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and bw_sblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
        group by BW_SBKJ
    </select>

    <select id="scrapByInfo" resultType="map">
        select BW_SBCJ,
        BW_SBKJ,
        BW_SBLX,
        SBZSBH,
        <include refid="table_code_query"/>
        decode(YJXX,'0','非库移交','1','库存移交') yjzt,
        BW_YJLX,
        (
        <include refid="table_code">
            <property name="code" value="BW_YJLX"/>
            <property name="pcid" value="9"/>
        </include>
        ) yjlx_name,
        BW_YJRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_YJRY) yjry_name,
        (select DEPARTMENT_ID from TF_SMPN_USER_X where id = BW_YJRY) bw_yjbm,
        (select NAME from TF_SMPN_DEPARTMENT_X where ID = (select DEPARTMENT_ID from TF_SMPN_USER_X where id = BW_YJRY))
        yjbm_name,
        BW_YJRQ,
        BW_JSBM,
        BW_YJYY,
        YJSM,
        (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,
        CREATE_DATE,CREATE_USER,
        (select REAL_NAME from TF_SMPN_USER_X where id = r.CREATE_USER) user_name
        from TF_YWPN_BWSBYJGL_W r
        <where>
            <include refid="outBound_common">
                <property name="dept" value="BW_JSBM"/>
            </include>
            and BW_YJLX != 1
        </where>
    </select>

    <select id="disputeCount" resultType="map">
        select BW_SBCJ,
        (
        <include refid="table_code">
            <property name="code" value="BW_SBCJ"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        count(*) num
        from TF_YWPN_BWZYSBJDDATAIL_W
        where
        <include refid="dispute_common"/>
        group by BW_SBCJ
    </select>

    <select id="disputeByCal" resultType="map">
        select BW_SBKJ,
        (
        <include refid="table_code">
            <property name="code" value="BW_SBKJ"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        <include refid="table_type_count_common"/>
        from TF_YWPN_BWZYSBJDDATAIL_W
        where
        <include refid="dispute_common"/>
        group by BW_SBKJ
    </select>

    <select id="disputeByInfo" resultType="map">
        select
        BW_YHBH,
        BW_YHXM,
        BW_YHDZ,
        BW_LXDH,
        SBBH,
        BW_SBCJ,
        BW_SBKJ,
        BW_SBLX,
        <include refid="table_code_query"/>
        decode(BW_JYJG,'1','合格','2','不合格') jyjg,
        BW_JYRQ,
        BW_JYRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_JYRY) jyry_name,
        CREATE_USER,
        (select REAL_NAME from TF_SMPN_USER_X where id = r.CREATE_USER) user_name,
        CREATE_DATE,
        BW_TBSL,
        BW_BSL,
        BW_BZ
        from TF_YWPN_BWZYSBJDDATAIL_W r
        where
        <include refid="dispute_common"/>
    </select>

    <sql id="outBound_common">
        to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.startStr} and #{dto.endStr}
        <if test="dto.dept != null and dto.dept.size() != 0">
            and ${dept} in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </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.cal != null and dto.cal.size() != 0">
            and bw_sbkj in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and bw_sblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
    </sql>

    <sql id="changeTable_common">
        to_char(h.CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.dept != null and dto.dept.size() != 0">
            and BW_HBBM in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_XBCJ 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_XBKJ in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and BW_XBLX in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
    </sql>

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

    <sql id="dispute_common">
        to_char(CREATE_DATE,'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="," index=")">
                #{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>
        <if test="dto.type != null and dto.type.size() != 0">
            and bw_sblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
    </sql>

    <sql id="table_type_count_common">
        sum
            (case when BW_SBLX = 1 then 1 else 0 end)
            ptb_num,
        sum(case when BW_SBLX = 2 then 1 else 0 end) wlb_num,
        sum(case when BW_SBLX = 3 then 1 else 0 end) kb_num,
        sum(case when BW_SBLX = 4 then 1 else 0 end) ycb_num
    </sql>

    <sql id="table_code_query">
        (
        <include refid="table_code">
            <property name="code" value="r.BW_SBCJ"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        (
        <include refid="table_code">
            <property name="code" value="r.BW_SBLX"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        (
        <include refid="table_code">
            <property name="code" value="r.BW_SBKJ"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
    </sql>
</mapper>