from pot_libs.es_util.es_utils import EsUtil
from pot_libs.logger import log
from unify_api.constants import COMPANY_15MIN_POWER, POINT_15MIN_POWER
from unify_api.utils.time_format import convert_es_str
from pot_libs.mysql_util.mysql_util import MysqlUtil

index = COMPANY_15MIN_POWER
index_point = POINT_15MIN_POWER


async def point_day_power_dao(cid, start, end):
    sql = f"""
    SELECT DATE_FORMAT(pp.create_time, '%%Y-%%m-%%d') create_time, 
    SUM(pp.kwh) kwh, SUM(pp.p) p, SUM(pp.charge) charge
     from point_1day_power pp LEFT JOIN point p 
    on pp.pid=p.pid LEFT JOIN monitor m on m.mtid=p.mtid 
    where p.cid=%s and pp.create_time>='{start}'
    and pp.create_time<='{end}' and m.demolished=0 GROUP BY pp.create_time 
    ORDER BY pp.create_time
    """
    async with MysqlUtil() as conn:
        data = await conn.fetchall(sql, args=(cid, ))
    return data


async def get_total_kwh_dao(cid, start, end):
    sql = f"""
        SELECT sum(pp.kwh) total_kwh from point_1day_power pp 
        LEFT JOIN point p ON p.pid=pp.pid LEFT JOIN monitor m 
        on m.mtid=p.mtid where p.cid=%s and pp.create_time>='{start}' 
        and pp.create_time<='{end}' and m.demolished=0
        """
    async with MysqlUtil() as conn:
        total_kwh = await conn.fetchone(sql, args=(cid, ))
    return total_kwh


async def get_kwh_charge(table_name, name, value, start, end):
    sql = f"SELECT sum(kwh) sum_kwh, sum(charge) sum_charge  " \
          f"FROM {table_name} where create_time>='{start}' and " \
          f"create_time<='{end}' and {name} = %s"
    async with MysqlUtil() as conn:
        datas = await conn.fetchone(sql, args=(value, ))
    return datas


async def query_charge_aggs(date_start, date_end, cid_list):
    """
    参数: cid_list, start, end
    查询: 根据参数query, aggs cid, aggs power/charge求sum
    返回: [
        {
          "key" : 32,
          "doc_count" : 44,
          "charge" : {
            "value" : 8658.496337890625
          },
          "kwh" : {
            "value" : 25314.447940826416
          }
        },
        {
          "key" : 44,
          "doc_count" : 43,
          "charge" : {
            "value" : 13868.499267578125
          },
          "kwh" : {
            "value" : 31743.359497070312
          }
        }
      ]
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)

    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "cid": cid_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "cids": {
                "terms": {
                    "field": "cid",
                    "size": 1000
                },
                "aggs": {
                    "kwh": {
                        "sum": {
                            "field": "kwh"
                        }
                    },
                    "charge": {
                        "sum": {
                            "field": "charge"
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index)
    return es_re["aggregations"]["cids"]["buckets"]


async def query_charge_new15(date_start, date_end, cid_list):
    sql = f"""
        select cid,sum(kwh) kwh,sum(charge) charge from company_15min_power
        where cid in %s and create_time BETWEEN %s and %s
        group by cid
    """
    async with MysqlUtil() as conn:
        datas = await conn.fetchall(sql=sql, args=(cid_list, date_start,
                                                   date_end))
    return datas


async def power_charge_p_aggs(date_start, date_end, cid_list, interval):
    """
    date_histogram,
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)

    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "cid": cid_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "quarter_time": {
                "date_histogram": {
                    "field": "quarter_time",
                    "interval": interval,
                    "time_zone": "+08:00",
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
                "aggs": {
                    "kwh": {
                        "sum": {
                            "field": "kwh"
                        }
                    },
                    "charge": {
                        "sum": {
                            "field": "charge"
                        }
                    },
                    "p": {
                        "sum": {
                            "field": "p"
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index)
    return es_re["aggregations"]["quarter_time"]["buckets"]


async def power_charge_p_cid_aggs(date_start, date_end, cid_list, interval):
    """
    excel下载, 按照cid,date_histogram两次聚合,求电量电费
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)

    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "cid": cid_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "cids": {
                "terms": {
                    "field": "cid",
                    "size": 1000
                },
                "aggs": {
                    "quarter_time": {
                        "date_histogram": {
                            "field": "quarter_time",
                            "interval": interval,
                            "time_zone": "+08:00",
                            "format": "yyyy-MM-dd HH:mm:ss"
                        },
                        "aggs": {
                            "kwh": {
                                "sum": {
                                    "field": "kwh"
                                }
                            },
                            "charge": {
                                "sum": {
                                    "field": "charge"
                                }
                            },
                            "p": {
                                "sum": {
                                    "field": "p"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index)
    return es_re["aggregations"]["cids"]["buckets"]


async def query_charge_aggs_points(date_start, date_end, point_list):
    """
    返回: [
        {
          "key" : 32,
          "doc_count" : 44,
          "charge" : {
            "value" : 8658.496337890625
          },
          "kwh" : {
            "value" : 25314.447940826416
          }
        },
        {
          "key" : 44,
          "doc_count" : 43,
          "charge" : {
            "value" : 13868.499267578125
          },
          "kwh" : {
            "value" : 31743.359497070312
          }
        }
      ]
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)

    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "pid": point_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "points": {
                "terms": {
                    "field": "pid",
                    "size": 10000
                },
                "aggs": {
                    "kwh": {
                        "sum": {
                            "field": "kwh"
                        }
                    },
                    "charge": {
                        "sum": {
                            "field": "charge"
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index_point)
    return es_re["aggregations"]["points"]["buckets"]


async def query_charge_aggs_points_new15(start, end, point_list):
    sql = f"SELECT pid,sum(kwh) kwh,SUM(charge) charge " \
          f"FROM `point_15min_power` " \
          f"where pid in %s and create_time BETWEEN '{start}' and '{end}' " \
          f"GROUP BY pid"
    async with MysqlUtil() as conn:
        datas = await conn.fetchall(sql, args=(point_list, ))
    return datas


async def histogram_aggs_points(date_start, date_end, point_list, interval):
    """date_histogram"""
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)

    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "pid": point_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "quarter_time": {
                "date_histogram": {
                    "field": "quarter_time",
                    "interval": interval,
                    "time_zone": "+08:00",
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
                "aggs": {
                    "kwh": {
                        "sum": {
                            "field": "kwh"
                        }
                    },
                    "charge": {
                        "sum": {
                            "field": "charge"
                        }
                    },
                    "p": {
                        "sum": {
                            "field": "p"
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index_point)
    return es_re["aggregations"]["quarter_time"]["buckets"]


async def power_charge_p_point_aggs(date_start, date_end, pid_list, interval):
    """
    excel下载, 按照pid,date_histogram两次聚合,求电量电费
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)

    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "pid": pid_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "pids": {
                "terms": {
                    "field": "pid",
                    "size": 10000
                },
                "aggs": {
                    "quarter_time": {
                        "date_histogram": {
                            "field": "quarter_time",
                            "interval": interval,
                            "time_zone": "+08:00",
                            "format": "yyyy-MM-dd HH:mm:ss"
                        },
                        "aggs": {
                            "kwh": {
                                "sum": {
                                    "field": "kwh"
                                }
                            },
                            "charge": {
                                "sum": {
                                    "field": "charge"
                                }
                            },
                            "p": {
                                "sum": {
                                    "field": "p"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index_point)
    return es_re["aggregations"]["pids"]["buckets"]


async def point_aggs_kwh(point_list, start=None, end=None):
    """根据pid,求电量电费"""
    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "pid": point_list
                        }
                    }
                ]
            }
        },
        "aggs": {
            "kwh": {
                "sum": {
                    "field": "kwh"
                }
            },
            "charge": {
                "sum": {
                    "field": "charge"
                }
            }
        }
    }
    if start and end:
        start_es = convert_es_str(start)
        end_es = convert_es_str(end)
        query_body["query"]["bool"]["must"].append(
            {
                "range": {
                    "datetime": {
                        "gte": start_es,
                        "lte": end_es
                    }
                }
            }
        )
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index_point)
    return es_re


async def point_aggs_kwh_new15(point_list, start=None, end=None):
    """1.5版本根据pid,求电量电费"""
    if start and end:
        sql = f"SELECT sum(kwh) kwh, sum(charge) charge FROM " \
              f"`point_15min_power` " \
              f"where pid in %s and create_time BETWEEN {start} and {end}"
    else:
        sql = "SELECT sum(kwh) kwh,sum(charge) charge FROM point_15min_power" \
              " where pid in %s"
    async with MysqlUtil() as conn:
        data = await conn.fetchone(sql, args=(point_list, ))
    return data


async def extended_bounds_agg(date_start, date_end, cid_list, interval):
    """
    date_histogram,
    """
    start_es = convert_es_str(date_start)
    end_es = convert_es_str(date_end)

    query_body = {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "cid": cid_list
                        }
                    },
                    {
                        "range": {
                            "quarter_time": {
                                "gte": start_es,
                                "lte": end_es
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "quarter_time": {
                "date_histogram": {
                    "field": "quarter_time",
                    "interval": interval,
                    "time_zone": "+08:00",
                    "format": "yyyy-MM-dd HH:mm:ss",
                    "extended_bounds": {
                        "min": date_start,
                        "max": date_end
                    }
                },
                "aggs": {
                    "kwh": {
                        "sum": {
                            "field": "kwh"
                        }
                    },
                    "charge": {
                        "sum": {
                            "field": "charge"
                        }
                    },
                    "p": {
                        "sum": {
                            "field": "p"
                        }
                    }
                }
            }
        }
    }
    log.info(query_body)
    async with EsUtil() as es:
        es_re = await es.search_origin(body=query_body, index=index)
    return es_re["aggregations"]["quarter_time"]["buckets"]
