from jinja2 import PackageLoader, Environment
from app.service.result_service import UserCustomerResultAdaptor


# 准备数据
from app.utils.draw import draw_month_return_chart

ifa_id = 'USER_INFO15914346866762'
customer_id = '202009281545001'

user_customer = UserCustomerResultAdaptor(ifa_id, customer_id)

df = user_customer.calculate_total_data()
d = user_customer.calculate_group_result_data()

print(d)

# 几月综述部分

total_cost = df["total_cost"]/10000
now_yield = round((df['contribution_decomposition']-1)*100,2)
index_yield = round((df["index_result"]["return_ratio"]-1)*100, 2)
now_withdrawal = round(df["max_drawdown"][0]*100,2)
index_withdrawal = round(df["index_result"]["max_drawdown"][0]*100, 2)
# monthly_return_performance_pic = '_'.join((ifa_id, customer_id, '20201109', 'monthly_return_performance.png'))
# image_dir = './img/'
# monthly_return_performance_pic = image_dir + monthly_return_performance_pic
xlabels, product_list, cumulative = user_customer.get_month_return_chart()
monthly_return_performance_pic = draw_month_return_chart(xlabels, product_list, cumulative)

# 组合数据
group_result = d["default"]

now_month_income = int(group_result["cur_month_profit"])   # 本月收益
now_year_income = int(group_result["cur_year_profit"])   # 今年累计收益
totoal_rate_of_return = round((group_result['contribution_decomposition']-1)*100, 2)       # 累计收益率
month_rise = round(group_result["cur_month_profit_ratio"]*100, 2)       # 本月涨幅
year_totoal_rate_of_return = round(group_result["cur_year_profit_ratio"]*100, 2)        # 今年累计收益率
annualised_return = round(group_result["return_ratio_year"]*100, 2)     # 年化收益率
volatility = round(group_result["volatility"]*100, 2)
max_withdrawal = round(group_result["max_drawdown"][0]*100, 2)
sharpe_ratio = round(group_result["sharpe"], 2)
cost_of_investment = int(group_result["total_cost"])    # 投资成本
final_balance = int(group_result["total_cost"] + group_result["cumulative_profit"])    # 期末资产
total_profit = int(group_result["cumulative_profit"])  # 累计盈利

index_section_return = round((group_result["index_result"]["return_ratio"]-1)*100, 2)
index_annualised_return = round(group_result["index_result"]["return_ratio_year"]*100, 2)     # 年化收益率
index_volatility = round(group_result["index_result"]["volatility"]*100, 2)
index_max_withdrawal = round(group_result["index_result"]["max_drawdown"][0]*100, 2)
index_sharpe_ratio = round(group_result["index_result"]["sharpe"], 2)

group_nav_info = group_result["group_nav_info"]
group_hoding_info = group_result["group_hoding_info"]

# 全部数据
data = {'customer_name':'成龙', 'customer_gender':'女',
        'year_month':'2020年10月', 'ifa_company':'飞度工作室',
        'title':'10月综述', 'brand_name':'飞度工作室',
        'customer_old':42, 'customer_level':'平衡型',
        'position_years':'5年', 'planned_allocation_amount':2000.00,
        'now_allocation_amount':total_cost, 'now_yield':now_yield, 'index_yield':index_yield, 'expected_yield':20,
        'now_withdrawal':now_withdrawal, 'index_withdrawal': index_withdrawal, 'expected_withdrawal': 20,
        'now_year_income':now_year_income,'now_month_income':now_month_income,'totoal_rate_of_return':totoal_rate_of_return,
        'month_rise':month_rise, 'year_totoal_rate_of_return':year_totoal_rate_of_return,
        'annualised_return': annualised_return, 'cost_of_investment': cost_of_investment,
        'final_balance':final_balance, 'total_profit':total_profit,
        'latest_worth_day':'2020-11-05',

        'index_comparison':{'section_return': totoal_rate_of_return, 'annualized_returns': annualised_return,
                            'volatility':volatility, 'max_withdrawal':max_withdrawal, 'sharpe_ratio': sharpe_ratio},
        'index_comparison_500':{'section_return':index_section_return, 'annualized_returns': index_annualised_return,
                            'volatility':index_volatility, 'max_withdrawal':index_max_withdrawal, 'sharpe_ratio':index_sharpe_ratio},
        'monthly_return_performance_pic':monthly_return_performance_pic,
        'group_nav_info': group_nav_info,
        'group_hoding_info': group_hoding_info
        }


# 开始渲染html模板
env = Environment(loader=PackageLoader('app','templates'))    # 创建一个包加载器对象
template = env.get_template('monthReport.html')    # 获取一个模板文件
monthReport_html = template.render(data)  # 渲染

# 保存 monthReport_html
save_file = "app/html/monthReport.html"
with open(save_file, 'w', encoding="utf-8") as f:
    f.write(monthReport_html)