<?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="net.northking.cctp.saas.db.dao.SaasTenantDao">

  <select id="findByIdxTenantName" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      AND tenant_name = #{0}
      AND is_deleted = 0
    </trim>
  </select>
  <select id="findByIdxTenantCode" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      AND tenant_code = #{0}
      AND is_deleted = 0
    </trim>
  </select>

  <!-- 请在本文件内添加自定义SQL语句 -->

  <resultMap id="DetailMap" type="net.northking.cctp.saas.dto.tenant.SaaSTenantDetailDto">
    <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
    <!-- 租户代码 -->
    <result column="tenant_code" jdbcType="VARCHAR" property="tenantCode"/>
    <!-- 租户名称 -->
    <result column="tenant_name" jdbcType="VARCHAR" property="tenantName"/>
    <!-- 数据隔离级别 -->
    <result column="isolation_level" jdbcType="VARCHAR" property="isolationLevel"/>
    <!-- 创建人 -->
    <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
    <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
    <!-- 租户状态 -->
    <result column="tenant_status" jdbcType="VARCHAR" property="tenantStatus"/>
    <!-- 租户联系电话 -->
    <result column="tenant_phone" jdbcType="VARCHAR" property="tenantPhone"/>
    <!-- 租户邮箱 -->
    <result column="tenant_email" jdbcType="VARCHAR" property="tenantEmail"/>
    <!-- 租户类型 -->
    <result column="tenant_type" jdbcType="VARCHAR" property="tenantType"/>
    <!-- license数量 -->
    <result column="license_count" jdbcType="INTEGER" property="licenseCount"/>
    <!-- 启动/禁用 -->
    <result column="is_enable" jdbcType="BIT" property="enable"/>
    <!-- 租户结束时间 -->
    <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
    <!-- 是否展示二级菜单 -->
    <result column="position" jdbcType="BIT" property="position"/>
    <!-- 启动/禁用 -->
    <result column="project_space" jdbcType="BIT" property="projectSpace"/>
    <!-- 租户logoId-->
    <result column="logo_id" jdbcType="VARCHAR" property="logoId"/>
  </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.saas.db.entity.SaasTenant">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="tenantCode != null">
        AND LOCATE(#{tenantCode},tenant_code)>0
      </if>
      <if test="tenantName != null">
        AND LOCATE(#{tenantName},tenant_name)>0
      </if>
      <if test="deleted != null">
        AND is_deleted = #{deleted,jdbcType=BIT}
      </if>
      <if test="tenantStatus != null">
        AND tenant_status=#{tenantStatus,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>

  <update id="updateTenantStatus" parameterType="net.northking.cctp.saas.db.entity.SaasTenant">
    update
    <include refid="Table_Name"/>
    set tenant_status = 3
    where #{endTime} >  end_time and tenant_status = 2
    and is_deleted = 0
  </update>

  <select id="findTenantId" resultType="string">
    select id
    from
    <include refid="Table_Name"/>
    where #{endTime} >  end_time and tenant_status = 2
    and is_deleted = 0
  </select>
</mapper>