<?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.RoleMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.Role">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="role_name" jdbcType="VARCHAR" property="roleName" />
    <result column="role_type" jdbcType="INTEGER" property="roleType" />
  </resultMap>
  <sql id="Base_Column_List">
    id, role_name, role_type ,creater,create_time as createTime
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from role
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from role
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.Role">
    insert into role (id, role_name,creater,create_time)
    values (#{id,jdbcType=VARCHAR}, #{roleName,jdbcType=VARCHAR},
    		#{creater,jdbcType=VARCHAR},#{createTime,jdbcType=VARCHAR})
    ON DUPLICATE KEY UPDATE
	role_name = values(role_name)
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.Role">
    insert into role
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="roleName != null">
        role_name,
      </if>
      <if test="roleType != null">
        role_type,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="roleName != null">
        #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="roleType != null">
        #{roleType,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.Role">
    update role
    <set>
      <if test="roleName != null">
        role_name = #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="roleType != null">
        role_type = #{roleType,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.Role">
    update role
    set role_name = #{roleName,jdbcType=VARCHAR},
      role_type = #{roleType,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <insert id="insertBatch" parameterType="com.goafanti.common.model.Role">
    insert into role (id, name)
    values 
    <foreach item="item" index="index" collection="list" separator=",">
      (#{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR})
    </foreach>
    ON DUPLICATE KEY UPDATE
	    name = values(name)
  </insert>
  <select id="findRolePermissions" resultType="com.goafanti.permission.bo.RolePermissionBo">
    select tr.id as rid, tr.role_name as rname, tr.role_type,tp.id as pid,tp.name as pname,tp.url
    from role tr
    left join role_permission trp on trp.rid=tr.id
    left join permission tp on tp.id=trp.pid
    where tr.role_type <![CDATA[ < ]]> 999999
  </select>
  <select id="findRolePermissionsExceptAreaManager" resultType="com.goafanti.permission.bo.RolePermissionBo">
    select tr.id as rid, tr.role_name as rname, tr.role_type,tp.id as pid,tp.name as pname,tp.url
    from role tr
    left join role_permission trp on trp.rid=tr.id
    left join permission tp on tp.id=trp.pid
    where tr.role_type <![CDATA[ < ]]> 99999
  </select>
  <select id="findRoleByUserId" resultType="java.lang.String">
    select tr.role_type from role tr
    left join user_role tur on tur.rid=tr.id
    where tur.uid = #{userId,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKeys" parameterType="java.lang.String">
    delete from role
    where id in 
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
	  #{item,jdbcType=VARCHAR}  
    </foreach>
  </delete>
 <!-- ******************************************************************** --> 
  <select id="selectRoleList" resultType="com.goafanti.permission.bo.RoleBo">
    select id,role_name as roleName,role_type as roleType,creater,create_time as createTime
    from role 
    where role_type <![CDATA[ < ]]> 999999
    <if test="roleName !=null">
		AND role_name=#{roleName,jdbcType=VARCHAR}
	</if>
	<if test="page_sql != null">
    		${page_sql}
	</if>
  </select>
  <select id="selectRoleCount" resultType="java.lang.Integer">
  	select
		count(0)
		from role
		where
		1=1
		<if test="roleName !=null">
		AND role_name=#{roleName,jdbcType=VARCHAR}
		</if>
  </select>
  
  <select id="selectRolePList" resultType="com.goafanti.permission.bo.RolePermissionBo">
  	select tr.id as rid, tr.role_name as rname, tr.role_type,tp.id as pid,tp.name as pname,tp.url
    from role tr
    left join role_permission trp on trp.rid=tr.id
    left join permission tp on tp.id=trp.pid
    where
    tr.id=#{id,jdbcType=VARCHAR}
  </select>
  <select id="selectCountByRoleId" parameterType="java.lang.String" resultType="java.lang.Integer">
  	select 
  	count(0) 
  	from user_role 
   	where 
   	rid in 
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
	  #{item,jdbcType=VARCHAR}  
    </foreach>  
  </select>
  <select id="selectIdByName" parameterType="java.lang.String" resultType="java.lang.String">
  	select
  	id
  	from role
  	where
  	role_name=#{roleName,jdbcType=VARCHAR}
  </select>
  <select id="selectNameById" parameterType="java.lang.String" resultType="java.lang.String">
  	select
  	role_name as roleName
  	from role
  	where
  	id=#{id,jdbcType=VARCHAR}
  </select>
  <select id="selectIdByNamei" parameterType="com.goafanti.common.model.Role" resultType="java.lang.String">
  	select
  	id
  	from role
  	where
  	role_name=#{roleName,jdbcType=VARCHAR}
  	and
  	id!=#{id,jdbcType=VARCHAR}
  </select>
</mapper>