<?xml version="1.0" encoding="utf-8"?>
<sqltoy xmlns="http://www.sagframe.com/schema/sqltoy"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sagframe.com/schema/sqltoy http://www.sagframe.com/schema/sqltoy/sqltoy.xsd"
>
	
  <sql id="SQL4Sys_userRole">
	        <value>
	            <![CDATA[
	                SELECT t1.role_id,t1.role_name
					FROM t_sys_role t1
					WHERE t1.role_id IN (SELECT role_ids
	      		  		FROM t_sys_user t
		          		WHERE 1=1 #[AND t.user_name=:userName]
		        		)
	            ]]>
	        </value>
	  </sql>
	 <!-- rp.perm  dd.dict_value as perm -->
	 <sql id="sys_queryRolePerm">
	    <value>
          <![CDATA[
		    SELECT rp.role_id,rp.perm,rp.menu_id,r.role_name 
			FROM t_sys_role_perm rp
            LEFT JOIN t_sys_role r ON r.role_id = rp.role_id 
          ]]>
   	    </value>
  	 </sql>
	 <!-- 共有三类：M目录 C菜单 F按钮 DISTINCT-->
  	 <sql id="sys_queryMenu_N">
	    <value>
          <![CDATA[
		    SELECT  m.*,rp.perm AS perms 
			FROM t_sys_menu m
            LEFT JOIN t_sys_role_perm rp ON rp.menu_id = m.menu_id 
            LEFT JOIN t_sys_role_menu rm ON m.menu_id = rm.menu_id
          	WHERE (m.menu_type IN('M','C') AND m.status="0" AND m.meta_is_hide="0")
               #[AND rm.role_id =:roleId]
            ORDER BY m.parent_id,m.menu_sort         
          ]]>
   	    </value>
  	 </sql> 
   	 <sql id="sys_queryMenuTreeByRoleId">
	    <value>
          <![CDATA[
		    SELECT DISTINCT m.* 
			FROM t_sys_menu m 
        	WHERE 1=1  
          	   #[@if(:status!="") AND (m.status =:status)]
			   #[@if(:status!="0" && :status!="1") AND (m.status ="0")]
			   #[AND m.meta_title =:metaTitle]
               #[@if(:menuType!="") AND m.menu_type =:menuType]
               #[@if(:menuType!="M" && :menuType!="C" && :menuType!="F") AND m.menu_type IN('M', 'C')]         
          ]]>
   	    </value>
  	  </sql> 
  	  <sql id="sys_queryMenuTreeByRoleId_N">
	    <value>
          <![CDATA[
		    SELECT DISTINCT m.*,rp.perm AS perms 
			FROM t_sys_menu m 
        	LEFT JOIN t_sys_role_menu rm ON m.menu_id = rm.menu_id
		    LEFT JOIN t_sys_role_perm rp ON rp.menu_id = rm.menu_id AND rm.role_id
          	WHERE 1=1  
          	   #[AND rm.role_id =:roleId]
			   #[@if(:menuType!="") AND m.menu_type =:menuType]
               #[@if(:menuType!="M" && :menuType!="C" && :menuType!="F") AND m.menu_type IN('M', 'C')]  
               #[AND m.meta_title =:metaTitle]
               #[@if(:status!="") AND (m.status =:status)]
			   #[@if(:status!="0" && :status!="1") AND (m.status ="0")]
             ORDER BY m.parent_id,m.menu_sort
          ]]>
   	    </value>
  	  </sql> 
  	  <!--  AND rm.role_id =@value(:roleId) 字面值：rm.role_id=1-->
  	  <sql id="sys_queryMenuListByRoleId">
	    <value>
          <![CDATA[
		    SELECT m.menu_id 
			FROM t_sys_menu m 
        	LEFT JOIN t_sys_role_menu rm ON m.menu_id = rm.menu_id
          	WHERE 1=1  
          	   #[AND rm.role_id =:roleId]
               #[AND m.menu_id NOT IN (
          	        SELECT m.parent_id
          	        FROM t_sys_menu m
          	        INNER JOIN t_sys_role_menu rm
          	        ON m.menu_id=rm.menu_id AND rm.role_id =:roleId 
          	    )]
          	ORDER BY m.parent_id, m.menu_sort
          ]]>
	    </value>
  	  </sql> 
  	  <sql id="sys_deleteDictAndItem">
	    <value>
          <![CDATA[
		    DELETE p.*, pp.* 
			FROM t_sys_dict_type p 
        	LEFT JOIN t_sys_dict_data pp ON p.type = pp.dict_type
          	WHERE #[p.id IN (:id)]
           ]]>
	    </value>
  	  </sql>
      <sql id="sys_queryDeptListByRoleId">
	     <page-optimize alive-max="100" alive-seconds="120" />
		<value>
          <![CDATA[
			SELECT a.*, CONCAT_WS(',',ancestors,a.dept_id) AS ancestors
        	FROM  t_sys_dept a
            LEFT JOIN t_sys_role_dept b ON a.dept_id=b.dept_id
          	WHERE (a.status = 0 AND a.del_flag != 2)
             	#[AND b.role_id = :roleId]
           ]]>
	    </value>
  	 </sql>

	<sql id="sys_delBatch">
		<value>
			<![CDATA[
		    DELETE
			FROM @value(:tableName)
        	WHERE #[@value(:keyId) IN (:keyValue)]
           ]]>
		</value>
	</sql>
   </sqltoy>