<?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.ruoyi.system.mapper.SysTenantMapper">

    <select id="selectUserTenantList" resultType="SysTenant">
        select t.tenant_id,t.tenant_name
        from sys_tenant t,sys_user_tenant ut
        where t.delete_flag='0'
          and t.tenant_id=ut.tenant_id and ut.user_id=#{userId}
        <if test="limitTenantUserId != null and limitTenantUserId != ''">
            and exists(select 1 from sys_user_tenant utl where utl.user_id=#{limitTenantUserId} and utl.tenant_id=t.tenant_id)
        </if>
        order by t.create_time desc
    </select>
    <select id="selectInUseUserTenant" resultType="SysTenant">
        select * from sys_tenant t,sys_user_tenant ut
        where t.tenant_id=ut.tenant_id
          and ut.user_id=#{userId} and ut.tenant_id=#{tenantId}
          and now() between t.begin_time and t.end_time
          and t.in_use='1'
          and t.delete_flag='0'
    </select>
    <select id="tenantHaveContent" resultType="integer">
        select
            sum(CASE
                    WHEN s.student_name is not null  THEN 1
                    WHEN c.course_name is not null  THEN 1
                    WHEN a.cla_name is not null  THEN 1
                    ELSE 0			END)
        from sys_tenant t
                 LEFT JOIN sc_student s on t.tenant_id=s.tenant_id
                 LEFT JOIN sc_course c on t.tenant_id=c.tenant_id
                 LEFT JOIN sc_course_cla a on t.tenant_id=a.tenant_id
        WHERE t.tenant_id=#{tenantId}
    </select>
</mapper>
