<?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.ZnbCbLogMapper">
    <sql id="querySql">
        SELECT a.*,
               a.bcds-a.scds AS sl,
               b.jsrq AS jssj,
               a.syje AS bcye, CASE WHEN b.id IS NULL THEN '未结算' ELSE '已结算' END AS jsbz,
            d.yhxm, d.yhdz, g.cbpqmc AS sscbpqmc, f.cbbmc AS sscbbbm, e.name AS sccjmc
        FROM znb_cb_log a
        LEFT JOIN znb_csjs_log b on a.id = b.qflsbh
        LEFT JOIN yh_khjbxx d on a.yhbh = d.yhbh
        LEFT JOIN bm_sbsccj e on a.sccj = e.code
        LEFT JOIN bm_cbb f on d.sscbb = f.cbbbm
        LEFT JOIN bm_cbpq g on d.sscbpq = g.cbpqbm
        WHERE 1=1
        <!--模糊查询 -->
        <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.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.sscj!=null">
            AND a.sscj=#{filter.sscj}
        </if>
        <!-- 操作时间-->
        <if test="filter.czsjStart!=null and filter.czsjEnd!=null">
            AND a.czsj BETWEEN #{filter.czsjStart} AND
            #{filter.czsjEnd}
        </if>
        <!--水量(>=)-->
        <if test="filter.slgt!=null">
            AND a.bcds-a.scds>=#{filter.slgt}
        </if>
        <!--处理结果-->
        <if test="filter.clCljg!=null">
            AND a.cl_cljg=#{filter.clCljg}
        </if>
    </sql>
    <select id="getPage" resultType="com.tofly.feesapi.cbgl.entity.vo.ZnbCbLogSelect">
        <include refid="querySql"></include>
    </select>
    <select id="getList" resultType="com.tofly.feesapi.cbgl.entity.vo.ZnbCbLogSelect">
        <include refid="querySql"></include>
    </select>
    <select id="getLjl" resultType="java.math.BigDecimal">
        SELECT SUM(a.bcds-a.scds) AS ljl
        FROM znb_cb_log a
        WHERE a.bcds IS NOT NULL AND a.scds IS NOT NULL
            AND a.yhbh = #{yhbh} AND a.cbrq BETWEEN #{startTime} AND #{endTime}
    </select>
</mapper>
