idcard monthly script
Showing
2 changed files
with
68 additions
and
13 deletions
... | @@ -14,7 +14,7 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -14,7 +14,7 @@ class Command(BaseCommand, LoggerMixin): |
14 | 14 | ||
15 | def __init__(self): | 15 | def __init__(self): |
16 | super().__init__() | 16 | super().__init__() |
17 | self.sheet_names = (consts.AFC_PREFIX, consts.HIL_PREFIX) | 17 | self.sheet_name = '身份证' |
18 | self.header = ('申请号', '身份证号', '民族', '时间戳') | 18 | self.header = ('申请号', '身份证号', '民族', '时间戳') |
19 | 19 | ||
20 | def add_arguments(self, parser): | 20 | def add_arguments(self, parser): |
... | @@ -35,21 +35,26 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -35,21 +35,26 @@ class Command(BaseCommand, LoggerMixin): |
35 | else: | 35 | else: |
36 | date_str = date.strftime('%Y-%m-%d') | 36 | date_str = date.strftime('%Y-%m-%d') |
37 | 37 | ||
38 | excel_dir = os.path.join(conf.DATA_DIR, 'AFC', 'Logs') | 38 | afc_excel_dir = os.path.join(conf.DATA_DIR, 'AFC', 'IdCard') |
39 | if not os.path.exists(excel_dir): | 39 | hil_excel_dir = os.path.join(conf.DATA_DIR, 'HIL', 'IdCard') |
40 | print('excel dir not exists') | 40 | if not os.path.exists(afc_excel_dir) or not os.path.exists(hil_excel_dir): |
41 | print('excel_dir not exist') | ||
41 | return | 42 | return |
42 | excel_path = os.path.join(excel_dir, 'idcard_{0}.xlsx'.format(date_str)) | 43 | |
43 | log_path = os.path.join(conf.LOG_DIR, 'idcard.log.{0}'.format(date_str)) | 44 | log_path = os.path.join(conf.LOG_DIR, 'idcard.log.{0}'.format(date_str)) |
44 | if not os.path.exists(log_path): | 45 | if not os.path.exists(log_path): |
45 | print('log_path not exists') | 46 | print('log_path not exists') |
46 | return | 47 | return |
47 | 48 | ||
48 | wb = Workbook() | 49 | wb_afc = Workbook() |
49 | for name in self.sheet_names: | 50 | ws_afc = wb_afc.create_sheet(self.sheet_name) |
50 | ws = wb.create_sheet(name) | 51 | ws_afc.append(self.header) |
51 | ws.append(self.header) | 52 | wb_afc.remove(wb_afc.get_sheet_by_name('Sheet')) |
52 | wb.remove(wb.get_sheet_by_name('Sheet')) | 53 | |
54 | wb_hil = Workbook() | ||
55 | ws_hil = wb_hil.create_sheet(self.sheet_name) | ||
56 | ws_hil.append(self.header) | ||
57 | wb_hil.remove(wb_hil.get_sheet_by_name('Sheet')) | ||
53 | 58 | ||
54 | with open(log_path, 'r', encoding='utf-8') as fp: | 59 | with open(log_path, 'r', encoding='utf-8') as fp: |
55 | for line in fp: | 60 | for line in fp: |
... | @@ -73,8 +78,14 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -73,8 +78,14 @@ class Command(BaseCommand, LoggerMixin): |
73 | doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc | 78 | doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc |
74 | application_id = doc_class.objects.filter(id=int(doc_id_str)).values_list('application_id', flat=True) | 79 | application_id = doc_class.objects.filter(id=int(doc_id_str)).values_list('application_id', flat=True) |
75 | 80 | ||
76 | ws = wb.get_sheet_by_name(business_type) | 81 | if business_type == consts.HIL_PREFIX: |
82 | for id_num, nation in content_list: | ||
83 | ws_hil.append((application_id[0], id_num, nation, time_str)) | ||
84 | else: | ||
77 | for id_num, nation in content_list: | 85 | for id_num, nation in content_list: |
78 | ws.append((application_id[0], id_num, nation, time_str)) | 86 | ws_afc.append((application_id[0], id_num, nation, time_str)) |
79 | 87 | ||
80 | wb.save(excel_path) | 88 | afc_excel_path = os.path.join(afc_excel_dir, 'idcard_{0}.xlsx'.format(date_str)) |
89 | hil_excel_path = os.path.join(hil_excel_dir, 'idcard_{0}.xlsx'.format(date_str)) | ||
90 | wb_afc.save(afc_excel_path) | ||
91 | wb_hil.save(hil_excel_path) | ... | ... |
1 | import os | ||
2 | import datetime | ||
3 | from calendar import monthrange | ||
4 | from openpyxl import Workbook, load_workbook | ||
5 | from django.core.management import BaseCommand | ||
6 | from settings import conf | ||
7 | from common.mixins import LoggerMixin | ||
8 | |||
9 | |||
10 | class Command(BaseCommand, LoggerMixin): | ||
11 | |||
12 | def __init__(self): | ||
13 | super().__init__() | ||
14 | self.dirs = ('AFC', 'HIL') | ||
15 | |||
16 | def handle(self, *args, **kwargs): | ||
17 | now_time = datetime.datetime.now() | ||
18 | end_day_in_mouth = now_time.replace(day=1) | ||
19 | pre_mouth = end_day_in_mouth - datetime.timedelta(days=1) | ||
20 | |||
21 | for target_dir in self.dirs: | ||
22 | excel_dir = os.path.join(conf.DATA_DIR, target_dir, 'IdCard') | ||
23 | if not os.path.exists(excel_dir): | ||
24 | print('excel dir not exists: {0}'.format(excel_dir)) | ||
25 | return | ||
26 | |||
27 | monthly_wb = Workbook() | ||
28 | |||
29 | for d in range(1, monthrange(pre_mouth.year, pre_mouth.month)[1] + 1): | ||
30 | date_str = '{:04d}-{:02d}-{:02d}'.format(pre_mouth.year, pre_mouth.month, d) | ||
31 | daily_excel_path = os.path.join(excel_dir, 'idcard_{0}.xlsx'.format(date_str)) | ||
32 | if not os.path.exists(daily_excel_path): | ||
33 | print('daily excel path not exists: {0}'.format(daily_excel_path)) | ||
34 | continue | ||
35 | |||
36 | monthly_ws = monthly_wb.create_sheet(date_str) | ||
37 | daily_wb = load_workbook(daily_excel_path) | ||
38 | daily_ws = daily_wb.get_sheet_by_name('身份证') | ||
39 | for row in daily_ws.iter_rows(min_row=1, values_only=True): | ||
40 | monthly_ws.append(row) | ||
41 | |||
42 | monthly_excel_path = os.path.join(excel_dir, 'idcard_{0}.xlsx'.format(pre_mouth.strftime('%Y-%m'))) | ||
43 | monthly_wb.remove(monthly_wb.get_sheet_by_name('Sheet')) | ||
44 | monthly_wb.save(monthly_excel_path) |
-
Please register or sign in to post a comment