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

150 lines
6.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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