-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderoperation.py
185 lines (151 loc) · 6.26 KB
/
orderoperation.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
import os
import csv
import random
import time
import matplotlib.pyplot as plt
class OrderOperation:
@staticmethod
def generate_unique_order_id():
order_id = "o_" + str(random.randint(10000, 99999))
while OrderOperation.check_duplicate_order_id(order_id):
order_id = "o_" + str(random.randint(10000, 99999))
return order_id
@staticmethod
def check_duplicate_order_id(order_id):
with open("data/orders.txt", "r") as file:
lines = file.readlines()
for line in lines:
order = eval(line)
if order["order_id"] == order_id:
return True
return False
@staticmethod
def create_an_order(customer_id, product_id, create_time=None):
if create_time is None:
create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
order_id = OrderOperation.generate_unique_order_id()
order_data = {"order_id": order_id, "customer_id": customer_id, "product_id": product_id, "create_time": create_time}
with open("data/orders.txt", "a") as file:
file.write(str(order_data) + "\n")
return True
@staticmethod
def delete_order(order_id):
with open("data/orders.txt", "r") as file:
lines = file.readlines()
found = False
with open("data/orders.txt", "w") as file:
for line in lines:
order = eval(line)
if order["order_id"] != order_id:
file.write(line)
else:
found = True
return found
@staticmethod
def get_order_list(customer_id, page_number):
orders_per_page = 10
with open("data/orders.txt", "r") as file:
lines = file.readlines()
customer_orders = []
for line in lines:
order = eval(line)
if order["customer_id"] == customer_id:
customer_orders.append(order)
total_pages = (len(customer_orders) + orders_per_page - 1) // orders_per_page
start_index = (page_number - 1) * orders_per_page
end_index = min(start_index + orders_per_page, len(customer_orders))
customer_orders = customer_orders[start_index:end_index]
return customer_orders, page_number, total_pages
@staticmethod
def generate_test_order_data():
customer_ids = []
with open("data/customers.txt", "r") as file:
lines = file.readlines()
for line in lines:
customer = eval(line)
customer_ids.append(customer["customer_id"])
product_ids = []
with open("data/products.txt", "r") as file:
lines = file.readlines()
for line in lines:
product = eval(line)
product_ids.append(product["pro_id"])
for _ in range(10):
customer_id = random.choice(customer_ids)
num_orders = random.randint(50, 200)
for _ in range(num_orders):
product_id = random.choice(product_ids)
OrderOperation.create_an_order(customer_id, product_id)
@staticmethod
def generate_single_customer_consumption_figure(customer_id):
consumption_per_month = {i: 0 for i in range(1, 13)}
with open("data/orders.txt", "r") as file:
lines = file.readlines()
for line in lines:
order = eval(line)
if order["customer_id"] == customer_id:
month = int(order["create_time"].split("-")[1])
consumption_per_month[month] += OrderOperation.get_product_price(order["product_id"])
months = list(consumption_per_month.keys())
consumption = list(consumption_per_month.values())
plt.plot(months, consumption)
plt.xlabel("Month")
plt.ylabel("Consumption")
plt.title("Customer {} Consumption".format(customer_id))
plt.savefig("data/figure/single_customer_consumption.png")
plt.close()
@staticmethod
def generate_all_customers_consumption_figure():
consumption_per_month = {i: 0 for i in range(1, 13)}
with open("data/orders.txt", "r") as file:
lines = file.readlines()
for line in lines:
order = eval(line)
month = int(order["create_time"].split("-")[1])
consumption_per_month[month] += OrderOperation.get_product_price(order["product_id"])
months = list(consumption_per_month.keys())
consumption = list(consumption_per_month.values())
plt.plot(months, consumption)
plt.xlabel("Month")
plt.ylabel("Consumption")
plt.title("All Customers Consumption")
plt.savefig("data/figure/all_customers_consumption.png")
plt.close()
@staticmethod
def generate_all_top_10_best_sellers_figure():
product_sales = {}
with open("data/orders.txt", "r") as file:
lines = file.readlines()
for line in lines:
order = eval(line)
product_id = order["product_id"]
if product_id in product_sales:
product_sales[product_id] += 1
else:
product_sales[product_id] = 1
sorted_sales = sorted(product_sales.items(), key=lambda x: x[1], reverse=True)
top_10_products = sorted_sales[:10]
products = []
sales = []
for product, sale in top_10_products:
products.append(product)
sales.append(sale)
plt.bar(products, sales)
plt.xlabel("Product ID")
plt.ylabel("Sales")
plt.title("Top 10 Best-Selling Products")
plt.savefig("data/figure/top_10_best_sellers.png")
plt.close()
@staticmethod
def delete_all_orders():
with open("data/orders.txt", "w") as file:
pass
@staticmethod
def get_product_price(product_id):
with open("data/products.txt", "r") as file:
lines = file.readlines()
for line in lines:
product = eval(line)
if product["pro_id"] == product_id:
return float(product["pro_current_price"])
return 0.0