-- 应用接入命名空间
#namespace("app")
  -- 根据appid获取app信息
  #sql("getAppInfoByAppid")
    select t1.appid,t1.appkey,t1.appname,t1.sort_id,t1.system_type_id,t1.create_time,t1.update_ts,t1.bureau_id,t1.developer,
    (select t2.org_name from t_base_organization as t2 where t1.bureau_id=t2.org_id) as bureau_name,
    (select t2.system_type_name from t_dm_integrated_system_type as t2 where t2.system_type_id=t1.system_type_id) as system_type_name,
    t1.is_system from t_base_app as t1  where appid=?
  #end

  -- 获取应用系统的列表
  #sql("getAppList")
    select t1.appid,t1.appkey,t1.appname,t1.sort_id,t1.system_type_id,t1.create_time,t1.update_ts,bureau_id,developer,
    is_system,(select t2.system_type_name from t_dm_integrated_system_type as t2 where t2.system_type_id=t1.system_type_id)
    as system_type_name,
    (case when (select count(*) as c from t_integrated_system as t2 where t2.appid=t1.appid)>0 then 1 else 0 end)
    as integrated_exist,
    (case when (select count(*) as c from t_datashare_publish as t2 where t2.appid=t1.appid)>0 then 1 else 0 end)
    as publish_exist,
    (case when (select count(*) as c from t_datashare_subscribe as t2 where t2.appid=t1.appid)>0 then 1 else 0 end)
    as subscribe_exist,
    t1.b_use from t_base_app as t1 order by t1.sort_id
  #end

    -- 获取集成系统的系统类型
  #sql("getIntegratedSystemType")
      select system_type_id,system_type_name from t_dm_integrated_system_type
  #end

  -- 通过appid 获取app的info信息
  #sql("getAppInfo")
      select appid,appkey,appname,sort_id,system_type_id,create_time,update_ts,bureau_id,developer,is_system from t_base_app
      where appid=?
  #end

  -- 设置app为禁用或启用
  #sql("changeAppStatus")
    update t_base_app set b_use=ABS(b_use-1) where appid=?
  #end

  -- 通过appid清空它的发布系统配置
  #sql("deletePublishByAppid")
    delete from t_datashare_publish where appid=?
  #end

  -- 通过appid清空它的订阅系统配置
  #sql("deleteSubscribeByAppid")
    delete from t_datashare_subscribe where appid=?
  #end

  -- 删除一个集成的系统(可视范围表)
  #sql("deleteVisiableByAppid")
    delete from t_base_app_visiable where appid=?
  #end

    --按系统获取系统与可视范围的关系
  #sql("appid_visible")
    select t1.id,t1.appid,t1.city_id,t1.area_id,t1.bureau_id,t1.level_id,t1.update_ts,
          (select t2.org_name from t_base_organization as t2 where t1.bureau_id=t2.org_id) as bureau_name
     from t_base_app_visiable as t1 where appid=?
  #end
#end