<?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.personName != null and query.personName != ''">
                AND e.person_name LIKE CONCAT('%', #{query.personName}, '%')
            </if>
            <if test="query.storeName != null and query.storeName != ''">
                AND e.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 t.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>


</mapper>
