-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPro2.py
More file actions
243 lines (223 loc) · 7.61 KB
/
Pro2.py
File metadata and controls
243 lines (223 loc) · 7.61 KB
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
import sys
import psycopg2 as pg
from psycopg2.extensions import *
from Task1 import StockIn
from Task2 import PlaceOrder
from Task3 import UpdateOrder
from Task4 import DeleteOrder
from Select import SelectItem
methods = {}
connected = False
output = open('output.txt', 'w')
def connect_db():
global connected
try:
conn = pg.connect(database='project_2', user='postgres',
password='123456', host='localhost', port=5432)
connected = True
conn.autocommit = False
return conn
except Exception:
return None
def create_tables():
global conn
cur: cursor = conn.cursor()
cur.execute('''create table if not exists center
(
id integer primary key,
name varchar not null unique
);''')
cur.execute('''create table if not exists product
(
id integer primary key,
number char(8) not null,
model varchar unique,
name varchar not null,
unit_price numeric not null
);''')
cur.execute('''create table if not exists enterprise
(
id integer primary key,
name varchar not null unique,
country varchar not null,
city varchar,
supply_center varchar not null,
industry varchar not null,
foreign key (supply_center) references center (name)
);''')
cur.execute('''create table if not exists staff
(
id integer primary key,
name varchar not null,
age integer not null,
gender varchar not null check ( gender in ('Male', 'Female')),
number char(8) not null unique,
supply_center varchar not null,
mobile_number char(11) not null,
type varchar check ( type in ('Director', 'Supply Staff', 'Salesman', 'Contracts Manager')),
foreign key (supply_center) references center (name)
);''')
cur.execute('''create table if not exists stockIn
(
id integer primary key,
supply_center varchar not null,
product_model varchar not null,
supply_staff char(8) not null,
date date not null,
purchase_price numeric not null,
quantity integer not null,
foreign key (supply_center) references center (name),
foreign key (product_model) references product (model),
foreign key (supply_staff) references staff (number)
);''')
cur.execute('''create table if not exists orders
(
id serial primary key,
contract_num char(10) not null,
enterprise varchar not null,
product_model varchar not null,
quantity integer not null,
contract_manager varchar not null,
contract_date date not null,
estimate_delivery_date date not null,
lodgement_date date,
salesman_num char(8) not null,
contract_type varchar not null,
foreign key (enterprise) references enterprise (name),
foreign key (product_model) references product (model),
foreign key (salesman_num) references staff (number),
foreign key (contract_manager) references staff (number)
);''')
cur.execute('''
create table if not exists stock
(
model varchar not null,
center varchar not null,
quantity integer not null,
current_quantity integer not null,
primary key (model, center)
);''')
cur.close()
conn.commit()
def import_data():
global conn
cur: cursor = conn.cursor()
drop_tables()
create_tables()
center = open('center.csv')
center.readline()
enterprise = open('enterprise.csv')
enterprise.readline()
product = open('model.csv')
product.readline()
staff = open('staff.csv')
staff.readline()
cur.copy_expert('copy center from stdin with (format csv)', center)
cur.copy_expert('copy enterprise from stdin with (format csv)', enterprise)
cur.copy_expert('copy product from stdin with (format csv)', product)
cur.copy_expert('copy staff from stdin with (format csv)', staff)
center.close()
enterprise.close()
product.close()
staff.close()
cur.close()
conn.commit()
t1 = StockIn(conn)
t1.insert_data()
t2 = PlaceOrder(conn)
t2.insert_data()
t3 = UpdateOrder(conn)
t3.update_data()
t4 = DeleteOrder(conn)
t4.delete_data()
def drop_tables():
global conn
cur: cursor = conn.cursor()
cur.execute('drop table if exists product, enterprise, center, staff, stockIn, orders, contracts, stock;')
cur.close()
conn.commit()
def get_format(result: list):
m = [(max(len(str(a[i])) for a in result) + 1) for i in range(len(result[0]))]
s = [('%-' + str(i) + 's') for i in m]
out = ''.join(s)
return out
if __name__ == '__main__':
conn: connection
conn = connect_db()
if conn is None:
print('connection failed')
sys.exit(1)
methods = {}
drop_tables()
create_tables()
import_data()
# t1 = StockIn(conn)
# t1.insert_data()
# t2 = PlaceOrder(conn)
# t2.insert_data()
# t3 = UpdateOrder(conn)
# t3.update_data()
# t4 = DeleteOrder(conn)
# t4.delete_data()
select = SelectItem(conn)
methods.setdefault('Q6', select.getAllStaffCount)
methods.setdefault('Q7', select.getContractCount)
methods.setdefault('Q8', select.getOrderCount)
methods.setdefault('Q9', select.getNeverSoldProductCount)
methods.setdefault('Q10', select.getFavoriteProductModel)
methods.setdefault('Q11', select.getAvgStockByCenter)
methods.setdefault('Q12', select.getProductByNumber)
methods.setdefault('Q13', select.getContractInfo)
output = open('output.txt', 'w')
commands = ['Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11']
product_nums = input().split(',')
contract_nums = input().split(',')
for command in commands:
output_str = ''
result = methods.get(command)()
if command in ['Q7', 'Q8', 'Q9']:
output_str += (command + '\t')
output_str += str(result[0][0]) + '\n'
else:
out = get_format(result)
output_str += (command + '\n')
for i in result:
i = [str(k) for k in i]
output_str += (out % tuple(i))
output_str += '\n'
print(output_str, end='')
output.write(output_str)
# Q12
for product_num in product_nums:
output_str = ''
result = methods.get('Q12')(product_num)
output_str += ('Q12' + '\n')
result.insert(0, ['supply_center', 'product_number', 'product_model', 'purchase_price', 'quantity'])
out = get_format(result)
for i in result:
i = [str(k) for k in i]
output_str += (out % tuple(i))
output_str += '\n'
print(output_str, end='')
# Q13
for contract_num in contract_nums:
output_str = ''
result, result1 = methods.get('Q13')(contract_num)
if not result:
result = ['None', 'None', 'None', 'None']
output_str += ('Q13' + '\n')
output_str += ('number: ' + str(result[0]) + '\n')
output_str += ('manager: ' + str(result[1]) + '\n')
output_str += ('enterprise: ' + str(result[2]) + '\n')
output_str += ('supply_center: ' + str(result[3]) + '\n')
result1.insert(0, ['product_model', 'salesman', 'quantity', 'unit_price', 'estimate_delivery_date',
'lodgement_date'])
out = get_format(result1)
for i in result1:
i = [str(k) for k in i]
output_str += (out % tuple(i))
output_str += '\n'
print(output_str, end='')
output.flush()
output.close()
conn.close()