浏览代码

系统所在楼层完成

jxing 5 年之前
父节点
当前提交
0970c05cb1
共有 2 个文件被更改,包括 48 次插入1 次删除
  1. 2 1
      datacenter/docs/relation_calc/Sy2Bd.md
  2. 46 0
      datacenter/docs/relation_calc/Sy2Fl.md

+ 2 - 1
datacenter/docs/relation_calc/Sy2Bd.md

@@ -4,6 +4,7 @@
     2. 系统下的设备有所属建筑的关系
 ## 计算流程
     1. 通过 系统 --> 设备 --> 建筑 的间接关系, 获取系统所在建筑的关系
+    2. 结果存储在 r_sy_in_bd 表中
     
 ## 函数
 ~~~
@@ -16,7 +17,7 @@ try:
     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", ["text"])
+        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:

+ 46 - 0
datacenter/docs/relation_calc/Sy2Fl.md

@@ -0,0 +1,46 @@
+系统所在楼层
+## 前置条件
+    1. 系统下有设备
+    2. 系统下的设备有所属楼层关系
+## 计算流程
+    1. 通过 系统 --> 设备 --> 楼层 的间接关系, 获取系统所在楼层的关系
+    2. 计算关系存储在表 r_sy_in_fl 中
+## 函数
+~~~
+-- 系统所在楼层
+create or replace function public.rel_sy2fl(project_id character varying) returns boolean
+as
+$$
+try:
+    # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
+    with plpy.subtransaction():
+        delete_plan = plpy.prepare("delete from r_sy_in_fl where project_id = $1 and sign = 2", ["text"])
+        delete_plan.execute([project_id])
+        join_plan = plpy.prepare("select sy.sys_id, eq.building_id, eq.floor_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.floor_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']
+            floor = row['floor_id']
+            if sys not in sy2bd:
+                sy2bd[sys] = dict()
+            sys_dict = sy2bd[sys]
+            sys_dict[floor] = building
+        for sid, sys_dict in sy2bd.items():
+            for floor, building in sys_dict.items():
+                plan = plpy.prepare("insert into r_sy_in_fl(sys_id, floor_id, building_id, project_id, sign) values($1, $2, $3, $4, 2)", ["text", "text", "text", "text"])
+                plan.execute([sid, floor, building, project_id])
+except Exception as e:
+    plpy.warning(e)
+    return False
+else:
+    return True
+$$
+LANGUAGE 'plpython3u' VOLATILE;
+~~~
+
+## 入参
+    1. 项目id
+## 例子
+    select public.rel_sy2fl('Pj1101010015');