<?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.cftech.sys.dao.PermissionMapper">
    <resultMap id="BaseResultMap" type="com.cftech.sys.model.Permission">
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="permission_name" property="permissionName" jdbcType="VARCHAR" />
        <result column="permission_sign" property="permissionSign" jdbcType="VARCHAR" />
        <result column="description" property="description" jdbcType="VARCHAR" />
         <result column="parentid" property="parentid" jdbcType="VARCHAR" />
         <result column="longnum" property="longnum" jdbcType="VARCHAR" />
         <result column="isshow" property="isshow" jdbcType="INTEGER"/>
           <result column="isfunction" property="isfunction" jdbcType="INTEGER"/>
          <result column="url" property="url" jdbcType="VARCHAR" />
          <result column="sort" property="sort" jdbcType="INTEGER" />
    </resultMap>
    <sql id="Example_Where_Clause">
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" suffix=")" prefixOverrides="and">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Update_By_Example_Where_Clause">
        <where>
            <foreach collection="example.oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" suffix=")" prefixOverrides="and">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Base_Column_List">
        id, permission_name, permission_sign, description,parentid,longnum,isshow,isfunction,url,fonts, sort
    </sql>
    <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.cftech.sys.model.PermissionExample">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List" />
        from permission
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List" />
        from permission
        where id = #{id,jdbcType=BIGINT}
    </select>
    <update id="deleteByPrimaryKey" parameterType="java.lang.Long">
        update  permission set delflag='1'
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="deleteByExample" parameterType="com.cftech.sys.model.PermissionExample">
        delete  permission set delflag='0'
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </update>
    <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.cftech.sys.model.Permission">
        insert into permission ( permission_name, permission_sign,url,createtime,timestamp,isfunction,isshow,
        description,parentid,longnum,fonts,sort)
        values (
        #{permissionName,jdbcType=VARCHAR},
        #{permissionSign,jdbcType=VARCHAR},
        #{url,jdbcType=VARCHAR},
        #{createtime,jdbcType=TIMESTAMP},
        #{timestamp,jdbcType=BIGINT},
        #{isfunction,jdbcType=INTEGER},
        #{isshow,jdbcType=INTEGER},
        #{description,jdbcType=VARCHAR},
        #{parentid,jdbcType=BIGINT},
        #{longnum,jdbcType=VARCHAR},
        #{fonts,jdbcType=VARCHAR},
        #{sort,jdbcType=INTEGER}
        )
    </insert>
    <insert id="insertSelective" parameterType="com.cftech.sys.model.Permission">
        insert into permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="permissionName != null">
                permission_name,
            </if>
            <if test="permissionSign != null">
                permission_sign,
            </if>
            <if test="description != null">
                description,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="permissionName != null">
                #{permissionName,jdbcType=VARCHAR},
            </if>
            <if test="permissionSign != null">
                #{permissionSign,jdbcType=VARCHAR},
            </if>
            <if test="description != null">
                #{description,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="com.cftech.sys.model.PermissionExample" resultType="java.lang.Integer">
        select count(*) from permission
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update permission
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=BIGINT},
            </if>
            <if test="record.permissionName != null">
                permission_name = #{record.permissionName,jdbcType=VARCHAR},
            </if>
            <if test="record.permissionSign != null">
                permission_sign = #{record.permissionSign,jdbcType=VARCHAR},
            </if>
            <if test="record.description != null">
                description = #{record.description,jdbcType=VARCHAR},
            </if>
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        update permission
        set id = #{record.id,jdbcType=BIGINT},
        permission_name = #{record.permissionName,jdbcType=VARCHAR},
        permission_sign = #{record.permissionSign,jdbcType=VARCHAR},
        description =
        #{record.description,jdbcType=VARCHAR}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="com.cftech.sys.model.Permission">
        update permission
        <set>
            <if test="permissionName != null">
                permission_name = #{permissionName,jdbcType=VARCHAR},
            </if>
            <if test="permissionSign != null">
                permission_sign = #{permissionSign,jdbcType=VARCHAR},
            </if>
            <if test="description != null">
                description = #{description,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.cftech.sys.model.Permission">
        update permission
        set permission_name = #{permissionName,jdbcType=VARCHAR},
        permission_sign = #{permissionSign,jdbcType=VARCHAR},
        description =
        #{description,jdbcType=VARCHAR},
        fonts = #{fonts,jdbcType=VARCHAR}
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!-- 通过角色id 查询角色 拥有的权限 -->
    <select id="selectPermissionsByRoleId" parameterType="java.lang.Long" resultMap="BaseResultMap">
        SELECT p.* FROM permission p inner join role_permission rp ON rp.permission_id = p.id
        WHERE rp.role_id =
        #{id,jdbcType=BIGINT} and p.delflag = 0 and p.permission_sign !='' and p.permission_sign is not null 
    </select>
    
   <!-- 根据用户查询所 具有的权限 -->
        <select id="selectPermissionsByUserId" parameterType="java.lang.Long" resultMap="BaseResultMap">
 			   SELECT
per.permission_name,per.permission_sign,per.id,per.description
FROM permission per
INNER JOIN role_permission roleper ON roleper.permission_id = per.id
INNER JOIN role rol ON rol.id = roleper.role_id
INNER JOIN user_role userrole ON userrole.role_id = userrole.id
AND userrole.user_id =   #{userid,jdbcType=BIGINT}
AND per.delflag = 0
AND rol.delflag = 0   	
    	</select>
 
 
 		<!-- 显示所有树 -->
 		<select id="getMenuTree" resultMap="BaseResultMap">
 		   SELECT per.permission_name,per.permission_sign,per.id,per.description,per.parentid, per.sort
			FROM permission per
 		   -- where per.delflag = 0 order by per.id asc
            where per.delflag = 0 order by per.sort, per.id asc
 		</select>
 
 
 <!-- 除去本节点以外的数 -->
     <select id="getMenuTreeExcludeCurrent" resultMap="BaseResultMap">
 		   SELECT per.permission_name,per.permission_sign,per.id,per.description,per.parentid,per.longnum
			FROM permission per
		   where per.id!=1 and per.delflag = 0  order by per.id asc 
 	</select>
 	
 	
 	<update id="updateLevelByPrimaryKey" parameterType="java.util.Map">
 		update permission set parentid = #{parentid,jdbcType=BIGINT},longnum =  #{longnum,jdbcType=VARCHAR}
 		where id = #{id,jdbcType=BIGINT};
 		update permission set longnum = replace(longnum,#{previouslong,jdbcType=VARCHAR},#{longnum,jdbcType=VARCHAR}) 
 		 where longnum like CONCAT('',#{previouslong,jdbcType=VARCHAR},'%' )  ;	
 	</update>
 	
 	  <insert id="insertPermission" useGeneratedKeys="true" keyProperty="id" parameterType="com.cftech.sys.model.Permission">
        insert into permission ( permission_name, permission_sign,url,createtime,timestamp,isfunction,isshow,
        description,parentid,longnum,updatetime)
        values (
        #{permissionName,jdbcType=VARCHAR},
        #{permissionSign,jdbcType=VARCHAR},
        #{url,jdbcType=VARCHAR},
        #{createtime,jdbcType=TIMESTAMP},
        #{timestamp,jdbcType=BIGINT},
        #{isfunction,jdbcType=INTEGER},
        #{isshow,jdbcType=INTEGER},
        #{description,jdbcType=VARCHAR},
        #{parentid,jdbcType=BIGINT},
        #{longnum,jdbcType=VARCHAR},
        #{createtime,jdbcType=TIMESTAMP},
        #{sort,jdbcType=INTEGER}
        )
    </insert>
    
     <update id="updateLevel" parameterType="java.util.Map">
 		update permission set longnum =  #{longnum,jdbcType=VARCHAR}
 		where id = #{id,jdbcType=BIGINT};
 	</update>
 	
 	
 	<update id="updatePermissions"  parameterType="com.cftech.sys.model.Permission">
 		  update permission
        <set>
            <if test="permissionName != null">
                permission_name = #{permissionName,jdbcType=VARCHAR},
            </if>
            <if test="permissionSign != null">
                permission_sign = #{permissionSign,jdbcType=VARCHAR},
            </if>
            <if test="description != null">
                description = #{description,jdbcType=VARCHAR},
            </if>
               <if test="url != null">
                url = #{url,jdbcType=VARCHAR},
            </if>
            <if test="sort != null">
                sort = #{sort,jdbcType=INTEGER},
            </if>
            fonts = #{fonts,jdbcType=VARCHAR},
             isfunction =  #{isfunction,jdbcType=INTEGER},
    		 isshow =  #{isshow,jdbcType=INTEGER},
    		 updatetime =  #{updatetime,jdbcType=TIMESTAMP}
        </set>
        where id = #{id,jdbcType=BIGINT}
 	</update>
 	
 	<!-- 根据权限选择 菜单 -->
 	<select id="getMenuByRoles" resultMap="BaseResultMap" >
 	SELECT
	p.id, p.permission_name,p.parentid,p.longnum,p.isshow,p.isfunction,p.url,p.fonts,
	(select count(1) from permission per where  FIND_IN_SET(per.id,p.longnum)>0 ) levelCount
FROM
	permission p
INNER JOIN (
	SELECT
		rp.permission_id
	FROM
		role_permission rp 
	WHERE 
		rp.role_id IN (${roleids}) 
) roleper ON p.id = roleper.permission_id and p.delflag = 0 and p.isshow = 1 and p.isfunction = 0 
group by id
-- order by LENGTH(p.longnum) asc ,(p.longnum) asc
 ORDER BY
	p.sort ASC,levelCount asc
 	</select>

    <!-- 根据角色id 获取所有的function节点 -->
    <select id="getPermissionsByRoles" resultMap="BaseResultMap" >
        SELECT
        p.id, p.permission_name,p.permission_sign
        FROM
        permission p
        INNER JOIN (
        SELECT
        rp.permission_id
        FROM
        role_permission rp
        WHERE
        rp.role_id IN (${roleids})
        ) roleper ON p.id = roleper.permission_id and p.delflag = 0  and p.isfunction = 1
    </select>

 	<select id="getAllPermission" resultMap="BaseResultMap">
 		SELECT
	p.id, p.permission_name,p.parentid,p.longnum,p.isshow,p.isfunction,p.url,p.fonts,p.permission_sign
FROM
	permission p where p.delflag = 0 and p.permission_sign !=''
 	</select>
 	
</mapper>