<?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="${basePackage}.dao.${lowerTableName}.I${firstUpperTableName}Dao">

    <resultMap id="${firstLowerTableName}DTO" type="${basePackage}.pojo.dtos.${lowerTableName}.${firstUpperTableName}DTO">
        <id column="${underLineTableName}_id" property="${firstLowerTableName}Id"/>
        <#list fieldList! as field>
        <result column="${field.underLineFieldName}" property="${field.fieldName}"/>
            <#-- 字典 start -->
            <#if field.fieldType == "radio" || field.fieldType == "checkbox" || field.fieldType == "select">
        <result column="${field.underLineFieldName}_dictionary_name" property="${field.fieldName}DictionaryName"/>
            </#if>
            <#-- 字典 end -->
            <#-- 联表 start -->
            <#if field.fieldType == "innerJoin" || field.fieldType == "leftJoin" || field.fieldType == "rightJoin">
                <#list field.listJoinTableField! as joinTableField>
                    <#if joinTableField.listShow == 1>
        <result column="${field.underLineJoinTable}_${joinTableField.underLineFieldName}" property="${field.joinTable}${joinTableField.firstUpperFieldName}"/>
                    </#if>
                </#list>
            </#if>
            <#-- 联表 end -->
        </#list>
    </resultMap>

    <!-- 新增${tableExplain} -->
    <insert id="save${firstUpperTableName}" parameterType="map">
        INSERT INTO ${tablePrefix}${underLineTableName}(
            ${underLineTableName}_id,
            <#list fieldList! as field>
            ${field.underLineFieldName},
            </#list>
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            ${r"#{"}${firstLowerTableName}Id${r"}"},
            <#list fieldList! as field>
            ${r"#{"}${field.fieldName}${r"}"},
            </#list>
            ${r"#{creator}"},
            ${r"#{gmtCreate}"},
            ${r"#{modifier}"},
            ${r"#{gmtModified}"},
            ${r"#{isDelete}"}
        )
    </insert>

    <!-- 删除${tableExplain} -->
    <update id="remove${firstUpperTableName}" parameterType="map">
        UPDATE
            ${tablePrefix}${underLineTableName}
        SET
            is_delete = 1,
            modifier = ${r"#{modifier}"},
            gmt_modified = ${r"#{gmtModified}"}
        WHERE
            ${underLineTableName}_id IN
        <foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
            ${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
        </foreach>
    </update>

    <!-- 删除${tableExplain}（物理） -->
    <update id="delete${firstUpperTableName}" parameterType="map">
        DELETE FROM
            ${tablePrefix}${underLineTableName}
        WHERE
            ${underLineTableName}_id IN
        <foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
            ${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
        </foreach>
    </update>

    <!-- 修改${tableExplain} -->
    <update id="update${firstUpperTableName}" parameterType="map">
        UPDATE
            ${tablePrefix}${underLineTableName}
        SET
        <#list fieldList! as field>
            <#if field.fieldType == "number" || field.fieldType == "double">
        <if test="${field.fieldName} != null">
            ${field.underLineFieldName} = ${r"#{"}${field.fieldName}${r"}"},
        </if>
            <#else>
        <if test="${field.fieldName} != null and ${field.fieldName} != ''">
            ${field.underLineFieldName} = ${r"#{"}${field.fieldName}${r"}"},
        </if>
            </#if>
        </#list>
            modifier = ${r"#{modifier}"},
            gmt_modified = ${r"#{gmtModified}"}
        WHERE
            ${underLineTableName}_id = ${r"#{"}${firstLowerTableName}${r"Id}"}
    </update>

    <!-- ${tableExplain}详情 -->
    <select id="get${firstUpperTableName}" parameterType="map" resultMap="${firstLowerTableName}DTO">
        SELECT
            <#list fieldList! as field>
                <#if field.formShow == 1>
            t1.${field.underLineFieldName},
                </#if>
            </#list>
            t1.${underLineTableName}_id
        FROM
            ${tablePrefix}${underLineTableName} t1
        WHERE
            t1.is_delete = 0
        <if test="${firstLowerTableName}Id != null and ${firstLowerTableName}Id != ''">
        AND
            t1.${underLineTableName}_id = ${r"#{"}${firstLowerTableName}${r"Id}"}
        </if>
    </select>

    <!-- ${tableExplain}列表 -->
    <select id="list${firstUpperTableName}" parameterType="map" resultMap="${firstLowerTableName}DTO">
        SELECT
            <#list fieldList! as field>
                <#if field.listShow == 1>
            t1.${field.underLineFieldName},
                    <#-- 字典 start -->
                    <#if field.fieldType == "radio" || field.fieldType == "select">
            dt${field.dictionaryIndex}.dictionary_name ${field.underLineFieldName}_dictionary_name,
                    </#if>
                    <#if field.fieldType == "checkbox">
            GROUP_CONCAT(DISTINCT dt${field.dictionaryIndex}.dictionary_name) ${field.underLineFieldName}_dictionary_name,
                    </#if>
                    <#-- 字典 end -->
                </#if>
                <#-- 联表 start -->
                <#if field.fieldType == "innerJoin" || field.fieldType == "leftJoin" || field.fieldType == "rightJoin">
                    <#list field.listJoinTableField! as joinTableField>
                        <#if joinTableField.listShow == 1>
            jt${field.joinTableIndex}.${joinTableField.underLineFieldName} ${field.underLineJoinTable}_${joinTableField.underLineFieldName},
                        </#if>
                    </#list>
                </#if>
                <#-- 联表 end -->
            </#list>
            t1.${underLineTableName}_id
        FROM
            ${tablePrefix}${underLineTableName} t1
        <#list fieldList! as field>
            <#if field.listShow == 1>
                <#if field.fieldType == "radio" || field.fieldType == "select">
        LEFT JOIN
            data_dictionary dt${field.dictionaryIndex}
        ON
            dt${field.dictionaryIndex}.dictionary_id = t1.${field.underLineFieldName}
        AND
            dt${field.dictionaryIndex}.is_delete = 0
                <#elseif field.fieldType == "checkbox">
        LEFT JOIN
            data_dictionary dt${field.dictionaryIndex}
        ON
            dt${field.dictionaryIndex}.dictionary_id = t1.${field.underLineFieldName}
        AND
            dt${field.dictionaryIndex}.is_delete = 0
                </#if>
            </#if>
            <#-- 联表 start -->
            <#if field.fieldType == "innerJoin" || field.fieldType == "leftJoin" || field.fieldType == "rightJoin">
                <#if field.fieldType == "innerJoin">
        INNER JOIN
                <#elseif field.fieldType == "leftJoin">
        LEFT JOIN
                <#elseif field.fieldType == "rightJoin">
        RIGHT JOIN
                </#if>
            ${tablePrefix}${field.underLineJoinTable} jt${field.joinTableIndex}
        ON
            t1.${field.underLineFieldName} = jt${field.joinTableIndex}.${field.underLineJoinTableField}
        AND
            jt${field.joinTableIndex}.is_delete = 0
            </#if>
            <#-- 联表 end -->
        </#list>
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.id LIKE CONCAT('%', ${r"#{keywords}"}, '%')
            OR
            t1.${underLineTableName}_id LIKE CONCAT('%', ${r"#{keywords}"}, '%')
            <!-- 这里添加其他条件 -->
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> ${r"#{startTime}"}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> ${r"#{endTime}"}
        </if>
        <if test="${firstLowerTableName}Ids != null and ${firstLowerTableName}Ids.size > 0">
        AND
            t1.${underLineTableName}_id IN
            <foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
            ${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
            </foreach>
        </if>
        <#if isCheckboxExist?? && isCheckboxExist == "exist">
        GROUP BY
            <#list fieldList! as field>
                <#if field.listShow == 1>
            t1.${field.underLineFieldName},
                    <#if field.fieldType == "radio" || field.fieldType == "select">
            dt${field.dictionaryIndex}.dictionary_name,
                    </#if>
                </#if>
            <#-- 联表 start -->
                <#if field.fieldType == "innerJoin" || field.fieldType == "leftJoin" || field.fieldType == "rightJoin">
                    <#list field.listJoinTableField! as joinTableField>
                        <#if joinTableField.listShow == 1>
            jt${field.joinTableIndex}.${joinTableField.underLineFieldName},
                        </#if>
                    </#list>
                </#if>
            <#-- 联表 end -->
            </#list>
            t1.${underLineTableName}_id
        </#if>
    </select>

    <!-- ${tableExplain}统计  -->
    <select id="count${firstUpperTableName}" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            ${tablePrefix}${underLineTableName} t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>