<?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.feesapi.sfgl.mapper.YySspjxxReportMapper">

    <sql id="commSql">
         select t2.yslx, t3.name yslx_name, sum(t1.sl) sl, sum(t1.sf) sf, t1.fffs, 1 as type
         from yy_sspjxx t1
         inner join bm_sj t2
         on t1.sjbm=t2.code
         inner join bm_yslx t3
         on t2.yslx=t3.code
        left join bm_cbb cbb
         on t1.sscbb=cbb.cbbbm
       <where>
           <include refid="commWhereLx"></include>
       </where>
        group by  t2.yslx, t3.name, t1.fffs
     union all
        select t2.yslx, t3.name yslx_name, sum(t1.sl1) sl, sum(t1.sf1) sf, t1.fffs, 1 as type
        from yy_sspjxx t1
        inner join bm_sj t2
        on t1.sjbm1=t2.code
        inner join bm_yslx t3
        on t2.yslx=t3.code
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhereLx"></include>
        </where>
        group by  t2.yslx, t3.name, t1.fffs
        union all
        select t2.yslx, t3.name yslx_name, sum(t1.sl2) sl, sum(t1.sf2) sf, t1.fffs, 1 as type
        from yy_sspjxx t1
        inner join bm_sj t2
        on t1.sjbm2=t2.code
        inner join bm_yslx t3
        on t2.yslx=t3.code
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhereLx"></include>
        </where>
        group by  t2.yslx, t3.name, t1.fffs
        union all
        select t2.yslx, t3.name yslx_name, sum(t1.sl3) sl, sum(t1.sf3) sf, t1.fffs, 1 as type
        from yy_sspjxx t1
        inner join bm_sj t2
        on t1.sjbm3=t2.code
        inner join bm_yslx t3
        on t2.yslx=t3.code
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhereLx"></include>
        </where>
        group by  t2.yslx, t3.name, t1.fffs

        union all
        select '1000' yslx,'污水费' yslx_name,
        sum(t1.yssl) sl, sum(t1.yswsf) sf,t1.fffs,2 as type
        from yy_sspjxx t1
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhere"></include>
        </where>
        group by t1.fffs
        union all
        select '2000' yslx,'垃圾费' yslx_name,
        null as sl,sum(t1.ysljf) sf,
       t1.fffs,2 as type
        from yy_sspjxx t1
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhere"></include>
        </where>
        group by t1.fffs
        union all
        select '3000' yslx,'违约金' yslx_name,
        null as sl, sum(t1.ssznj) sf,
       t1.fffs,2 as type
        from yy_sspjxx t1
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhere"></include>
        </where>
        group by t1.fffs
        union all
        select '4000' yslx,'预交水费' yslx_name,
        null as sl,sum(t1.bcyc) sf,
        t1.fffs,2 as type
        from yy_sspjxx t1
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhere"></include>
        </where>
        group by t1.fffs
        union all
        select '5000' yslx,'冲预交水费' yslx_name,
        null as sl,sum(0-t1.bckj) sf,
       t1.fffs,2 as type
        from yy_sspjxx t1
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhere"></include>
        </where>
        group by t1.fffs
        union all
        select '6000' yslx,'上月结余' yslx_name,
        null as sl,  sum(t1.scye) sf,
       t1.fffs,2 as type
        from yy_sspjxx t1
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhere"></include>
        </where>
        group by t1.fffs
        union all
        select '7000' yslx,'本月结余' yslx_name,
        null as sl, sum(t1.bcye) sf,
       t1.fffs,2 as type
        from yy_sspjxx t1
        left join bm_cbb cbb
        on t1.sscbb=cbb.cbbbm
        <where>
            <include refid="commWhere"></include>
        </where>
        group by t1.fffs

    </sql>


    <sql id="commWhereLx">
        <include refid="commWhere"></include>
        and  t1.fylx  in(1,2)
    </sql>
    <sql id="commWhere">
       and t1.sfbz=1
        and t1.sfrq between to_date('${startTime}','yyyy-MM-dd hh24:mi:ss') and to_date('${endTime}','yyyy-MM-dd hh24:mi:ss')
        <if test="sfrybm!=null and sfrybm.trim()!=''">
        and t1.sfrybm=#{sfrybm}
       </if>
        <if test="ssyys!=null and ssyys.trim()!=''">
            and cbb.ssyys=#{ssyys}
        </if>
        <if test="sscbpq!=null and sscbpq.trim()!=''">
            and t1.sscbpq=#{sscbpq}
        </if>
        <if test="sscbb!=null and sscbb.trim()!=''">
            and t1.sscbb=#{sscbb}
        </if>
        <if test="cbbConfigList!=null and cbbConfigList.size()>0">
            AND t1.sscbb in
            <foreach collection="cbbConfigList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
       </sql>



    <select id="getYySspjxxReport" resultType="com.tofly.feesapi.sfgl.entity.vo.YySspjxxReport">
        select sum(case
                       when t.fffs = '1' then
                           sf
                       else
                           0
            end) sf_1,
               sum(case
                       when t.fffs= '1'  then
                           sl
                       else
                           0
                   end) sl_1,
               sum(case
                       when t.fffs = '2'  then
                           sf
                       else
                           0
                   end) sf_2,
               sum(case
                       when t.fffs = '2'  then
                           sl
                       else
                           0
                   end) sl_2,
               sum(case
                       when t.fffs = '3'  then
                           sf
                       else
                           0
                   end) sf_3,
               sum(case
                       when t.fffs  = '3' then
                           sl
                       else
                           0
                   end) sl_3,
               sum(case
                       when t.fffs  = '4' then
                           sf
                       else
                           0
                   end) sf_4,
               sum(case
                       when t.fffs  = '4' then
                           sl
                       else
                           0
                   end) sl_4,
               sum(case
                       when t.fffs = '5' then
                           sf
                       else
                           0
                   end) sf_5,
               sum(case
                       when t.fffs = '5' then
                           sl
                       else
                           0
                   end) sl_5,
               sum(case
                       when to_number(t.fffs) > 5 then
                           sf
                       else
                           0
                   end) sf_6,
               sum(case
                       when to_number(t.fffs) > 5 then
                           sl
                       else
                           0
                   end) sl_6,
               sum(sf) sf_7,
               sum(sl) sl_7,
               yslx,
               yslx_name,
               type
        from
        (
             <include refid="commSql"></include>
        ) t
      group by  yslx,
        yslx_name,
        type
    order by type,yslx
    </select>

    <sql id="commMxReport">
        select
        t.sfrq,--收费日期
        t.yhbh,--用户编号（收费号？？）
        t.bz, --备注
        t.cwrq,--财务日期
        t2.yslx,
        t3.name yslx_name,--用水性质
        t.yswsf,--污水费
        t.ysljf,--垃圾费
        t.ysqt,--其他费用
        t.ssznj,--违约金
        t.scye,--上月余额
        t.bcye,--本月余额
        t.yssf, --应收水费
        t.bcyc, --本次预存（预交水费）
        t.bckj,--本次扣减（充预交水费??）
        t.sszje,--收费金额
        t.scds, --起度
        t.bcds,--止度
        t.yssl, --水量
        t.sfryxm, --收费人员
        t.yhxm,--用户姓名
        t.sflsbh,--流水号
        t.fffs,--付费方式
        t4.name fffs_name
        from yy_sspjxx t
        left join yh_cbkxx t1
        on t.yhbh = t1.yhbh
        left join bm_sj t2
        on t1.sjbm=t2.code
        left join bm_yslx t3
        on t2.yslx=t3.code
        left join bm_fffs t4
        on t.fffs=t4.code
        left join bm_cbb cbb
        on t.sscbb=cbb.cbbbm
        where t.sfbz=1

        <if test="filter.sscbpq!=null and filter.sscbpq.trim()!=''">
            and t.sscbpq = #{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null and filter.sscbb.trim()!=''">
            and t.sscbb = #{filter.sscbb}
        </if>
        <if test="filter.cbbConfigList!=null and filter.cbbConfigList.size()>0">
            AND t.sscbb in
            <foreach collection="filter.cbbConfigList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="filter.sfrybm!=null">
            and t.sfrybm = #{filter.sfrybm}
        </if>
        <if test="filter.ssyys!=null and filter.ssyys.trim()!=''">
            and cbb.ssyys=#{filter.ssyys}
        </if>

        <if test="filter.startTime!=null">
            and t.sfrq >=to_date('${filter.startTime}','yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="filter.yslx!=null and filter.yslx.trim()!=''">
            and t2.yslx = #{filter.yslx}
        </if>
        <if test="filter.endTime!=null">
            <![CDATA[ and  t.sfrq <=to_date('${filter.endTime}','yyyy-MM-dd hh24:mi:ss') ]]>
        </if>
        order by t.sfrq
    </sql>

    <select id="getYySspjxxMxReport" resultType="com.tofly.feesapi.sfgl.entity.vo.YySspjxxMxReport">

    <include refid="commMxReport"></include>

    </select>
    <select id="getYySspjxxLsMxReport" resultType="com.tofly.feesapi.sfgl.entity.vo.YySspjxxLsMxReport">

        <include refid="commMxReport"></include>

    </select>

</mapper>
