<?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.mosty.qwzx.mapper.TbQwZbpbMapper">

    <!--根据条件按查询总数-->
    <select id="getCount" resultType="Integer" parameterType="Map">
        select count(1) from tb_qw_zbpb a where a.xt_scbz = '0' and a.xt_scbz = '0'
        and a.id in (
        select pb_id from tb_qw_zbpb_zq b where b.xt_scbz = '0' and b.xt_scbz = '0'
        and (
        b.zbld_id = #{ryid} or b.zbzhz_id = #{ryid} or b.zzzhz_id = #{ryid} or b.fzzb_id = #{ryid}
        or b.id in (
        select ywid from tb_qw_jl c where c.xt_scbz = '0' and c.xt_scbz = '0' and jl_id = #{ryid}
        )
        )
        )
        <if test="dto.jsrqlx != null and dto.jsrqlx != ''">
            and a.jsrqlx = #{dto.jsrqlx}
        </if>
        <if test="dto.xhlx != null and dto.xhlx != ''">
            and a.xhlx = #{dto.xhlx}
        </if>
        <if test="dto.xhzq != null">
            and a.xhzq = #{dto.xhzq}
        </if>
        <if test="dto.zbpbksrq != null and dto.zbpbjsrq != null">
            and (
            a.zbpbksrq between #{dto.zbpbksrq} and #{dto.zbpbjsrq} or
            a.zbpbjsrq between #{dto.zbpbksrq} and #{dto.zbpbjsrq}
            )
        </if>
    </select>

    <!--根据条件按查询总数-->
    <select id="getList" resultType="com.mosty.base.model.entity.qwzx.TbQwZbpb" parameterType="Map">
        select a.* from tb_qw_zbpb a where a.xt_scbz = '0' and a.xt_scbz = '0'
        and a.id in (
        select pb_id from tb_qw_zbpb_zq b where b.xt_scbz = '0' and b.xt_scbz = '0'
        and (
        b.zbld_id = #{ryid} or b.zbzhz_id = #{ryid} or b.zzzhz_id = #{ryid} or b.fzzb_id = #{ryid}
        or b.id in (
        select ywid from tb_qw_jl c where c.xt_scbz = '0' and c.xt_scbz = '0' and jl_id = #{ryid}
        )
        )
        )
        <if test="dto.jsrqlx != null and dto.jsrqlx != ''">
            and a.jsrqlx = #{dto.jsrqlx}
        </if>
        <if test="dto.xhlx != null and dto.xhlx != ''">
            and a.xhlx = #{dto.xhlx}
        </if>
        <if test="dto.xhzq != null">
            and a.xhzq = #{dto.xhzq}
        </if>
        <if test="dto.zbpbksrq != null and dto.zbpbjsrq != null">
            and (
            a.zbpbksrq between #{dto.zbpbksrq} and #{dto.zbpbjsrq} or
            a.zbpbjsrq between #{dto.zbpbksrq} and #{dto.zbpbjsrq}
            )
        </if>
        order by a.zbpbksrq desc
        limit #{pageIndex},#{pageSize}
    </select>



    <select id="checkThisTimeZbpb" resultType="com.mosty.base.model.entity.qwzx.TbQwZbpbMrjl">
        <![CDATA[
            select * from tb_qw_zbpb_mrjl a where
            zbkssj BETWEEN DATE_FORMAT(date_add(now(),interval - 2 hour), '%Y-%m-%d %H:%i:%s')
            and DATE_FORMAT(date_add(now(),interval + 2 hour), '%Y-%m-%d %H:%i:%s')
        ]]>
    </select>

    <select id="getZbBySjAndJl" resultType="com.mosty.base.model.entity.qwzx.TbQwZbpb">
        select * from tb_qw_zbpb a where a.xt_sjzt = '1' and a.xt_scbz = '0'
        <if test="id != null and id !=''">
            and a.id != #{id}
        </if>
        <![CDATA[
        and a.id in (
            select b.pb_id from tb_qw_zbpb_zq b where b.xt_sjzt = '1' and b.xt_scbz = '0'
            and (
                b.zbld_id = #{jlId} or
                b.zbzhz_id = #{jlId} or
                b.zzzhz_id = #{jlId} or
                b.fzzb_id = #{jlId} or
                b.id in (
                    select c.ywid from tb_qw_jl c where c.jl_id = #{jlId}
                )
            )
            and b.id in (
                select d.pbzq_id from tb_qw_zbpb_mrjl d
                case when b.kssj >= b.jssj then
                #{kssj} between DATE_FORMAT(b.kssj,'%Y-%m-%d %H:%i:%s') and DATE_FORMAT(b.jssj,'%Y-%m-%d %H:%i:%s') or
                #{jssj} between DATE_FORMAT(b.kssj,'%Y-%m-%d %H:%i:%s') and DATE_FORMAT(b.jssj,'%Y-%m-%d %H:%i:%s')
            )
        )
        and (
            #{zbpbksrq} between DATE_FORMAT(a.zbpbksrq,'%Y-%m-%d') and DATE_FORMAT(a.zbpbjsrq,'%Y-%m-%d') or
            #{zbpbjsrq} between DATE_FORMAT(a.zbpbksrq,'%Y-%m-%d') and DATE_FORMAT(a.zbpbjsrq,'%Y-%m-%d')
        )
        ]]>
    </select>
</mapper>
