<?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.sunyard.ssp.modules.user.mapper.ScPermissionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.sunyard.ssp.modules.user.entity.ScPermission">
        <id column="ID" property="id" />
        <result column="DESCRIPTION" property="description" />
        <result column="NAME" property="name" />
        <result column="PARENT_ID" property="parentId" />
        <result column="P_TYPE" property="pType" />
        <result column="SORT_ORDER" property="sortOrder" />
        <result column="COMPONENT" property="component" />
        <result column="PATH" property="path" />
        <result column="TITLE" property="title" />
        <result column="ICON" property="icon" />
        <result column="P_LEVEL" property="pLevel" />
        <result column="BUTTON_TYPE" property="buttonType" />
        <result column="P_STATUS" property="pStatus" />
        <result column="URL" property="url" />
        <result column="NEED_VERIFY" property="needVerify"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID, DESCRIPTION, NAME, PARENT_ID, P_TYPE, SORT_ORDER, COMPONENT, PATH, TITLE, ICON, P_LEVEL, BUTTON_TYPE, P_STATUS, URL, NEED_VERIFY
    </sql>
    <sql id="FindByParent_Column_List">
        tp.id,
        tp.description,
        tp.name,
        tp.parent_id,
        tp.p_type,
        tp.sort_order,
        tp.component,
        tp.path,
        tp.title,
        tp.icon,
        tp.p_level,
        tp.button_type,
        tp.p_status,
        tp.url,
        tp.need_verify
    </sql>
    <select id="findByUserId" resultType="com.sunyard.ssp.modules.user.entity.ScPermission">
        SELECT DISTINCT tp.*
        FROM sc_user u
        LEFT JOIN sc_user_role ur ON u.id = ur.user_id
        LEFT JOIN sc_role_permission rp ON ur.role_id = rp.role_id
        LEFT JOIN sc_permission tp ON tp.id = rp.permission_id
        WHERE u.id = #{userId} AND tp.p_status = 0
        ORDER BY tp.sort_order ASC
    </select>
    <select id="findByLevelOrderBySortOrder" resultType="com.sunyard.ssp.modules.user.entity.ScPermission">
        SELECT DISTINCT tp.* from sc_permission tp
        WHERE
        tp.p_level = #{level}
        order by tp.sort_order
    </select>
    <select id="findByParentIdOrderBySortOrder" resultType="com.sunyard.ssp.modules.user.entity.ScPermission">
        SELECT DISTINCT tp.* from sc_permission tp
        where
        tp.parent_id = #{parentId}
        order by tp.sort_order
    </select>
    <select id="findAll" resultType="com.sunyard.ssp.modules.user.entity.ScPermission">
        select <include refid="Base_Column_List"></include> from  sc_permission
    </select>

</mapper>
