report_service.py 13.6 KB
Newer Older
pengxiong's avatar
pengxiong committed
1 2 3 4 5 6 7 8
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : report_service.py
# @Time       : 2021/1/13 下午3:14
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
pengxiong's avatar
pengxiong committed
9
import datetime
pengxiong's avatar
pengxiong committed
10
import json
pengxiong's avatar
pengxiong committed
11 12
import os
import uuid
pengxiong's avatar
pengxiong committed
13

pengxiong's avatar
pengxiong committed
14
from sqlalchemy import and_, or_
pengxiong's avatar
pengxiong committed
15 16
# from sqlalchemy import union_all

wang zhengwei's avatar
wang zhengwei committed
17 18
from app.api.engine import TAMP_SQL, tamp_diagnose_app_engine, template_folder, temp_img_save_folder, pdf_save_folder
from app.model.tamp_diagnose_app import HoldReport, HoldDiagnoseReport, PeriodicReport, Customer, ReportView, FundReportManange
pengxiong's avatar
pengxiong committed
19
from app.utils.jinjia2html_v2 import DataIntegrate
20
from app.celery import save_pdf
21
from app.utils.format_transfer import npEncoder
pengxiong's avatar
pengxiong committed
22 23 24 25 26 27


def get_report_list(args):
    """."""
    type = args.get('type')
    ifa_id = args.get('ifa_id')
pengxiong's avatar
pengxiong committed
28
    name = args.get('name')
pengxiong's avatar
pengxiong committed
29 30 31 32 33 34
    pageNumber = args['pageNumber']
    pageSize = args['pageSize']
    start_time = args.get('start_time')
    end_time = args.get('end_time')
    offset = (pageNumber - 1) * pageSize
    res = []
pengxiong's avatar
pengxiong committed
35
    totalSize = 0
pengxiong's avatar
pengxiong committed
36 37
    conditions = ()
    if type == 0:
wang zhengwei's avatar
wang zhengwei committed
38 39 40 41 42 43 44 45 46 47 48 49
        if start_time and end_time:
            conditions = [
                    ReportView.ifa_id == ifa_id,
                    or_(ReportView.name.like('%' + name + '%'), ReportView.customer_name.like('%' + name + '%')),
                    ReportView.create_time >= start_time,
                    ReportView.create_time <= end_time
            ]
        else:
            conditions = [
                    ReportView.ifa_id == ifa_id,
                    or_(ReportView.name.like('%' + name + '%'), ReportView.customer_name.like('%' + name + '%'))
                ]
pengxiong's avatar
pengxiong committed
50
    else:
wang zhengwei's avatar
wang zhengwei committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
        if start_time and end_time:
            conditions = [
                    ReportView.ifa_id == ifa_id,
                    ReportView.type == type,
                    or_(ReportView.name.like('%' + name + '%'), ReportView.customer_name.like('%' + name + '%')),
                    ReportView.create_time >= start_time,
                    ReportView.create_time <= end_time
            ]
        else:
            conditions = [
                    ReportView.ifa_id == ifa_id,
                    ReportView.type == type,
                    or_(ReportView.name.like('%' + name + '%'), ReportView.customer_name.like('%' + name + '%'))
            ]
65
    allow_field = ['id', 'customer_id', 'ifa_id', 'update_time', 'create_time', 'update_status', 'file', 'be_viewed', 'name', 'customer_name', 'author_name', 'type']
pengxiong's avatar
pengxiong committed
66 67
    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_session = tamp_diagnose_app.session
pengxiong's avatar
pengxiong committed
68
        report_res = tamp_diagnose_session.query(ReportView).filter(and_(*conditions))
pengxiong's avatar
pengxiong committed
69
        totalSize = report_res.count()
70
        report_res = report_res.order_by(ReportView.create_time.desc()).offset(offset).limit(pageSize)
pengxiong's avatar
pengxiong committed
71
        res = [r.to_dict(allow_field=allow_field) for r in report_res]
pengxiong's avatar
pengxiong committed
72

pengxiong's avatar
pengxiong committed
73
    return {
pengxiong's avatar
pengxiong committed
74
        'content': res,
pengxiong's avatar
pengxiong committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        'pageNum': pageNumber,
        'pageSize': pageSize,
        'totalSize': totalSize
    }


def delete_report(args):
    """."""
    type = args.get('type')
    id = args.get('id')
    ifa_id = args.get('ifa_id')
    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_session = tamp_diagnose_app.session
        if type == 1:
            tamp_diagnose_session.query(HoldReport).filter(and_(
                HoldReport.ifa_id == ifa_id,
                HoldReport.id == id
            )).delete()
        elif type == 2:
            tamp_diagnose_session.query(HoldDiagnoseReport).filter(and_(
                HoldDiagnoseReport.ifa_id == ifa_id,
                HoldDiagnoseReport.id == id
            )).delete()
        elif type == 3:
            tamp_diagnose_session.query(PeriodicReport).filter(and_(
                PeriodicReport.ifa_id == ifa_id,
                PeriodicReport.id == id
            )).delete()
pengxiong's avatar
pengxiong committed
103 104 105 106 107 108 109


def get_one_report(args):
    """."""
    id = args.get('id')
    type = args.get('type')
    ifa_id = args.get('ifa_id')
pengxiong's avatar
pengxiong committed
110
    data = []
pengxiong's avatar
pengxiong committed
111 112
    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_session = tamp_diagnose_app.session
pengxiong's avatar
pengxiong committed
113 114 115
        res = tamp_diagnose_session.query(ReportView).filter(and_(
            ReportView.id == id,
            ReportView.type == type,
pengxiong's avatar
pengxiong committed
116
            ReportView.ifa_id == ifa_id,
pengxiong's avatar
pengxiong committed
117
        ))
pengxiong's avatar
pengxiong committed
118
        data = [r.to_dict() for r in res]
pengxiong's avatar
pengxiong committed
119 120 121
    if data:
        return data[0]
    return False
pengxiong's avatar
pengxiong committed
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
def edit_report_name(args):
    """编辑报告"""
    id = args.get('id')
    type = args.get('type')
    ifa_id = args.get('ifa_id')
    name = args.get('name')
    res = []
    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_session = tamp_diagnose_app.session
        if type == 1:
            res = tamp_diagnose_session.query(HoldReport).filter(and_(
                HoldReport.id == id,
                HoldReport.ifa_id == ifa_id,
            ))
        elif type == 2:
            res = tamp_diagnose_session.query(HoldDiagnoseReport).filter(and_(
                HoldDiagnoseReport.id == id,
                HoldDiagnoseReport.ifa_id == ifa_id,
            ))
        elif type == 3:
            res = tamp_diagnose_session.query(PeriodicReport).filter(and_(
                PeriodicReport.id == id,
                PeriodicReport.ifa_id == ifa_id,
            ))
        else:
            return False
        r = res.update({
            'name': name,
            'update_time': datetime.datetime.now()
        })
    return True
pengxiong's avatar
pengxiong committed
154 155 156 157 158 159 160

def edit_report(args):
    """编辑报告"""
    id = args.get('id')
    type = args.get('type')
    ifa_id = args.get('ifa_id')
    report_data = args.get('report_data')
pengxiong's avatar
pengxiong committed
161
    pdf_name = str(uuid.uuid4()) + '.pdf'
pengxiong's avatar
pengxiong committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    res = []
    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_session = tamp_diagnose_app.session
        hold_report = tamp_diagnose_session.query(HoldReport).filter(and_(
            HoldReport.id == id,
            HoldReport.ifa_id == ifa_id,
        ))
        hold_diagnose_report = tamp_diagnose_session.query(HoldDiagnoseReport).filter(and_(
            HoldDiagnoseReport.id == id,
            HoldDiagnoseReport.ifa_id == ifa_id,
        ))
        periodic_report = tamp_diagnose_session.query(PeriodicReport).filter(and_(
            PeriodicReport.id == id,
            PeriodicReport.ifa_id == ifa_id,
        ))
        if type == 1:
            res = hold_report
        elif type == 2:
            res = hold_diagnose_report
        elif type == 3:
            res = periodic_report
        if not res:
            return False
        r = res.update({
            'report_data': report_data,
187
            'update_status': 1,
pengxiong's avatar
pengxiong committed
188 189
            'update_time': datetime.datetime.now()
        })
190
        
191
    save_pdf.delay(id, ifa_id, res[0].customer_id, pdf_name, type, report_data)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        # try:
        #     dt = DataIntegrate(ifa_id=ifa_id, customer_id=res[0].customer_id, pdf_name=pdf_name, type=type)
        #     dt.render_data(data=json.loads(report_data.replace(template_folder, temp_img_save_folder)))
        # except:
        #     pass
        # with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        #     tamp_diagnose_session = tamp_diagnose_app.session
        #     hold_report = tamp_diagnose_session.query(HoldReport).filter(and_(
        #         HoldReport.id == id,
        #         HoldReport.ifa_id == ifa_id,
        #     ))
        #     hold_diagnose_report = tamp_diagnose_session.query(HoldDiagnoseReport).filter(and_(
        #         HoldDiagnoseReport.id == id,
        #         HoldDiagnoseReport.ifa_id == ifa_id,
        #     ))
        #     periodic_report = tamp_diagnose_session.query(PeriodicReport).filter(and_(
        #         PeriodicReport.id == id,
        #         PeriodicReport.ifa_id == ifa_id,
        #     ))
        #     if type == 1:
        #         res = hold_report
        #     elif type == 2:
        #         res = hold_diagnose_report
        #     elif type == 3:
        #         res = periodic_report
        #     if not res:
        #         return False
        #     res.update({
        #         'file': pdf_name,
        #         'update_status': 2,
        #         'update_time': datetime.datetime.now()
        #     })
pengxiong's avatar
pengxiong committed
224
    return True
pengxiong's avatar
pengxiong committed
225 226 227 228 229 230 231 232


def make_report(args):
    """制作报告."""
    ifa_id = args.get('ifa_id')
    customer_id = args.get('customer_id')
    type = args.get('type')
    name = args.get('name')
233
    report_data = json.loads(args.get('report_data').replace(template_folder, temp_img_save_folder))
pengxiong's avatar
pengxiong committed
234
    pdf_name = str(uuid.uuid4()) + '.pdf'
235
    args['create_time'] = datetime.datetime.now()
pengxiong's avatar
pengxiong committed
236 237
    args['update_status'] = 1

pengxiong's avatar
pengxiong committed
238
    args.pop('type')
pengxiong's avatar
pengxiong committed
239 240 241 242 243 244 245 246 247 248 249 250 251
    record_id = 0
    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_session = tamp_diagnose_app.session
        report_record = None
        if type == 1:
            report_record = HoldReport(**args)
        elif type == 2:
            report_record = HoldDiagnoseReport(**args)
        elif type == 3:
            report_record = PeriodicReport(**args)
        if not report_record:
            return False
        tamp_diagnose_session.add(report_record)
pengxiong's avatar
pengxiong committed
252
        tamp_diagnose_session.flush()
pengxiong's avatar
pengxiong committed
253
        record_id = report_record.id
pengxiong's avatar
pengxiong committed
254

255
    save_pdf.delay(record_id, ifa_id, customer_id, pdf_name, type, report_data)
pengxiong's avatar
pengxiong committed
256
    return {'record_id': record_id}
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    # pid = os.fork()
    # if pid == 0:
    #     try:
    #         dt = DataIntegrate(ifa_id=ifa_id, customer_id=customer_id, pdf_name=pdf_name, type=type)
    #         dt.render_data(data=report_data)
    #     except:
    #         pass
    #     with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
    #         tamp_diagnose_session = tamp_diagnose_app.session
    #         hold_report = tamp_diagnose_session.query(HoldReport).filter(and_(
    #             HoldReport.id == record_id,
    #             HoldReport.ifa_id == ifa_id,
    #         ))
    #         hold_diagnose_report = tamp_diagnose_session.query(HoldDiagnoseReport).filter(and_(
    #             HoldDiagnoseReport.id == record_id,
    #             HoldDiagnoseReport.ifa_id == ifa_id,
    #         ))
    #         periodic_report = tamp_diagnose_session.query(PeriodicReport).filter(and_(
    #             PeriodicReport.id == record_id,
    #             PeriodicReport.ifa_id == ifa_id,
    #         ))
    #         if type == 1:
    #             res = hold_report
    #         elif type == 2:
    #             res = hold_diagnose_report
    #         elif type == 3:
    #             res = periodic_report
    #         if not res:
    #             return False
    #         res.update({
    #             'file': pdf_name,
    #             'update_status': 2,
    #             'update_time': datetime.datetime.now()
    #         })
    # else:
    #     return {'record_id': record_id}
wang zhengwei's avatar
wang zhengwei committed
293 294 295 296 297 298 299 300 301 302

def make_app_report(args):
    """制作报告."""
    ifa_id = args.get('ifa_id')
    customer_id = args.get('customer_id')
    type = 1
    pdf_name = str(uuid.uuid4()) + '.pdf'
    args['file'] = pdf_name
    args['create_time'] = datetime.datetime.now()
    args['update_status'] = 2
303
    custom_time = args['custom_time']
wang zhengwei's avatar
wang zhengwei committed
304 305 306 307 308 309 310 311 312 313 314 315

    data = {}
    record_id = 0
    report_data = ''
    default_template = ''
    with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
        tamp_diagnose_session = tamp_diagnose_app.session
        # 查找当前时间是否有生成的pdf
        hr = tamp_diagnose_session.query(HoldReport).filter(HoldReport.custom_time == custom_time,
                                                       HoldReport.ifa_id == ifa_id,
                                                       HoldReport.customer_id == customer_id).first()
        if hr:
wang zhengwei's avatar
wang zhengwei committed
316
            return hr.id
wang zhengwei's avatar
wang zhengwei committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        customer_name = ''
        type_name = '持仓报告'
        create_date = str(datetime.datetime.now().date())
        sql = "select customer_name from customer_view where id = '{}'".format(customer_id)
        res = tamp_diagnose_session.execute(sql)
        res = res.fetchone()
        if res:
            customer_name = res[0]
        args['name'] = customer_name + type_name + create_date
        
        template = tamp_diagnose_session.query(FundReportManange).filter(and_(
            FundReportManange.ifa_id == ifa_id,
            FundReportManange.type == 1,
            FundReportManange.default == 1
        )).first()
wang zhengwei's avatar
wang zhengwei committed
332 333
        default_template = ""
        if template and template.default_template:
wang zhengwei's avatar
wang zhengwei committed
334
            default_template = json.loads(template.default_template)
wang zhengwei's avatar
wang zhengwei committed
335
        elif template:
wang zhengwei's avatar
wang zhengwei committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
            default_template = json.loads(template.custom_template)
    # save_pdf.delay(record_id, ifa_id, customer_id, pdf_name, type)
    try:
        dt = DataIntegrate(ifa_id=ifa_id, customer_id=customer_id, pdf_name=pdf_name, type=type, end_date = custom_time)
        report_data = dt.get_template_data(default_template=default_template)
        report_data = json.dumps(report_data, cls=npEncoder, ensure_ascii=False)
        args['report_data'] = report_data
        dt.render_data(data=json.loads(report_data.replace(template_folder, temp_img_save_folder)))
    except OSError:
        pass
    except Exception as e:
        return False
    
    if os.path.exists(pdf_save_folder + pdf_name):
        with TAMP_SQL(tamp_diagnose_app_engine) as tamp_diagnose_app:
            tamp_diagnose_session = tamp_diagnose_app.session
            args['update_time'] = datetime.datetime.now()
            report_record = HoldReport(**args)
            tamp_diagnose_session.add(report_record)
            tamp_diagnose_session.flush()
wang zhengwei's avatar
wang zhengwei committed
356
            return report_record.id
wang zhengwei's avatar
wang zhengwei committed
357 358
 
    return False