# 系统所在竖井
## 前置条件
    1. 系统下有设备
    2. 系统下的设备有所在竖井关系
## 依赖函数
    Eq2Sh
## 处理逻辑
    1. 根据 系统 --> 设备 --> 竖井 来间接获取系统所属竖井关系
    2. 计算后的关系存储在表 r_sy_in_sh (先删除以前的自动计算结果)
## 函数

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

```
-- 系统所在竖井
create or replace function public.rel_sy2sh(project_id character varying) returns boolean
as
$$
try:
    # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
    with plpy.subtransaction():
        delete_plan = plpy.prepare("delete from r_sy_in_sh where project_id = $1 and sign = 2", ["text"])
        delete_plan.execute([project_id])
        join_plan = plpy.prepare("select sy.sys_id, rel.shaft_id from r_sy_eq sy inner join r_eq_in_sh rel on sy.equip_id = rel.equip_id where rel.project_id = $1 and sy.project_id = $1", ["text"])
        rel = join_plan.execute([project_id])
        sy2sh = dict()
        for row in rel:
            sys = row['sys_id']
            shaft = row['shaft_id']
            if sys not in sy2sh:
                sy2sh[sys] = set()
            shaft_set = sy2sh[sys]
            shaft_set.add(shaft)
        for sid, shaft_set in sy2sh.items():
            for shaft in shaft_set:
                plan = plpy.prepare("insert into r_sy_in_sh(sys_id, shaft_id, project_id, sign) values($1, $2, $3, 2)", ["text", "text", "text"])
                plan.execute([sid, shaft, project_id])
except Exception as e:
    plpy.warning(e)
    return False
else:
    return True
$$
LANGUAGE 'plpython3u' VOLATILE;
```

</details>

## 输入
    1. 项目id

## 例子
    select public.rel_sy2sh('Pj1101010015');