<?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="cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="selectTenantPage" resultType="cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO">
        SELECT
            st.id,
            st.NAME,
            st.contact_user_id,
            st.contact_name,
            st.contact_mobile,
            st.STATUS,
            st.website,
            st.package_id,
            st.expire_time,
            st.account_count,
            st.create_time,
            st.groups_id,
            st.groups_manager,
            sg.NAME AS groupName
        FROM
            system_tenant st
        LEFT JOIN system_groups sg ON st.groups_id = sg.id
        WHERE st.deleted = 0
        <if test="pageVO.name != null">
            AND st.NAME LIKE CONCAT('%', #{pageVO.name}, '%')
        </if>
        <if test="pageVO.contactName != null">
            AND st.contact_name LIKE CONCAT('%', #{pageVO.contactName}, '%')
        </if>
        <if test="pageVO.contactMobile != null">
            AND st.contact_mobile LIKE CONCAT('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.groupName != null">
            AND sg.NAME LIKE CONCAT('%', #{pageVO.groupName}, '%')
        </if>
        <if test="pageVO.createTime != null">
            AND st.create_time between #{pageVO.createTime[0]} and #{pageVO.createTime[1]}
        </if>
        <if test="pageVO.status != null">
            and st.`status` = #{pageVO.status}
        </if>
        <if test="pageVO.groupsManager != null">
            and st.groups_manager = #{pageVO.groupsManager}
        </if>
        ORDER BY st.id DESC
    </select>

    <select id="checkGroupsManager" resultType="java.lang.Integer">
        SELECT
            st.groups_manager
        FROM
            system_tenant st
                INNER JOIN system_users su ON st.id = su.tenant_id
        WHERE
            su.id = #{loginUserId}
    </select>

    <select id="selectListByUserId" resultType="cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO">
        SELECT
            id,
            name
        FROM system_tenant WHERE groups_id = (
            SELECT
                st.groups_id
            FROM
                system_tenant st
            INNER JOIN system_users su ON st.id = su.tenant_id
            WHERE su.id = #{loginUserId}
        )
    </select>


    <select id="selectOneByUserId" resultType="cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO">
        SELECT
            id,
            name
        FROM system_tenant WHERE id = (
            SELECT
                st.groups_id
            FROM
                system_tenant st
            INNER JOIN system_users su ON st.id = su.tenant_id
            WHERE su.id = #{loginUserId}
        )
    </select>

    <select id="selectByHomeData" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyHomeTenantRespVO">
        SELECT
            st.id AS tenantId,
            st.NAME AS tenantName,
            st.update_time,
            stei.enterprise_name,
            stei.enterprise_abbreviation,
            stei.province,
            stei.business_scope,
            stei.detailed_address,
            stei.picture,
            stei.introduction,
            stbi.org_level,
            stbi.cover_area,
            stbi.full_bed_num,
            stbi.daycare_bed_num,
            stbi.nursing_bed_num,
            stbi.creation_date
        FROM
            system_tenant st
                LEFT JOIN system_tenant_enterprise_info stei ON st.id = stei.tenant_id
                LEFT JOIN system_tenant_base_info stbi ON st.id = stbi.tenant_id
        WHERE
            1 = 1
            <if test="reqVO.province != null">
                AND  stei.province = #{reqVO.province}
            </if>

            <if test="reqVO.tenantIds != null">
                AND st.id IN
                <foreach item="id" collection="reqVO.tenantIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>


    </select>


</mapper>