<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.dao.ibatis.${ClassName}DaoImpl">
    <#if table.tplCategory=="sub" >
        <#assign SubArray><collection property="${subclassName}List" column="${pkColumn.columnName}" ofType="${subClassName}Entity" javaType="ArrayList" select="get${subClassName}By${subTableFkClassName}"></collection></#assign>
    <#elseif table.tplCategory=="subone">
        <#assign SubArray><association property="${subclassName}Entity"  column="${pkColumn.columnName}" select="get${subClassName}By${subTableFkClassName}"></association></#assign>

    <#else>
        <#assign SubArray></#assign>
    </#if>

    <!-- 字段和属性映射 -->
    <resultMap type="${ClassName}Entity" id="${ClassName}Entity-Map">
        <#list columns as column>
        <#if column.isPrimaryKey==1>
            <id  property="${column.javaField}" column="${column.columnName}" />
            <#else >
            <result property="${column.javaField}" column="${column.columnName}" />
        </#if>
        </#list>
      <#--      ${SubArray}-->
    </resultMap>
<#--    <#if table.tplCategory=="sub" ||table.tplCategory=="subone">
    <resultMap type="${subClassName}Entity" id="${subClassName}Entity-Map">
        <#list subTable.columns as column>
            <result property="${column.javaField}" column="${column.columnName}" />
        </#list>
    </resultMap>
    </#if>-->

    <#-- 判断是否分表，如果分表 则不指定表名 -->
    <#if table.dividedTableType==0 >
        <#assign TempTable>${tableName}</#assign>
        <#if table.tplCategory=="sub" ||table.tplCategory=="subone"  >
            <#assign TempSubTable>${subTableName}</#assign>
            <#else >
            <#assign TempSubTable></#assign>
        </#if>
    <#else>
        <#assign TempTable>`<#noparse>$</#noparse>{tableName}`</#assign>
        <#if table.tplCategory=="sub" ||table.tplCategory=="subone" >
            <#assign TempSubTable>`<#noparse>$</#noparse>{subTableName}`</#assign>
        <#else >
            <#assign TempSubTable></#assign>
        </#if>
    </#if>
    <#if table.dividedTableType!=0>
    <update id="createTable" parameterType="paramDto">
        CREATE TABLE ${TempTable}(
        <#list columns as column>
            <#if column.isIncrement==1 >
                <#assign autoInc>AUTO_INCREMENT</#assign>
            <#else>
                <#assign autoInc></#assign>
            </#if>
            <#if column.isRequired==1 >
                <#assign Required>NOT NULL</#assign>
            <#else>
                <#assign Required></#assign>
            </#if>
            <#if column.isPrimaryKey==1 >
                <#assign Required>NOT NULL</#assign>
            </#if>
            <#if column.isRequired==1 >
                <#assign Required>NOT NULL</#assign>
            <#else>
                <#assign Required></#assign>
            </#if>
            <#if column.javaType=="String" >
                <#assign Type>CHARACTER SET utf8 COLLATE utf8_general_ci</#assign>
            <#else>
                <#assign Type></#assign>
            </#if>
            <#if column.defaultValue??>
                <#assign Default>DEFAULT '${column.defaultValue}'</#assign>
            <#else>
                <#assign Default></#assign>
            </#if>
            ${column.isKeyWord(column.columnName)} ${column.columnType} ${Required}  ${autoInc}  COMMENT '${column.columnComment}',
        </#list>
        PRIMARY KEY  (`${pkColumn.columnName}`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='${functionName}';
    </update>
    </#if>

    <!-- 表所有列 -->
    <sql id="_columns">
        <trim suffixOverrides="," suffix="">
            <#list columns as column>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('${column.javaField}') or colPickMode == 1 and data.containsKey('${column.javaField}')))">
                    a.${column.isKeyWord(column.columnName)},
                </if>
            </#list>
        </trim>
    </sql>
    <#if table.tplCategory=="sub" || table.tplCategory=="subone">
    <!-- 子表所有列 -->
    <sql id="_columns_sub">
        <trim suffixOverrides="," suffix="">
            <#list subColumns as column>b.${column.isKeyWord(column.columnName)},</#list>
        </trim>
    </sql>
    </#if>
    <!-- 新增 区分主键自增加还是业务插入 -->
    <insert id="insert" parameterType="${ClassName}Entity" <#if pkColumn.isIncrement?? && pkColumn.isIncrement==1> useGeneratedKeys="true" keyProperty="${pkColumn.javaField}"</#if>>
        insert into ${TempTable}
        (<#list columns as column><#if column.columnName != pkColumn.columnName || pkColumn.isIncrement!=1>${column.isKeyWord(column.columnName)}<#sep>,</#if></#list>)
        VALUES
        (<#list columns as column><#if column.columnName != pkColumn.columnName || pkColumn.isIncrement!=1><#noparse>#</#noparse>{${column.javaField}}<#sep>,</#if></#list>)
    </insert>

    <!-- 批量新增 -->
    <insert id="insertBatch" parameterType="paramDto">
        insert into ${TempTable}
        (<#list columns as column><#if column.columnName != pkColumn.columnName || pkColumn.isIncrement!=1>${column.isKeyWord(column.columnName)}<#sep>,</#if></#list>)
        VALUES
        <foreach collection="data.dataList" item="item" index="index" separator="," >
            (<#list columns as column><#if column.columnName != pkColumn.columnName || pkColumn.isIncrement!=1><#noparse>#</#noparse>{item.${column.javaField}}<#sep>,</#if></#list>)
        </foreach>
    </insert>


    <!-- 根据ParamDto更新 -->
    <update id="update" parameterType="paramDto">
        update ${TempTable} as a
        set
        <trim suffixOverrides="," suffix="">
        <#list columns as column>
        <#-- 非主键更新 -->
        <#if column.columnName != pkColumn.columnName>
            <if test="(colPickMode==0 and data.containsKey('${column.javaField}')) or (colPickMode==1 and !data.containsKey('${column.javaField}'))">
                a.${column.isKeyWord(column.columnName)}=<#noparse>#</#noparse>{data.${column.javaField}},
            </if>
            <#if column.javaType == "Integer"||column.javaType == "Long"||column.javaType == "BigDecimal">
            <if test="(colPickMode==0 and data.containsKey('${column.javaField}Increment')) or (colPickMode==1 and !data.containsKey('${column.javaField}Increment'))">
                a.${column.isKeyWord(column.columnName)}=ifnull(a.${column.isKeyWord(column.columnName)},0) + <#noparse>#</#noparse>{data.${column.javaField}Increment},
            </if>
            </#if>
        </#if>
        </#list>
        </trim>
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
    </update>
    <!-- 批量更新 -->
    <update id="updateBatch" parameterType="paramDto">
        update ${TempTable} as a
        <trim prefix="set" suffixOverrides=",">
            <#list columns as column>
                <#if column.columnName != pkColumn.columnName>
                     <#if column.javaType == "String"||column.javaType == "Date">
                <trim prefix="${column.isKeyWord(column.columnName)}=(case" suffix="ELSE ${column.isKeyWord(column.columnName)} end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('${column.javaField}')) or (colPickMode==1 and !item.containsKey('${column.javaField}'))">
                            when a.${pkColumn.columnName}=<#noparse>#</#noparse>{item.${pkColumn.javaField}} then <#noparse>#</#noparse>{item.${column.javaField}}
                        </if>
                    </foreach>
                </trim>
                     </#if>
                    <#if column.javaType == "Integer"||column.javaType == "Long"||column.javaType == "BigDecimal">
                        <trim prefix="${column.isKeyWord(column.columnName)}=(case" suffix="ELSE ${column.isKeyWord(column.columnName)} end),">
                            <foreach collection="data.dataList" item="item" index="index" separator="" >
                                <choose>
                                    <when test="(colPickMode==0 and item.containsKey('${column.javaField}')) or (colPickMode==1 and !item.containsKey('${column.javaField}'))">
                                        when a.${pkColumn.columnName}=<#noparse>#</#noparse>{item.${pkColumn.javaField}} then <#noparse>#</#noparse>{item.${column.javaField}}
                                    </when>
                                    <when test="(colPickMode==0 and item.containsKey('${column.javaField}Increment')) or (colPickMode==1 and !item.containsKey('${column.javaField}Increment'))">
                                        when a.${pkColumn.columnName}=<#noparse>#</#noparse>{item.${pkColumn.javaField}} then ifnull(a.${column.isKeyWord(column.columnName)},0) + <#noparse>#</#noparse>{item.${column.javaField}Increment}
                                    </when>
                                </choose>
                            </foreach>
                        </trim>
                    </#if>
                </#if>
            </#list>
        </trim>
        where ${pkColumn.columnName} in
        <foreach collection="data.dataList" item="item" index="index" open="(" separator="," close=")">
            <#noparse>#</#noparse>{item.${pkColumn.javaField}}
        </foreach>
    </update>
    <!-- 根据主健查询 -->
    <select id="getByKey" parameterType="paramDto" resultMap="${ClassName}Entity-Map">
        select <include refid="_columns"/>
        from ${TempTable} as a
        where a.${pkColumn.columnName}=<#noparse>#</#noparse>{condition.${pkColumn.javaField}}
    </select>
    <!-- 根据主健删除 -->
    <delete id="deleteByKey" parameterType="paramDto">
        delete a.* from ${TempTable} as a where a.${pkColumn.columnName}=<#noparse>#</#noparse>{condition.${pkColumn.javaField}}
    </delete>
    <!-- 根据主健删除一批，针对单一主健有效 -->
    <delete id="deleteByKeys">
        delete from ${TempTable} where ${pkColumn.columnName} in
        <foreach collection="array" item="item" index="index" open="(" separator="," close=")">
            <#noparse>#</#noparse>{item}
        </foreach>
    </delete>
    <!-- 根据主健列表删除一批，针对单一主健有效 -->
    <delete id="deleteByKeyList">
        delete from ${TempTable} where ${pkColumn.columnName} in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            <#noparse>#</#noparse>{item}
        </foreach>
    </delete>

    <!-- 根据对象列表删除一批，针对单一主健有效 -->
    <delete id="deleteByEntityList">
        delete from ${TempTable} where ${pkColumn.columnName} in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            <#noparse>#</#noparse>{item.${pkColumn.columnName}}
        </foreach>
    </delete>
    <!-- 根据paramDto删除一批 -->
    <delete id="deleteByMap" parameterType="paramDto">
        delete a.* from ${TempTable} as a
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
    </delete>
    <!-- 获取列表 -->
    <select id="getList" parameterType="paramDto" resultMap="${ClassName}Entity-Map">
        select <include refid="_columns"/>
        from ${TempTable} as a
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
        <include refid="_orderCols_"/>
    </select>
    <#if table.tplCategory=="sub" || table.tplCategory=="subone">
    <!-- 获取子列表 -->
        <#if subTableFkType=="Integer" >
            <#assign ParameterType>java.lang.Integer</#assign>
        <#elseif subTableFkType=="Long">
            <#assign ParameterType>java.lang.Long</#assign>
        <#elseif subTableFkType=="String">
            <#assign ParameterType>java.lang.String</#assign>
        </#if>
<#--    <select id="get${subClassName}By${subTableFkClassName}" parameterType="${ParameterType}" resultMap="${subClassName}Entity-Map">
        select <include refid="_columns_sub"/>
        from ${TempSubTable} as b
        <trim suffixOverrides="where" suffix="">
            where b.${subTableFkName} = <#noparse>#</#noparse>{${pkColumn.columnName}}
        </trim>
    </select>-->
    </#if>


    <#if table.tplCategory=="tree" >
    <!-- 获取子节点 -->
    <select id="selectChildren${ClassName}ById" parameterType="String" resultMap="${ClassName}Entity-Map">
        select * from ${TempTable} as a where find_in_set(<#noparse>#</#noparse>{${ClassName?uncap_first}Id}, ancestors)
    </select>
    </#if>

    <!-- 获取 -->
    <select id="getListCount" parameterType="paramDto" resultType="int">
        select count(1)
        from ${TempTable} as a
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
    </select>
    <!-- 条件映射 -->
    <sql id="_condition_">
        <if test="condition != null and !condition.isEmpty()">
            <!-- 条件映射-普通条件 -->
            <include refid="_condition_param_">
                <property name="_conditionParam_" value="condition"/>
                <property name="_conditionType_" value="and"/>
            </include>
            <!-- 条件映射-集合之间使用AND，集合中元素使用OR-(list[0].1 or list[0].2) and (list[1].3 or list[1].4) -->
            <if test="condition.containsKey('andConditionList') and !condition.andConditionList.isEmpty()">
                and
                <foreach collection="condition.andConditionList" open="(" close=")" index="index" item="andCondition" separator=" and ">
                    <trim prefixOverrides="or" prefix="(" suffix=")">
                        <include refid="_condition_param_">
                            <property name="_conditionParam_" value="andCondition"/>
                            <property name="_conditionType_" value="or"/>
                        </include>
                    </trim>
                </foreach>
            </if>
            <!-- 条件映射-集合之间使用OR，集合中元素使用AND-(list[0].1 and list[0].2) or (list[1].3 and list[1].4) -->
            <if test="condition.containsKey('orConditionList') and !condition.orConditionList.isEmpty()">
                and
                <foreach collection="condition.orConditionList" open="(" close=")" index="index" item="orCondition" separator=" or ">
                    <trim prefixOverrides="and" prefix="(" suffix=")">
                        <include refid="_condition_param_">
                            <property name="_conditionParam_" value="orCondition"/>
                            <property name="_conditionType_" value="and"/>
                        </include>
                    </trim>
                </foreach>
            </if>
        </if>
    </sql>
    <!-- 条件映射-代参数 -->
    <sql id="_condition_param_">
        <bind name="conditionParamRef" value="<#noparse>${_conditionParam_}</#noparse>"/>
        <if test="permissionSql != null and permissionSql != ''">
            <#noparse>${permissionSql}</#noparse>
        </if>

        <if test="conditionParamRef.containsKey('id')">
            <if test="conditionParamRef.id != null">
                <#noparse>${_conditionType_}</#noparse> a.${pkColumn.columnName}=<#noparse>#{${_conditionParam_}.id}</#noparse>
            </if>
        </if>
    <#list columns as column>
        <#if column.javaType == "Long"||column.javaType == "Integer"||column.javaType == "BigDecimal">
            <if test="conditionParamRef.containsKey('${column.javaField}')">
                <if test="conditionParamRef.${column.javaField} != null ">
                    <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} = <#noparse>#{${_conditionParam_}</#noparse>.${column.javaField}}
                </if>
                <if test="conditionParamRef.${column.javaField} == null">
                    <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}List') and conditionParamRef.${column.javaField}List.size() > 0">
                <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} in
                <foreach collection="conditionParamRef.${column.javaField}List" open="(" close=")" index="index" item="item" separator=",">
                    <#noparse>#{item}</#noparse>
                </foreach>
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}NotList') and conditionParamRef.${column.javaField}NotList.size() > 0">
                <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} not in
                <foreach collection="conditionParamRef.${column.javaField}NotList" open="(" close=")" index="index" item="item" separator=",">
                    <#noparse>#{item}</#noparse>
                </foreach>
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}Start') and conditionParamRef.${column.javaField}Start != null">
                <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} <![CDATA[ >= ]]> <#noparse>#{${_conditionParam_}</#noparse>.${column.javaField}Start}
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}End') and conditionParamRef.${column.javaField}End != null">
                <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} <![CDATA[ <= ]]> <#noparse>#{${_conditionParam_}</#noparse>.${column.javaField}End}
            </if>
        </#if>

        <#if column.javaType == "String">
            <if test="conditionParamRef.containsKey('${column.javaField}')">
                <if test="conditionParamRef.${column.javaField} != null and conditionParamRef.${column.javaField} != ''">
                    <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} like <#noparse>#{${_conditionParam_}</#noparse>.${column.javaField}}
                </if>
                <if test="conditionParamRef.${column.javaField} == null">
                    <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}List') and conditionParamRef.${column.javaField}List.size() > 0">
                <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} in
                <foreach collection="conditionParamRef.${column.javaField}List" open="(" close=")" index="index" item="item" separator=",">
                    <#noparse>#</#noparse>{item}
                </foreach>
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}NotList') and conditionParamRef.${column.javaField}NotList.size() > 0">
                <#noparse>${_conditionType_}</#noparse> a.${column.isKeyWord(column.columnName)} not in
                <foreach collection="conditionParamRef.${column.javaField}NotList" open="(" close=")" index="index" item="item" separator=",">
                    <#noparse>#</#noparse>{item}
                </foreach>
            </if>
        </#if>
        <#if column.javaType == "Date">
            <if test="conditionParamRef.containsKey('${column.javaField}')">
                <if test="conditionParamRef.${column.javaField} != null ">
                    <#noparse>$</#noparse>{_conditionType_} a.${column.isKeyWord(column.columnName)} = <#noparse>#{$</#noparse>{_conditionParam_}.${column.javaField}}
                </if>
                <if test="conditionParamRef.${column.javaField} == null">
                    <#noparse>$</#noparse>{_conditionType_} a.${column.isKeyWord(column.columnName)} is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}Start') and conditionParamRef.${column.javaField}Start != null and conditionParamRef.${column.javaField}Start!=''">
                <#noparse>$</#noparse>{_conditionType_} a.${column.isKeyWord(column.columnName)} <![CDATA[ >= ]]> STR_TO_DATE(left(concat(<#noparse>#{$</#noparse>{_conditionParam_}.${column.javaField}Start},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s')
            </if>
            <if test="conditionParamRef.containsKey('${column.javaField}End') and conditionParamRef.${column.javaField}End != null and conditionParamRef.${column.javaField}End!=''">
                <#noparse>$</#noparse>{_conditionType_} a.${column.isKeyWord(column.columnName)} <![CDATA[ <= ]]> STR_TO_DATE(left(concat(<#noparse>#{$</#noparse>{_conditionParam_}.${column.javaField}End},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s')
            </if>
        </#if>
    </#list>
    </sql>
    <sql id="_orderCols_">
        <if test="orderColList != null and !orderColList.isEmpty()">
            order by
            <#list columns as column>
                <#if column.javaType == "Long"||column.javaType == "Integer"||column.javaType == "BigDecimal">
                    <if test="conditionParamRef.containsKey('${column.javaField}List') and conditionParamRef.${column.javaField}List.size() > 0">
                        field(a.${column.javaField},
                        <foreach collection="conditionParamRef.${column.javaField}List" open="" close=")" index="index" item="item" separator=",">
                            <#noparse>#{item}</#noparse>
                        </foreach>
                        ,
                    </if>
                </#if>
            </#list>
            <trim suffixOverrides=","  suffix="">
                <foreach collection="orderColList" open="" close="" index="index" item="item" separator=",">
                    <#noparse> a.${item.colName} ${item.sortKind}</#noparse>
                </foreach>
            </trim>
        </if>
        <if test="(orderColList == null or orderColList.isEmpty()) and orderCol != null and !orderCol.isEmpty()">
            order by
            <#list columns as column>
                <#if column.javaType == "Long"||column.javaType == "Integer"||column.javaType == "BigDecimal">
                    <if test="conditionParamRef.containsKey('${column.javaField}List') and conditionParamRef.${column.javaField}List.size() > 0">
                        field(a.${column.javaField},
                        <foreach collection="conditionParamRef.${column.javaField}List" open="" close=")" index="index" item="item" separator=",">
                            <#noparse>#{item}</#noparse>
                        </foreach>
                        ,
                    </if>
                </#if>
            </#list>
            <trim suffixOverrides=","  suffix="">
                <#list columns as column>
                    <if test="orderCol.containsKey('${column.javaField}')">
                        a.${column.isKeyWord(column.columnName)}
                        <if test='orderCol.${column.javaField} != null and "DESC".equalsIgnoreCase(orderCol.${column.javaField})'>DESC</if>
                        ,
                    </if>
                </#list>
            </trim>
        </if>


    </sql>
    <sql id="_group_by_">
        <if test="groupList != null and !groupList.isEmpty()">
            GROUP BY
            <trim suffixOverrides=","  suffix="">
                <foreach collection="groupList" open="" close="" index="index" item="item" separator=",">
                    <#noparse>$</#noparse>{item}
                </foreach>
            </trim>
        </if>
    </sql>
</mapper>