<?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="SysFunctionMapper" >
	<sql id="sys_function_column">
	SYS_FUNCTION.FUNCTION_ID,
	SYS_FUNCTION.PARENT_ID,
	SYS_FUNCTION.FUNCTION_NAME,
	SYS_FUNCTION.FUNCTION_URL,
	SYS_FUNCTION.FUNCTION_TYPE,
	SYS_FUNCTION.CREATE_TIME,
	SYS_FUNCTION.SORT
	</sql>
	<sql id="sys_function_property">
	#{functionId},
	#{parentId},
	#{functionName},
	#{functionUrl},
	#{functionType},
	#{createTime},
	#{sort}
	</sql>
	
    <resultMap type="SysFunction" id="SysFunctionResult">
    <result property="functionId" column="FUNCTION_ID"/>
    <result property="parentId" column="PARENT_ID"/>
    <result property="functionName" column="FUNCTION_NAME"/>
    <result property="functionUrl" column="FUNCTION_URL"/>
    <result property="functionType" column="FUNCTION_TYPE"/>
    <result property="createTime" column="CREATE_TIME"/>
    <result property="sort" column="SORT"/>
    </resultMap>
    
    <!-- 查询所有的权限记录 -->
    <select id="queryAllSysFunction" resultMap="SysFunctionResult">
    	SELECT <include refid="sys_function_column"/> FROM SYS_FUNCTION ORDER BY SYS_FUNCTION.SORT DESC
    </select>
    <!-- 添加权限 -->
    <insert id="cresateSysFunction" parameterType="SysFunction" keyProperty="functionId" keyColumn="FUNCTION_ID" useGeneratedKeys="true">
    	INSERT INTO SYS_FUNCTION(<include refid="sys_function_column"/>)
    	VALUE(<include refid="sys_function_property"/>)
    </insert>
    
    <!-- 修改权限 -->
    <update id="updateFunction" parameterType="SysFunction">
    	UPDATE SYS_FUNCTION SET 
    	
    	SYS_FUNCTION.FUNCTION_NAME=#{functionName},
    	SYS_FUNCTION.FUNCTION_URL=#{functionUrl},
    	SYS_FUNCTION.FUNCTION_TYPE=${functionType},
    	SYS_FUNCTION.SORT=#{sort}
    	WHERE SYS_FUNCTION.FUNCTION_ID=#{functionId}
    </update>
    
    <update id="updateFunctionParentId" parameterType="java.util.Map">
    	UPDATE SYS_FUNCTION SET SYS_FUNCTION.PARENT_ID=#{parentId} WHERE SYS_FUNCTION.FUNCTION_ID=#{functionId}
    </update>
    
    <delete id="deleteFunctionByIds" parameterType="java.lang.String">
    	DELETE FROM SYS_FUNCTION WHERE SYS_FUNCTION.FUNCTION_ID IN (${value})
    </delete>
    <!-- 查询用户权限 -->
    <select id="querySysUserFunction" parameterType="int" resultMap="SysFunctionResult">
    SELECT <include refid="sys_function_column"/> FROM SYS_FUNCTION
	LEFT JOIN SYS_ROLE_FUNCTION ON SYS_ROLE_FUNCTION.FUNCTION_ID = SYS_FUNCTION.FUNCTION_ID
	LEFT JOIN SYS_USER ON SYS_USER.ROLE_ID = SYS_ROLE_FUNCTION.ROLE_ID
	WHERE SYS_USER.USER_ID=#{value}  ORDER BY SYS_FUNCTION.SORT DESC
    </select>
</mapper>