2b705da4 by 周伟奇

add license statistics

1 parent 0c124b56
1 import json
2 import re
3 import ast
4 from openpyxl import Workbook
5 from django.core.management import BaseCommand
6 from common.mixins import LoggerMixin
7 from apps.doc.models import HILDoc, AFCDoc
8 from apps.doc import consts
9
10
11 class Command(BaseCommand, LoggerMixin):
12
13 def __init__(self):
14 super().__init__()
15 self.log_base = '[license statistics]'
16 self.header_map = {
17 consts.MVI_CLASSIFY: [('申请ID', '发票代码', '发票号码', '开票日期', '不含税价', '发票联', '购买方名称',
18 '购买方证件号码', '纳税人识别号', '车架号', '价税合计小写', '销货单位名称', '增值税税额',
19 '增值税税率', '发票章有无', '价税合计大写', '发动机号', '车辆类型', '厂牌型号', '产地',
20 '合格证号', '进口证明书号', '商检单号', '电话', '销货方纳税人识别号', '账号', '地址',
21 '开户银行', '主管税务机关及代码', '吨位', '限乘人数')],
22 consts.IC_CLASSIFY: [('申请ID', '姓名', '公民身份号码', '出生年月', '住址', '性别', '民族'),
23 ('申请ID', '有效期限', '签发机关')],
24 consts.RP_CLASSIFY: [('申请ID', '姓名', '公民身份号码', '出生年月', '住址', '性别'),
25 ('申请ID', '有效期限', '签发机关')],
26 consts.BC_CLASSIFY: [('申请ID', '发卡行名称', '银行卡号', '银行卡类型', '持卡人姓名')],
27 consts.BL_CLASSIFY: [('申请ID', '统一社会信用代码', '名称', '类型', '法定代表人', '成立日期', '营业期限', '注册资本',
28 '住所', '经营范围')],
29 consts.UCI_CLASSIFY: [('申请ID', '发票代码', '发票号码', '开票日期', '车价合计小写', '发票联', '买方单位/个人',
30 '买方单位代码/身份证号码', '车架号', '车价合计大写', '二手车市场', '发票章有无', '车牌照号',
31 '登记证号', '买方单位/住址', '车辆类型', '厂牌型号', '转入地车辆管理所名称', '卖方单位/个人',
32 '卖方单位代码/身份证号码', '卖方单位/个人住址')],
33 consts.EEP_CLASSIFY: [('申请ID', '姓名', '证件号码', '换证次数(签发次数)', '有效期限', '出生日期', '性别',
34 '签发机关', '签发地点')],
35 consts.DL_CLASSIFY: [('申请ID', '1 号牌号码', '3 所有人', '5 使用性质', '7 车辆识别代号', '9 注册日期', '10 发证日期',
36 '2 车辆类型', '4 住址', '6 品牌型号', '8 发动机号码'),
37 ('申请ID', '1 号牌号码', '11 档案编号', '12 核定载人数', '13 总质量', '14 整备质量',
38 '15 核对载质量', '16 外廓尺寸', '17 准牵引总质量')],
39 consts.PP_CLASSIFY: [('申请ID', '类型/Type', '姓名/Name', '护照号码/Passport No', '有效期至/Date of expiry',
40 '签发日期/Date of issue', '国家码/Country Code', '性别/Sex', '国籍/Nationality',
41 '出生日期/Date of birth', '出生地点/Place of birth', '签发地点/Place of issue')],
42 consts.MVC_CLASSIFY: [('申请ID', '机动车所有人/身份证明名称/号码', '登记日期', '车辆识别代号/车架号', '车辆出厂日期',
43 '发证日期', '使用性质', '车辆获得方式', '机动车登记编号', '车辆类型', '车辆品牌', '车辆型号',
44 '车身颜色', '国产/进口', '发动机号', '发动机型号', '制造厂名称', '登记机关', '机动车登记证书编号'),
45 ('姓名/名称', '身份证明名称/号码', '转移登记日期')],
46 consts.VAT_CLASSIFY: [('申请ID', '发票代码', '发票代码(开具)', '发票号码', '发票号码(开具)', '开票日期', '校验码',
47 '货物或应税劳务、服务名称', '开具金额合计(不含税)', '税率', '税额合计', '价税合计(小写)',
48 '价税合计(大写)', '购买方名称', '购买方纳税人识别号', '购买方地址、电话', '购买方开户行及账号',
49 '销售方名称', '销售方纳税人识别号', '销售方地址、电话', '销售方开户行及账号', '销售方:(章)',
50 '备注')],
51 }
52
53 def handle(self, *args, **kwargs):
54 excel_path = '/bmw-ocr/data/license_res.xlsx'
55 wb = Workbook()
56 for classify, (_, name, _, _, _, _) in consts.LICENSE_ORDER:
57 ws = wb.create_sheet(name)
58 headers = self.header_map.get(classify, [])
59 for header in headers:
60 ws.append(header)
61 wb.remove(wb.get_sheet_by_name('Sheet'))
62
63 with open('/bmw-ocr/logs/license_bak.log', 'r', encoding='utf-8') as fp:
64 for line in fp:
65 search_obj = re.search(r'task=(.*) license_summary=(.*)', line)
66 task_str = search_obj.group(1)
67 license_summary = ast.literal_eval(search_obj.group(2))
68 business_type, doc_id_str = task_str.split(consts.SPLIT_STR)
69 doc_id = int(doc_id_str)
70 doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
71 application_id = doc_class.objects.filter(id=doc_id).values_list('application_id', flat=True)
72
73 for classify, (_, name, field_order, side_diff, _, _) in consts.LICENSE_ORDER:
74 license_list = license_summary.get(classify)
75 if not license_list:
76 continue
77 ws = wb.get_sheet_by_name(name)
78 for license_dict in license_list:
79 if classify == consts.IC_CLASSIFY and license_dict.get('类别') == '1': # 居住证处理
80 license_summary.setdefault(consts.RP_CLASSIFY, []).append(license_dict)
81 continue
82 if side_diff:
83 key, field_order_yes, field_order_no = consts.FIELD_ORDER_MAP.get(classify)
84 field_order = field_order_yes if key in license_dict else field_order_no
85 all_value = []
86 for search_field, write_field in field_order:
87 if write_field is None:
88 continue
89 field_value = license_dict.get(search_field, '')
90 if isinstance(field_value, list):
91 all_value.append('\n'.join(field_value))
92 else:
93 all_value.append(field_value)
94 ws.append((application_id[0], *all_value))
95 wb.save(excel_path)
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!