<?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.yc.ship.module.resource.dal.mysql.guidecheckin.GuideCheckInMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->


    <select id="queryPage" resultType="com.yc.ship.module.resource.dal.dataobject.guidecheckin.GuideCheckInDO">
        SELECT
            t1.id guide_id,
            t1.`name`,
            t1.sex,
            t2.check_time,
            t2.status
        FROM dispatch_guide t1
        left join dispatch_guide_check_in t2 on t2.guide_id=t1.id
        where t1.deleted=0 and t1.use_status=1
        <if test="param.guideId != null and param.guideId !=''">
            and t1.id = #{param.guideId}
        </if>
        <if test="param.checkTime != null and param.checkTime !=''">
            and t2.check_time BETWEEN #{param.startCheckTime} and #{param.endCheckTime}
        </if>
        order by t1.create_time desc , t1.update_time desc

    </select>

    <select id="queryLeaveData" resultType="map">

        select
            t.status type,
            sum(t.num) num
        from(
            select t1.status, count(1) num
            from dispatch_guide_check_in t1
            where 1=1  and t1.deleted=0 and t1.check_time_type=2
            <if test="param.guideId != null and param.guideId !=''">
                and t1.guide_id = #{param.guideId}
            </if>
            <if test="param.startCheckTime != null and param.endCheckTime != null">
                and t1.check_time BETWEEN #{param.startCheckTime} and #{param.endCheckTime}
            </if>
            GROUP BY t1.status
            union all
            select t1.status, round(count(1)/2,1) num
            from dispatch_guide_check_in t1
            where 1=1  and t1.deleted=0 and t1.check_time_type!=2
            <if test="param.guideId != null and param.guideId !=''">
                and t1.guide_id = #{param.guideId}
            </if>
            <if test="param.startCheckTime != null and param.endCheckTime != null">
                and t1.check_time BETWEEN #{param.startCheckTime} and #{param.endCheckTime}
            </if>
            GROUP BY t1.status
        )t
        group by t.status
    </select>
</mapper>
