<?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.ruoyi.system.mapper.SysTenantMapper">

    <resultMap type="LoginSysTenant" id="LoginSysTenantResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="companyName" column="company_name"/>
    </resultMap>

    <resultMap type="SysTenant" id="SysTenantResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="tenantPackageId" column="tenant_package_id"/>
        <result property="tenantUserId" column="tenant_user_id"/>
        <result property="contactUserName" column="contact_user_name"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="companyName" column="company_name"/>
        <result property="intro" column="intro"/>
        <result property="accountCount" column="account_count"/>
        <result property="expireTime" column="expire_time"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
        <result property="creator" column="creator"/>
        <result property="createTime" column="create_time"/>
        <result property="updater" column="updater"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="SysTenantVo" id="SysTenantVoResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="tenantPackageId" column="tenant_package_id"/>
        <result property="tenantUserId" column="tenant_user_id"/>
        <result property="contactUserName" column="contact_user_name"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="companyName" column="company_name"/>
        <result property="intro" column="intro"/>
        <result property="accountCount" column="account_count"/>
        <result property="expireTime" column="expire_time"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
        <result property="creator" column="creator"/>
        <result property="createTime" column="create_time"/>
        <result property="updater" column="updater"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectSysTenantVo">
        select tenant_id,
               tenant_package_id,
               tenant_user_id,
               contact_user_name,
               contact_phone,
               company_name,
               intro,
               account_count,
               expire_time,
               status,
               del_flag,
               remark,
               creator,
               create_time,
               updater,
               update_time
        from sys_tenant
    </sql>

    <select id="selectSysTenantList" parameterType="SysTenant" resultMap="SysTenantResult">
        <include refid="selectSysTenantVo"/>
        <where>
            <if test="tenantPackageId != null ">and tenant_package_id = #{tenantPackageId}</if>
            <if test="contactUserName != null  and contactUserName != ''">and contact_user_name like concat('%',
                #{contactUserName}, '%')
            </if>
            <if test="contactPhone != null  and contactPhone != ''">and contact_phone = #{contactPhone}</if>
            <if test="companyName != null  and companyName != ''">and company_name like concat('%', #{companyName},
                '%')
            </if>
            <if test="params.beginExpireTime != null and params.beginExpireTime != '' and params.endExpireTime != null and params.endExpireTime != ''">
                and expire_time between #{params.beginExpireTime} and #{params.endExpireTime}
            </if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="creator != null  and creator != ''">and creator = #{creator}</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
            <if test="updater != null  and updater != ''">and updater = #{updater}</if>
            and del_flag = '0'
        </where>
    </select>

    <select id="selectSysTenantByTenantId" parameterType="String" resultMap="SysTenantResult">
        <include refid="selectSysTenantVo"/>
        where tenant_id = #{tenantId}
    </select>

    <select id="loginSysTenantList" resultMap="LoginSysTenantResult">
        select tenant_id,
               company_name
        from sys_tenant
        where del_flag = '0'
          and status = '0'
    </select>
    <select id="selectSysTenantByCompanyName" parameterType="String" resultMap="SysTenantResult">
        <include refid="selectSysTenantVo"/>
        where company_name = #{companyName}
    </select>
    <select id="selectSysTenantByContactPhone" parameterType="String" resultMap="SysTenantResult">
        <include refid="selectSysTenantVo"/>
        where contact_phone = #{contactPhone}
    </select>
    <select id="selectSysTenantListByTenantPackageId" resultMap="SysTenantVoResult">
        <include refid="selectSysTenantVo"/>
        where tenant_package_id = #{tenantPackageId}
    </select>

    <select id="selectSysTenantByTenantPackageId" resultType="java.lang.Integer">
        select count(1) from sys_tenant where tenant_package_id = #{tenantPackageId}
    </select>

    <insert id="insertSysTenant" parameterType="SysTenant">
        insert into sys_tenant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">tenant_id,</if>
            <if test="tenantPackageId != null">tenant_package_id,</if>
            <if test="tenantUserId != null">tenant_user_id,</if>
            <if test="contactUserName != null">contact_user_name,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="companyName != null">company_name,</if>
            <if test="intro != null">intro,</if>
            <if test="accountCount != null">account_count,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="creator != null and creator != ''">creator,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updater != null">updater,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">#{tenantId},</if>
            <if test="tenantPackageId != null">#{tenantPackageId},</if>
            <if test="tenantUserId != null">#{tenantUserId},</if>
            <if test="contactUserName != null">#{contactUserName},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="intro != null">#{intro},</if>
            <if test="accountCount != null">#{accountCount},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updater != null">#{updater},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateSysTenant" parameterType="SysTenant">
        update sys_tenant
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantPackageId != null">tenant_package_id = #{tenantPackageId},</if>
            <if test="tenantUserId != null">tenant_user_id = #{tenantUserId},</if>
            <if test="contactUserName != null">contact_user_name = #{contactUserName},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="intro != null">intro = #{intro},</if>
            <if test="accountCount != null">account_count = #{accountCount},</if>
            expire_time = #{expireTime},
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="creator != null and creator != ''">creator = #{creator},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updater != null">updater = #{updater},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where tenant_id = #{tenantId}
    </update>

    <delete id="deleteSysTenantByTenantId" parameterType="String">
        update sys_tenant
        set del_flag = '1'
        where tenant_id = #{tenantId}
    </delete>

    <delete id="deleteSysTenantByTenantIds" parameterType="String">
        update sys_tenant set del_flag = '1'
        where tenant_id in
        <foreach item="tenantId" collection="array" open="(" separator="," close=")">
            #{tenantId}
        </foreach>
    </delete>
</mapper>
