report.py 4.77 KB
Newer Older
pengxiong's avatar
pengxiong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : report.py
# @Time       : 2020/11/18 下午3:19
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
import datetime
import time

from flask_restful import Resource, reqparse
from flask import request, render_template
from flask import make_response

from app.api.engine import config
from app.utils.html_to_pdf import html_to_pdf
wang zhengwei's avatar
wang zhengwei committed
18
from app.api.engine import pdf_folder, redis, env, TAMP_SQL, tamp_diagnose_app_engine, http_dir
pengxiong's avatar
pengxiong committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
from app.utils.jinjia2html_v2 import DataIntegrate


class ReportHandlers(Resource):
    """."""

    def __init__(self):
        """."""
        self.parser = reqparse.RequestParser()

    def get(self):
        """."""
        # self.parser.add_argument('ifa_id', type=str, required=True, help='ifa_id不能为空')
        self.parser.add_argument('customer_id', type=str, required=True, help='customer_id不能为空')
        args = self.parser.parse_args()
        token = request.headers.get('Authorization', '')
        token = 's:sid:' + token.split(' ')[1]
        ifa_id = redis.get(token).decode()
wang zhengwei's avatar
wang zhengwei committed
37

pengxiong's avatar
pengxiong committed
38 39
        if not ifa_id:
            return {"code":40005,"data":None,"lang":"zh_CN","msg":"请登陆"}
wang zhengwei's avatar
wang zhengwei committed
40 41 42 43
        if '\''in  ifa_id:
            ifa_id = ifa_id.replace('\'', '')
        if '\"'in  ifa_id:
            ifa_id = ifa_id.replace('\"', '')
pengxiong's avatar
pengxiong committed
44
        customer_id = args['customer_id']
wang zhengwei's avatar
wang zhengwei committed
45
        pdf_name = str(ifa_id) + str(customer_id) + '.pdf'
pengxiong's avatar
pengxiong committed
46 47 48 49 50 51 52 53 54 55 56

        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,
                ifa_id,
                customer_id
            )
            tamp_diagnose_app_session.execute(sql)
        start = time.time()
wang zhengwei's avatar
wang zhengwei committed
57
        check = 0
pengxiong's avatar
pengxiong committed
58 59 60
        try:
            dt = DataIntegrate(ifa_id=ifa_id, customer_id=customer_id, pdf_name=pdf_name)
            dt.render_data()
wang zhengwei's avatar
wang zhengwei committed
61
            check = 1
wang zhengwei's avatar
wang zhengwei committed
62 63
        except OSError:
            check = 1
赵杰's avatar
赵杰 committed
64 65
        except:
            check = 0
pengxiong's avatar
pengxiong committed
66
        print('耗时{}秒'.format(round(time.time() - start, 2)))
wang zhengwei's avatar
wang zhengwei committed
67
        if check == 0:
赵杰's avatar
赵杰 committed
68 69 70 71 72 73 74 75 76
            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(),
                    0,
                    ifa_id,
                    customer_id
                )
                tamp_diagnose_app_session.execute(sql)
wang zhengwei's avatar
wang zhengwei committed
77
            return {"code":-1, "msg": "内部错误"}
pengxiong's avatar
pengxiong committed
78

wang zhengwei's avatar
wang zhengwei committed
79 80 81
        # auth = oss2.Auth(config[env]['oss']['account'], config[env]['oss']['password'])
        # # Endpoint以杭州为例,其它Region请按实际情况填写。
        # bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com', config[env]['oss']['env'])
pengxiong's avatar
pengxiong committed
82 83

        # 生成下载文件的签名URL,有效时间为60s。
wang zhengwei's avatar
wang zhengwei committed
84 85
        # path = bucket.sign_url('GET', config[env]['oss']['path'] + pdf_name, config[env]['oss']['validtime'])

pengxiong's avatar
pengxiong committed
86 87 88 89 90 91 92 93 94 95
        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,
                ifa_id,
                customer_id
            )
            tamp_diagnose_app_session.execute(sql)
wang zhengwei's avatar
wang zhengwei committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109
        
        with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
            
            tamp_diagnose_app_session = tamp_diagnose_app.session
            sql = "SELECT id, file, customer_id, ifa_id, update_status, update_time from hold_report where ifa_id='{}' and customer_id='{}'".format(
                ifa_id,
                customer_id,
            )
            ret = tamp_diagnose_app_session.execute(sql).fetchone()

        data = {"id":ret[0],"file": http_dir + ret[1],"customer_id":ret[2],
                        "ifa_id":ret[3],"update_status":ret[4],
                        "update_time":ret[5].strftime("%Y-%m-%d %H:%M:%S")}
        
pengxiong's avatar
pengxiong committed
110 111
        resp = {
            "code": 0,
wang zhengwei's avatar
wang zhengwei committed
112
            "data": data,
pengxiong's avatar
pengxiong committed
113 114 115
            "lang": "zh_CN",
            "msg": "成功"
        }
wang zhengwei's avatar
wang zhengwei committed
116
        
pengxiong's avatar
pengxiong committed
117 118 119 120 121 122 123 124 125 126 127 128 129
        return resp

    def post(self):
        """."""
        pass

    def put(self, id):
        """."""
        pass

    def delete(self, id):
        """."""
        pass