<?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="com.${bean.pacageName}.dao.${bean.classOracleName}Dao">
	<!-- Result Map -->
	<resultMap id="BaseResultMap" type="com.${bean.pacageName}.bean.${bean.classOracleName}">
	    <#list bean.fields as field>
	    <result column="${field.fieldName}" property="${field.fieldJavaName}"/>
		</#list>
	</resultMap>

	<!-- ${bean.tableName} table all fields -->
	<sql id="Base_Column_List">
		<#list bean.fields as field><#if field_index==0>${field.fieldName}</#if><#if field_index!=0>,${field.fieldName}</#if></#list>
	</sql>

	<!-- 公共查询条件 -->
	<sql id="Example_Where_Clause">
		where 1=1
		<#list bean.fields as field>
		<if test="bean.${field.fieldJavaName}!=null <#if field.fieldType=="VARCHAR2">and bean.${field.fieldJavaName}!=''</#if>">and ${field.fieldName} = _#_{bean.${field.fieldJavaName}, <#if field.fieldType=="VARCHAR2">jdbcType=VARCHAR</#if><#if field.fieldType=="NUMBER">jdbcType=DOUBLE</#if><#if field.fieldType!="VARCHAR2" && field.fieldType!="NUMBER">jdbcType=${field.fieldType}</#if>}</if>
		</#list>
	</sql>

	<!-- 添加记录（所有字段） -->
	<insert id="save" parameterType="Object"  >
	    <selectKey keyProperty="bean.id" resultType="String" order="BEFORE" >
			select lower(sys_guid()) from dual
		</selectKey>
		insert into ${bean.tableName}
		(
		<#list bean.fields as field><#if field_index==0>${field.fieldName}</#if><#if field_index!=0>,${field.fieldName}</#if></#list>
		)
		values 
		(
		<#list bean.fields as field>
		<#if field_index==0>_#_{bean.${field.fieldJavaName}}</#if><#if field_index!=0>,_#_{bean.${field.fieldJavaName},<#if field.fieldType=="VARCHAR2">jdbcType=VARCHAR</#if><#if field.fieldType=="NUMBER">jdbcType=DOUBLE</#if><#if field.fieldType!="VARCHAR2" && field.fieldType!="NUMBER" >jdbcType=${field.fieldType}</#if>}
		</#if>
		</#list>
		)
	</insert>

	<!-- 修改记录，只修改只不为空的字段 -->
	<update id="update" parameterType="Object">
	
		update ${bean.tableName}
		<set>
			<#list bean.fields as field>
			<if test="bean.${field.fieldJavaName} != null <#if field.fieldType=="VARCHAR2">and bean.${field.fieldJavaName}!=''</#if>" >${field.fieldName} = _#_{bean.${field.fieldJavaName}, <#if field.fieldType=="VARCHAR2">jdbcType=VARCHAR</#if><#if field.fieldType=="NUMBER">jdbcType=DOUBLE</#if><#if field.fieldType!="VARCHAR2" && field.fieldType!="NUMBER" >jdbcType=${field.fieldType}</#if>} </if>
			</#list>
		</set>
		where id = _#_{id}

	</update>

	<!-- 删除记录 -->
	<delete id="delete" parameterType="Object">
		delete from ${bean.tableName} where id=_#_{id}
	</delete>
	
	<update id="deleteFlagById" parameterType="Object" >
		update ${bean.tableName} set del_tag = 1 where id=_#_{id}
	</update>

	<!-- 根据id查询 -->
	<select id="getById" resultMap="BaseResultMap" parameterType="Object">
		select <include refid="Base_Column_List" /> from ${bean.tableName} where del_tag=0 and id=_#_{id}
	</select>

	<!-- 列表总数 -->
	<select id="queryByCount" resultType="java.lang.Integer" parameterType="Object">
		select count(1) from ${bean.tableName} <include refid="Example_Where_Clause" />
	</select>
	<!-- 查询列表 -->
	<select id="getList" resultMap="BaseResultMap" parameterType="Object">
		select <include refid="Base_Column_List" /> from ${bean.tableName} <include refid="Example_Where_Clause" />
	</select>
</mapper>


