<?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.cbgl.mapper.YyCblrLsMapper">

    <resultMap id="yyCblrLsMap" type="com.tofly.feesapi.cbgl.entity.YyCblrLs">
        <id property="shryxm" column="SHRYXM"/>
        <result property="id" column="ID"/>
        <result property="yhbh" column="YHBH"/>
        <result property="cbkh" column="CBKH"/>
        <result property="cbkid" column="CBKID"/>
        <result property="ssgs" column="SSGS"/>
        <result property="sscbpq" column="SSCBPQ"/>
        <result property="sscbb" column="SSCBB"/>
        <result property="cwrq" column="CWRQ"/>
        <result property="scds" column="SCDS"/>
        <result property="bcds" column="BCDS"/>
        <result property="jjl" column="JJL"/>
        <result property="cbrybm" column="CBRYBM"/>
        <result property="cbryxm" column="CBRYXM"/>
        <result property="cbrq" column="CBRQ"/>
        <result property="lrrybm" column="LRRYBM"/>
        <result property="lrryxm" column="LRRYXM"/>
        <result property="lrrq" column="LRRQ"/>
        <result property="photo" column="PHOTO"/>
        <result property="cbbz" column="CBBZ"/>
        <result property="shbz" column="SHBZ"/>
        <result property="jsbz" column="JSBZ"/>
        <result property="cbcs" column="CBCS"/>
        <result property="cbxh" column="CBXH"/>
        <result property="bz" column="BZ"/>
        <result property="cblx" column="CBLX"/>
        <result property="cbfs" column="CBFS"/>
        <result property="jfzt" column="JFZT"/>
        <result property="sl" column="SL"/>
        <result property="jbzd" column="JBZD"/>
        <result property="xbqd" column="XBQD"/>
        <result property="jbbh" column="JBBH"/>
        <result property="cbly" column="CBLY"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="shry" column="SHRY"/>
        <result property="shsj" column="SHSJ"/>
        <result property="shsm" column="SHSM"/>
        <result property="ssregion" column="SSREGION"/>
        <result property="nbzxds" column="NBZXDS"/>
        <result property="nbzxcbrq" column="NBZXCBRQ"/>
        <result property="sccbrq" column="SCCBRQ"/>
    </resultMap>
    <select id="cbsltj" resultType="com.tofly.feesapi.cbgl.entity.vo.CbSltjSelect">
        <if test="filter.tjlb=='cbry'">
            SELECT
            cbrybm AS tjlb,
            COUNT(1) AS cbhs,
            SUM(sl) AS cbsl
            FROM
            YY_CBLR_LS
            WHERE
            jfzt = '1'
            AND cbbz = '1'
            <if test="filter.ssgs!=null">
                AND ssgs = #{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null">
                AND sscbpq = #{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            <if test="filter.cwrq!=null">
                AND cwrq = #{filter.cwrq}
            </if>
            GROUP BY
            cbrybm
        </if>
        <if test="filter.tjlb=='ysxz'">
            SELECT
            a.NAME AS tjlb,
            NVL(COUNT(1),0) AS cbhs,
            NVL(SUM(sl),0) AS cbsl
            FROM bm_yslx a
            Left JOIN BM_SJ b ON b.yslx = a.CODE
            Left JOIN YH_CBKXX c ON c.sjbm=b.code
            Left join YY_CBLR_LS d ON c.CBKID = d.CBKID
            WHERE d.jfzt = '1'
            AND d.cbbz = '1'
            <if test="filter.ssgs!=null">
                AND d.ssgs = #{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null">
                AND d.sscbpq = #{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND d.sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND d.sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            <if test="filter.cwrq!=null">
                AND d.cwrq = #{filter.cwrq}
            </if>
            GROUP BY
            a.NAME
        </if>
        <if test="filter.tjlb=='cbb'">
            SELECT
            b.CBBMC AS tjlb,
            NVL(COUNT(1),0) AS cbhs,
            NVL(SUM(sl),0) AS cbsl
            FROM
            YY_CBLR_LS a
            LEFT JOIN BM_CBB b ON a.sscbb = b.cbbbm
            WHERE
            a.jfzt = '1'
            AND a.cbbz = '1'
            <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 a.cwrq = #{filter.cwrq}
            </if>
            GROUP BY
            b.CBBMC
        </if>
    </select>
    <select id="cbltj" resultType="com.tofly.feesapi.cbgl.entity.vo.CbltjSelect">
        <if test="filter.tjlb=='cbry'">
            SELECT
            b.CBY AS tjlb,
            COUNT(1) AS ycbs,
            SUM(CASE WHEN a.cbbz = '1' THEN 1 ELSE 0 END) AS scbs,
            SUM(CASE WHEN a.cbbz = '1' THEN sl ELSE 0 END) AS scsl
            FROM
            YY_CBLR_LS a
            LEFT JOIN BM_CBB b ON a.sscbb = b.cbbbm
            WHERE a.jfzt = '1' and a.xycb=1
            <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>
            AND a.cwrq = #{filter.cwrq}
            GROUP BY
            b.CBY
        </if>
        <if test="filter.tjlb=='cbb'">
            SELECT
            b.cby as cby,
            b.cbbmc AS tjlb,
            COUNT(1) AS ycbs,
            SUM(CASE WHEN a.cbbz = '1' THEN 1 ELSE 0 END) AS scbs,
            SUM(CASE WHEN a.cbbz = '1' THEN sl ELSE 0 END) AS scsl
            FROM
            YY_CBLR_LS a
            LEFT JOIN BM_CBB b ON a.sscbb = b.cbbbm
            WHERE a.jfzt = '1' and a.xycb=1
            <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>
            AND a.cwrq = #{filter.cwrq}
            GROUP BY b.cbbmc,b.cby
            order by b.cby asc, b.cbbmc asc
        </if>
    </select>
    <!--抄表异常明细-->
    <select id="cbycmx" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCbycmxSelect">
        <include refid="mainSql"/>
    </select>

    <!--抄表异常明细（分页）-->
    <select id="cbycmxPage" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCbycmxSelect">
        <include refid="mainSql"/>
    </select>

    <sql id="mainSql">
        SELECT
        g.mc AS ssgsmc, e.cbpqmc AS sscbpqmc, f.cbbmc AS sscbbmc,
        b.yhbh, b.yhxm, b.yhdz, b.bcds, h.name AS cblxmc, b.sl,
        NVL(b1.sl, 0) AS scsl1, NVL(b2.sl, 0) AS scsl2, NVL(b3.sl, 0) AS scsl3,
        ROUND(NVL(b1.sl, 0) + NVL(b2.sl, 0) + NVL(b3.sl, 0) / 3, 2) AS avgsl
        FROM
        (
        <include refid="subSql"/>
        AND a.cwrq = #{filter.cwrq}
        ) b

        LEFT JOIN
        (
        <include refid="subSql"/>
        AND a.cwrq = #{cwrq1}
        ) b1 ON b.cbkid = b1.cbkid

        LEFT JOIN
        (
        <include refid="subSql"/>
        AND a.cwrq = #{cwrq2}
        ) b2 ON b.cbkid = b2.cbkid

        LEFT JOIN
        (
        <include refid="subSql"/>
        AND a.cwrq = #{cwrq3}
        ) b3 ON b.cbkid = b3.cbkid

        LEFT JOIN
        bm_cbpq e on b.sscbpq = e.cbpqbm
        LEFT JOIN
        bm_cbb f on b.sscbb = f.cbbbm
        LEFT JOIN
        bm_gs g on b.ssgs = g.bm
        LEFT JOIN
        bm_cblx h on b.cblx = h.code
        WHERE
        1=1
        <if test="filter.bfb!=null">
            AND ((b.sl = 0 AND ROUND(NVL(b1.sl, 0) + NVL(b2.sl, 0) + NVL(b3.sl, 0) / 3, 2) > 0)
            OR (b.sl > 0 AND ROUND((ABS(b.sl - ROUND(NVL(b1.sl, 0) + NVL(b2.sl, 0) + NVL(b3.sl, 0) / 3, 2)) * 100) /
            b.sl, 0) > #{filter.bfb}))
        </if>
        <if test="filter.zjl>0">
            AND (b.sl-b1.sl)> #{filter.zjl}
        </if>
        <if test="filter.flxl!=null">
            <if test="filter.flxl">
                AND b1.sl=b2.sl AND b2.sl=b3.sl
            </if>
        </if>
        <if test="filter.flw0!=null">
            <if test="filter.flw0">
                AND b.sl=0
            </if>
        </if>
        <if test="filter.wcb3!=null">
            <if test="filter.wcb3">
                AND b1.cbbz=0 and b2.cbbz=0 AND b3.cbbz=0
            </if>
        </if>
    </sql>

    <sql id="subSql">
        SELECT
        a.yhbh, a.cblx, a.cbkid, a.cbrybm, a.bcds, a.sl,
        d.ssgs, d.sscbpq, d.sscbb, d.yhxm, d.yhdz, a.cbbz
        FROM
        YY_CBLR_LS a
        LEFT JOIN
        YH_KHJBXX d ON a.yhbh = d.yhbh WHERE a.jfzt = '1'
        <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.cblx!=null">
            AND a.cblx = #{filter.cblx}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm = #{filter.cbrybm}
        </if>
        <if test="filter.searchText!=null">
            AND (a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%')
        </if>
        <if test="filter.sbzt!=null">
            AND a.sbzt = #{filter.sbzt}
        </if>
    </sql>
</mapper>
