29 lines
1010 B
Python
29 lines
1010 B
Python
import uuid
|
|
|
|
from custom_decorators import singleton
|
|
from repositories.order import OrderRepository
|
|
from utils.datetime import current, current_timestamp, is_time_difference_greater_than
|
|
|
|
|
|
@singleton
|
|
class OrderService:
|
|
def __init__(self):
|
|
self.order_repo = OrderRepository()
|
|
|
|
def create_order(self, from_address, to_address, *args, **kwargs):
|
|
date_str = current().strftime('%Y%m%d%H%M%S')
|
|
unique_id = str(uuid.uuid4()).split('-')[0]
|
|
order_id = f"{date_str}-{unique_id}"
|
|
self.order_repo.create(order_id, from_address, to_address)
|
|
return order_id
|
|
|
|
def finish_order(self, order_id):
|
|
# 判断支付时间是否超过订单存活时间
|
|
order_creation_time = self.order_repo.get_creation_time(order_id)
|
|
if is_time_difference_greater_than(current_timestamp(), order_creation_time, minutes=15):
|
|
status = 2
|
|
else:
|
|
status = 1
|
|
self.order_repo.update_status(order_id, status)
|
|
return status
|