add DDA excel
Showing
1 changed file
with
185 additions
and
0 deletions
1 | import re | ||
2 | import os | ||
3 | import datetime | ||
4 | from openpyxl import Workbook | ||
5 | from django.core.management import BaseCommand | ||
6 | from common.mixins import LoggerMixin | ||
7 | from apps.doc.models import DDARecords | ||
8 | from apps.doc import consts | ||
9 | |||
10 | |||
11 | class Command(BaseCommand, LoggerMixin): | ||
12 | |||
13 | def __init__(self): | ||
14 | super().__init__() | ||
15 | self.sheet_info = [ | ||
16 | ('今日新建', | ||
17 | ('Application No', 'DDA Found?', 'DDA Found Time', 'ID Found?', 'ID Found Time', 'BC Found?', | ||
18 | 'BC Found Time'), | ||
19 | ('application_id', 'is_dda_found', 'is_id_found', 'is_bc_found', 'dda_found_time', 'id_found_time', | ||
20 | 'bc_found_time')), | ||
21 | ('今日完成', | ||
22 | ('Application No', 'DDA Found Time', 'ID Found Time', 'BC Found Time', 'Create Time'), | ||
23 | ('application_id', 'dda_found_time', 'id_found_time', 'bc_found_time', 'create_time')), | ||
24 | ('累计进行中', | ||
25 | ('Application No', 'DDA Found?', 'DDA Found Time', 'ID Found?', 'ID Found Time', 'BC Found?', | ||
26 | 'BC Found Time', 'Create Time'), | ||
27 | ('application_id', 'is_dda_found', 'is_id_found', 'is_bc_found', 'dda_found_time', 'id_found_time', | ||
28 | 'bc_found_time', 'create_time')), | ||
29 | ] | ||
30 | self.month_sheet_info = [ | ||
31 | ('本月新建', | ||
32 | ('Application No', 'DDA Found?', 'DDA Found Time', 'ID Found?', 'ID Found Time', 'BC Found?', | ||
33 | 'BC Found Time'), | ||
34 | ('application_id', 'is_dda_found', 'is_id_found', 'is_bc_found', 'dda_found_time', 'id_found_time', | ||
35 | 'bc_found_time')), | ||
36 | ('本月完成', | ||
37 | ('Application No', 'DDA Found Time', 'ID Found Time', 'BC Found Time', 'Create Time'), | ||
38 | ('application_id', 'dda_found_time', 'id_found_time', 'bc_found_time', 'create_time')), | ||
39 | ('累计进行中', | ||
40 | ('Application No', 'DDA Found?', 'DDA Found Time', 'ID Found?', 'ID Found Time', 'BC Found?', | ||
41 | 'BC Found Time', 'Create Time'), | ||
42 | ('application_id', 'is_dda_found', 'is_id_found', 'is_bc_found', 'dda_found_time', 'id_found_time', | ||
43 | 'bc_found_time', 'create_time')), | ||
44 | ] | ||
45 | self.dda_dir = os.path.join('SF5-CL-S-1', 'DDA') | ||
46 | self.excel_name_base = 'DDA_Data' | ||
47 | # self.complete_dda_dir = os.path.join(self.dda_dir, 'complete') | ||
48 | # self.wanting_dda_dir = os.path.join(self.dda_dir, 'wanting') | ||
49 | # self.dda_img_prefix = '{0}_0'.format(consts.DDA_FIELD) | ||
50 | # self.id_img_prefix = '{0}_0'.format(consts.IC_FIELD) | ||
51 | # self.bc_img_prefix = '{0}_0'.format(consts.BC_FIELD) | ||
52 | |||
53 | def add_arguments(self, parser): | ||
54 | parser.add_argument( | ||
55 | '--date', | ||
56 | default=datetime.date.today() - datetime.timedelta(days=1), | ||
57 | dest='date', | ||
58 | help='将要计算的日期,格式: 2018-01-01' | ||
59 | ) | ||
60 | |||
61 | def handle(self, *args, **kwargs): | ||
62 | date = kwargs.get('date') | ||
63 | if isinstance(date, str): | ||
64 | if not re.match(r'\d{4}-\d{2}-\d{2}', date): | ||
65 | print('date format error') | ||
66 | return | ||
67 | date_str = date | ||
68 | else: | ||
69 | date_str = date.strftime('%Y-%m-%d') | ||
70 | |||
71 | target_date = datetime.datetime.strptime(date_str, '%Y-%m-%d') | ||
72 | next_date = target_date + datetime.timedelta(days=1) | ||
73 | do_month = True if next_date.day == 1 else False | ||
74 | wanting_querysets = None | ||
75 | |||
76 | excel_name = '{0}({1}).xlsx'.format(self.excel_name_base, target_date) | ||
77 | excel_path = os.path.join(self.dda_dir, excel_name) | ||
78 | |||
79 | wb = Workbook() | ||
80 | for sheet_name, header, query_fields in self.sheet_info: | ||
81 | ws = wb.create_sheet(sheet_name) | ||
82 | ws.append(header) | ||
83 | |||
84 | if sheet_name == '今日新建': | ||
85 | querysets = DDARecords.objects.filter(create_time__gt=target_date, | ||
86 | create_time__lt=next_date).values(*query_fields) | ||
87 | for queryset in querysets: | ||
88 | col_values = ( | ||
89 | queryset.get('application_id', ''), | ||
90 | queryset.get('is_dda_found', 'False'), | ||
91 | queryset.get('dda_found_time', ''), | ||
92 | queryset.get('is_id_found', 'False'), | ||
93 | queryset.get('id_found_time', ''), | ||
94 | queryset.get('is_bc_found', 'False'), | ||
95 | queryset.get('bc_found_time', ''), | ||
96 | ) | ||
97 | ws.append(col_values) | ||
98 | elif sheet_name == '今日完成': | ||
99 | querysets = DDARecords.objects.filter(update_time__gt=target_date, | ||
100 | update_time__lt=next_date, | ||
101 | all_found=True).values(*query_fields) | ||
102 | for queryset in querysets: | ||
103 | col_values = ( | ||
104 | queryset.get('application_id', ''), | ||
105 | queryset.get('dda_found_time', ''), | ||
106 | queryset.get('id_found_time', ''), | ||
107 | queryset.get('bc_found_time', ''), | ||
108 | queryset.get('create_time', ''), | ||
109 | ) | ||
110 | ws.append(col_values) | ||
111 | else: | ||
112 | querysets = DDARecords.objects.filter(all_found=True).values(*query_fields) | ||
113 | if do_month: | ||
114 | wanting_querysets = queryset | ||
115 | |||
116 | for queryset in querysets: | ||
117 | col_values = ( | ||
118 | queryset.get('application_id', ''), | ||
119 | queryset.get('is_dda_found', 'False'), | ||
120 | queryset.get('dda_found_time', ''), | ||
121 | queryset.get('is_id_found', 'False'), | ||
122 | queryset.get('id_found_time', ''), | ||
123 | queryset.get('is_bc_found', 'False'), | ||
124 | queryset.get('bc_found_time', ''), | ||
125 | queryset.get('create_time', ''), | ||
126 | ) | ||
127 | ws.append(col_values) | ||
128 | |||
129 | wb.remove(wb.get_sheet_by_name('Sheet')) | ||
130 | wb.save(excel_path) | ||
131 | |||
132 | if do_month: | ||
133 | target_month = target_date.month | ||
134 | target_year = target_date.year | ||
135 | month_excel_name = '{0}({1}-{2}).xlsx'.format(self.excel_name_base, target_year, target_month) | ||
136 | month_excel_path = os.path.join(self.dda_dir, month_excel_name) | ||
137 | |||
138 | month_wb = Workbook() | ||
139 | for sheet_name, header, query_fields in self.month_sheet_info: | ||
140 | ws = month_wb.create_sheet(sheet_name) | ||
141 | ws.append(header) | ||
142 | |||
143 | if sheet_name == '本月新建': | ||
144 | querysets = DDARecords.objects.filter(create_time__year=target_year, | ||
145 | create_time__month=target_month).values(*query_fields) | ||
146 | for queryset in querysets: | ||
147 | col_values = ( | ||
148 | queryset.get('application_id', ''), | ||
149 | queryset.get('is_dda_found', 'False'), | ||
150 | queryset.get('dda_found_time', ''), | ||
151 | queryset.get('is_id_found', 'False'), | ||
152 | queryset.get('id_found_time', ''), | ||
153 | queryset.get('is_bc_found', 'False'), | ||
154 | queryset.get('bc_found_time', ''), | ||
155 | ) | ||
156 | ws.append(col_values) | ||
157 | elif sheet_name == '本月完成': | ||
158 | querysets = DDARecords.objects.filter(update_time__year=target_year, | ||
159 | update_time__month=target_month, | ||
160 | all_found=True).values(*query_fields) | ||
161 | for queryset in querysets: | ||
162 | col_values = ( | ||
163 | queryset.get('application_id', ''), | ||
164 | queryset.get('dda_found_time', ''), | ||
165 | queryset.get('id_found_time', ''), | ||
166 | queryset.get('bc_found_time', ''), | ||
167 | queryset.get('create_time', ''), | ||
168 | ) | ||
169 | ws.append(col_values) | ||
170 | else: | ||
171 | for queryset in wanting_querysets: | ||
172 | col_values = ( | ||
173 | queryset.get('application_id', ''), | ||
174 | queryset.get('is_dda_found', 'False'), | ||
175 | queryset.get('dda_found_time', ''), | ||
176 | queryset.get('is_id_found', 'False'), | ||
177 | queryset.get('id_found_time', ''), | ||
178 | queryset.get('is_bc_found', 'False'), | ||
179 | queryset.get('bc_found_time', ''), | ||
180 | queryset.get('create_time', ''), | ||
181 | ) | ||
182 | ws.append(col_values) | ||
183 | |||
184 | month_wb.remove(month_wb.get_sheet_by_name('Sheet')) | ||
185 | month_wb.save(month_excel_path) |
-
Please register or sign in to post a comment