<?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.bjgl.mapper.YhCbkxxMapper">

    <!--查询用户水表档案 -->
    <select id="SelectYhcbkxx" resultType="com.tofly.feesapi.bjgl.entity.YhCbkxx2">
        select * from yh_cbkxx where 1=1
        <if test="yhbh!=null and yhbh!=''">
            and yhbh=#{yhbh}
        </if>
        <if test="cbkh!=null and cbkh!=''">
            and cbkh=#{cbkh}
        </if>
    </select>
    <!--自定义获取用户是否存在 -->
    <select id="selectExistYhbh" resultType="int">
        SELECT COUNT(0) sl
        FROM yh_cbkxx
        WHERE yhbh = #{yhbh}
          and cbkh#{cbkh}
    </select>
    <!--查询用户档案连表查询信息-->
    <select id="getYhdaSbdaPage" resultType="com.tofly.feesapi.bjgl.entity.YhDaCbkxxSelect">
        select a.yhbh,a.yhxm,a.yhdz,a.lxdh,a.ssgs,a.sscbpq,a.sscbb,b.sblb,b.sbsccj,b.sbbh,b.sblc,b.sbxh,b.sbkj,b.bjwz
        from yh_khjbxx a,yh_cbkxx b where a.yhbh=b.yhbh
        <!--模糊查询 -->
        <if test="searchText!=null">
            AND ( a.yhbh like '%${searchText}%'
            OR a.yhxm like '%${searchText}%'
            OR a.yhdz like '%${searchText}%'
            OR a.lxdh like '%${searchText}%')
        </if>
        <if test="ssgs!=null and ssgs!=''">
            and a.ssgs=#{ssgs}
        </if>
        <if test="sscbpq!=null and sscbpq!=''">
            and a.sscbpq=#{sscbpq}
        </if>
        <if test="sscbb!=null and sscbb!=''">
            and a.sscbb=#{sscbb}
        </if>
        <if test="startDate!=null and endDate!=null">
            and a.jdrq between #{startDate} and #{endDate}
        </if>
    </select>
    <!--换表查看用户、水表档案 -->
    <select id="SelectYhdaSbda" resultType="com.tofly.feesapi.bjgl.entity.YhDaCbkxxSelect">
        select a.yhbh,a.yhxm,a.yhdz,a.lxdh,a.ssgs,a.sscbpq,a.sscbb,b.sblb,b.sbsccj,b.sbbh,b.sblc,b.sbxh,b.sbkj,b.bjwz,
        b.sbazfs,b.sbyt
        from yh_khjbxx a,yh_cbkxx b where a.yhbh=b.yhbh
        <if test="yhbh!=null and yhbh!=''">
            and a.yhbh=#{yhbh}
        </if>
        <if test="cbkh!=null and cbkh!=''">
            and b.cbkh=#{cbkh}
        </if>
    </select>


    <select id="getList" resultType="com.tofly.feesapi.bjgl.entity.vo.YhCbkxxSelect">
        SELECT yhbh,
               cbkh,
               cbkid,
               c.name   AS sblbmc,
               d.name   AS sbsccjmc,
               sbccrq,
               e.name   AS sbxhmc,
               f.ccname AS sbkjmc,
               sbbh,
               b.name      sjmc,
               jfzt
        FROM yh_cbkxx a
                 LEFT JOIN bm_sj b ON a.sjbm = b.code
                 LEFT JOIN bm_sblb c ON a.sblb = c.code
                 LEFT JOIN bm_sbsccj d ON a.sbsccj = d.code
                 LEFT JOIN bm_sbxh e ON a.sbxh = e.code
                 LEFT JOIN (SELECT * FROM sys_child_code WHERE pcid = '12') f ON a.sbkj = f.cccode
        WHERE a.yhbh = #{yhbh}
    </select>
    <!--自定义水表档案分页查询 -->
    <select id="getPage" resultType="com.tofly.feesapi.bjgl.entity.vo.YhCbkxxSelect2">
        SELECT a.yhbh, a.cbkh,a.cbkid, c.yhxm, c.yhdz, a.ybdjbz, a.wsfbz, a.sjbm,a.slbl, a.sjbm1, a.slbl1, a.sjbm2, a.slbl2,
        a.sjbm3, a.slbl3,
        a.cbxh, a.sbbh, a.fmzt, f.cbbmc AS sscbbmc, g.cbpqmc AS sscbpqmc, c.lxdh, d.name AS sblbmc,
        cc.ccname as sbztmc
        FROM yh_cbkxx a
        LEFT JOIN yh_khjbxx c ON a.yhbh = c.yhbh
        LEFT JOIN bm_sblb d ON a.sblb = d.code
        LEFT JOIN bm_cbb f ON c.sscbb = f.cbbbm
        LEFT JOIN bm_cbpq g ON c.sscbpq = g.cbpqbm
        LEFT JOIN bm_sj b ON a.sjbm = b.code
        LEFT JOIN sys_child_code cc on a.sbzt=cc.cccode and cc.pcid='34'
        WHERE 1=1
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND (c.yhbh like '%${filter.searchText}%'
            OR c.yhxm like '%${filter.searchText}%'
            OR c.yhdz like '%${filter.searchText}%'
            OR c.lxdh like '%${filter.searchText}%')
        </if>
        <!--所属公司-->
        <if test="filter.ssgs!=null">
            AND c.ssgs=#{filter.ssgs}
        </if>
        <!--所属片区-->
        <if test="filter.sscbpq!=null">
            AND c.sscbpq=#{filter.sscbpq}
        </if>
        <!--所属抄表本-->
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND c.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND c.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.cby!=null">
            AND f.cby=#{filter.cby}
        </if>
        <if test="filter.qyfzr!=null">
            AND f.qyfzr=#{filter.qyfzr}
        </if>
        <!-- 用户状态-->
        <if test="filter.yhzt!=null">
            AND c.yhzt=#{filter.yhzt}
        </if>
        <!-- 垃圾费标志-->
        <if test="filter.ljfbz!=null">
            AND a.ljfbz=#{filter.ljfbz}
        </if>
        <!--建档日期-->
        <if test="filter.jdrqStart!=null and filter.jdrqEnd!=null">
            AND TRUNC(c.jdrq) BETWEEN #{filter.jdrqStart} AND
            #{filter.jdrqEnd}
        </if>
        <!-- 用户姓名-->
        <if test="filter.yhxm!=null">
            AND c.yhxm=#{filter.yhxm}
        </if>
        <!-- 用户编号-->
        <if test="filter.yhbh!=null">
            AND c.yhbh=#{filter.yhbh}
        </if>
        <!-- 用户地址-->
        <if test="filter.yhdz!=null">
            AND c.yhdz=#{filter.yhdz}
        </if>
        <!-- 联系电话-->
        <if test="filter.lxdh!=null">
            AND c.lxdh=#{filter.lxdh}
        </if>
        <!-- 是否分配抄表本(册)-->
        <if test="filter.sffpcbb!=null">
            <if test="filter.sffpcbb">
                AND c.sscbb >' '
            </if>
            <if test="!filter.sffpcbb">
                AND c.sscbb IS NULL
            </if>
        </if>
        <!-- 抄表序号-->
        <if test="filter.cbxh!=null">
            AND a.cbxh=#{filter.cbxh}
        </if>
        <!-- 是否分配抄表序号-->
        <if test="filter.sffpcbxh!=null">
            <if test="filter.sffpcbxh">
                AND (a.cbxh IS NOT NULL AND a.cbxh > 0)
            </if>
            <if test="!filter.sffpcbxh">
                AND (a.cbxh IS NULL OR a.cbxh = 0)
            </if>
        </if>
        <if test="sblbs!=null and sblbs.size()>0">
            AND a.sblb IN
            <foreach collection="sblbs" item="sblb" index="index" open="(" close=")" separator=",">
                #{sblb}
            </foreach>
        </if>
        <if test="filter.sjbm!=null">
            AND a.sjbm=#{filter.sjbm}
        </if>
    </select>
    <select id="getMaxCbxh" resultType="Integer">
        SELECT NVL(MAX(cbxh),0) AS cbxh FROM yh_cbkxx WHERE yhbh IN
        (select yhbh from yh_khjbxx where sscbb=${sscbb})
        <!--        <foreach collection="yhbhs" item="yhbh" index="index" open="(" close=")" separator=",">-->
        <!--            #{yhbh}-->
        <!--        </foreach>-->
    </select>
    <select id="getNextCbxh" resultType="Integer">
        SELECT NVL(MAX(cbxh),0) AS cbxh FROM yh_cbkxx WHERE yhbh IN
        (select yhbh from yh_khjbxx where sscbb=${sscbb}) and cbxh = ${cbxh}
        <!--        <foreach collection="yhbhs" item="yhbh" index="index" open="(" close=")" separator=",">-->
        <!--            #{yhbh}-->
        <!--        </foreach>-->
    </select>
    <select id="getCbkxxSbbh" resultType="Integer">
        SELECT count(*)
        FROM yh_cbkxx
        WHERE yhbh = ${yhbh}
          and sbbh = ${ssbh}
    </select>
    <select id="getUp" resultType="com.tofly.feesapi.bjgl.entity.YhCbkxx">
        SELECT c.sscbb,
               a.*
        FROM yh_cbkxx a
                 LEFT JOIN yh_khjbxx c ON
            a.yhbh = c.yhbh
        WHERE c.sscbb = #{sscbb}
          AND ${cbxh} > a.cbxh
          AND a.cbxh > 0
          AND ROWNUM = 1
        ORDER BY a.cbxh
    </select>
    <select id="getDown" resultType="com.tofly.feesapi.bjgl.entity.YhCbkxx">
        SELECT c.sscbb,
               a.*
        FROM yh_cbkxx a
                 LEFT JOIN yh_khjbxx c ON
            a.yhbh = c.yhbh
        WHERE c.sscbb = #{sscbb}
          AND a.cbxh > ${cbxh}
          AND a.cbxh > 0
          AND ROWNUM = 1
        ORDER BY a.cbxh
    </select>
    <select id="lockTable">
        LOCK
        TABLE yh_cbkxx IN SHARE MODE NOWAIT
    </select>
    <select id="getBySbsccj" resultType="com.tofly.feesapi.bjgl.entity.dto.YhCbkxxSync">
        SELECT a.sbbh,
               b.yhbh,
               b.yhxm,
               b.yhdz,
               b.lxdh
        FROM yh_cbkxx a
                 LEFT JOIN yh_khjbxx b ON
            a.yhbh = b.yhbh
        WHERE a.sbsccj = #{sbsccj}
    </select>

    <select id="sblbtj" resultType="com.tofly.feesapi.bjgl.entity.vo.YhCbkxxSelectSbfl">
        select yslx.name yhxz,
        sbsccj.name sbcj,
        sbxh.name sbxh,
        sblb.name sblb,
        a.sbkj,a.sbsl
        from (
        select sbkj,sbxh,sbsccj,yslx,sblb,sum(1) sbsl
        from yh_cbkxx
        <where>
            <if test="ysxz!=null">
                AND yslx = #{ysxz}
            </if>
            <if test="sbkj!=null">
                AND sbkj = #{sbkj}
            </if>
        </where>
        GROUP BY sbkj,
        sbxh,
        sbsccj,
        yslx,
        sblb
        ) A
        left join bm_yslx yslx on yslx.code = a.yslx
        left join bm_sbsccj sbsccj on sbsccj.code = a.sbsccj
        left join bm_sbxh sbxh on sbxh.code = a.sbxh
        left join bm_sblb sblb on sblb.code=a.sblb
    </select>

    <update id="updatecbxh">
        UPDATE YH_CBKXX b SET cbxh=(select cbxh from yy_cblr a where a.cbkid=b.cbkid)
        <where>
            and b.cbkid in
            <foreach collection="cbkids" item="cbkid" index="index" open="(" close=")" separator=",">
                #{cbkid}
            </foreach>
        </where>
    </update>
</mapper>
