<?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="cn.com.countrygarden.bi.main.dao.sys.RoleMapper">
  <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.sys.Role">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="enable" jdbcType="BIT" property="enable" />
    <result column="role_type" jdbcType="INTEGER" property="roleType" />
    <result column="created_date" jdbcType="TIMESTAMP" property="createdDate" javaType="java.util.Date"/>
    <result column="created_by" jdbcType="VARCHAR" property="createdBy" />
    <result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate" javaType="java.util.Date"/>
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy" />
  </resultMap>

  <sql id="selectAllRole">
    <![CDATA[
            SELECT r.id, r.code, r.name, r.enable, r.created_date, r.created_by, r.updated_date, r.updated_by
            FROM sys_role AS r
        ]]>
  </sql>
	
	<select id="getUserWhiteUrlByRoleCode" parameterType="java.lang.String" resultType="cn.com.countrygarden.bi.main.model.sys.UserWhiteUrl">
  	SELECT
  		DISTINCT
		rm.role_code AS roleCode,
		sm.white_url AS whiteUrl
	FROM
		role_menus AS rm
	INNER JOIN sys_menu AS sm ON rm.menu_code = sm.`code`
	WHERE
		rm.role_code = #{roleCode}
  </select>
  
  <select id="findByUserCode" parameterType="string" resultMap="BaseResultMap">
    <include refid="selectAllRole"></include>
    <![CDATA[
            INNER JOIN  user_roles us ON us.role_code = r.code
            WHERE us.role_code = r.code AND us.user_code=#{userCode}
            ]]>
  </select>

  <select id="selectDataByPrimaryKey" resultMap="BaseResultMap">
     <![CDATA[
            SELECT r.id, r.code, r.name, r.enable,r.role_type
            FROM sys_role AS r WHERE r.id=#{id}
        ]]>
  </select>
  
  <select id="getUserRoleList" parameterType="cn.com.countrygarden.bi.main.model.sys.Role" resultMap = "BaseResultMap">
  	SELECT
  		DISTINCT
	  	sr.`id`,
		sr.`code`,
		sr.`name`,
		sr.`enable`,
		sr.created_date
	FROM
		sys_role AS sr
	LEFT JOIN user_roles AS ur ON sr.`code` = ur.role_code
	<where>
	1=1
		<if test="userCode != null and userCode != ''">
		and	ur.user_code = #{userCode}
		</if>
		<if test="role.code != null and role.code != ''">
			and	sr.code like CONCAT('%',#{role.code},'%')
		</if>
		<if test="role.name != null and role.name != ''">
			and	sr.name like CONCAT('%',#{role.name},'%')
		</if>
	</where>
	ORDER BY 	sr.created_date,
	  sr.code,
	  sr.name,
	  sr.enable  DESC
  </select>
  
  <select id="checkIsAdmin" parameterType="java.lang.String" resultType="java.lang.Integer">
  	SELECT count(id) AS c FROM user_roles WHERE user_code = #{userCode} AND role_code = 'admin'
  </select>
  
  <select id="checkIsConsoleAdmin" parameterType="java.lang.String" resultType="java.lang.Integer" >
  	SELECT
		COUNT(*) AS c
	FROM
		user_roles AS ur
	INNER JOIN sys_role AS sr ON ur.role_code = sr.`code`
	WHERE
		ur.user_code = #{userCode}
		AND sr.role_type = 2
  </select>
</mapper>