# encoding: utf8
import pandas as pd
from sqlalchemy import create_engine

pd.options.mode.chained_assignment = None
pd.set_option('display.float_format', lambda x: '%.5f' % x)

path_out = u'E:/财务/'

engine_audit = create_engine('mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/test_8_2?charset=utf8', echo=True)

sql = """select date_format(lm.loan_paid_at, '%%Y-%%m') 年月, lc.class 客户等级, sum(lm.contract_loan_amount) 本金（债权）
 from loan_manifest lm join loan_class lc on lc.loanid = lm.loanid where lm.loan_paid_at >= '2015-01-01 00:00:00' 
 and lm.loan_paid_at < '2017-01-01 00:00:00' and lm.is_active in (1,-2) group by 1,2;"""

df = pd.read_sql(sql=sql, con=engine_audit)
df.to_excel(path_out + u'现金贷按月按等级汇总放款额(2015-2016).xlsx', index=None)