<?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.goafanti.common.dao.PermissionMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.Permission">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="url" jdbcType="VARCHAR" property="url" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="create_id" jdbcType="VARCHAR" property="createId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="deleted_sign" jdbcType="VARCHAR" property="deletedSign" />
    <result column="aut_no" jdbcType="VARCHAR" property="autNo" />
    <result column="hierarchies" jdbcType="VARCHAR" property="hierarchies" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="show_order" jdbcType="VARCHAR" property="showOrder" />
    <result column="super_id" jdbcType="VARCHAR" property="superId" />
    <result column="type" jdbcType="INTEGER" property="type"/>
  </resultMap>
  <sql id="Base_Column_List">
    id, url, name,create_id as createId ,create_time as createTime,update_time as updateTime,
     deleted_sign as deletedSign, aut_no as autNo, hierarchies,status ,show_order as showOrder, super_id as superId,type
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from permission
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from permission
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.Permission">
    insert into permission (id, url, name
      )
    values (#{id,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.Permission">
    insert into permission
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="url != null">
        url,
      </if>
      <if test="name != null">
        name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="url != null">
        #{url,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.Permission">
    update permission
    <set>
      <if test="url != null">
        url = #{url,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.Permission">
    update permission
    set url = #{url,jdbcType=VARCHAR},
      name = #{name,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <select id="findPermissionByUserId" parameterType="java.lang.String" resultType="java.lang.String">
    select tp.url
    from permission tp
    left join role_permission trp on tp.id=trp.pid
    left join user_role tur on tur.rid=trp.rid
    where tur.uid = #{userId,jdbcType=VARCHAR}
  </select>
  <select id="findAll" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from permission order by name
  </select>
  
  <insert id="insertBatch" parameterType="com.goafanti.common.model.Permission">
    insert into permission (id, name, url)
    values 
    <foreach item="item" index="index" collection="list" separator=",">
      (#{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, #{item.url,jdbcType=VARCHAR})
    </foreach>
    ON DUPLICATE KEY UPDATE
	    name = values(name),
	    url = values(url)
  </insert>
  
  <delete id="deleteByPrimaryKeys" parameterType="java.lang.String">
    delete from permission
    where id in 
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
	  #{item,jdbcType=VARCHAR}  
    </foreach>
  </delete>
  <insert id="insertn" parameterType="com.goafanti.common.model.Permission">
    insert into permission (id, url, name, 
      create_id, create_time, update_time, 
      deleted_sign, aut_no, hierarchies, 
      status, show_order, super_id,type
      )
    values (#{id,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, 
      #{createId,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{deletedSign,jdbcType=VARCHAR}, #{autNo,jdbcType=VARCHAR}, #{hierarchies,jdbcType=VARCHAR}, 
      #{status,jdbcType=VARCHAR}, #{showOrder,jdbcType=VARCHAR}, #{superId,jdbcType=VARCHAR},#{type,jdbcType=INTEGER}
      )
  </insert>
  <select id="selectAdminNameById" parameterType="java.lang.String" resultType="java.lang.String">
  		select 
  		name
  		from admin
  		where 
  		id=#{id,jdbcType=VARCHAR}
  </select>
  <select id="selectAutNoCount" parameterType="String" resultType="java.lang.Integer">
		select
		count(0)
		from permission
		where
		super_id=#{superId,jdbcType=VARCHAR}
  </select>	
  <select id="selectIdByAutNo" parameterType="java.lang.String" resultType="java.lang.String">
  		select
  		id
  		from permission
  		where
  		aut_no=#{autNo,jdbcType=VARCHAR}
  </select>
  <select id="selectAutNoByName" resultType="java.lang.String">
		select
		aut_No as AutNo
		from permission
		where
		id=#{name,jdbcType=VARCHAR}
  </select>
  <select id="selectSuperIdById" parameterType="java.lang.String" resultType="com.goafanti.common.model.Permission">
  		select
		name as superId
		from permission
		where
		id=#{id,jdbcType=VARCHAR} 
  </select>
  <delete id="deleteById" parameterType="java.lang.String">
	    delete from permission
	    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <select id="selectAllById" parameterType="java.lang.String" resultType="com.goafanti.common.model.Permission">
  		select 
    	<include refid="Base_Column_List" />
    	from permission
    	where
    	id=#{id,jdbcType=VARCHAR}
    	order by name
  </select>
  <select id="selectCountBySuperId" parameterType="String" resultType="java.lang.Integer">
  		select
		count(0)
		from permission
		where
		super_id=#{superId,jdbcType=VARCHAR}
  </select>
  <select id="selectName" resultType="com.goafanti.common.model.Permission">
		select
		name
		from permission
		where
		name  like concat('%',#{name,jdbcType=VARCHAR},'%')
  </select>
  <update id="updateById"  parameterType="com.goafanti.common.model.Permission">
  	update permission
    <set>
      <if test="url != null">
        url = #{url,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="createId != null">
        create_id = #{createId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedSign != null">
        deleted_sign = #{deletedSign,jdbcType=VARCHAR},
      </if>
      <if test="autNo != null">
        aut_no = #{autNo,jdbcType=VARCHAR},
      </if>
      <if test="hierarchies != null">
        hierarchies = #{hierarchies,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=VARCHAR},
      </if>
      <if test="showOrder != null">
        show_order = #{showOrder,jdbcType=VARCHAR},
      </if>
      <if test="superId != null">
        super_id = #{superId,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
      	type = #{type,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR} 		
  </update>
  <select id="selectIDNBySuperId" parameterType="java.lang.String" resultType="com.goafanti.common.model.Permission">
  		select
  		<include refid="Base_Column_List" />
  		from permission 		
  		where
  		super_id=#{superId,jdbcType=VARCHAR}
  		order by aut_no
  </select>
  <update id="updateByNumber" parameterType="com.goafanti.common.model.Permission">
  		update permission
	    <set>
	    	aut_no = #{autNo,jdbcType=VARCHAR}
	    </set>
	    where
	    id=#{id,jdbcType=VARCHAR}
  </update>
  <select id="selectOneAll" resultType="com.goafanti.permission.bo.PermissionBo">
  		select
  		<include refid="Base_Column_List" />
  		from permission 		
  		where
  		hierarchies=1
  		order by aut_no
  </select>
  <select id="selectNameById" parameterType="java.lang.String" resultType="java.lang.String">
  		select
		name as superId
		from permission
		where
		id=#{id,jdbcType=VARCHAR} 
  </select>
  <select id="selectAllBySuperId" parameterType="String" resultType="com.goafanti.permission.bo.PermissionBo">
  		select
		<include refid="Base_Column_List" />
		from permission
		where
		super_id=#{superId,jdbcType=VARCHAR}
		order by aut_no
  </select>
  <select id="selectAutNoByNamei" resultType="java.lang.String">
		select
		aut_No as AutNo
		from permission
		where
		name=#{name,jdbcType=VARCHAR}
  </select>
  <select id="selectIdByName" resultType="java.lang.String">
		select
		id
		from permission
		where
		name=#{name,jdbcType=VARCHAR}
  </select>
  
  <select id="selectIdByNameCount" resultType="java.lang.Integer">
		select count(*)
		from permission
		where
		name=#{name,jdbcType=VARCHAR} and id != #{id,jdbcType=VARCHAR}
  </select>
  <select id="selectCountByid" parameterType="java.lang.String" resultType="java.lang.Integer" >
  		select
  		count(0)
  		from role_permission
  		where
  		pid=#{id,jdbcType=VARCHAR} 
  </select>
  <select id="selectNavList" parameterType="java.lang.String" resultType="com.goafanti.permission.bo.Navigation">
		<choose>
			<when test="aid == null">
				select
					id,
					name,
					show_order,
					url
				from
					permission
				where type = 0	 
			</when>
			<otherwise>
				select
					d.id,
					d.name,
					show_order,
					d.url
				from
					user_role a left join role b on
					a.rid = b.id left join role_permission c on
					b.id = c.rid left join permission d on
					c.pid = d.id
				where d.type = 0 and a.uid = #{aid,jdbcType=VARCHAR}
			</otherwise>
		</choose>
		order by show_order
  </select>
  <select id="selectMenuList" parameterType="java.lang.String" resultType="com.goafanti.permission.bo.SubMenu">
  		<choose>
  			<when test="aid == null">
		  		select
					id,
					name,
					url
				from
					permission
				where
					super_id = #{superId,jdbcType=VARCHAR}
					and (type = 1 or type = 2)
  			</when>
  			<otherwise>				
				select
					d.id,
					d.name,
					d.url
				from
					user_role a left join role b on
					a.rid = b.id left join role_permission c on
					b.id = c.rid left join permission d on
					c.pid = d.id
				where
					(d.type = 1 or d.type = 2)
					and d.super_id = #{superId,jdbcType=VARCHAR}
					and a.uid = #{aid,jdbcType=VARCHAR}
  			</otherwise>
  		</choose>
  		order by show_order
  </select>
  <select id="selectTopNavBySubUrl" resultType="java.lang.String" parameterType="java.lang.String">
  	select super_id  from permission where id = (select super_id from permission where url = #{subUrl,jdbcType=VARCHAR})
  </select>
</mapper>