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

    <select id="count" resultType="map">
        select bw_sbcj, sbcj_name, rk_num,ck_num,(rk_num - ck_num) sy_num from (select BW_SBCJ,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        count(*) rk_num,
        sum(case when BW_SBZT != 1 and BW_SBZT != 8 then 1 else 0 end) ck_num
        from TF_YWPN_BWRKSBDATAIL_W a
        where 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="," close=")">
                #{v}
            </foreach>
        </if>
        group by BW_SBCJ)
    </select>

    <select id="queryByVendor" resultType="map">
        select BW_SBKJ,
        (
        <include refid="codeQuery">
            <property name="ccode" value="BW_SBKJ"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        (
        <include refid="type_count">
            <property name="type" value="1"/>
        </include>
        ) jxb_num,
        (
        <include refid="type_count">
            <property name="type" value="3"/>
        </include>
        ) icb_num,
        (
        <include refid="type_count">
            <property name="type" value="4"/>
        </include>
        ) ycb_num,
        (
        <include refid="type_count">
            <property name="type" value="2"/>
        </include>
        ) wlb_num
        from TF_YWPN_BWRKSBDATAIL_W b
        where
        <choose>
            <when test="dto.start != null and dto.end != null">
                to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                CREATE_DATE between add_months(sysdate,-1) and sysdate
            </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>
        <choose>
            <when test="dto.state == 1">
                and bw_sbzt != 1
            </when>
            <when test="dto.state == 2">
                and bw_sbzt = 1
            </when>
        </choose>
        group by BW_SBKJ
    </select>

    <select id="queryInfo" resultType="map">
        select r.id,
        r.bw_sbbh,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        r.bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        r.bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        r.bw_sbzt,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbzt"/>
            <property name="pcid" value="8"/>
        </include>
        ) lzzt,
        r.create_user,
        (select REAL_NAME from TF_SMPN_USER_X where ID = r.create_user)  rkry_name,
        r.create_date rksj,
        c.BW_LQRY,
        (select REAL_NAME from TF_SMPN_USER_X where ID = c.BW_LQRY) lqry_name,
        c.BW_LQBM,
        (select name from TF_SMPN_DEPARTMENT_X where ID = c.BW_LQBM) lqbm_name,
        c.CREATE_USER   ckry,
        (select REAL_NAME from TF_SMPN_USER_X where ID = c.create_user) ckry_name,
        c.CREATE_DATE cksj
        from TF_YWPN_BWRKSBDATAIL_W r
        left join TF_YWPN_BWCKDATAIL_W c on r.CK_ID = c.ID
        where r.BW_SBLX = #{dto.type}
        <choose>
            <when test="dto.start != null and dto.end != null">
               and to_char(r.CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
               and r.CREATE_DATE between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <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="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <choose>
            <when test="dto.state == 1">
                and bw_sbzt != 1
            </when>
            <when test="dto.state == 2">
                and bw_sbzt = 1
            </when>
        </choose>
    </select>

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

    <sql id="type_count">
        select count(*)
        from TF_YWPN_BWRKSBDATAIL_W
        where BW_SBLX = ${type}
        and BW_SBKJ = b.BW_SBKJ
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and CREATE_DATE between add_months(sysdate,-1) and sysdate
            </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>
        <choose>
            <when test="dto.state == 1">
                and bw_sbzt != 1
            </when>
            <when test="dto.state == 2">
                and bw_sbzt = 1
            </when>
        </choose>
    </sql>

    <sql id="ck_num">
        select count(*) from TF_YWPN_BWRKSBDATAIL_W b where BW_SBZT != 1 and b.BW_SBCJ = a.BW_SBCJ
        <choose>
            <when test="dto.start != null and dto.end != null">
               and to_char(CREATE_DATE,'yyyy-mm-dd hh24:mi:ss') between #{dto.start} and ${dto.end}
            </when>
            <otherwise>
               and CREATE_DATE between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
    </sql>
</mapper>