-- 菜单命名空间
#namespace("menu")
  -- 获取获取异步接口
  #sql("selectChildAsync")
select t1.menu_id,t1.menu_name,t1.parent_id,t1.sort_id,t1.url,t1.memo,t1.create_time,t1.update_ts,t1.level_id,
       (select count(1) from t_base_menu as t2 where t1.menu_id=t2.parent_id) as is_leaf
from t_base_menu as t1 where parent_id=? order by t1.sort_id
    #end

 -- 获取全部菜单
 #sql("selectAll")
        select t1.menu_id as id,t1.menu_name as name,t1.parent_id as pId,t1.sort_id,t1.url,t1.memo,t1.create_time,t1.update_ts,t1.level_id,
       (select count(1) from t_base_menu as t2 where t1.menu_id=t2.parent_id) as is_leaf
    from t_base_menu as t1  order by t1.sort_id
 #end
  -- 获取所有不为学校提供的数据
  #sql("selectNotForSchool")
    select menu_id from t_base_menu where for_school=0
  #end
  -- 获取所有不为单位提供的数据
  #sql("selectNotForBureau")
    select menu_id from t_base_menu where for_bureau=0
  #end

   --删除指定菜单ID
   #sql("deletePrivilageByMenuId")
        delete from t_base_menu_privilage where menu_id=?
   #end

  -- 根据菜单ID返回这个菜单的可用身份
  #sql("selectIdentityByMenuId")
select identity_id from t_base_menu_privilage where menu_id=?
    #end

  -- 获取指定的节点信息
  #sql("selectSingle")
    select t1.menu_id,t1.menu_name,t1.parent_id,t1.sort_id,t1.url,t1.memo,t1.create_time,t1.update_ts,t1.level_id,t1.for_school,t1.for_bureau,
       (select count(1) from t_base_menu as t2 where t1.menu_id=t2.parent_id) as is_leaf
    from t_base_menu as t1 where t1.menu_id=?
  #end

  -- 通过身份获取有哪些菜单ID
  #sql("selectMenuByIdentityId")
     select menu_id from t_base_menu_privilage where identity_id=?
     union
     select 1
  #end

  -- 查询菜单的ids
  #sql("selectMenuForIds")
    select t1.menu_id,t1.menu_name,t1.parent_id,t1.sort_id,t1.url,t1.memo,t1.create_time,t1.update_ts,t1.level_id,
       (select count(1) from t_base_menu as t2 where t1.menu_id=t2.parent_id) as is_leaf
    from t_base_menu as t1 where t1.menu_id in
                             (
                                 #for(x:ids)
                                     #(for.index == 0 ? "" : ",")  #para(x)
                #end
                                 ) order by t1.sort_id
  #end

    -- 删除指定职务+分管工作的所有菜单，准备重新设置
    #sql("deleteMenuByDutyInCharge")
        delete from t_base_principalship_menu where duties_id=#(duties_id)  and in_charge_id=#(in_charge_id)
    #end

    -- 获取指定职务+分管工作的所有菜单，用于显示
    #sql("selectMenuByDutyInCharge")
        select duties_id,in_charge_id,menu_id from t_base_principalship_menu where duties_id=? and in_charge_id=?
    #end

    -- 人员根据职务+分管工作可见的菜单
    #sql("selectMenuByPersonId")
        select distinct menu_id from t_base_principalship_menu as t1 inner join t_person_duty_charge as t2 on t1.duties_id=t2.`duties_id`
            and t1.in_charge_id=t2.`in_charge_id` where  t2.person_id=? and t2.b_use=1
    #end
#end