Sy2Fl.md 1.8 KB

系统所在楼层

前置条件

1. 系统下有设备
2. 系统下的设备有所属楼层关系

依赖函数

Eq2Fl

计算流程

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');