<?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">
<!--  Copyright (c) ${Company} Corporation ${Year} . All rights reserved. -->
<!--  Mybatis数据库持久层（底层映射文件）：${javaClassRef.getComment()} -->
<!--  version: ${Version}  -->
<!--  author:  ${Author}    -->
<!--  date:  ${now}  -->
<mapper namespace="${daoPackage}.${javaClassRef.getClassName()}Dao">
  <resultMap id="BaseResultMap" type="${entityPackage}.${javaClassRef.getClassName()}">
#foreach( ${column} in ${tableRef.getColumns()} )
  #if( ${column.isPrimaryKey()})
    <!-- ${column.getComment()} -->
    <id column="${column.getName()}" jdbcType="${column.getAttributesRef().getJdbcTypeName()}" property="${column.getAttributesRef().getRefName()}"/>
  #end
#end
#foreach( ${column} in ${tableRef.getColumns()} )
  #if( ${column.isPrimaryKey()})
  #elseif( ${column.getAttributesRef().getJdbcTypeName()} == "BLOB" )
    <!-- ${column.getComment()} -->
    <result column="${column.getName()}" jdbcType="${column.getAttributesRef().getJdbcTypeName()}" property="${column.getAttributesRef().getRefName()}"  typeHandler="org.apache.ibatis.type.BlobInputStreamTypeHandler"/>
  #else
    <!-- ${column.getComment()} -->
    <result column="${column.getName()}" jdbcType="${column.getAttributesRef().getJdbcTypeName()}" property="${column.getAttributesRef().getRefName()}"/>
  #end
#end
  </resultMap>

#set($primaryKey=${tableRef.getPrimaryKey().getColumnRef()})
  <sql id="Base_Column_List">
#foreach( ${column} in ${tableRef.getColumns()} )
#if(${velocityCount} == 1)
    ${column.getName()}
#else
    ,${column.getName()}
#end
#end
  </sql>

  <sql id="Table_Name">
    ${DEFAULT_SCHEMA}${SCHEMA_SUFFIX}.${tableRef.getTableName().toLowerCase()}${TABLE_SUFFIX}
  </sql>

  <select id="findByPrimaryKey" parameterType="${primaryKey.getAttributesRef().getJavaTypeName()}" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where ${primaryKey.getName()} = #{${primaryKey.getAttributesRef().getRefName()},jdbcType=${primaryKey.getAttributesRef().getJdbcTypeName()}}
  </select>

  <select id="findByPrimaryKeys" parameterType="java.util.List">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where ${primaryKey.getName()} in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=${primaryKey.getAttributesRef().getJdbcTypeName()}}
    </foreach>
  </select>

  <delete id="deleteByPrimaryKey" parameterType="${primaryKey.getAttributesRef().getJavaTypeName()}">
    delete from
    <include refid="Table_Name"/>
    where ${primaryKey.getName()} = #{${primaryKey.getAttributesRef().getRefName()},jdbcType=${primaryKey.getAttributesRef().getJdbcTypeName()}}
  </delete>

  <delete id="deleteByPrimaryKeys" parameterType="${primaryKey.getAttributesRef().getJavaTypeName()}">
    delete from
    <include refid="Table_Name"/>
    where ${primaryKey.getName()} in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=${primaryKey.getAttributesRef().getJdbcTypeName()}}
    </foreach>
  </delete>

  <delete id="deleteByExample" parameterType="${entityPackage}.${javaClassRef.getClassName()}">
    delete from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
#foreach( ${column} in ${tableRef.getColumns()} )
      <if test="${column.getAttributesRef().getRefName()} != null">
          AND ${column.getName()}=#{${column.getAttributesRef().getRefName()},jdbcType=${column.getAttributesRef().getJdbcTypeName()}}
      </if>
#end
    </trim>
  </delete>

  <insert id="insert" parameterType="${entityPackage}.${javaClassRef.getClassName()}">
    insert into
    <include refid="Table_Name"/>
    <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach( ${column} in ${tableRef.getColumns()} )
      <if test="${column.getAttributesRef().getRefName()} != null">
        ${column.getName()},
      </if>
#end
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
#foreach( ${column} in ${tableRef.getColumns()} )
      <if test="${column.getAttributesRef().getRefName()} != null">
          #{${column.getAttributesRef().getRefName()}, jdbcType=${column.getAttributesRef().getJdbcTypeName()}},
      </if>
#end
    </trim>
  </insert>

  <insert id="insertByBatch" parameterType="java.util.List">
    insert into
    <include refid="Table_Name"/>
    <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach( ${column} in ${tableRef.getColumns()} )
    ${column.getName()},
#end
    </trim>
    values
    <foreach collection="list" item="item" index="index" separator=",">
      <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach( ${column} in ${tableRef.getColumns()} )
    #{item.${column.getAttributesRef().getRefName()}, jdbcType=${column.getAttributesRef().getJdbcTypeName()}},
#end
      </trim>
    </foreach>
  </insert>

  <update id="updateByPrimaryKey" parameterType="${entityPackage}.${javaClassRef.getClassName()}">
    update
    <include refid="Table_Name"/>
    <set>
#foreach( ${column} in ${tableRef.getColumns()} )
#if(!${column.isPrimaryKey()} && ${column.getName()}!="tenant_id")
      <if test="${column.getAttributesRef().getRefName()} != null">
        ${column.getName()} = #{${column.getAttributesRef().getRefName()}, jdbcType=${column.getAttributesRef().getJdbcTypeName()}},
      </if>
#end
#end
    </set>
    where ${primaryKey.getName()} = #{${primaryKey.getAttributesRef().getRefName()},jdbcType=${primaryKey.getAttributesRef().getJdbcTypeName()}}
    AND tenant_id = #{tenantId,jdbcType=VARCHAR}
  </update>

  <select id="query" resultMap="BaseResultMap" parameterType="${entityPackage}.${javaClassRef.getClassName()}">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
#foreach( ${column} in ${tableRef.getColumns()} )
      <if test="${column.getAttributesRef().getRefName()} != null">
        AND ${column.getName()} = #{${column.getAttributesRef().getRefName()},jdbcType=${column.getAttributesRef().getJdbcTypeName()}}
      </if>
#end
    </trim>
  </select>

  <select id="queryCount" resultType="java.lang.Long" parameterType="${entityPackage}.${javaClassRef.getClassName()}">
    select count(*) from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
#foreach( ${column} in ${tableRef.getColumns()} )
      <if test="${column.getAttributesRef().getRefName()} != null">
        AND ${column.getName()}=#{${column.getAttributesRef().getRefName()},jdbcType=${column.getAttributesRef().getJdbcTypeName()}}
      </if>
#end
    </trim>
  </select>

</mapper>