# 系统所在建筑
## 前置条件
    1. 系统下有设备
    2. 系统下的设备有所属建筑的关系
## 依赖函数
    Eq2Bd
## 处理逻辑
    1. 通过 系统 --> 设备 --> 建筑 的间接关系, 获取系统所在建筑的关系
    2. 结果存储在 r_sy_in_bd 表中 (先删除以前的自动计算结果)
    
## 函数

<details>
<summary>源码</summary>

~~~
-- 系统所在建筑
CREATE OR REPLACE FUNCTION "public"."rel_sy2bd"("project_id" varchar)
  RETURNS "pg_catalog"."bool" AS $BODY$
try:
    # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
    with plpy.subtransaction():
        delete_plan = plpy.prepare("delete from r_sy_in_bd where project_id = $1 and sign = 2", ["text"])
        delete_plan.execute([project_id])
        join_plan = plpy.prepare("select sy.sys_id, eq.building_id from r_sy_eq sy inner join equipment eq on sy.equip_id = eq.id where eq.project_id = $1 and sy.project_id = $1 and eq.building_id is not null", ["text"])
        rel = join_plan.execute([project_id])
        sy2bd = dict()
        for row in rel:
            sys = row['sys_id']
            building = row['building_id']
            if sys not in sy2bd:
                sy2bd[sys] = set()
            sys_set = sy2bd[sys]
            sys_set.add(building)
        for sid, sys_set in sy2bd.items():
            for building in sys_set:
                try:
                    plan = plpy.prepare("insert into r_sy_in_bd(sys_id, building_id, project_id, sign) values($1, $2, $3, 2)", ["text", "text", "text"])
                    plan.execute([sid, building, project_id])
                except Exception as ex:
                    pass
except Exception as e:
    plpy.warning(e)
    return False
else:
    return True
$BODY$
  LANGUAGE plpython3u VOLATILE
  COST 100
~~~

</details>

## 入参
    1. 项目id
## 例子
    select public.rel_sy2bd('Pj1101010015');