<?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="{{packageName}}.core.{{entity.module}}.mapper.{{entity.codeName}}Mapper">

        <!--通过mybatis将查询结果注入到entity中,通过配置autoMapping="true"由mybatis自动处理映射关系 -->
    <resultMap id="{{entity.codeName}}ResultMap" type="{{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}}" autoMapping="true">
		{{#entity.keyField.phisicalDEField}}
		<id property="{{camelCase entity.keyField.codeName}}" column="{{entity.keyField.columnName}}" /><!--主键字段映射-->
		{{/entity.keyField.phisicalDEField}}
		{{#each entity.fields}}
            {{#alias}}
                {{#unless deepStructure}}
                    {{#unless keyDEField}}
		<result property="{{alias}}" column="{{columnName}}" />
                    {{/unless}}
                {{/unless}}
            {{/alias}}
        {{/each}}
        {{#each entity.nesteds}}
            {{#columnName}}
        <result property="{{camelCase codeName}}" column="{{columnName}}" typeHandler="{{packageName}}.core.{{module}}.domain.handlers.{{entityCodeName}}TypeHandler" />
            {{/columnName}}
        {{/each}}
        {{#each entity.references}}
        <association property="{{camelCase codeName}}" javaType="{{packageName}}.core.{{module}}.domain.{{entityCodeName}}" column="{{fkField.columnName}}" select="{{packageName}}.core.{{module}}.mapper.{{entityCodeName}}Mapper.selectById" fetchType="lazy"></association>
        {{/each}}
    </resultMap>

    <select id="selectById"  resultMap="{{entity.codeName}}ResultMap">
    {{#if entity.viewDataQuery}}
        {{#if entity.keyField.phisicalDEField}}
        <include refid="view" /> 
            {{#if entity.viewDataQuery.where}}
         and 
            {{else}}
         where 
            {{/if}}
        {{else}}
        select t1.* from ( <include refid="view" /> ) t1 
         where 
        {{/if}}
    {{else}}
        select t1.* from {{entity.viewName}} t1 
         where 
        {{#if entity.logicValid}}
            {{#if entity.logicValidField.type.number}}
         {{entity.logicValidField.columnName}} = {{entity.validLogicValue}} 
            {{else}}
         {{entity.logicValidField.columnName}} = '{{entity.validLogicValue}}' 
            {{/if}}
         and   
        {{/if}}
    {{/if}}
         t1.{{entity.keyField.columnName}}=#{id}
    </select>

    <select id="selectEntity" parameterType="{{packageName}}.core.{{entity.module}}.domain.{{entity.codeName}}" resultMap="{{entity.codeName}}ResultMap">
    {{#if entity.viewDataQuery}}
        <include refid="view" /> 
            {{#if entity.viewDataQuery.where}}
         and 
            {{else}}
         where 
            {{/if}}
    {{else}}
        select t1.* from {{entity.viewName}} t1 
         where 
        {{#if entity.logicValid}}
            {{#if entity.logicValidField.type.number}}
         {{entity.logicValidField.columnName}} = {{entity.validLogicValue}} 
            {{else}}
         {{entity.logicValidField.columnName}} = '{{entity.validLogicValue}}' 
            {{/if}}
         and   
        {{/if}}
    {{/if}}
         ({{#each entity.keyFields as |field|}}{{#unless @first}} and {{/unless}}{{field.columnName}} = #{ {{camelCase field.codeName}} }{{/each}})
    </select>

    <select id="selectEntities" parameterType="java.util.List" resultMap="{{entity.codeName}}ResultMap">
    {{#if entity.viewDataQuery}}
        <include refid="view" /> 
            {{#if entity.viewDataQuery.where}}
         and 
            {{else}}
         where 
            {{/if}}
    {{else}}
        select t1.* from {{entity.viewName}} t1 
         where 
        {{#if entity.logicValid}}
            {{#if entity.logicValidField.type.number}}
         {{entity.logicValidField.columnName}} = {{entity.validLogicValue}} 
            {{else}}
         {{entity.logicValidField.columnName}} = '{{entity.validLogicValue}}' 
            {{/if}}
         and   
        {{/if}}
    {{/if}}
         ({{#each entity.keyFields as |field|}}{{#unless @first}},{{/unless}}{{field.columnName}}{{/each}})
         in (<foreach collection="list" item="item" index="index" separator=","> ({{#each entity.keyFields as |field|}}{{#unless @first}},{{/unless}}#{ item.{{camelCase field.codeName}} }{{/each}}) </foreach>)
    </select>

    {{#each entity.dataQueries}}
    <sql id="{{camelCase codeName}}" databaseId="{{dsType}}">
        <![CDATA[ 
            {{dsCode}}
        ]]>    
    </sql>

    {{/each}}
    {{#each entity.dataSets}}
    <select id="search{{pascalCase codeName}}"  parameterType="{{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext"  {{#if enableGroup}}resultType="java.util.HashMap"{{else}}resultMap="{{entity.codeName}}ResultMap"{{/if}}>
        {{select}} 
        from (
            {{#each queries}}
            <include refid="{{camelCase this}}"/>
                {{#unless @last}}
                 union all 
                {{/unless}}
            {{/each}}
        ) t1 
        <where><if test="ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere">${ew.sqlSegment}</if></where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">${ew.sqlSegment}</if>
        {{groupBy}}
        {{orderBy}}    
    </select>
    
    {{/each}}
    {{#each entity.references as | reference |}}
    <select id="selectBy{{pascalCase reference.fkField.codeName}}" resultMap="{{entity.codeName}}ResultMap">
        {{#if entity.defaultDataQuery}}
        <include refid="{{camelCase entity.defaultDataQuery.codeName}}" />
            {{#if entity.defaultDataQuery.where}}
         and 
            {{else}}
         where 
            {{/if}}
        {{else}}
        select t1.* from {{#if entity.viewName}}{{entity.viewName}}{{else}}{{entity.tableName}}{{/if}} t1 where 
        {{/if}}
        {{reference.fkField.columnName}} = #{ {{camelCase reference.fkField.codeName}} }
    </select>

    {{/each}}

</mapper>