-
Notifications
You must be signed in to change notification settings - Fork 0
/
infra_service.py
315 lines (245 loc) · 11.7 KB
/
infra_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
154
155
156
157
158
159
160
161
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
187
188
189
190
191
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine, pool
from sqlalchemy.orm import sessionmaker
from dao_model import Plan, Section, SectionMaterial, Material, Base, Order
from exporter import Exporter
import logging
import json
import traceback
db_engine = create_engine('mysql+pymysql://gztrend:gztrend@localhost:3306/gztrend?charset=utf8mb4', encoding='utf8', echo=True,
pool_size=4)
Base.metadata.bind = db_engine
db_session = sessionmaker(bind=db_engine)
class InfraService:
def __init__(self):
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
def add_plan(self, data):
session = db_session()
plan_data = json.loads(s=data, encoding='utf8')
try:
# insert plan
plan = Plan()
plan.name = plan_data['name'].strip()
plan.description = plan_data['description'].strip()
plan.footer = plan_data['footer'].strip()
plan.comments = plan_data['comments'].strip()
plan.type = plan_data['type'].strip()
plan.status = 1
session.add(plan)
session.flush()
plan_id = plan.plan_id
sections_data = plan_data['sections']
section_idx = 1
# adding sections
for section_data in sections_data:
new_section = Section()
new_section.plan_id = plan_id
new_section.description = section_data['description'].strip()
new_section.layer = 1
new_section.sequence = section_idx
new_section.name = section_data['name'].strip()
new_section.status = 1
session.add(new_section)
session.flush()
materials_data = section_data['materials']
priorities_dict = {}
# adding materials to section
for material_data in materials_data:
material_name = material_data['name'].upper().strip()
material_spec = material_data['spec'].upper().strip()
material_brand = material_data['brand'].upper().strip()
material_unit = material_data['unit'].upper().strip()
material_unit_price = material_data['unit_price'].strip()
material_comments = material_data['comments'].strip()
material = session.query(Material).filter(Material.name == material_name,
Material.spec == material_spec,
Material.brand == material_brand,
Material.unit == material_unit,
Material.unit_price == material_unit_price,
Material.comments == material_comments,
Material.status == 1).first()
if material is None:
material = Material()
material.name = material_name
material.spec = material_spec
material.brand = material_brand
material.unit = material_unit
material.unit_price = material_unit_price
material.comments = material_comments
session.add(material)
session.flush()
sequence = material_data['sequence']
section_material = SectionMaterial()
section_material.section_id = new_section.section_id
section_material.material_id = material.material_id
section_material.sequence = sequence
section_material.default_quantity = material_data['default_quantity']
section_material.is_fixed_amount = material_data['is_fixed_amount']
section_material.is_fixed_price = material_data['is_fixed_price']
section_material.price_formula = material_data['price_formula']
if priorities_dict.has_key(sequence) is False:
priorities_dict[sequence] = 0
priority = priorities_dict[sequence]
section_material.priority = priority
priorities_dict[sequence] = priority + 1
section_material.status = 1
session.add(section_material)
session.flush()
session.commit()
except Exception:
traceback.print_exc()
session.rollback()
raise
finally:
session.close()
print data
return
def get_plan_summary(self, type):
session = db_session()
if type is None or type.strip() == '':
plans = session.query(Plan).filter(Plan.status == 1).all()
else:
if type == '_NULL':
type = None
plans = session.query(Plan).filter(Plan.type == type, Plan.status == 1).all()
result = []
for plan in plans:
cur_summary = dict()
cur_summary['plan_id'] = plan.plan_id
cur_summary['name'] = plan.name
result.append(cur_summary)
session.close()
return result
def get_sections(self, plan_id):
session = db_session()
result = dict()
if plan_id is None:
result['errcode'] = 1
result['msg'] = 'plan_id is None'
plan = session.query(Plan).filter(Plan.plan_id == plan_id, Plan.status == 1).one()
result['name'] = plan.name
result['section_relation'] = plan.section_relation
result['description'] = plan.description
result['footer'] = plan.footer
result['comments'] = plan.comments
sections = session.query(Section).filter(Section.plan_id == plan_id, Section.status == 1).all()
section_list = list()
for section in sections:
section_info = dict()
section_info['section_id'] = section.section_id
section_info['name'] = section.name
section_info['sequence'] = section.sequence
section_list.append(section_info)
result['sections'] = section_list
session.close()
return result
def get_details(self, plan_id, section_ids):
session = db_session()
result = dict()
if plan_id is None:
result['errcode'] = 1
result['msg'] = 'plan_id is None'
if section_ids is None:
result['errcode'] = 2
result['msg'] = 'section_ids is None'
plan = session.query(Plan).filter(Plan.plan_id == plan_id, Plan.status == 1).one()
result['plan_id'] = plan_id
result['name'] = plan.name
result['section_relation'] = plan.section_relation
result['description'] = plan.description
result['footer'] = plan.footer
result['comments'] = plan.comments
section_results = list()
result['sections'] = section_results
sections = session.query(Section).order_by(Section.sequence).filter(Section.section_id.in_(section_ids), Section.status == 1).all()
for section in sections:
# get session_materials per section
section_materials = session.query(SectionMaterial) \
.order_by(SectionMaterial.sequence.asc(), SectionMaterial.priority.asc()) \
.filter(SectionMaterial.section_id == section.section_id, SectionMaterial.status == 1).all()
# construct material_ids to get concrete materials
material_ids = list()
for section_material in section_materials:
material_ids.append(section_material.material_id)
materials = session.query(Material).filter(Material.material_id.in_(material_ids)).all()
material_results = list()
material_tmp_dict = dict() # dict to put material info instances
material_grouped_dict = dict()
# 1st stage: adding material info itself
for material in materials:
material_result = dict()
material_tmp_dict[material.material_id] = material_result
material_result['material_id'] = material.material_id
material_result['name'] = material.name
material_result['type'] = material.comments
material_result['spec'] = material.spec
material_result['brand'] = material.brand
material_result['unit'] = material.unit
material_result['unit_price'] = material.unit_price
material_result['comments'] = material.comments
max_sequence = 0
# 2nd stage: adding section-related info
for section_material in section_materials:
sequence = section_material.sequence
material_result = material_tmp_dict[section_material.material_id]
material_result['is_fixed_amount'] = section_material.is_fixed_amount
material_result['priority'] = section_material.priority
material_result['sequence'] = sequence
material_result['default_quantity'] = section_material.default_quantity
material_result['is_fixed_amount'] = section_material.is_fixed_amount
material_result['is_fixed_price'] = section_material.is_fixed_price
material_result['price_formula'] = section_material.price_formula
if sequence not in material_grouped_dict:
material_grouped_dict[sequence] = list()
if sequence > max_sequence:
max_sequence = sequence
material_grouped_dict[sequence].append(material_result)
for i in range(1, max_sequence + 1):
material_results.append(material_grouped_dict[i])
# adding section info
section_result = dict()
section_result['section_id'] = section.section_id
section_result['name'] = section.name
section_result['sequence'] = section.sequence
section_result['description'] = section.description
section_result['layer'] = section.layer
section_result['materials'] = material_results
section_results.append(section_result)
result['errcode'] = 0
result['msg'] = 'ok'
session.close()
return result
def get_type(self):
session = db_session()
types = session.query(Plan.type).group_by(Plan.type).all()
result = []
for type in types:
result.append(type.type)
result.sort()
session.close()
return result
def save_order(self, content):
session = db_session()
order = Order()
order.content = content
session.add(order)
session.commit()
session.close()
return order.order_id
def export_detail(self, order_id):
session = db_session()
order = session.query(Order).filter(Order.order_id == order_id).one()
exporter = Exporter()
output = exporter.export_detail(order.content, order.order_id.__str__() + '_内部版')
session.close()
return output
def export_summary(self, order_id):
session = db_session()
session.close()
return
def run(self):
session = db_session()
plan = session.query(Plan).one()
session.close()
return plan