from pot_libs.settings import SETTING
from unify_api.modules.common.service.td_engine_service import \
    get_td_engine_data
from unify_api.utils.taos_new import parse_td_columns


async def elec_current_data(mtids, cid):
    res_map = {}
    url = f"{SETTING.stb_url}db_electric?tz=Asia/Shanghai"
    sql = f"""
            select tbname,last_row(*) from electric_stb
            where cpyid={cid}
            group by tbname
        """
    is_succ, results = await get_td_engine_data(url, sql)
    if is_succ:
        head = parse_td_columns(results)
        for res in results["data"]:
            data = dict(zip(head, res))
            if data["mtid"] in mtids:
                res_map[data["mtid"]] = data
    return res_map
