# coding:utf-8
import json

from django.http import HttpResponse
from django.shortcuts import render

from aircraft_config import AC_WQAR_CONFIG
from list2string_and_echarts_function import Echarts_option
from list2string_and_echarts_function import LIST_to_STR
from influxdb_function import influxDB_interface
from main_web.models import Stencil
from arrow_time import today_date_for_influxd_sql
from arrow_time import ten_day_ago_for_influxd_sql
from arrow_time import three_day_ago_for_influxd_sql

def all_childtable_index_list(request):
    if request.method == 'POST':
        post_data = request.POST
        date_range = post_data["date_range"]
        date_start = date_range.split(' to ')[0]
        date_end = date_range.split(' to ')[1]

    else:
        date_start = today_date_for_influxd_sql()
        date_end = today_date_for_influxd_sql()

    where_str = " WHERE time > " + "'" + date_start + "'" + " - 8h" + " AND time < " + "'" + date_end + "'" + " + 16h"
    infdb_if = influxDB_interface()
    sector_index = infdb_if.inf_query("DB_sector_index", "*", "index", where_str)
    if sector_index <> {}:
        df = sector_index['index']
        result_json = df.to_json(orient="records")
        return render(request, 'all_childtable_index_list.html', {'result_json': result_json,
                                                                  'date_start': date_start,
                                                                  'date_end': date_end})
    else:
        return render(request, 'all_childtable_index_list.html', {'date_start': date_start,
                                                                  'date_end': date_end + "  no data"})

# 该页面未使用
def query_index(request):
    if request.method == 'POST':
        post_data = request.POST
        if post_data["date_start"]=='' or post_data["date_end"] == '':
            date_start = ten_day_ago_for_influxd_sql()
            date_end = today_date_for_influxd_sql()
        else:
            date_start = post_data["date_start"]
            date_end = post_data["date_end"]
        AC_id = post_data["AC_id"]
        where_str = " WHERE time > " + "'" + date_start + "'" + " AND time < " + "'" + date_end + "'" + " + 1d" + " AND AC=" + "'" + AC_id + "'"
        infdb_if = influxDB_interface()
        sector_index = infdb_if.inf_query("DB_sector_index", "*", "index", where_str)
        if sector_index <> {}:
            df = sector_index['index']
            result_json = df.to_json(orient="records")
            return render(request, 'single_plane_query.html', {'result_json': result_json,
                                                               'date_start': date_start,
                                                               'date_end': date_end,
                                                               'AC_id': AC_id})
        else:
            return render(request, 'single_plane_query.html', {'date_start': date_start,
                                                               'date_end': date_end + "  no data",
                                                               'AC_id': AC_id})
    else:
        return render(request, 'single_plane_query.html')

def runup_list(request):
    if request.method == 'POST':
        post_data = request.POST
        date_range = post_data["date_range"]
        date_start = date_range.split(' to ')[0]
        date_end = date_range.split(' to ')[1]

    else:
        date_start = ten_day_ago_for_influxd_sql()
        date_end = today_date_for_influxd_sql()

    where_str = " WHERE time > " + "'" + date_start + "'" + " - 8h" + " AND time < " + "'" + date_end + "'" + " + 16h"
    where_str = where_str + " AND FLT_status='GROUND'"
    infdb_if = influxDB_interface()
    sector_index = infdb_if.inf_query("DB_sector_index", "*", "index", where_str)
    df = sector_index['index']
    result_json = df.to_json(orient="records")
    return render(request, 'all_childtable_index_list.html',{'result_json': result_json})

def tendency_total(request):
    if request.method == 'POST':
        post_data = request.POST
        date_range = post_data["date_range"]
        date_start = date_range.split(' to ')[0]
        date_end = date_range.split(' to ')[1]
        tendency_type = post_data["tendency_type"]

        where_str = " WHERE time > " + "'" + date_start + "'" + " AND time < " + "'" + date_end + "'" + " + 1d"
        infdb_if = influxDB_interface()
        sector_index = infdb_if.inf_query("tendency", "*", tendency_type, where_str)
        if sector_index <> {}:
            df = sector_index[tendency_type]
            result_json = df.to_json(orient="records")
            return render(request, 'tendency_total.html', {'result_json': result_json,
                                                               'date_start': date_start,
                                                               'date_end': date_end,
                                                               })
        else:
            return render(request, 'tendency_total.html', {'date_start': date_start,
                                                               'date_end': date_end + "  no data",
                                                               })
    else:
        date_start = ten_day_ago_for_influxd_sql()
        date_end = today_date_for_influxd_sql()
        where_str = " WHERE time > " + "'" + date_start + "'" + " AND time < " + "'" + date_end + "'" + " + 1d"
        infdb_if = influxDB_interface()
        sector_index = infdb_if.inf_query("tendency", "*", "tendency_total", where_str)
        df = sector_index['tendency_total']
        result_json = df.to_json(orient="records")
        return render(request, 'tendency_total.html', {'result_json': result_json,
                                                                  'date_start': date_start,
                                                                  'date_end': date_end})

def tendency_single(request):
    all_aircraft_list = json.dumps(AC_WQAR_CONFIG().all_aircraft())
    if request.method == 'POST':
        post_data = request.POST
        date_range = post_data["date_range"]
        date_start = date_range.split(' to ')[0]
        
date_end = date_range.split(' to ')[1]
        para_name = post_data["para_name"]
        para_name_sed = "\"" + para_name + "\""
        exclude_list = ["AC","AC_sector","FLT_number","FLT_status","update_date","update_time"]
        exclude_list = map(lambda x:"\"" + x + "\"", exclude_list)
        exclude_list.append(para_name_sed)
        query_para = ",".join(exclude_list)
        print query_para
        AC_id = post_data
["AC_id"]
        AC_id_sed = "\'" + AC_id + "\'"
        qar_conf = AC_WQAR_CONFIG().juge_config(AC_id)
        if qar_conf == "737_7":
            tendency_type = "tendency_737_7"
        elif qar_conf == "737_3C":
            tendency_type = "tendency_737_3C"
        where_str = " WHERE AC = " + AC_id_sed + " AND " + "time > " + "'" + date_start + "'" + " AND time < " + "'" + date_end + "'" + " + 1d"
        infdb_if = influxDB_interface()
        sector_index = infdb_if.inf_query("tendency", query_para, tendency_type, where_str)
        if sector_index <> {}:
            df = sector_index[tendency_type]
            result_json = df.to_json(orient="records")
            return render(request, 'tendency_single.html', {'all_ac':all_aircraft_list,
                                                            'result_json': result_json,
                                                               'date_start': date_start,
                                                               'date_end': date_end,
                                                               })
        else:
            return render(request, 'tendency_single.html', {'all_ac':all_aircraft_list,
                                                            'date_start': date_start,
                                                               'date_end': date_end + "  no data",
                                                               })
    else:
        return render(request, 'tendency_single.html', {'all_ac':all_ai