timed_task.py 4.46 KB
Newer Older
wang zhengwei's avatar
wang zhengwei committed
1 2 3 4 5 6 7
# coding: utf-8

"""
找出所有 ifa
        找出 ifa 所有的客户
                           进行月报生成
"""
pengxiong's avatar
pengxiong committed
8
import datetime
pengxiong's avatar
pengxiong committed
9
import time
pengxiong's avatar
pengxiong committed
10
import traceback
pengxiong's avatar
pengxiong committed
11 12 13 14
import uuid

import oss2

wang zhengwei's avatar
wang zhengwei committed
15 16 17
# from app.api.engine import logging, pdf_folder, tamp_diagnose_app_engine, config, env
from app.api.engine import pdf_folder, tamp_diagnose_app_engine, config, env

pengxiong's avatar
pengxiong committed
18
import os
wang zhengwei's avatar
wang zhengwei committed
19 20 21

from app.api.engine import tamp_user_engine, tamp_order_engine, TAMP_SQL
from sqlalchemy.ext.declarative import declarative_base
pengxiong's avatar
pengxiong committed
22
from sqlalchemy import Column, String, Integer, and_
pengxiong's avatar
pengxiong committed
23 24

from app.service.portfolio_diagnose import fund_rank, tamp_fund
pengxiong's avatar
2  
pengxiong committed
25
from app.utils.jinjia2html_v2 import DataIntegrate
pengxiong's avatar
pengxiong committed
26
from concurrent import futures
wang zhengwei's avatar
wang zhengwei committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

Base = declarative_base()
class IfaUser(Base):
    __tablename__ = "user_info"

    id = Column(String(64), primary_key=True)
    ui_username = Column(String(64))
    ui_sex = Column(String(64))
        
class Ifa_Customer(Base):
    __tablename__ = "ifa_customer"

    id = Column(String(64), primary_key=True)
    customer_id = Column(String(32))
    ifa_id = Column(String(64))
    
        
class Customer(Base):
    __tablename__ = "customer"

    id = Column(String(64), primary_key=True)
    customer_name = Column(String(128))

pengxiong's avatar
pengxiong committed
50 51 52 53 54 55 56 57 58 59 60
# class CustomerOrder(Base):
#     __tablename__ = 'customer_order'
#
#     user_id = Column(String(128))
#     customer_id = Column(String(128))

# class UserInfo(Base):
#     __tablename__ = 'user_info'
#
#     id = Column(String(64), primary_key=True)
#     ui_username = Column(String(128))
wang zhengwei's avatar
wang zhengwei committed
61 62

def call_month_report(ifauser_id, ifauser_name, customer_id, customer_name):
pengxiong's avatar
pengxiong committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    # filename = '{}_{}_.pdf'.format(ifauser_name, customer_name)
    # if os.path.exists(pdf_folder + filename):
    #     return None
    pdf_name = str(uuid.uuid4()) + '.pdf'

    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_app_session = tamp_diagnose_app.session
        sql = "update hold_report set update_time='{}', update_status='{}' where ifa_id='{}' and customer_id='{}'".format(
            datetime.datetime.now(),
            1,
            ifauser_id,
            customer_id
        )
        tamp_diagnose_app_session.execute(sql)

pengxiong's avatar
pengxiong committed
78 79 80
    print('开始制作IFA: {}, 客户: {}的基金月报'.format(ifauser_name, customer_name))
    start = time.time()
    try:
pengxiong's avatar
pengxiong committed
81 82
        dt = DataIntegrate(ifauser_id, customer_id, '{}_{}_.pdf'.format(ifauser_name, customer_name))
        dt.render_data()
pengxiong's avatar
pengxiong committed
83 84 85
        print('IFA: {}, 客户: {}的基金月报已经生成, 耗时{}秒'.format(ifauser_name, customer_name, time.time()-start))
    except OSError:
        print('IFA: {}, 客户: {}的基金月报已经生成,耗时{}秒'.format(ifauser_name, customer_name, time.time()-start))
pengxiong's avatar
pengxiong committed
86
    except Exception as e:
wang zhengwei's avatar
wang zhengwei committed
87
        # logging.error("IFAID{}: IFAName{}, 客户ID{}, 客户Name: {}的基金月报生成异常,错误追踪:{}".format(ifauser_id, ifauser_name, customer_id, customer_name, traceback.format_exc()))
pengxiong's avatar
pengxiong committed
88
        print("IFA: {}, 客户: {}的基金月报生成异常,错误追踪:{}".format(ifauser_name, customer_name, traceback.format_exc()))
赵杰's avatar
赵杰 committed
89
    finally:
pengxiong's avatar
pengxiong committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        auth = oss2.Auth(config[env]['oss']['account'], config[env]['oss']['password'])
        # Endpoint以杭州为例,其它Region请按实际情况填写。
        bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com', 'tamperdev')

        # 生成下载文件的签名URL,有效时间为60s。
        path = bucket.sign_url('GET', 'productionenv/Start/' + pdf_name, 60)
        with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
            tamp_diagnose_app_session = tamp_diagnose_app.session
            sql = "update hold_report set update_time='{}', update_status='{}', file='{}' where ifa_id='{}' and customer_id='{}'".format(
                datetime.datetime.now(),
                2,
                pdf_name,
                ifauser_id,
                customer_id
            )
            tamp_diagnose_app_session.execute(sql)
wang zhengwei's avatar
wang zhengwei committed
106

pengxiong's avatar
pengxiong committed
107 108 109 110 111 112
if __name__ == '__main__':
    data = []
    with TAMP_SQL(tamp_user_engine) as tamp_user, TAMP_SQL(tamp_order_engine) as tamp_order:
        tamp_user_session = tamp_user.session
        res = tamp_user_session.execute('SELECT DISTINCT user_id, customer_id , realname, customer_name FROM (select f1.user_id, f1.customer_id, f2.realname,f3.customer_name from customer_order f1, user_info f2,customer_info f3   where f2.id=f1.user_id and f3.id=f1.customer_id) res;')
        data = res.fetchall()
pengxiong's avatar
pengxiong committed
113 114
    for d in data:
        call_month_report(d[0], d[2], d[1], d[3])
pengxiong's avatar
pengxiong committed
115