<?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="${package.Mapper}.${table.mapperName}">

    <!-- 通用设置 -->
<#if baseColumnList>
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        <#list table.commonFields as field>
            ${field.name},
        </#list>
        ${table.fieldNames}
    </sql>

    <#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
    <#list table.fields as field>
        <#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}"/>
        </#if>
    </#list>
    <#list table.commonFields as field><#--生成公共字段 -->
    <result column="${field.name}" property="${field.propertyName}"/>
    </#list>
    <#list table.fields as field>
        <#if !field.keyFlag><#--生成普通字段 -->
        <#if field.propertyType =="Date">
            <result column="${field.name}" property="${field.propertyName}" typeHandler="TimestampTypeHandler"/>
        </#if>
        <#if field.propertyType !="Date">
            <#if field.type =="bigint[]">
                <result column="${field.name}" property="${field.propertyName}" typeHandler="BigIntListTypeHandler"/>
            <#elseif field.type =="int[]" || field.type == "integer[]">
                <result column="${field.name}" property="${field.propertyName}" typeHandler="IntListTypeHandler"/>
            <#elseif field.type =="uuid">
                <result column="${field.name}" property="${field.propertyName}" typeHandler="UuidTypeHandler"/>
            <#elseif field.type =="uuid[]">
                <result column="${field.name}" property="${field.propertyName}" typeHandler="UuidListTypeHandler"/>
            <#elseif field.type =="jsonb">
                <result column="${field.name}" property="${field.propertyName}" typeHandler="JsonTypeHandler"/>
            <#else >
                <result column="${field.name}" property="${field.propertyName}"/>
            </#if>
        </#if>
        </#if>
    </#list>
    </resultMap>
    </#if>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
        <#list table.commonFields as field><#--生成公共字段-->
            <#if field.propertyName == "flgDel">
                <if test="flgDel != null">
                    AND ${field.name} = ${r"#{"}${field.propertyName}${r"}"}
                </if>
                <if test="flgDel == null">
                    AND ${field.name} = false
                </if>
            <#else>
                <if test="${field.propertyName} != null">
                    AND ${field.name} = ${r"#{"}${field.propertyName}${r"}"}
                </if>
            </#if>
    </#list>
    <#list table.fields as field>
    <#--生成普通字段 -->
        <#if !field.keyFlag>
            <#if field.columnType=="STRING">
            <if test="${field.propertyName} != null and ${field.propertyName} != ''">
                AND ${field.name} = ${r"#{"}${field.propertyName}${r"}"}
            </if>
            <#elseif field.type =="uuid">
            <if test="${field.propertyName} != null">
                AND ${field.name} = ${r"#{"}${field.propertyName}${r"}"}::uuid
            </if>
            <#else >
            <if test="${field.propertyName} != null">
                AND ${field.name} = ${r"#{"}${field.propertyName}${r"}"}
            </if>
            </#if>
        </#if>
    </#list>
        </where>
    </sql>
</#if>

    <#list table.fields as field>
        <#if field.keyFlag>
            <#list table.fields as field>
                <#if field.keyFlag>
    <sql id="idsForeach">
        <!-- 根据主键${field.propertyName}批量操作 -->
        WHERE ${field.name} in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            ${"#"}{item}
        </foreach>
    </sql>
                </#if>
            </#list>
        </#if>
    </#list>

    <!-- 查询表${table.name}，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.name}
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit ${"#"}{end} offset ${"#"}{start}
        </if>
    </select>

    <!-- 查询表${table.name}，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM ${table.name}
        <include refid="Condition"/>
    </select>

    <#list table.fields as field>
        <#if field.keyFlag && field.type == "uuid">
    <!-- 根据主键查询表${table.name}的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.name}
        WHERE ${field.name} = ${r"#{"}${field.propertyName}${r"}"}::uuid
    </select>
        </#if>
    </#list>

    <#list table.fields as field>
        <#if field.keyFlag>
    <!-- 根据主键锁定表${table.name}的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.name}
        WHERE ${field.name} = ${r"#{"}${field.propertyName}${r"}"}
        for update
    </select>
        </#if>
    </#list>

    <#list table.fields as field>
        <#if field.keyFlag>
    <!-- 根据主键锁定表${table.name}的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.name}
        <include refid="idsForeach"/>
        for update
    </select>
        </#if>
    </#list>

    <insert id="insertBatch">
        insert into ${table.name}
        (
        <trim suffixOverrides=",">
        <#list table.fields as field>
            <#if !field.keyFlag && field.name != "flg_valid" && field.name != "flg_del" &&
            field.name != "op_create_time" && field.name != "op_create_user_id" &&
            field.name != "op_update_time" && field.name != "op_update_user_id" &&
            field.name != "op_app_id" && field.name != "op_api_id" && field.name != "op_timestamp" && field.name != "op_db_user">
            ${field.name},
            </#if>
        </#list>
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
            <#list table.fields as field>
                <#if !field.keyFlag && field.name != "flg_valid" && field.name != "flg_del" &&
                field.name != "op_create_time" && field.name != "op_create_user_id" &&
                field.name != "op_update_time" && field.name != "op_update_user_id" &&
                field.name != "op_app_id" && field.name != "op_api_id" && field.name != "op_timestamp" && field.name != "op_db_user">
                    <#if field.type =="uuid">
                ${r"#{item."}${field.propertyName}${r"}"}::uuid,
                    </#if>
                    <#if field.type !="uuid">
                ${r"#{item."}${field.propertyName}${r"}"},
                    </#if>
                </#if>
            </#list>
            </trim>
            )
        </foreach>
    </insert>
</mapper>
