You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.6 KiB
Python

from website.handler import BaseHandler
from sqlalchemy import text
from typing import Any
from website.db_mysql import get_session, get_async_session, to_json, to_json_list
import json
# 获取企业模型数量
def get_enterprise_model_count(id: int) -> int:
return 0
# 获取企业设备数量
def get_enterprise_device_count(id: int) -> int:
return 0
# 获取所有企业实体数量
def get_enterprise_entity_count(engine: Any) -> int:
with engine.connect() as conn:
count_sql_text = "select count(*) from enterprise "
count = conn.execute(text(count_sql_text)).fetchone()
if count:
return count[0]
return 0
# 获取所有企业模型数量
def get_enterprise_model_count(entity_id: int = 0, entity_suid: str = "") -> int:
with get_session() as session:
sql = "select base_models from enterprise_busi_model where "
param = {}
if entity_id:
sql += "entity_id = :entity_id"
param["entity_id"] = entity_id
elif entity_suid:
sql += "entity_suid = :entity_suid"
param["entity_suid"] = entity_suid
cur = session.execute(text(sql), param)
res = to_json(cur)
if res:
base_model_list = json.loads(res)
return len(base_model_list)
return 0
# 获取所有企业设备数量
def get_enterprise_device_count(entity_id: int = 0, entity_suid: str = "") -> int:
with get_session() as session:
sql = "select count(id) as device_count from enterprise_device "
param = {}
if entity_id:
sql += "where entity_id = :entity_id"
param = {"entity_id": entity_id}
elif entity_suid:
sql += "where entity_suid = :entity_suid"
param = {"entity_suid": entity_suid}
cur = session.execute(text(sql), param)
res = to_json(cur)
if res:
return res["device_count"]
return 0
async def get_enterprise_model_and_device_count(
entity_id: int = 0, entity_suid: str = ""
) -> (int, int):
model_count = 0
device_count = 0
async with get_async_session() as session:
# sql_model = "select base_models from enterprise_busi_model where "
# param_model = {}
# if entity_id:
# sql_model += "entity_id = :entity_id"
# param_model["entity_id"] = entity_id
# elif entity_suid:
# sql_model += "entity_suid = :entity_suid"
# param_model["entity_suid"] = entity_suid
# cur_model = await session.execute(text(sql_model), param_model)
# res_model = to_json(cur_model)
# if res_model:
# base_model_list = json.loads(res_model)
# model_count = len(base_model_list)
# sql_device = "select count(id) as device_count from enterprise_device where "
# param_device = {}
# if entity_id:
# sql_device += "entity_id = :entity_id"
# param_device["entity_id"] = entity_id
# elif entity_suid:
# sql_device += "where entity_suid = :entity_suid"
# param_device = {"entity_suid": entity_suid}
# cur_device = await session.execute(text(sql_device), param_device)
# res_device = to_json(cur_device)
# if res_device:
# device_count = res_device["device_count"]
sql_device_count = "SELECT COUNT(*) AS device_count FROM enterprise_device WHERE {where_clause} "
sql_base_model = "SELECT base_models FROM enterprise_busi_model WHERE {where_clause} "
where_clause = ""
params = {}
if entity_id:
where_clause = "entity_id = :entity_id"
params["entity_id"] = entity_id
elif entity_suid:
where_clause = "entity_suid = :entity_suid"
params["entity_suid"] = entity_suid
sql_device_count = sql_device_count.format(where_clause=where_clause)
result_device_count = await session.execute(text(sql_device_count), params)
device_count = to_json(result_device_count)["device_count"]
sql_base_model = sql_base_model.format(where_clause=where_clause)
result_base_model = await session.execute(text(sql_base_model), params)
base_models = to_json_list(result_base_model)
base_model_ids = []
for item in base_models:
base_models = json.loads(item["base_models"])
for base_model in base_models:
if base_model["id"] not in base_model_ids:
base_model_ids.append(base_model["id"])
model_count = len(base_model_ids)
return model_count, device_count