<?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.yonge.cooleshow.biz.dal.mapper.TenantPersonStatMapper">
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ AS id
        , t.tenant_id_ AS tenantId
        , t.student_num_ AS studentNum
        , t.teacher_num_ AS teacherNum
        , t.day_ AS day
        , t.month_ AS month
        , t.year_ AS year
        , t.create_time_ AS createTime
        </sql> 
    
    <select id="init" resultType="com.yonge.cooleshow.biz.dal.entity.TenantPersonStat">
        select ti.id_ tenantId,COUNT(distinct s.user_id_) studentNum,COUNT(distinct t.user_id_) teacherNum from tenant_info ti
        left join student s ON s.tenant_id_ = ti.id_ AND s.lock_flag_ = 0
        left join teacher t ON t.tenant_id_ = ti.id_ AND t.lock_flag_ = 0
        group by ti.id_
    </select>
    <resultMap id="TenantPersonStatDto" type="com.yonge.cooleshow.biz.dal.wrapper.TenantPersonStatWrapper$TenantPersonStatDto">
        <result property="studentNum" column="studentNum"/>
        <result property="teacherNum" column="teacherNum"/>
        <result property="date" column="date"/>
    </resultMap>
    <select id="indexSum" resultMap="TenantPersonStatDto">
        SELECT DATE_FORMAT(day_,#{query.format}) date,SUM(student_num_) studentNum,SUM(teacher_num_) teacherNum FROM tenant_person_stat
        WHERE tenant_id_ = #{query.tenantId} AND day_ BETWEEN #{query.startTime} AND #{query.endTime}
        GROUP BY ${query.groupBy}
    </select>
    <select id="sumByNow" resultType="com.yonge.cooleshow.biz.dal.wrapper.TenantPersonStatWrapper$TenantPersonStat">
        select COUNT(distinct s.user_id_) studentNum,COUNT(distinct t.user_id_) teacherNum from tenant_info ti
        left join student s ON s.tenant_id_ = ti.id_ AND s.lock_flag_ = 0
        left join teacher t ON t.tenant_id_ = ti.id_ AND t.lock_flag_ = 0
        where ti.id_ = #{tenantId}
    </select>

</mapper>