<?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.jeeplus.modules.sysmodular.dao.SysModularDao">
    
	<sql id="sysModularColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.name AS "name",
		a.sql_string,
		a.table_name AS "tableName",
		a.range_field AS "rangeField",
		a.sort as "sort"
	</sql>
	
	<sql id="sysModularJoins">
	</sql>
	
    
	<select id="get" resultType="SysModular" >
		SELECT 
			<include refid="sysModularColumns"/>
		FROM sys_modular a
		<include refid="sysModularJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="SysModular" >
		SELECT 
			<include refid="sysModularColumns"/>
		FROM sys_modular a
		<include refid="sysModularJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="tableName != null and tableName != ''">
				AND a.table_name = #{tableName}
			</if>
			<if test="rangeField != null and rangeField != ''">
				AND a.range_field = #{rangeField}
			</if>
		</where>
		ORDER BY a.sort ASC

	</select>
	
	<select id="findAllList" resultType="SysModular" >
		SELECT 
			<include refid="sysModularColumns"/>
		FROM sys_modular a
		<include refid="sysModularJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO sys_modular(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			name,
			table_name,
			range_field,
			sort,
			sql_string
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{name},
			#{tableName},
			#{rangeField},
			#{sort},
			#{sqlString}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_modular SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			table_name = #{tableName},
			range_field = #{rangeField},
			sort=#{sort},
			sql_string=#{sqlString}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_modular
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_modular SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SysModular" statementType="STATEMENT">
		select * FROM sys_modular  where ${propertyName} = '${value}'
	</select>
	<select id="findTable" resultType="SysModular" parameterType="java.lang.String">
		SELECT
		table_name AS "tableName"
		FROM
		information_schema. TABLES
		<where>
			table_schema = #{dbName}
		</where>
	</select>
	<select id="findByTableName" resultType="SysModular">
		SELECT
		<include refid="sysModularColumns"/>
		FROM sys_modular a
		<include refid="sysModularJoins"/>
		<where>
			<if test="tableName != null and tableName != ''">
				AND a.table_name = #{tableName}
			</if>
			<if test="id != null and id != ''">
				AND a.id != #{id}
			</if>
		</where>
	</select>
</mapper>