<?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.TbQwZbbbMapper">
    <resultMap id="BaseResultMap" type="com.mosty.base.model.vo.qwzx.TbQwZbbbVo">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <id column="qwbcid" property="qwbcid" jdbcType="VARCHAR"/>
        <result column="zbrq" property="zbrq"/>
        <result column="kssj" property="kssj"/>
        <result column="jssj" property="jssj"/>
        <result column="zbsc" property="zbsc"/>
        <result column="zbldXm" property="zbld_xm"/>
        <result column="zbldDh" property="zbld_dh"/>
        <result column="zbld_id" property="zbldId"/>
        <result column="zbzhz_xm" property="zbzhzXm"/>
        <result column="zbzhz_id" property="zbzhzId"/>
        <result column="zzzhz_xm" property="zzzhzXm"/>
        <result column="zzzhz_id" property="zzzhzId"/>
        <result column="fzzb_xm" property="fzzbXm"/>
        <result column="fzzb_id" property="fzzbId"/>
        <result column="zbmj" property="zbmj"/>
        <result column="mjsl" property="mjsl"/>
        <result column="txzbsl" property="txzbsl"/>
        <result column="hh" property="hh"/>
        <result column="zbzt" property="zbzt"/>
        <result column="ssbm" property="ssbm"/>
        <result column="ssbmdm" property="ssbmdm"/>
        <result column="ssxgaj" property="ssxgaj"/>
        <result column="ssxgajdm" property="ssxgajdm"/>
        <result column="sssgaj" property="sssgaj"/>
        <result column="sssgajdm" property="sssgajdm"/>
        <result column="bz" property="bz"/>
        <result column="zbmjxm" property="zbmjxm"/>
        <collection property="jlList" javaType="ArrayList"
                    ofType="com.mosty.base.model.dto.qwzx.TbQwJlDto" select="selectJlList"
                    column="{ywid = id,zbmjxm = zbmjxm}"/>
    </resultMap>

    <resultMap id="resultMap" type="com.mosty.base.model.vo.qwzx.TbQwZbbbVo">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <id column="qwbcid" property="qwbcid" jdbcType="VARCHAR"/>
        <result column="zbrq" property="zbrq"/>
        <result column="kssj" property="kssj"/>
        <result column="jssj" property="jssj"/>
        <result column="zbsc" property="zbsc"/>
        <result column="zbldXm" property="zbld_xm"/>
        <result column="zbldDh" property="zbld_dh"/>
        <result column="zbld_id" property="zbldId"/>
        <result column="zbzhz_xm" property="zbzhzXm"/>
        <result column="zbzhz_id" property="zbzhzId"/>
        <result column="zzzhz_xm" property="zzzhzXm"/>
        <result column="zzzhz_id" property="zzzhzId"/>
        <result column="fzzb_xm" property="fzzbXm"/>
        <result column="fzzb_id" property="fzzbId"/>
        <result column="zbmj" property="zbmj"/>
        <result column="mjsl" property="mjsl"/>
        <result column="txzbsl" property="txzbsl"/>
        <result column="hh" property="hh"/>
        <result column="zbzt" property="zbzt"/>
        <result column="ssbm" property="ssbm"/>
        <result column="ssbmdm" property="ssbmdm"/>
        <result column="ssxgaj" property="ssxgaj"/>
        <result column="ssxgajdm" property="ssxgajdm"/>
        <result column="sssgaj" property="sssgaj"/>
        <result column="sssgajdm" property="sssgajdm"/>
        <result column="bz" property="bz"/>
    </resultMap>

    <resultMap type="com.mosty.base.model.dto.qwzx.TbQwJlDto" id="TbQwJlDto">
        <id column="id" property="id"/>
        <result column="ywid" property="ywid"/>
        <result column="jl_id" property="jlId"/>
        <result column="jllx" property="jllx"/>
        <result column="jlxm" property="jlxm"/>
        <result column="jh" property="jh"/>
        <result column="xbdm" property="xbdm"/>
        <result column="nl" property="nl"/>
        <result column="jlzt" property="jlzt"/>
        <result column="zblx" property="zblx"/>
    </resultMap>

    <select id="selectJlList" resultMap="TbQwJlDto">

                select b.id,
                       b.ywid,
                       b.jl_id,
                       b.jllx,
                       b.jlxm,
                       b.xbdm,
                       b.nl,
                       b.jlzt,
                       b.zblx
                from tb_qw_jl b
                where b.xt_sjzt = '1'
                  and b.xt_scbz = '0'
                  and b.ywid = #{ywid}
                  and b.jllx = '01'

    </select>

    <!--查询-->
    <select id="queryList" resultMap="BaseResultMap">
        select a.id,a.zbrq,a.ksrq,a.jsrq,a.zbsc,a.zbld_xm,a.zbld_dh,a.zbmj,a.mjsl,a.txzbsl,a.hh,a.zbzt,
        a.ssbm,a.ssbmdm,a.bz,#{params.zbmjxm} as zbmjxm
        from tb_qw_zbbb a
        where a.xt_scbz = '0'
        <if test="params.zbmjxm != null and params.zbmjxm != ''">
            and a.zbmj like concat('%',#{params.zbmjxm},'%')
        </if>
        <if test="params.kssj != null and params.jssj == null">
            and a.zbrq &gt;= #{params.kssj,jdbcType=DATE}
        </if>
        <if test="params.kssj == null and params.jssj != null">
            and a.zbrq &lt;= #{params.jssj,jdbcType=DATE}
        </if>
        <if test="params.kssj != null and params.jssj != null">
            and a.zbrq between #{params.kssj,jdbcType=DATE} and #{params.jssj,jdbcType=DATE}
        </if>
        <if test="params.zbld != null and params.zbld != ''">
            and a.zbld like concat('%',#{params.zbld},'%')
        </if>
        <if test="params.zbzt != null and params.zbzt != ''">
            and a.zbld = #{params.zbld,jdbcType=VARCHAR}
        </if>
        <if test="params.xtSjly != null and params.xtSjly != ''">
            and a.xt_sjly = #{params.xtSjly,jdbcType=VARCHAR}
        </if>
        <if test="params.xtSjzt != null and params.xtSjzt != ''">
            and a.xt_sjzt = #{params.xtSjzt,jdbcType=VARCHAR}
        </if>
        <if test="params.ssbmdm != null and params.ssbmdm != ''">
            and a.ssbmdm like  concat(#{params.ssbmdm,jdbcType=VARCHAR},'%')
        </if>
<!--        <if test="params.ssxgajdm != null and params.ssxgajdm != ''">-->
<!--            and a.ssxgajdm = #{params.ssxgajdm,jdbcType=VARCHAR}-->
<!--        </if>-->
<!--        <if test="params.sssgajdm != null and params.sssgajdm != ''">-->
<!--            and a.sssgajdm = #{params.sssgajdm,jdbcType=VARCHAR}-->
<!--        </if>-->
        <!-- 数据范围过滤 -->
<!--        ${createSql}-->
    </select>

    <!--id查询-->
    <select id="queryById" resultMap="BaseResultMap">

                select a.*, b.*, '' as zbmjxm
                from tb_qw_zbbb a
                         inner join tb_qw_jl b on a.id = b.ywid
                where a.xt_scbz = '0'
                  and a.id = #{id,jdbcType=VARCHAR}

    </select>

    <!--删除值班-->
    <update id="deleteZb">
        update tb_qw_zbbb set xt_scbz = '1' where id in
        <foreach collection="id" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <!--APP获取-->
    <select id="getMyBbInfoToday" resultType="com.mosty.base.model.entity.qwzx.TbQwZbbb">

                select *
                from tb_qw_zbbb a
                where a.xt_scbz = '0'
                  and a.xt_sjzt = '1'
                  and DATEDIFF(a.zbrq, NOW()) = 0
                  and a.bbzt = '0'
                  and a.bbjssj is null
                  and (
                     a.zbld_id = #{userId}
                     or a.zbzhz_id = #{userId}
                     or a.zzzhz_id = #{userId}
                     or a.fzzb_id = #{userId}
                     or a.id in (select ywid
                     from tb_qw_jl b
                     where b.xt_scbz = '0'
                     and b.xt_sjzt = '1'
                     and b.ywid = a.id
                     and b.jl_id = #{userId}
                     )
                  )
                  limit 1

    </select>

    <select id="getMybbTodayByTime" resultType="com.mosty.base.model.entity.qwzx.TbQwZbbb">
        select *
        from tb_qw_zbbb a
        where a.xt_scbz = '0'
          and a.xt_sjzt = '1'
        <if test="time != null and time != ''">
            and date_format(a.zbrq,'%Y-%m-%d') = #{time}
        </if>
          and a.bbzt = '0'
          and a.bbjssj is null
          and (
                a.zbld_id = #{userId}
                        or a.zbzhz_id = #{userId}
                        or a.zzzhz_id = #{userId}
                        or a.fzzb_id = #{userId}
                        or a.id in (select ywid
                                    from tb_qw_jl b
                                    where b.xt_scbz = '0'
                                      and b.xt_sjzt = '1'
                                      and b.ywid = a.id)
                )
    </select>
</mapper>
