#coding=utf-8
# 计算里程
from math import radians, cos, sin, asin, sqrt
import pandas as pd
import numpy as np
from datetime import timedelta
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from GpsRank import *
from ProcessDfBms import *
from ProcessDfGps import *
from LIB.MIDDLE.odo.CalDist import *
from LIB.BACKEND import DBManager, Log
import pdb
from urllib import parse
import traceback
import os
import time, datetime
import pymysql
import dateutil.relativedelta

if __name__ == "__main__":
    
    # 时间设置
    now_time = datetime.datetime.now()
    pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)
    end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
    start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
    
    # end_time = "2021-08-13 00:00:00"
    # start_time = "2019-01-01 00:00:00"
    
    # start_hour='00:00:00'#每日查询最早时间
    # end_hour='23:59:00'#每日查询最晚时间
    
    # 更新sn列表
    host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
    port=3306
    db='qixiang_oss'
    user='qixiang_oss'
    password='Qixiang2021'
    conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
    cursor = conn.cursor()
    cursor.execute("select sn, imei from app_device")
    res = cursor.fetchall()
    df_sn = pd.DataFrame(res, columns=['sn', 'imei'])
    df_sn = df_sn.sort_values(['sn'])
    df_sn = df_sn.reset_index(drop=True)
    conn.close();
    
    # 结果数据库配置
    host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
    port = 3306
    user = 'qx_cas'
    password = parse.quote_plus('Qx@123456')
    database = 'qx_cas'

    db_engine = create_engine(
        "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
            user, password, host, port, database
        ))
    DbSession = sessionmaker(bind=db_engine)
    
    # 日志配置
    now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
    log_path = 'log/' + now_str
    if not os.path.exists(log_path):
        os.makedirs(log_path)
    log = Log.Mylog(log_name='odo', log_level = 'info')
    log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
    log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
    logger = log.get_logger()

    logger.info("pid is + {}".format(os.getpid()))

    # date_index=pd.date_range(start_time[:10], end_time[:10])
    # for date in date_index:
    #     '''遍历日期'''
    input_starttime = start_time
    input_endtime = end_time

    drive_info_aday=pd.DataFrame()

    for SN in df_sn['sn'].tolist()[1200:]:
        '''遍历SN号'''
        SN=SN.strip('\t')
        SN=SN.strip('\n')
        try:
            logger.info("pid-{} {}---{} START!".format(os.getpid(), SN, str(input_starttime)))
            range=GetDistInfo(SN,input_starttime,input_endtime)
            range_df=pd.DataFrame([range])
            drive_info_aday=pd.concat([drive_info_aday,range_df],axis=0)
            logger.info("{}---{} DONE!".format(SN, str(input_starttime)))
            if not drive_info_aday.empty:
                drive_info_aday['time'] = input_starttime[0:10]
                drive_info_aday.columns = ['sn', 'odo', 'accum_soc', 'start_soc', 'end_soc', 'start_time','end_time', 'min_soc', 'time']
                drive_info_aday.to_sql("odo_result",con=db_engine, if_exists="append",index=False)
        except Exception as e:
            logger.error(traceback.format_exc)
            logger.error(u"{} ：{},{} 任务运行错误".format(SN,input_starttime,input_endtime), exc_info=True)


    