<?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.FunctionMapper">
  <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.sys.Function">
    <!--
      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="description" jdbcType="VARCHAR" property="description" />
    <result column="enable" jdbcType="BIT" property="enable" />
    <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 f.id, f.code, f.name, f.description, f.enable, f.created_date, f.created_by, f.updated_date, f.updated_by
      FROM sys_function f
    ]]>
  </sql>

  <select id="findByUserCode" parameterType="string" resultMap="BaseResultMap">
      <include refid="selectAll"></include>
      <![CDATA[
              INNER JOIN role_sys_functions rf ON rf.function_code = f.code
              INNER JOIN user_roles ur ON ur.role_code = rf.role_code
              WHERE f.enable = 1 AND ur.user_code=#{userCode}
              UNION
            ]]>
      <include refid="selectAll"></include>
      <![CDATA[
                INNER JOIN user_sys_functions uf ON uf.function_code = f.code
                WHERE f.enable = 1 AND uf.user_code=#{userCode}
              ]]>
  </select>

  <select id="selectCheckListByRole" resultType="cn.com.countrygarden.bi.main.dto.CheckListDTO">
    SELECT sf.code,CASE (SELECT COUNT(1) FROM role_sys_functions rsf WHERE rsf.role_code=#{roleCode} AND sf.`code`=rsf.function_code) WHEN 0 THEN FALSE ELSE TRUE END  checked,
    sf.`name` label FROM sys_function sf
  </select>

  <select id="selectExistFunctionsByRole" resultType="java.lang.String">
    SELECT function_code FROM role_sys_functions rsf WHERE rsf.role_code=#{roleCode}
  </select>

  <select id="selectDataByPrimaryKey" resultMap="BaseResultMap">
    SELECT id,code,`name`,description,enable FROM sys_function WHERE id=#{id}
  </select>
</mapper>