<?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>
    <insert id="cloneSysTenantConfig">
        insert into sys_tenant_config (tenant_id,login_title,login_logo,login_back_url,login_footer,login_type,middle_logo,middle_back_url,middle_title,middle_footer,home_logo,home_page_url,home_layout,home_style,create_by,create_time,label_icon,middle_status,large_screen_url,cloud_type,cloud_url,message_status,middle_type)
        select  #{tenantId},login_title,login_logo,login_back_url,login_footer,login_type,middle_logo,middle_back_url,middle_title,middle_footer,home_logo,home_page_url,home_layout,home_style,#{createBy},sysdate(),label_icon,middle_status,large_screen_url,cloud_type,cloud_url,message_status,middle_type
            from sys_tenant_config
            <where>
                tenant_id = #{originalTenantId}
            </where>
    </insert>
    <insert id="cloneSysTenantMenu">
        insert into sys_tenant_menu (tenant_id,menu_id,menu_alias_name)
        select  #{tenantId},menu_id,menu_alias_name
        from sys_tenant_menu
        <where>
            tenant_id = #{originalTenantId}
        </where>
    </insert>
    <insert id="cloneSysMobileTenantConfig">
        insert into sys_mobile_tenant_config (tenant_id,login_domain,login_title,login_logo,login_back_url,login_footer,login_type,middle_url,home_logo,home_layout,home_style,create_by,create_time)
        select  #{tenantId},login_domain,login_title,login_logo,login_back_url,login_footer,login_type,middle_url,home_logo,home_layout,home_style,#{createBy},sysdate()
        from sys_mobile_tenant_config
        <where>
            tenant_id = #{originalTenantId}
        </where>
    </insert>
    <insert id="cloneSysMobileBanner">
        INSERT INTO sys_mobile_banner (tenant_id,open_not,carousel_time,banner_path1,banner_path2,banner_path3,banner_path4,banner_path5,link_type1,link_type2,link_type3,link_type4,link_type5,link_url1,link_url2,link_url3,link_url4,link_url5,create_by,create_time)
        SELECT  #{tenantId},open_not,carousel_time,banner_path1,banner_path2,banner_path3,banner_path4,banner_path5,link_type1,link_type2,link_type3,link_type4,link_type5,link_url1,link_url2,link_url3,link_url4,link_url5,#{createBy},sysdate()
        FROM sys_mobile_banner
        <where>
            tenant_id = #{originalTenantId}
        </where>
    </insert>

    <insert id="cloneSysTenantPlatform">
        INSERT INTO sys_tenant_platform (tenant_id,platform_id,is_default)
        SELECT  #{tenantId},platform_id,is_default
        FROM sys_tenant_platform
        <where>
            tenant_id = #{originalTenantId}
        </where>
    </insert>
    <insert id="cloneSysMobileTenantMenu">
        insert into sys_mobile_tenant_menu (tenant_id,menu_id,menu_alias_name)
        select  #{tenantId},menu_id,menu_alias_name
        from sys_mobile_tenant_menu
        <where>
            tenant_id = #{originalTenantId}
        </where>
    </insert>
    <select id="selectSysUserRoleList" resultType="com.usky.system.domain.SysUserRole">
        SELECT
        a.*
        FROM
        sys_user_role AS a
        <where>
            a.tenant_id = #{originalTenantId} and a.user_id = #{UserId}
        </where>
    </select>
    <insert id="cloneSysRoleMenu">
        insert into sys_role_menu (role_id,menu_id,menu_alias_name)
        select  #{roleId},menu_id,menu_alias_name
        from sys_role_menu
        <where>
            role_id = #{originalRoleId}
        </where>
    </insert>
    <insert id="insertSysUserRole">
        insert into sys_user_role (user_id,role_id,tenant_id)
        values(#{UserId},#{roleId},#{tenantId})
    </insert>

    <delete id="delSysTenant">
        delete from sys_tenant WHERE id = #{tenantId}
    </delete>
    <delete id="delSysDept">
        delete from sys_dept WHERE tenant_id = #{tenantId}
    </delete>
    <delete id="delSysUser">
        delete from sys_user where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysUserTenant">
        delete from sys_user_tenant where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysTenantConfig">
        delete from sys_tenant_config where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysTenantMenu">
        delete from sys_tenant_menu where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysTenantPlatform">
        delete from sys_tenant_platform where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysMobileBanner">
        delete from sys_mobile_banner where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysMobileTenantConfig">
        delete from sys_mobile_tenant_config where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysMobileTenantMenu">
        delete from sys_mobile_tenant_menu where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysRoleMenu">
        delete from sys_role_menu where role_id = #{roleId}
    </delete>
    <delete id="delSysRole">
        delete from sys_role where tenant_id = #{tenantId}
    </delete>
    <delete id="delSysUserRole">
        delete from sys_user_role where tenant_id = #{tenantId}
    </delete>

</mapper>
