管网系统确定流向
## 前置条件

1. 通过管网系统分块计算；
2. 指定源。

## 处理方式

1. 从connected_block表中取出所有关系，构建无向图
2. 从connected_block_source表中取出所有的源
3. 由图算法确定流向，构建有方向的图
4. 更新connected_block表


# 函数
```
create or replace function public.sys_direction(project_id character varying,building_id character varying,block_id character varying, system_name character varying,domain character varying,is_source boolean default true) returns boolean
as
$$
from relations.src.system_relation import calc_flowdirection, systemdatautils,systemutils

def get_connected_block_data(project_id,building_id,block_id,system_name,domain):
  sql ="select * from revit_calc.connected_block " \
		  "where project_id=$1" \
		  "and block_id=$2" \
		  "and mep_system_type=$3" \
		  "and domain=$4" \
		  "and ($5 is null or building_id=$5)" \
		  "order by cast(depth as int) asc"
  join_plan=plpy.prepare(sql,["text","text","text","text","text"])
  data=join_plan.execute([project_id,block_id,system_name,domain,building_id])
  objs=systemutils.sqldata2objlist(data)
  return objs

def get_connected_block_source_data(project_id,building_id,block_id,system_name,domain,is_source):
  sql = "select * from revit_calc.connected_block_source " \
		  "where project_id=$1" \
		  "and block_id=$2" \
		  "and mep_system_type=$3" \
		  "and domain=$4" \
		  "and ($5 is null or building_id=$5)" \
		  "and is_source=$6"
  join_plan=plpy.prepare(sql,["text","text","text","text","text","boolean"])
  data=join_plan.execute([project_id,block_id,system_name,domain,building_id,is_source])
  objs=systemutils.sqldata2objlist(data)
  return objs

def update_connected_block_data(block_data):
  direction=block_data.direction
  depth=block_data.depth
  id=block_data.id
  sql="update revit_calc.connected_block set direction=$1,depth=$2 where id=$3"
  join_plan=plpy.prepare(sql,['int','int','int'])
  join_plan.execute([direction,depth,id])

try:
  # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
  with plpy.subtransaction():
    block_datas=get_connected_block_data(project_id,building_id, block_id,system_name,domain)
    source_datas=get_connected_block_source_data(project_id,building_id, block_id,system_name,domain,is_source)
    block_datas= calc_flowdirection.calc(block_datas, source_datas)
    for block_data in block_datas:
      update_connected_block_data(block_data)
except Exception as e:
    plpy.warning(e)
    return False
else:
    return True
$$
LANGUAGE 'plpython3u' VOLATILE;

select public.sys_direction('Pj1101010015','Bd11010100153c05821ed8fd11e9b8f2d79d0a5b4bf6','0','冷冻水供水管','DomainPiping',true)
```

## 输入
    1. 项目id
    2. 需要计算的building_id，如果没有提供时请传入null，不能传""
    3. 需要计算的块id    
    4. 需要计算的系统名称
    5. 需要计算的管道系统类型，枚举（水管或风管）：DomainPiping，DomainHvac    
    6. 跟据源或端计算：bool。true表示源，false表示端
## 返回结果
    True    成功
    False   失败
    