<?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.SysReServiceTenantMapper">

    <resultMap type="com.vctgo.system.domain.SysReServiceTenant" id="SysReServiceTenantResult">
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="serviceId"    column="service_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="remark"    column="remark"    />
        <result property="createById" column="create_by_id"/>
        <result property="updateById" column="update_by_id"/>
    </resultMap>

    <sql id="selectSysReServiceTenantVo">
        select id, tenant_id, service_id, is_deleted, create_by, create_time, update_by, update_time, remark,create_by_id, update_by_id from sys_re_service_tenant
    </sql>

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

    <select id="selectSysReServiceTenantList"   resultMap="SysReServiceTenantResult">
        <include refid="selectSysReServiceTenantVo"/>
        <where>
            <include refid="defaultDeleteCondition"/>
            <if test="query.tenantId != null "> and tenant_id = #{query.tenantId}</if>
            <if test="query.serviceId != null "> and service_id = #{query.serviceId}</if>
            <if test="query.isDeleted != null  and query.isDeleted != ''"> and is_deleted = #{query.isDeleted}</if>
        </where>
    </select>

    <update id="logicalDeleteSysReServiceTenantByIds" parameterType="String">
        update sys_re_service_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="deleteSysReServiceTenantByIds" parameterType="String">
        delete from sys_re_service_tenant where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectSysReServiceTenantListByTenantId" resultType="com.vctgo.system.domain.SysReServiceTenant">
        SELECT
            st.id,
            st.tenant_id,
            st.service_id,
            s.service_name
        FROM
            sys_re_service_tenant st
                INNER JOIN sys_service s ON st.service_id = s.id
        WHERE
            st.is_deleted = '0'
          AND s.is_deleted = '0'
          AND st.tenant_id = #{tenantId}
    </select>

    <select id="selectMenuIdStringByTenantId" resultType="java.lang.String">
        <choose>
            <when test="tenantType ==1">
                SELECT
                    GROUP_CONCAT( ss.menu_ids )
                FROM
                    sys_re_service_tenant srst
                INNER JOIN sys_service ss ON srst.service_id = ss.id
                WHERE srst.tenant_id in  (select srgb.id from sys_re_garden_branch srgb where srgb.parent_id = #{tenantId} and srgb.is_deleted='0' ) and srst.is_deleted='0'
            </when>
            <otherwise>
                SELECT
                    GROUP_CONCAT( ss.menu_ids )
                FROM
                    sys_re_service_tenant srst
                INNER JOIN sys_service ss ON srst.service_id = ss.id
                WHERE srst.tenant_id = #{tenantId} and srst.is_deleted='0'
            </otherwise>
        </choose>
    </select>
</mapper>
