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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.system.domain.SysTenant">
        <id column="id" property="id"/>
        <result column="tenant_code" property="tenantCode"/>
        <result column="tenant_name" property="tenantName"/>
        <result column="tenant_manager" property="tenantManager"/>
        <result column="tenant_type" property="tenantType"/>
        <result column="domain" property="domain"/>
        <result column="email" property="email"/>
        <result column="phone_number" property="phoneNumber"/>
        <result column="address" property="address"/>
        <result column="geo_position" property="geoPosition"/>
        <result column="tenant_term" property="tenantTerm"/>
        <result column="remark" property="remark"/>
        <result column="status" property="status"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="system_name" property="systemName"/>
    </resultMap>

    <select id="getUserData" resultType="com.usky.system.service.vo.SysTenantVo">
        SELECT
        a.*,b.id,b.tenant_code,b.tenant_name,b.tenant_manager,b.tenant_type,b.domain,b.tenant_term,b.system_name
        FROM
        sys_user AS a
        LEFT JOIN sys_tenant AS b ON a.tenant_id = b.id
        <where>
            a.user_id = #{userId}
        </where>
    </select>

    <select id="getAppData" resultType="com.usky.system.domain.SysTenant">
        SELECT
        a.*
        FROM
        sys_tenant AS a
        <where>
            a.domain = #{domain}
        </where>
    </select>

    <select id="getTenantData" resultType="com.usky.system.service.vo.SysTenantTwoVo">
        SELECT
        a.*,b.platform_name
        FROM
        sys_tenant as a left join sys_platform as b on a.system_name=b.id
        <where>
            <if test="id != null and id != '' and id!=0">
                and a.id = #{id}
            </if>
            <if test="tenantName != null and tenantName != ''">
                and a.tenant_name LIKE CONCAT(CONCAT('%', #{tenantName}), '%')
            </if>
            <if test="tenantManager != null and tenantManager != ''">
                and a.tenant_manager LIKE CONCAT(CONCAT('%', #{tenantManager}), '%')
            </if>
            <if test="tenantType != null and tenantType != ''">
                and a.tenant_type = #{tenantType}
            </if>
            <if test="platformName != null and platformName != ''">
                and b.platform_name LIKE CONCAT(CONCAT('%', #{platformName}), '%')
            </if>
            <if test="status != null and status != ''">
                and a.status = #{status}
            </if>
            <if test="endTime != null and startTime != null and endTime != '' and startTime != ''">
                and a.tenant_term BETWEEN #{startTime} and #{endTime}
            </if>
        </where>
        order by a.id desc
        <if test="current != null and size != null and size != 0">
            limit #{current},#{size}
        </if>
    </select>

    <select id="getTenantDataOne" resultType="com.usky.system.service.vo.SysTenantTwoVo">
        SELECT
        a.*,b.platform_name
        FROM
        sys_tenant as a left join sys_platform as b on a.system_name=b.id
        <where>
            a.status=0
            <if test="tenantName != null and tenantName != ''">
                and a.tenant_name LIKE CONCAT(CONCAT('%', #{tenantName}), '%')
            </if>
            <if test="tenantManager != null and tenantManager != ''">
                and a.tenant_manager LIKE CONCAT(CONCAT('%', #{tenantManager}), '%')
            </if>
        </where>
        order by a.id desc
        <if test="current != null and size != null and size != 0">
            limit #{current},#{size}
        </if>
    </select>

</mapper>
