<?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.sysmtree.dao.SysMtreeDao">
    
	<sql id="sysMtreeColumns">
		a.id AS "id",
		a.parent_id AS "parentId",
		a.parent_ids AS "parentIds",
		a.name AS "name",
		a.sort AS "sort",
		a.description AS "description",
		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 AS "type",
		a.use_type AS "useType"
	</sql>
	
	<sql id="sysMtreeJoins">
	</sql>
	
    
	<select id="get" resultType="SysMtree" >
		SELECT 
			<include refid="sysMtreeColumns"/>
		FROM sys_mtree a
		<include refid="sysMtreeJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="SysMtree" >
		SELECT 
			<include refid="sysMtreeColumns"/>
		FROM sys_mtree a
		<include refid="sysMtreeJoins"/>
		<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>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="SysMtree" >
		SELECT 
			<include refid="sysMtreeColumns"/>
		FROM sys_mtree a
		<include refid="sysMtreeJoins"/>
		<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.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO sys_mtree(
			id,
			parent_id,
			parent_ids,
			name,
			sort,
			description,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			type,
			use_type
		) VALUES (
			#{id},
			#{parentId},
			#{parentIds},
			#{name},
			#{sort},
			#{description},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{type},
			#{useType}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_mtree SET 	
			parent_id = #{parentId},
			parent_ids = #{parentIds},
			name = #{name},
			sort = #{sort},
			description = #{description},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			type = #{type},
			use_type = #{useType}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_mtree
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_mtree SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SysMtree" statementType="STATEMENT">
		select * FROM sys_mtree  where ${propertyName} = '${value}'
	</select>
	
</mapper>