from pot_libs.mysql_util.mysql_util import MysqlUtil


async def get_sum_by_interval(table_name, time_format, start, end, sql_mid,
                              cid_list, point_id):
    sql = f"""SELECT SUM(p) p,sum(charge) charge,sum(kwh) kwh,
    DATE_FORMAT(create_time, '{time_format}') time_interval FROM 
    {table_name} where {sql_mid} and create_time >="{start}" and 
    create_time <="{end}" GROUP BY time_interval having sum(kwh) > 0
    """
    async with MysqlUtil() as conn:
        if point_id == -1:
            data = await conn.fetchall(sql, args=(cid_list,))
        else:
            data = await conn.fetchall(sql, args=(point_id,))
    return data


async def get_power_charge_dao(table_name, start, end, sql_mid):
    sql = f'SELECT * from {table_name} where {sql_mid} and ' \
          f'create_time >="{start}" and create_time<="{end}"'
    async with MysqlUtil() as conn:
        data = await conn.fetchall(sql)
    return data
