<?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.SysModularFieldDao">
    
	<sql id="sysModularFieldColumns">
		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.modular_id AS "modularId",
		a.field AS "field",
		a.notes AS "notes",
		a.sort AS "sort",
		a.is_sys_dict as "isSysDict",
		a.show_type AS "showType",
		a.field_type AS "fieldType",
		a.logic AS "logic",
		s.table_name AS "sysModular.tableName",
		s.range_field AS "sysModular.rangeField"
	</sql>
	
	<sql id="sysModularFieldJoins">
		left join sys_modular s on s.id=a.modular_id
	</sql>
	
    
	<select id="get" resultType="SysModularField" >
		SELECT 
			<include refid="sysModularFieldColumns"/>
		FROM sys_modular_field a
		<include refid="sysModularFieldJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="SysModularField" >
		SELECT 
			<include refid="sysModularFieldColumns"/>
		FROM sys_modular_field a
		<include refid="sysModularFieldJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="modularId != null and modularId != ''">
				AND a.modular_id = #{modularId}
			</if>
		</where>
		ORDER BY a.sort asc
	</select>
	
	<select id="findAllList" resultType="SysModularField" >
		SELECT 
			<include refid="sysModularFieldColumns"/>
		FROM sys_modular_field a
		<include refid="sysModularFieldJoins"/>
		<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_field(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			modular_id,
			field,
			notes,
			field_type,
			logic,
			sort,
			show_type,
			is_sys_dict
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{modularId},
			#{field},
			#{notes},
			#{fieldType},
			#{logic},
			#{sort},
			#{showType},
			#{isSysDict}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_modular_field SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			del_flag = #{delFlag},
			remarks = #{remarks},
			modular_id = #{modularId},
			field = #{field},
			notes = #{notes},
			field_type = #{fieldType},
			logic = #{logic},
			sort = #{sort},
			show_type = #{showType},
			is_sys_dict=#{isSysDict}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_modular_field
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_modular_field SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SysModularField" statementType="STATEMENT">
		select * FROM sys_modular_field  where ${propertyName} = '${value}'
	</select>

	<select id="find" resultType="SysModularField" >
		SELECT
		<include refid="sysModularFieldColumns"/>
		FROM sys_modular_field a
		<include refid="sysModularFieldJoins"/>
		WHERE a.notes in ('')
	</select>

	<select id="gridColumn" resultType="SysModularField" parameterType="java.lang.String">
		SELECT
		a.COLUMN_NAME as "field",
		a.ORDINAL_POSITION as "no",
		a.IS_NULLABLE as "isEmpty",
		a.COLUMN_TYPE as "fieldType",
		a.COLUMN_COMMENT as "notes"
		FROM
		information_schema. COLUMNS a
		<where>
			1=1
			<!--<if test="field != null and field != ''">
				AND a.COLUMN_NAME = #{field}
			</if>
			<if test="notes != null and notes != ''">
				AND a.COLUMN_COMMENT = #{notes}
			</if>-->
			and a.table_name=${tableName}
			and a.table_schema =${dbName}
		</where>
		ORDER BY a.ORDINAL_POSITION  asc
	</select>
</mapper>