Software_Engineering_Field_.../SiteManagementSystem(Softwa.../Service/PythonServiceFile/demo.py

150 lines
6.6 KiB
Python
Raw Normal View History

2024-11-05 23:06:55 +08:00
from cstime import *
2024-11-04 22:26:45 +08:00
from layer import Layer
2024-11-03 11:21:47 +08:00
from field import Field
from user import User
2024-11-05 21:04:21 +08:00
from field_record import FieldRecord
2024-11-03 11:21:47 +08:00
class Service:
2024-11-05 23:06:55 +08:00
layer = Layer(LAYER) # 初始化数据库数据并启动增删查功能
# 普通用户操作:
# 请求一条场地使用信息普通用户自动传入自己的用户id管理员用户手动选择传入用户id #redis
# 参数cs_fieldRecord(用户id场地id开始时间结束时间) 返回bool 是否预约成功
2024-11-05 23:14:31 +08:00
def add_appointment(self, cs_fieldRecord) -> bool:
2024-11-05 23:06:55 +08:00
fieldRecord = FieldRecord(cs_fieldRecord)
records = self.layer.find_record("Fid", fieldRecord.fid)
# 如果用户已经有预约了,就不能再提交预约
if len(self.layer.find_record("Uid", fieldRecord.uid)) != 0:
2024-11-04 22:26:45 +08:00
raise ValueError("Already has an appointment")
else:
2024-11-05 23:06:55 +08:00
# 检查场地此时是否有空
2024-11-04 22:26:45 +08:00
for x in records:
2024-11-05 23:06:55 +08:00
if not ((x.end_time >= fieldRecord.start_time) or (x.start_time <= fieldRecord.end_time)):
2024-11-04 22:26:45 +08:00
raise ValueError("Field busy")
2024-11-05 23:06:55 +08:00
# 添加记录
2024-11-04 22:26:45 +08:00
self.layer.add_record(fieldRecord)
return True
2024-11-05 23:06:55 +08:00
# 提前结束场地的使用 正在使用时提前结束 #finished
# 参数cs_user //cs_fieldRecord 返回bool 是否结束成功
2024-11-05 23:14:31 +08:00
def end_appointment(self, cs_user) -> bool:
2024-11-05 23:06:55 +08:00
user = User(cs_user)
records = self.layer.find_record("Uid", user.id)
if len(records) == 0:
2024-11-04 22:26:45 +08:00
raise ValueError("No appointment")
2024-11-05 23:06:55 +08:00
elif len(records) != 1:
2024-11-04 22:26:45 +08:00
raise ValueError("Appointment nums error")
else:
fid = records[0].fid
uid = user.id
start_time = records[0].start_time
2024-11-05 23:06:55 +08:00
# 获取当前时间
curtime = Time()
# 正在使用时可以提前结束,修改结束时间
if curtime >= start_time and curtime < records[0].end_time:
fieldRecord = FieldRecord(fid, uid, start_time, curtime)
2024-11-04 22:26:45 +08:00
self.layer.remove_record(records[0])
self.layer.add_record(fieldRecord)
return True
else:
raise ValueError("No ongoing appointment")
2024-11-05 23:06:55 +08:00
# 取消预约
2024-11-04 22:26:45 +08:00
# 参数cs_user //cs_fieldRecord(用户id场地id开始时间结束时间) 返回bool 是否取消成功
2024-11-05 23:14:31 +08:00
def cancel_appointment(self, cs_user) -> bool:
2024-11-05 23:06:55 +08:00
user = User(cs_user)
records = self.layer.find_record("Uid", user.id)
if len(records) == 0:
2024-11-04 22:26:45 +08:00
raise ValueError("No appointment or appointment is finished")
2024-11-05 23:06:55 +08:00
elif len(records) != 1:
2024-11-04 22:26:45 +08:00
raise ValueError("Appointment nums error")
else:
2024-11-05 23:06:55 +08:00
curtime = Time()
if curtime < records[0].start_time:
fieldRecord = FieldRecord(records[0])
2024-11-04 22:26:45 +08:00
return True
else:
raise ValueError("Appointment has started")
2024-11-05 23:06:55 +08:00
# 延长场地的使用时间 #redis
2024-11-04 22:26:45 +08:00
# 参数cs_fieldRecord(用户id场地id(null),开始时间(null),结束时间) 返回bool 是否延长成功
2024-11-05 23:14:31 +08:00
def extend_appointment(self, cs_fieldRecord) -> bool:
2024-11-05 23:06:55 +08:00
field_Record = FieldRecord(cs_fieldRecord)
records = self.layer.find_record("Uid", field_Record.uid)
if len(records) == 0:
2024-11-04 22:26:45 +08:00
raise ValueError("No appointment or appointment is finished")
2024-11-05 23:06:55 +08:00
elif len(records) != 1:
2024-11-04 22:26:45 +08:00
raise ValueError("Appointment nums error")
else:
2024-11-05 23:06:55 +08:00
curtime = Time()
start_time = records[0].start_time
end_time = records[0].end_time
if curtime >= start_time and curtime < end_time:
addrecord = FieldRecord(field_Record.uid, records[0].fid, start_time, field_Record.end_time)
2024-11-04 22:26:45 +08:00
self.layer.add_record(addrecord)
return True
else:
raise ValueError("Appointment is finished or not started")
2024-11-05 23:06:55 +08:00
# 查询场地占用
2024-11-04 22:26:45 +08:00
# 参数cs_fieldRecord(用户id(null)场地id开始时间(null),结束时间(null)) 返回list_time [(start_time,end_time),(...),(...),...]
2024-11-05 23:14:31 +08:00
def query_fieldUsage(self, cs_fieldRecord) -> []:
2024-11-05 23:06:55 +08:00
fid = FieldRecord(cs_fieldRecord).fid
records = self.layer.find_record("Fid", fid)
list_time: List[(Time, Time)] = []
2024-11-04 22:26:45 +08:00
for x in records:
2024-11-05 23:06:55 +08:00
tup = (x.start_time.struct_time, x.end_time.struct_time)
2024-11-04 22:26:45 +08:00
list_time.append(tup)
# 使用 sorted() 函数进行排序key 参数指定用于排序的键函数
2024-11-05 21:04:21 +08:00
sorted_time_list = sorted(list_time, key=lambda x: x[0])
2024-11-05 23:06:55 +08:00
# 从后往前遍历
for x in range(len(sorted_time_list) - 1, 0, -1):
2024-11-04 22:26:45 +08:00
strtm2 = sorted_time_list[x][0]
strtm1 = sorted_time_list[x - 1][1]
2024-11-05 23:06:55 +08:00
if Time(strtm2) - Time(strtm1) <= Duration.from_minutes(10):
2024-11-04 22:26:45 +08:00
tup = (sorted_time_list[x - 1][0], sorted_time_list[x][1])
sorted_time_list.pop(x)
sorted_time_list[x - 1] = tup
return sorted_time_list
2024-11-05 23:06:55 +08:00
# 查询用户的预约记录
2024-11-04 22:26:45 +08:00
# 参数cs_user(id) 返回array_cs_fieldRecord
2024-11-05 23:14:31 +08:00
def query_appointment_record(self, cs_user) -> []:
2024-11-05 23:06:55 +08:00
user = User(cs_user)
cs_fieldRecords = []
fieldRecords = self.layer.find_record("Uid", user.id)
if len(fieldRecords) == 1:
2024-11-05 21:04:21 +08:00
cs_fieldRecords.append(FieldRecord(fieldRecords[0]))
2024-11-05 23:06:55 +08:00
elif len(fieldRecords) != 0:
2024-11-05 21:04:21 +08:00
raise ValueError("Appointment nums error")
2024-11-05 23:06:55 +08:00
fieldRecords = self.layer.find_finished_record("Uid", user.id)
if len(fieldRecords) != 0:
2024-11-05 21:04:21 +08:00
for x in fieldRecords:
cs_fieldRecords.append(FieldRecord.parse_to_csharp_object(FieldRecord(x)))
return cs_fieldRecords
2024-11-05 23:06:55 +08:00
# 管理员操作:(包含普通用户操作以及一下特殊操作)
2024-11-05 21:04:21 +08:00
2024-11-05 23:06:55 +08:00
# 添加一个场地
# 参数cs_field(id,name,position,open_time,type) 返回bool 是否添加成功
2024-11-05 23:10:06 +08:00
def add_field(self, cs_field) -> bool:
2024-11-03 11:21:47 +08:00
field = Field(cs_field)
2024-11-05 21:04:21 +08:00
fields = self.layer.find_field("Name", field.name)
if len(fields) > 0:
raise ValueError("Duplicated name")
2024-11-03 11:21:47 +08:00
self.layer.add_field(field)
2024-11-05 21:04:21 +08:00
return True
2024-11-04 22:26:45 +08:00
2024-11-05 23:06:55 +08:00
# 删除一个场地
2024-11-04 22:26:45 +08:00
# 参数cs_field(id,name,position(null),open_time(null),type(null)) 返回bool 是否删除成功
2024-11-05 23:14:31 +08:00
def remove_field(self, cs_field) -> bool:
2024-11-05 23:06:55 +08:00
field = Field(cs_field)
fields = self.layer.find_field("Id", field.id)
2024-11-05 21:04:21 +08:00
if len(fields) == 0:
raise ValueError("No such field")
if len(fields) != 1:
raise ValueError("Field id duplication")
self.layer.remove_field(field)
return True