-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommended_product_functions.py
58 lines (42 loc) · 2.45 KB
/
recommended_product_functions.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
import datetime
import database_commands.database_commands as database_commands
class recommended_product_functions():
def __init__(self):
self.dataBase = database_commands.DataBase()
def generate_recommended_products(self, connection, cursor, product_count):
self.dataBase.clear_table(connection, cursor, "recommended_products")
cursor.execute("SELECT id FROM products")
dataBase_response = cursor.fetchall()
if product_count <= 0:
raise IndexError("Product count must be bigger than 0")
if product_count > len(dataBase_response):
raise IndexError("Product count must be smaller than the number of products in the database")
#if it is not an integer, raise TypeError
if type(product_count) != int:
raise TypeError("The product_count must be an integer")
products_sales = []
for product_id in dataBase_response:
sales_last_day = self.get_sales_last_day(connection, cursor, product_id[0])
#print("sales_last_day: ", sales_last_day)
products_sales.append([product_id[0], sales_last_day])
products_sales.sort(key=lambda x: x[1], reverse=True)
for i in range(product_count):
#print("products_sales[i]: ", products_sales[i])
product_id = products_sales[i][0]
product_sales = products_sales[i][1]
#print("product_id: ", product_id, "product_sales: ", product_sales)
self.dataBase.insert_data(connection, cursor, "recommended_products", "product_id, sales_last_day", (product_id, product_sales,))
def get_sales_last_day(self, connection, cursor, product_id):
# Get current time
current_time = datetime.datetime.now()
# Calculate the time 24 hours ago
last_day_time = current_time - datetime.timedelta(days=1)
# Format the last_day_time to a string
last_day_time_str = last_day_time.strftime('%Y-%m-%d %H:%M:%S')
# Execute SQL query to get sales from the last 24 hours
cursor.execute(f"SELECT id FROM sales WHERE sale_time >= '{last_day_time_str}' AND product_id = '{product_id}'")
dataBase_response = cursor.fetchall()
#print("dataBase_response: ", dataBase_response)
sales_last_day = len(dataBase_response)
#print("sales_last_day: ", sales_last_day)
return sales_last_day