-- 组织机构命名空间
#namespace("organization")

  -- 获取组织机构的最大主键号
  #sql("getMaxPkByOrg")
    select ifnull(max(org_pk_num),0) as org_pk_num from t_base_organization
  #end
  -- 获取指定单位或组织机构的信息
  #sql("getOrgInfoById")
    select org_id,org_code,org_name,parent_id,org_type_id,school_type_id,sort_id,create_time,department_type_id,
            update_ts,b_use,org_pk_num,ifnull(city_id, -1) as city_id,ifnull(area_id, -1) as area_id,
            ifnull(main_school_id, -1) as main_school_id,bureau_id,property_id,level_id
    from t_base_organization where org_id=? and b_use=1
  #end

  -- 判断一个组织机构号是不是单位
  #sql("IsBureau")
    select count(1) as c from t_base_organization where org_id=? and org_id=bureau_id
  #end


  -- 根据单位号获取下面正常部门的个数
  #sql("getOrgCountByBureauId")
    select count(1) as c from t_base_organization where bureau_id=? and org_id<>? and b_use=1
  #end


  -- 删除一个机构
  #sql("deleteOrgById")
    update t_base_organization set b_use=0,operator=?,ip_address=? where org_id=?
  #end

  -- 判断此ORG_CODE是不是存在
  #sql("getOrgCodeCount")
    select count(1) as c from t_base_organization where org_code=? and b_use=1
  #end

  -- 判断此ORG_CODE是不是存在(排除自己用于修改)
  #sql("getOrgCodeCountExceptSelf")
    select count(1) as c from t_base_organization where org_code=? and org_id!=? and b_use=1
  #end

 -- 获取部门列表
  #sql("getOrgList")
    select * from t_base_organization where parent_id=#para(0) and b_use=1 order by sort_id
  #end

  -- 获取学校列表
   #sql("getSchoolList")
    select t1.org_id,t1.org_code,t1.org_name,t1.parent_id,t1.org_type_id,t1.school_type_id,t1.sort_id,
    t1.create_time,t1.update_ts,t1.b_use,t1.org_pk_num,t1.city_id,t1.area_id,t1.main_school_id,t1.bureau_id,
    (select t2.school_type_name from t_dm_schooltype as t2 where t2.school_type_id=t1.school_type_id) as school_type_name
      from t_base_organization as t1 where t1.parent_id=#para(0) and t1.b_use=1 and t1.school_type_id>0
  #end

   -- 获取学校列表(指定学校类型)
   #sql("getSchoolListBySchoolType")
    select t1.org_id,t1.org_code,t1.org_name,t1.parent_id,t1.org_type_id,
    t1.school_type_id,t1.sort_id,t1.create_time,t1.update_ts,t1.b_use,
    t1.org_pk_num,t1.city_id,t1.area_id,t1.main_school_id,t1.bureau_id,
    (select t2.school_type_name from t_dm_schooltype as t2 where t2.school_type_id=t1.school_type_id) as school_type_name
     from t_base_organization as t1 where t1.parent_id=#para(0) and  t1.b_use=1 and t1.school_type_id =#para(1)
  #end


  -- 获取单位列表
   #sql("getBureauList")
    select t1.org_id,t1.org_code,t1.org_name,t1.parent_id,t1.org_type_id,
     (select t2.name from t_base_org_type_principalship as t2 WHERE t2.id=t1.org_type_id) as org_type_name,
    t1.school_type_id,t1.sort_id,t1.create_time,
    t1.update_ts,t1.b_use,t1.org_pk_num,t1.city_id,t1.area_id,t1.main_school_id,t1.bureau_id
     from t_base_organization as t1 where t1.parent_id=#para(0) and  t1.school_type_id<=0 and t1.b_use=1 order by t1.sort_id,t1.update_ts
  #end

  -- 获取单位列表(指定单位类型)
  #sql("getBureauListByBureauType")
    select t1.org_id,t1.org_code,t1.org_name,t1.parent_id,t1.org_type_id,
     (select t2.name from t_base_org_type_principalship as t2 WHERE t2.id=t1.org_type_id) as org_type_name,
    t1.school_type_id,t1.sort_id,t1.create_time,
    t1.update_ts,t1.b_use,t1.org_pk_num,t1.city_id,t1.area_id,t1.main_school_id,t1.bureau_id
     from t_base_organization as t1 where t1.parent_id=#para(0) and  t1.school_type_id<=0 and t1.org_type_id = #para(1)
     and t1.b_use=1 order by t1.sort_id,t1.update_ts
  #end

  -- 组织机构部门树
  #sql("getOrgTreeByBureauId")
    select org_id,org_name,(case when bureau_id=org_id then '-1' else parent_id end) as parent_id,bureau_id,school_type_id,department_type_id from t_base_organization where bureau_id=?
      and b_use=1 order by sort_id,update_ts
  #end

  -- 传入一个区域ID，获取它下面的学校
  #sql("getSchoolListByAreaId")
    select org_id,org_code,org_name from t_base_organization  where (city_id=#para(0) or area_id=#para(1)) and b_use=1 and is_bureau=1
      and org_name like #para(2) and school_type_id>0 order by area_id,sort_id
  #end
  -- 传入一个区域ID，获取它下面的单位
  #sql("getBureauListByAreaId")
    select org_id,org_code,org_name from t_base_organization  where (city_id=#para(0) or area_id=#para(1)) and b_use=1 and is_bureau=1
      and org_name like #para(2)  and school_type_id<=0 order by area_id,sort_id
  #end
  -- 传入一个区域ID，获取它下面全部
  #sql("getAllListByAreaId")
    select org_id,org_code,org_name from t_base_organization  where (city_id=#para(0) or area_id=#para(1)) and b_use=1 and is_bureau=1
      and org_name like #para(2)  order by area_id,sort_id
  #end

  --传入一个单位ID，获取它的一级部门有哪些
  #sql("getFirstLevelOrgName")
      select org_id,org_name from t_base_organization where bureau_id=? and parent_id=? and b_use=1
  #end

  #sql("getZyClassSelectTreeLevel1")
    select t1.org_id,t1.org_name,t1.parent_id,(select count(1) from t_base_organization as t2 where t1.parent_id=t2.org_id) as cnt
    from t_base_organization as t1 where t1.bureau_id='#(bureau_id)'
    and  ((t1.department_type_id =1) or (t1.department_type_id =2 and t1.parent_id='#(bureau_id)'))
  #end

  #sql("getZyClassSelectTreeLevel2")
    select org_id,org_name,parent_id,0 as cnt from t_base_organization where bureau_id='#(bureau_id)'
    and  (department_type_id =2 and parent_id<>'#(bureau_id)')
  #end
#end