<?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.ZnbCsjsLogMapper">
    <!--自定义智能表档案分页查询 -->
    <select id="getPage" resultType="com.tofly.feesapi.sfgl.entity.vo.ZnbCsjsSelect">
        select a.yhbh, a.yhxm, a.yhdz,a.lxdh,
        b.sbbh,l.cwrq,l.yysl,l.cbkid,
        f.cbbmc as sscbbmc,
        g.cbpqmc as sscbpqmc
        from yh_khjbxx a
        left join yh_cbkxx b on a.yhbh = b.yhbh and b.jfzt='1'
        left join (select yhbh,cbkid,cwrq,sum(nvl(yssl,0))yysl from znb_csjs_log
        group by cwrq,cbkid,yhbh) l on a.yhbh=l.yhbh
        left join bm_cbb f on a.sscbb = f.cbbbm
        left join bm_cbpq g on a.sscbpq = g.cbpqbm
        WHERE 1=1 and b.sblb in ('2','3')
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND (a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%')
        </if>
        <!--所属公司-->
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <!--所属片区-->
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <!--所属抄表本-->
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.cwrq!=null">
            AND l.cwrq=#{filter.cwrq}
        </if>
    </select>


    <select id="getYhysmxList" resultType="com.tofly.feesapi.sfgl.entity.ZnbCsjsLog">
        <include refid="querySql"/>
    </select>

    <sql id="querySql">
        SELECT
            a.*,
            c.sccj, c.czsj, c.cl_clsj ,case  c.jsbz when '1' then '已结算' ELSE '未知' END jsbz,
            row_number() over (partition by a.cbkid, a.cwrq, to_char(jsrq,'yyyy-mm-dd') order by jsrq desc) AS rn
        FROM
            znb_csjs_log a
        LEFT JOIN
            yh_cbkxx b ON a.cbkid = b.cbkid
        LEFT JOIN
            znb_cb_log c ON a.qflsbh = c.id
        WHERE
            1=1
        <if test="filter.cwrq != null and filter.cwrq != ''">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.yhbh != null and filter.yhbh != ''">
            AND a.yhbh=#{filter.yhbh}
        </if>
        <if test="filter.cbkid != null and filter.cbkid != ''">
            AND a.cbkid=#{filter.cbkid}
        </if>
        <if test="filter.startDate!=null and filter.endDate!=null">
            AND (a.jsrq BETWEEN #{filter.startDate} AND #{filter.endDate})
        </if>
        <if test="filter.sscbpq != null and filter.sscbpq != ''">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb != null and filter.sscbb != ''">
            AND (a.sscbb=#{filter.sscbb})
        </if>
        <if test="filter.sbsccj != null and filter.sbsccj != ''">
            AND (a.sbsccj=#{filter.sbsccj})
        </if>
        <if test="filter.sl != null and filter.sl != ''">
            AND (a.sl>=#{filter.sl})
        </if>

        <if test="filter.searchText != null and filter.searchText != ''">
            AND (a.yhbh like '%${filter.searchText}%' OR a.yhxm like '%${filter.searchText}%')
        </if>
    </sql>

</mapper>
