# -*- coding:utf-8 -*-
import sys
import datetime
import pandas as pd
import paramiko
import smtplib
from email.mime.text import MIMEText

from sqlalchemy import create_engine

reload(sys)
sys.setdefaultencoding("utf-8")

engine_qunaer = create_engine('mysql+mysqldb://linfang.wang:&*uijknbghyt63er45@172.16.3.184:7306/xyqb_btzt?charset=utf8', echo=False).connect()

def downloan_file(time_str):
    _username = 'fenqixpush'
    _password = '2A5ai08uczky'
    _host = '59.151.16.190'
    path_sftp = '/download/'

    t = paramiko.Transport((_host))
    t.connect(username=_username, password=_password)
    _sftp_fd = paramiko.SFTPClient.from_transport(t)
    try:
        _sftp_fd.get(path_sftp + '%s_LIANGHP_repaymentInfo.txt' % time_str, '/vpants/wlf/qunar_sftp/' + '%s_LIANGHP_repaymentInfo.txt' % time_str)
        _sftp_fd.get(path_sftp + '%s_LIANGHP_refundInfo.txt' % time_str,'/vpants/wlf/qunar_sftp/' + '%s_LIANGHP_refundInfo.txt' % time_str)
    except Exception, e:
        print 'sftp %s@%s: %s' % (_username, _host, e)
    _sftp_fd.close()


def get_now():
    n=datetime.datetime.now()
    b=datetime.date(n.year,n.month,n.day) + datetime.timedelta(-1)
    return b.strftime('%Y-%m-%d')

def read(time_str):
    ff = file('/vpants/wlf/qunar_sftp/'+'%s_LIANGHP_repaymentInfo.txt' % time_str).readlines()
    while len(ff[-1]) == 0 or ff[-1].startswith('F|'):
        del ff[-1]
    if ff[0].startswith('H|'):
        del ff[0]
    df_tmp = pd.DataFrame(ff, columns=['Line'])
    df_tmp['product_no'] = df_tmp['Line'].apply(lambda x: x.strip().split('|')[1])
    df_tmp['repay_time'] = df_tmp['Line'].apply(lambda x: x.strip().split('|')[3])
    df_tmp['repay_amount'] = df_tmp['Line'].apply(lambda x: x.strip().split('|')[6])
    ff = file('/vpants/wlf/qunar_sftp/'+'%s_LIANGHP_refundInfo.txt' % time_str).readlines()
    while len(ff[-1]) == 0 or ff[-1].startswith('F|'):
        del ff[-1]
    if ff[0].startswith('H|'):
        del ff[0]
    df_tmp2 = pd.DataFrame(ff, columns=['Line'])
    df_tmp2['product_no'] = df_tmp2['Line'].apply(lambda x: x.strip().split('|')[1])
    df_tmp2['repay_time'] = df_tmp2['Line'].apply(lambda x: x.strip().split('|')[3])
    df_tmp2['repay_amount'] = df_tmp2['Line'].apply(lambda x: x.strip().split('|')[4])
    df_tmp=pd.concat([df_tmp,df_tmp2],ignore_index=True)
    d=datetime.datetime.strptime(time_str,'%Y-%m-%d')
    df_tmp.repay_time = pd.to_datetime(df_tmp.repay_time)
    df_tmp=df_tmp.ix[df_tmp.repay_time >= datetime.datetime(d.year,d.month,d.day,15,0,0)]
    df_tmp.repay_amount=df_tmp.repay_amount.astype(float)
    #===哈银订单==
    sql_hy='''
    SELECT upstream_order_id FROM `zt_order` WHERE fund_id = 1 AND channel_id = 66 AND upstream_order_id in %s
    ''' % str(tuple(df_tmp.product_no.astype(str).tolist()))
    df_hy=pd.read_sql(sql_hy,engine_qunaer)
    df_tmp=df_tmp.ix[df_tmp.product_no.isin(df_hy.upstream_order_id)]
    return df_tmp['repay_amount'].sum()

def send_mail(to_list, sub, content):
        mail_host="mail.quantgroup.cn"
        mail_user="datamonitor"    # user
        mail_pass="!Moni123Tor456"    # passwd
        mail_postfix="quantgroup.cn"
        mail_port = "587"

        me = mail_user+"@"+mail_postfix
        msg = MIMEText(content,_subtype='html',_charset='UTF-8')
        msg['Subject'] = sub
        msg['From'] = me
        msg['To'] = ";".join(to_list)
        try:
                server = smtplib.SMTP()
                server.connect(mail_host,mail_port)
                server.starttls()
                server.login(me,mail_pass)
                server.sendmail(me, to_list, msg.as_string())
                server.close()
                return True
        except Exception, e:
                print str(e)
                return False

time_str = get_now()
downloan_file(time_str)
repay_amount=read(time_str)

to_list=['linfang.wang@quantgroup.cn','fengli.liu@quantgroup.cn','qian.peng@quantgroup.cn','jianyu.zhu@quantgroup.cn','yao.wang@quantgroup.cn','shanling.yu@quantgroup.cn']
# to_list=['linfang.wang@quantgroup.cn']
sub=u'去哪儿不可用—%s' % time_str

content='''
<table border="1">
	<tr>
		<td align="center">时间</td>
		<td align="center">不可用金额</td>
	</tr>
	<tr>
		<td align="center">%s</td>
		<td align="center">%s</td>
	</tr>
</table>
''' % (time_str,repay_amount)

send_mail(to_list,sub,content)