<?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.vctgo.system.mapper.SysUserTenantMapper">

    <resultMap type="com.vctgo.system.domain.SysUserTenant" id="SysUserTenantResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="userId" column="user_id"/>
        <result property="isDeleted" column="is_deleted"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="createById" column="create_by_id"/>
        <result property="updateById" column="update_by_id"/>
        <result property="tenantName" column="tenant_name"/>
        <result property="tenantEndTime" column="tenant_end_time"/>
        <result property="longTerm" column="long_term"/>
        <result property="status" column="status"/>
    </resultMap>

    <sql id="selectSysUserTenantVo">
        select id,
               tenant_id,
               user_id,
               is_deleted,
               create_by,
               create_time,
               update_by,
               update_time,
               create_by_id,
               update_by_id
        from sys_user_tenant
    </sql>

    <sql id="defaultDeleteCondition">
        is_deleted
        = 0
    </sql>

    <select id="selectSysUserTenantList" resultMap="SysUserTenantResult">
            SELECT mt.tenant_id,
                   mt.tenant_name,
                   mt.create_time,
                   mt.tenant_end_time,
                   mt.long_term,
                   mt.status
            FROM (SELECT sut.tenant_id,
                         st.tenant_name,
                         st.create_time,
                         st.tenant_end_time,
                         st.long_term,
                         st.STATUS
                  FROM sys_user_tenant sut
                           INNER JOIN sys_tenant st ON sut.tenant_id = st.id
                           INNER JOIN sys_re_garden_branch rgb ON sut.tenant_id = rgb.id
                      AND sut.is_deleted = '0'
                      AND sut.user_id = #{query.userId}
                      AND sut.tenant_id IN (SELECT rgb_f.id
                                            FROM sys_re_garden_branch rgb_f
                                            WHERE rgb_f.parent_id = (SELECT su.tenant_id
                                                                     FROM sys_user su
                                                                     WHERE su.user_id = #{query.userId}
                                                                       AND su.del_flag = '0'))
                  UNION
                  SELECT stf.id tenant_id,
                         stf.tenant_name,
                         stf.create_time,
                         stf.tenant_end_time,
                         1,
                         0
                  FROM sys_tenant stf
                  WHERE stf.del_flag = '0'
                    AND stf.id =
                        (SELECT su.tenant_id FROM sys_user su WHERE su.user_id = #{query.userId} AND su.del_flag = '0')) mt
            ORDER BY mt.create_time
    </select>

    <update id="logicalDeleteSysUserTenantByIds" parameterType="String">
        update sys_user_tenant set is_deleted = #{query.isDeleted},
        update_by_id = #{query.updateById}, update_by = #{query.updateBy},
        update_time = #{query.updateTime} where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteSysUserTenantByIds" parameterType="String">
        delete from sys_user_tenant where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
