timed_task.py 2.73 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 time
wang zhengwei's avatar
wang zhengwei committed
9 10 11

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
12
from sqlalchemy import Column, String, Integer, and_
pengxiong's avatar
2  
pengxiong committed
13
from app.utils.jinjia2html_v2 import DataIntegrate
pengxiong's avatar
pengxiong committed
14
from concurrent import futures
wang zhengwei's avatar
wang zhengwei committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

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))


def call_month_report(ifauser_id, ifauser_name, customer_id, customer_name):
pengxiong's avatar
pengxiong committed
40 41 42 43 44 45 46 47 48 49 50 51 52
    # ifauser_id = args[0]
    # ifauser_name = args[1]
    # customer_id = args[2]
    # customer_name = args[3]
    print('开始制作IFA: {}, 客户: {}的基金月报'.format(ifauser_name, customer_name))
    start = time.time()
    try:
        DataIntegrate(ifauser_id, customer_id, '{}_{}_.pdf'.format(ifauser_name, customer_name))
        print('IFA: {}, 客户: {}的基金月报已经生成, 耗时{}秒'.format(ifauser_name, customer_name, time.time()-start))
    except OSError:
        print('IFA: {}, 客户: {}的基金月报已经生成,耗时{}秒'.format(ifauser_name, customer_name, time.time()-start))
    except:
        print("IFA: {}, 客户: {}的基金月报生成异常,开始制作下一个".format(ifauser_name, customer_name))
wang zhengwei's avatar
wang zhengwei committed
53 54 55 56 57


with TAMP_SQL(tamp_user_engine) as tamp_user, TAMP_SQL(tamp_order_engine) as tamp_order:
    tamp_user_session = tamp_user.session
    # 获取所有 ifauser
pengxiong's avatar
pengxiong committed
58
    ifausers = tamp_user_session.query(IfaUser).all()
wang zhengwei's avatar
wang zhengwei committed
59 60 61
    
    tamp_order_session = tamp_order.session
    # for ifauser in ifausers:
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
62
    for ifauser in ifausers:
wang zhengwei's avatar
wang zhengwei committed
63 64
        # 获取某个 ifa 的所有 customer
        customers = tamp_order_session.query(Ifa_Customer).\
pengxiong's avatar
pengxiong committed
65
                        filter(Ifa_Customer.ifa_id == ifauser.id).all()
pengxiong's avatar
pengxiong committed
66

wang zhengwei's avatar
wang zhengwei committed
67
        # 循环这个 ifa 的所有 customer
pengxiong's avatar
pengxiong committed
68
        # args = []
wang zhengwei's avatar
wang zhengwei committed
69
        for customer in customers:
pengxiong's avatar
pengxiong committed
70
            customer_full = tamp_order_session.query(Customer).get(customer.customer_id)
wang zhengwei's avatar
wang zhengwei committed
71
            call_month_report(ifauser.id, ifauser.ui_username, customer_full.id, customer_full.customer_name)
pengxiong's avatar
pengxiong committed
72 73 74 75
            # args.append((ifauser.id, ifauser.ui_username, customer_full.id, customer_full.customer_name))
        # with futures.ProcessPoolExecutor(8) as executor:
        #     executor.map(call_month_report, args)