<?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.MenuMapper">
  <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.sys.Menu">
    <!--
      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="icon" jdbcType="VARCHAR" property="icon" />
    <result column="style" jdbcType="VARCHAR" property="style" />
    <result column="url" jdbcType="VARCHAR" property="url" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <result column="enable" jdbcType="BIT" property="enable" />
    <result column="parent_code" jdbcType="VARCHAR" property="parentCode" />
    <result column="order_num" jdbcType="INTEGER" property="orderNum" />
     <result column="white_url" jdbcType="VARCHAR" property="whiteUrl" />
    <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="selectAll">
    <![CDATA[
            SELECT m.id,m.code,m.name,m.icon,m.style,m.url,
            m.description,m.enable,m.parent_code,m.order_num,
            m.created_date,m.created_by,m.updated_date,m.updated_by,
            m.white_url
            FROM sys_menu m
            ]]>
  </sql>

  <select id="findByUserCode" parameterType="string" resultMap="BaseResultMap">
    <include refid="selectAll"></include>
    <![CDATA[
          INNER JOIN user_menus um ON um.menu_code = m.code
          WHERE m.enable = 1 and um.user_code=#{userCode}
          ]]>
    <![CDATA[
          UNION
          ]]>
    <include refid="selectAll"></include>
    <![CDATA[
            INNER JOIN role_menus rm ON rm.menu_code = m.code
            INNER JOIN user_roles ur ON ur.role_code = rm.role_code
            WHERE m.enable = 1 and ur.user_code=#{userCode}
            ORDER BY order_num ASC
           ]]>
  </select>

  <select id="selectAllMenus" resultMap="BaseResultMap">
    <![CDATA[
      SELECT id,`code`,`name`,parent_code FROM sys_menu
      WHERE `enable`=1  ORDER BY order_num ASC
    ]]>
  </select>
  
  <select id="selectTreeMenuByRole" parameterType="java.lang.String" resultMap="BaseResultMap">
  	SELECT
		id,
		`code`,
		`name`,
		parent_code
	FROM
		sys_menu
	WHERE
		`enable` = 1
		<if test="userCode != null and userCode != ''">
			AND `code` IN (
		SELECT DISTINCT
			menu_code
		FROM
			user_roles AS ur
		INNER JOIN role_menus AS rm ON ur.role_code = rm.role_code
		WHERE
			ur.user_code = #{userCode}
		)
		</if>
		ORDER BY
		order_num ASC
  </select>
  
  <select id="queryDataById" resultMap="BaseResultMap">
    SELECT m.id,m.code,m.name,m.icon,m.style,m.url,
            m.description,m.enable,m.parent_code,m.order_num,m.white_url
            FROM sys_menu m WHERE m.id=#{id}
  </select>


  <select id="checkExistChildNodes" resultType="java.lang.Integer">
    SELECT COUNT(1) FROM `sys_menu` WHERE parent_code=(select `code` FROM sys_menu WHERE id=#{id});
  </select>

</mapper>