#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import time
import sys
import pandas as pd
import os
import openpyxl
import easygui as EG
from pypinyin import lazy_pinyin

ThisYear = time.strftime("%Y", time.localtime())
ThisMonth = time.strftime("%m", time.localtime())
LastMonth = str(int(ThisMonth) - 1)
if len(LastMonth) == 1:
    LastMonth = '0' + LastMonth
Year_Month = ThisYear + LastMonth

city = {}
HigtWeather = []
Brooks = 'Commercial Excellence Team - OA'
Weather_xlsx = r'C:\xzhou\HTA\Weather.xlsx'
Commercial_xlsx = r'C:\xzhou\HTA\Commercial.xlsx'
HWA_xlsx = r'C:\xzhou\HTA\HighTemperatureAllowance.xlsx'

def GetTheLatestWeatherData():
    print('Get the latest weather data')
    Weather_Excel = pd.ExcelWriter(Weather_xlsx) #pylint:disable=abstract-class-instantiated
    for city_key in city.keys():
        city_value = city[city_key]
        print(city_value + ' weather data is being loaded ...')
        Weather_URL = 'http://www.tianqihoubao.com/weather/top/' + city_value + '.html'
        Weather_HTML = pd.read_html(Weather_URL, encoding='gbk')
        Weather_Data = Weather_HTML[0]
        Weather_Data.to_excel(Weather_Excel, sheet_name=city_key+ThisYear+ThisMonth, encoding='gbk', header=False, index=False)
    Weather_Excel.save()
    print('Weather data loading completed - GetTheLatestWeatherData')
    HighTemperatureAllowance(Weather_xlsx,3)


def GetHistoricalWeatherData():
    print('Get historical weather data')
    Weather_Excel = pd.ExcelWriter(Weather_xlsx) #pylint:disable=abstract-class-instantiated
    for city_key in city.keys():
        city_value = city[city_key]
        print(city_value + ' weather data is being loaded ...')
        Weather_URL = 'http://www.tianqihoubao.com/lishi/' + city_value + '/month/' + Year_Month + '.html'
        Weather_HTML = pd.read_html(Weather_URL, encoding='gbk')
        Weather_Data = Weather_HTML[0]
        Weather_Data.to_excel(Weather_Excel, sheet_name=city_key+ThisYear+LastMonth, encoding='gbk', header=False, index=False)
    Weather_Excel.save()
    print('Weather data loading completed - GetHistoricalWeatherData')
    HighTemperatureAllowance(Weather_xlsx,2)


def HighTemperatureAllowance(Weather_xlsx,x):
    if x == 2:
        r = 2
        c2 = 3
    elif x == 3:
        r = 3
        c2 = 5
    wb = openpyxl.load_workbook(Weather_xlsx)
    sheet = wb.sheetnames
    for wsName in sheet:
        ws = wb[wsName]
        rowNumber = ws.max_row
        rows = rowNumber + 1
        HW = 0
        for i in range(r,rows):
            ws_C = ws.cell(row=i,column=c2).value[:2]
            if int(ws_C) > 32:
                HW = HW + 1
        CHW = [wsName,HW]
        HigtWeather.append(CHW)
    wb.close()
    wb = openpyxl.load_workbook(HWA_xlsx)
    ws = wb.active
    i = 2
    ws.cell(row=1,column=1).value = '统计城市'
    ws.cell(row=1,column=2).value = '高温天数'
    ws.cell(row=1,column=3).value = '补贴金额'
    for W in HigtWeather:
        if W[1] > 5 and W[1] < 10:
            R = 100
        elif W[1] >= 10 and W[1] <= 15:
            R = 200
        elif W[1] > 15:
            R = 300
        else:
            R = 0
        ws.cell(row=i,column=1).value = W[0]
        ws.cell(row=i,column=2).value = W[1]
        ws.cell(row=i,column=3).value = R
        i = i + 1
    print('Weather data loading completed')
    wb.save(HWA_xlsx)
    wb.close()

if __name__ == '__main__':
    MK = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    GO = EG.buttonbox(msg=MK, title=Brooks, choices=("统计当月高温数据","统计上月高温数据","关闭高温统计助手"))
    if GO == '统计当月高温数据':
        wb = openpyxl.load_workbook(Commercial_xlsx)
        ws = wb.active
        sheet = wb.sheetnames
        for wsName in sheet:
            ws = wb[wsName]
            rowNumber = ws.max_row
            rows = rowNumber + 1
            HW = 0
            for i in range(2,rows):
                ws_V = ws.cell(row=i, column=2).value
                ws_X = ''.join(lazy_pinyin(ws_V))
                city[ws_V] = ws_X
        wb.close()
        print(city)
        GetTheLatestWeatherData()
        sys.exit()
        os.system("explorer.exe %s" % HWA_xlsx)
    elif GO == '统计上月高温数据':
        wb = openpyxl.load_workbook(Commercial_xlsx)
        ws = wb.active
        sheet = wb.sheetnames
        for wsName in sheet:
            ws = wb[wsName]
            rowNumber = ws.max_row
            rows = rowNumber + 1
            HW = 0
            for i in range(2,rows):
                ws_V = ws.cell(row=i, column=2).value
                ws_X = ''.join(lazy_pinyin(ws_V))
                city[ws_V] = ws_X
        wb.close()
        GetHistoricalWeatherData()
        sys.exit()
        os.system("explorer.exe %s" % HWA_xlsx)
    elif GO == '关闭高温统计助手':
        sys.exit()
    else:
        while True:
            xzhou = EG.msgbox(msg='灵魂拷问:谁是世界上最帅的人???', title=Brooks, ok_button='周晓竞')
            if xzhou:
                sys.exit()
            else:
                continue
    
COURIER ● 豫ICP备2020027789号 ● XZHOU