<?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.wxgl.wechatpay.mapper.PaySelectMapper">

    <select id="getDhYhqfxx" resultType="com.tofly.feesapi.wxgl.wechatpay.entity.vo.PaySelectQfVo">
        select
        nvl(a.zyssl,0) zyssl,
        nvl(a.zyssf,0) zyssf,
        nvl(a.zyswsf,0) zyswsf,
        nvl(a.zysljf,0) zysljf,
        nvl(a.zysszyf,0) zysszyf,
        nvl(a.zznj,0) zznj,
        nvl(a.zysje,0) zysje,
        nvl(a.bcds,0) bcds,
        GREATEST(NVL(a.zysje, 0) - NVL(d.syje, 0), 0) AS yjje,
        b.yhbh ,b.yhxm ,b.yhdz ,b.lxdh,d.syje,d.nbsyje,c.sbbh
        from yh_khjbxx b
        left join yh_cbkxx c on b.yhbh=c.yhbh
        left join yy_zjzh d on b.yhbh=d.yhbh
        left join (
        select #{payselect.yhbh} as yhbh,sum(nvl(yssl,0)) zyssl,sum(nvl(yssf,0)) zyssf,sum(nvl(yswsf,0))
        zyswsf,sum(nvl(ysljf,0))
        zysljf,sum(nvl(ysszyf,0)) zysszyf,
        sum(nvl(znj,0)) zznj,
        sum(nvl(ysje, 0) + nvl(znj, 0)) zysje,
        max(bcds) bcds
        from yy_qfmx  where sfbz=0 and chbz=0
        and yhbh in (
        select yhbh from yh_khjbxx where khbm=#{khbm}
        )) a on b.yhbh=a.yhbh
        where 1=1
        <if test="payselect.yhbh!=null">
            AND b.yhbh = #{payselect.yhbh}
        </if>
    </select>

    <select id="getYhqfxx" resultType="com.tofly.feesapi.wxgl.wechatpay.entity.vo.PaySelectQfVo">
        SELECT
        nvl(a.zyssl,0) zyssl,
        nvl(a.zyssf,0) zyssf,
        nvl(a.zyswsf,0) zyswsf,
        nvl(a.zysljf,0) zysljf,
        nvl(a.zysszyf,0) zysszyf,
        nvl(a.zznj,0) zznj,
        nvl(a.zysje,0) zysje,
        nvl(a.bcds,0) bcds,
        GREATEST(NVL(a.zysje, 0) - NVL(d.syje, 0), 0) AS yjje,
        b.yhbh, b.yhxm, b.yhdz, b.lxdh, d.syje,d.nbsyje, c.sbbh, b.dxh
        from yh_khjbxx b
        left join (select yhbh,LISTAGG(sbbh, ',') WITHIN GROUP (ORDER BY sbbh)sbbh from yh_cbkxx
        <where>
            <if test="payselect.yhbh!=null">
                AND yhbh = #{payselect.yhbh}
            </if>
        </where>
        group by yhbh
        ) c on b.yhbh=c.yhbh
        left join yy_zjzh d on b.yhbh = d.yhbh
        left join (
        select
        yhbh, sum(nvl(yssl, 0)) zyssl, sum(nvl(yssf, 0)) zyssf,
        sum(nvl(yswsf, 0)) zyswsf, sum(nvl(ysljf, 0))
        zysljf, sum(nvl(ysszyf, 0)) zysszyf,
        sum(nvl(znj, 0)) zznj,
        sum(nvl(ysje, 0) + nvl(znj, 0)) zysje,
        max(bcds) bcds
        FROM
        yy_qfmx  where sfbz = 0 and chbz = 0
            <if test="payselect.yhbh!=null">
                AND yhbh = #{payselect.yhbh}
            </if>
        GROUP BY yhbh
        ) a ON b.yhbh = a.yhbh
        where 1=1
        <if test="payselect.yhbh!=null">
            AND b.yhbh = #{payselect.yhbh}
        </if>
    </select>

    <select id="getOrderStatus" resultType="com.tofly.feesapi.wxgl.wechatpay.entity.vo.PaySelectOrderStatusVo">
        select yhbh, case when sfbz='1' then '已缴费' else '已作废' end orderstatus
        from yy_sspjxx a
        where rownum = '1'
        <if test="payselect.yhbh!=null">
            AND a.yhbh = '${payselect.yhbh}'
        </if>
        <if test="payselect.sflsbh!=null">
            AND a.sflsbh = '${payselect.sflsbh}'
        </if>
    </select>

    <select id="getSfmxList" resultType="com.tofly.feesapi.wxgl.wechatpay.entity.vo.PaySelectSfxmVo">
        select yhbh,sfrybm,sfrq,sflsbh,sszje from yy_sspjxx a
        where 1=1
        <if test="payselect.sfrybm!=null">
            AND a.sfrybm = '${payselect.sfrybm}'
        </if>
        <if test="payselect.sfrq!=null">
            AND a.sfrq > to_date('${payselect.sfrq} 00:00:00','yyyy-MM-dd hh24:mi:ss') and a.sfrq &lt;=
            to_date('${payselect.sfrq} 23:59:59','yyyy-MM-dd hh24:mi:ss')
        </if>
        order by a.sfrq desc
    </select>
    <select id="getYhSfmxList" resultType="com.tofly.feesapi.wxgl.wechatpay.entity.vo.PaySelectSfxmVo">
        select yhbh,sfryxm,min(sfrq) sfrq,sflsbh,sum(nvl(sszje,0)) sszje from yy_sspjxx a
        where 1=1 AND a.chbz='0' AND a.sflx in ('2','3','4','5','6') and a.sszje>0
        <if test="payselect.yhbh!=null">
            AND a.yhbh = '${payselect.yhbh}'
        </if>
        group by a.yhbh,a.sflsbh,a.sfryxm
        order by a.sfrq desc
    </select>
    <select id="getYhCbmxList" resultType="com.tofly.feesapi.wxgl.wechatpay.entity.vo.QfSelectCbxmVo">
        select yhbh,ysyf,to_char(SCDS)||'-'||to_char(BCDS) qzds,yssl,yssf,yswsf,ysljf,ysszyf,ysje,znj,ysje+znj hjje,case
        when sfbz='0'then '未收' else '已收' end sfbz from yy_qfmx a
        where 1=1 AND a.chbz='0'
        <if test="payselect.yhbh!=null">
            AND a.yhbh = '${payselect.yhbh}'
        </if>
        order by a.ysyf desc
    </select>
    <select id="getYhxxList" resultType="com.tofly.feesapi.wxgl.wechatpay.entity.vo.YhSelectxxVo">
        select nvl(a.zyssl,0) yssl,nvl(a.zyssf,0) yssf,nvl(a.zyswsf,0) yswsf,nvl(a.zysljf,0) ysljf,nvl(a.zysszyf,0)
        ysszyf,nvl(a.zznj,0) znj,nvl(a.zysje,0) ysje,nvl(a.bcds,0) bcds,
        case when (nvl(a.zysje,0)-nvl(d.syje,0)&lt;0) then 0 else (nvl(a.zysje,0)-nvl(d.syje,0)) end yjje,
        b.yhbh ,b.yhxm ,b.yhdz ,b.lxdh,b.djrq,d.syje,c.sbbh,e.name ysxz,f.name sblb,JTZQLJL nljl,jtbz,nvl(JT1_ZS,0)
        jt1,nvl(JT2_ZS,0) jt2,
        nvl(jt1_sj,0) jt1dj,nvl(jt2_sj,0) jt2dj,nvl(jt3_sj,0) jt3dj,
        case when JTZQLJL>nvl(JT2_ZS,0) then nvl(JT1_ZS,0) when JTZQLJL &lt;= nvl(JT2_ZS,0) and JTZQLJL>nvl(JT1_ZS,0)
        then nvl(JT1_ZS,0) else JTZQLJL end jtsyl1,
        case when JTZQLJL>nvl(JT2_ZS,0) then nvl(JT2_ZS,0) - nvl(JT1_ZS,0) when JTZQLJL&lt;= nvl(JT2_ZS,0) and
        JTZQLJL>nvl(JT1_ZS,0) then JTZQLJL - nvl(JT1_ZS,0) else 0 end jtsyl2,
        case when JTZQLJL>nvl(JT2_ZS,0) then JTZQLJL-nvl(JT2_ZS,0) else 0 end jtsyl3,
        from yh_khjbxx b
        left join yh_cbkxx c on b.yhbh=c.yhbh
        left join yy_zjzh d on b.yhbh=d.yhbh
        left join BM_SJ e on c.SJBM = e.code
        left join BM_SBLB f on c.SBLB = f.code
        left join (
        select yhbh,sum(nvl(yssl,0)) zyssl,sum(nvl(yssf,0)) zyssf,sum(nvl(yswsf,0)) zyswsf,sum(nvl(ysljf,0))
        zysljf,sum(nvl(ysszyf,0)) zysszyf,
        sum(nvl(znj,0)) zznj,sum(nvl(ysje,0))+sum(nvl(znj,0)) zysje,max(a.bcds) bcds
        from yy_qfmx a where a.sfbz=0 and a.chbz=0
        group by a.yhbh ) a on b.yhbh=a.yhbh
        where 1=1 and b.yhzt not in ('0','3','4')
        <if test="payselect.yhbh!=null">
            AND b.yhbh = #{payselect.yhbh}
        </if>
    </select>
</mapper>
