<?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.sys.dao.MainDictDao">
    
	<sql id="mainDictColumns">
		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.type_number AS "typeNumber",
		a.type_name AS "typeName",
		a.tree_sort AS "treeSort",
		a.type_sort AS "typeSort",
		a.dict_type AS "dictType",
		a.name as "name"
	</sql>
	
	<sql id="mainDictJoins">

	</sql>
	
    
	<select id="get" resultType="MainDict" >
		SELECT 
			<include refid="mainDictColumns"/>
		FROM main_dict a
		<include refid="mainDictJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="MainDict" >
		SELECT 
			<include refid="mainDictColumns"/>
		FROM main_dict a
		<include refid="mainDictJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				and a.id = #{id}
			</if>
			<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="typeName != null and typeName != ''">
				AND a.type_name LIKE
				<if test="dbName == 'oracle'">'%'||#{typeName}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{typeName}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{typeName},'%')</if>
			</if>
			<if test="dictType != null and dictType != ''">
				and a.dict_type = #{dictType}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.type_sort+0 ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="MainDict" >
		SELECT 
			<include refid="mainDictColumns"/>
		FROM main_dict a
		<include refid="mainDictJoins"/>
		<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 main_dict(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			type_number,
			type_name,
			tree_sort,
			type_sort,
			dict_type,
			name
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{typeNumber},
			#{typeName},
			#{treeSort},
			#{typeSort},
			#{dictType},
			#{name}
		)
	</insert>
	
	<update id="update">
		UPDATE main_dict SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			type_number = #{typeNumber},
			type_name = #{typeName},
			tree_sort = #{treeSort},
			type_sort = #{typeSort},
			dict_type = #{dictType},
			name = #{name}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM main_dict
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE main_dict SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="MainDict" statementType="STATEMENT">
		select * FROM main_dict  where ${propertyName} = '${value}'
	</select>

	<select id="getIdByName" resultType="MainDict">
		select * from main_dict where type_name = #{typeName}
	</select>
	<select id="getTypeName" resultType="MainDict" parameterType="java.lang.String">
		select * from main_dict where type_name = #{typeName} and id !=#{id}
	</select>
	<select id="findTypeList" resultType="string">
		SELECT
			type_name as type
		FROM main_dict
		WHERE del_flag = #{DEL_FLAG_NORMAL}
		GROUP BY type_name
	</select>
</mapper>