<?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="org.dromara.system.mapper.SysTenantMapper">

    <select id="getlist" resultType="org.dromara.system.domain.dto.TenantDTO">
        SELECT
        t.*,
        e.*,
        m.nickname AS inviteUserName,
        ct.template_name AS templateName,
        agency.contact_user_name AS agencyContactUserName
        FROM
        sys_tenant t
        LEFT JOIN
        sys_tenant_extend e ON t.tenant_id = e.tenant_id
        LEFT JOIN
        ums_member m ON e.invite_user_id = m.id
        LEFT JOIN
        sys_commission_template ct ON e.split_ratio = ct.id
        LEFT JOIN
        sys_tenant agency ON t.agency_tenant_id = agency.tenant_id
        <where>
            <if test="query.tenantId != null and query.tenantId != ''">
                AND t.tenant_id LIKE CONCAT('%', #{query.tenantId}, '%')
            </if>
            <if test="query.oneParentId != null ">
                AND t.parent_ids LIKE CONCAT('%', #{query.oneParentId}, '%')
            </if>
            <if test="query.personName != null and query.personName != ''">
                AND e.person_name LIKE CONCAT('%', #{query.personName}, '%')
            </if>
            <if test="query.storeName != null and query.storeName != ''">
                AND t.store_name LIKE CONCAT('%', #{query.storeName}, '%')
            </if>
            <if test="query.contactUserName != null and query.contactUserName != ''">
                AND t.contact_user_name LIKE CONCAT('%', #{query.contactUserName}, '%')
            </if>
            <if test="query.companyName != null and query.companyName != ''">
                AND t.company_name LIKE CONCAT('%', #{query.companyName}, '%')
            </if>
            <if test="query.contactPhone != null and query.contactPhone != ''">
                AND t.contact_phone LIKE CONCAT('%', #{query.contactPhone}, '%')
            </if>
            <if test="query.entryTime != null and query.entryTime != ''">
                AND e.entry_time LIKE CONCAT('%', #{query.entryTime}, '%')
            </if>
            <if test="query.birthday != null and query.birthday != ''">
                AND t.birthday LIKE CONCAT('%', #{query.birthday}, '%')
            </if>
            <if test="query.address != null and query.address != ''">
                AND t.address LIKE CONCAT('%', #{query.address}, '%')
            </if>
            <if test="query.county != null and query.county != ''">
                AND t.county LIKE CONCAT('%', #{query.county[0], jdbcType=VARCHAR}, '%')
            </if>
            <if test="query.companyType != null and query.companyType != ''">
                AND t.company_type LIKE CONCAT('%', #{query.companyType}, '%')
            </if>
            <if test="query.address != null and query.address != ''">
                AND t.address LIKE CONCAT('%', #{query.address}, '%')
            </if>
            <if test="query.signStatus != null">
                AND e.sign_status = #{query.signStatus}
            </if>
            <if test="query.promoterStatus != null">
                AND e.promoter_status = #{query.promoterStatus}
            </if>
            <if test="query.type != null">
                AND t.type = #{query.type}
            </if>
            <if test="query.signTime != null and query.signTime.size() == 2">
                AND e.sign_date BETWEEN #{query.signTime[0]} AND #{query.signTime[1]}
            </if>
        </where>
    </select>


    <delete id="batchDeleteTenantExtendByStoreIds">
        DELETE FROM sys_tenant_extend
        WHERE store_id IN
        <foreach item="id" index="index" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="batchDeleteTenantByIds">
        DELETE FROM sys_tenant
        WHERE id IN
        <foreach item="id" index="index" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getReferenceList" resultType="org.dromara.system.domain.vo.SysReferenceVO">
        SELECT
            st.* ,
            ste.*,
            st1.agency_name AS agencyTenantName
        FROM
            sys_tenant st
                LEFT JOIN sys_tenant_extend ste ON st.tenant_id = ste.tenant_id
                LEFT JOIN sys_tenant st1 ON st.agency_tenant_id = st1.tenant_id
        WHERE
            st.type = 5
          AND st.del_flag = 0
        AND st.parent_ids LIKE CONCAT( '%',#{query.oneParentId}, '%' )
        <if test="query.personName != null and query.personName != ''">
            AND ste.person_name LIKE CONCAT('%', #{query.personName}, '%')
        </if>
        <if test="query.personPhone != null and query.personPhone != ''">
            AND ste.person_phone LIKE CONCAT('%', #{query.personPhone}, '%')
        </if>
        <if test="query.status != null  and query.status != ''">
            AND st.status = #{query.status}
        </if>
        order by st.agency_tenant_id, st.create_time
    </select>
</mapper>
