<?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="cn.aqroid.business.tenant.mapper.TenantMapper">

    <resultMap type="Tenant" id="TenantResult">
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <result property="contactName"    column="contact_name"    />
        <result property="phone"    column="phone"    />
        <result property="email"    column="email"    />
        <result property="logo"    column="logo"    />
        <result property="status"    column="status"    />
        <result property="packageId"    column="package_id"    />
        <result property="expireTime"    column="expire_time"    />
        <result property="depositAmount"    column="deposit_amount"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectTenantVo">
        select tenant_id, tenant_name, contact_name, phone, email, logo, status, package_id, expire_time, deposit_amount, remark, create_time, update_time, del_flag from sys_tenant
    </sql>

    <select id="selectTenantList" parameterType="Tenant" resultMap="TenantResult">
        <include refid="selectTenantVo"/>
        <where>
            del_flag = '0'
            <if test="tenantName != null  and tenantName != ''"> and tenant_name like concat('%', #{tenantName}, '%')</if>
            <if test="contactName != null  and contactName != ''"> and contact_name like concat('%', #{contactName}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="email != null  and email != ''"> and email = #{email}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="packageId != null "> and package_id = #{packageId}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectTenantByTenantId" parameterType="Long" resultMap="TenantResult">
        <include refid="selectTenantVo"/>
        where tenant_id = #{tenantId} and del_flag = '0'
    </select>

    <insert id="insertTenant" parameterType="Tenant" useGeneratedKeys="true" keyProperty="tenantId">
        insert into sys_tenant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantName != null and tenantName != ''">tenant_name,</if>
            <if test="contactName != null">contact_name,</if>
            <if test="phone != null">phone,</if>
            <if test="email != null">email,</if>
            <if test="logo != null">logo,</if>
            <if test="status != null">status,</if>
            <if test="packageId != null">package_id,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="depositAmount != null">deposit_amount,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantName != null and tenantName != ''">#{tenantName},</if>
            <if test="contactName != null">#{contactName},</if>
            <if test="phone != null">#{phone},</if>
            <if test="email != null">#{email},</if>
            <if test="logo != null">#{logo},</if>
            <if test="status != null">#{status},</if>
            <if test="packageId != null">#{packageId},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="depositAmount != null">#{depositAmount},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateTenant" parameterType="Tenant">
        update sys_tenant
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantName != null and tenantName != ''">tenant_name = #{tenantName},</if>
            <if test="contactName != null">contact_name = #{contactName},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="logo != null">logo = #{logo},</if>
            <if test="status != null">status = #{status},</if>
            <if test="packageId != null">package_id = #{packageId},</if>
            <if test="expireTime != null">expire_time = #{expireTime},</if>
            <if test="depositAmount != null">deposit_amount = #{depositAmount},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where tenant_id = #{tenantId} and del_flag = '0'
    </update>

    <delete id="deleteTenantByTenantId" parameterType="Long">
        update sys_tenant set del_flag = '2' where tenant_id = #{tenantId}
    </delete>

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