강상위

Add getAvailableFacilities()

- 사용가능한 시설물 없을 시 처리포함
...@@ -27,7 +27,7 @@ import datetime ...@@ -27,7 +27,7 @@ import datetime
27 # 성공시 : tuple형태로 반환 => ( (1 ,"steven123", 7, "전자정보대학", "136호", 10, datetime.datetime(2018,12,5,12,0,0), datetime.datetime(2018,12,5,12,10,0), ), ) 27 # 성공시 : tuple형태로 반환 => ( (1 ,"steven123", 7, "전자정보대학", "136호", 10, datetime.datetime(2018,12,5,12,0,0), datetime.datetime(2018,12,5,12,10,0), ), )
28 # 순서 : reservations_id, user_id, facility_id, location, location_detail, capacity, start_time, end_time 28 # 순서 : reservations_id, user_id, facility_id, location, location_detail, capacity, start_time, end_time
29 29
30 - # 실패시 : tuple형태로 반환 => ( ("SQL Error!", ), ) 30 + # 실패시 : result[0][0]="SQL Error!"인 tuple 반환 => ( ("SQL Error!", ), )
31 # 사용예: 31 # 사용예:
32 # result = getReservations("khucse123") 32 # result = getReservations("khucse123")
33 # reservation_start_time = result[0][3] 33 # reservation_start_time = result[0][3]
...@@ -50,6 +50,7 @@ def getReservations(user_id): ...@@ -50,6 +50,7 @@ def getReservations(user_id):
50 db.close() 50 db.close()
51 51
52 52
53 +
53 # deleteReservations - 대여현황삭제 함수 54 # deleteReservations - 대여현황삭제 함수
54 # input: 55 # input:
55 # int reservations_id 56 # int reservations_id
...@@ -111,3 +112,49 @@ def addReservations(user_id, facility_id, start_time, end_time): ...@@ -111,3 +112,49 @@ def addReservations(user_id, facility_id, start_time, end_time):
111 finally: 112 finally:
112 db.close() 113 db.close()
113 114
115 +
116 +
117 +
118 +# getAvailableFacilities - 사용가능시설물 반환함수
119 +# input:
120 + # string location
121 + # int capacity
122 + # datetime.datetime start_time
123 + # datetime.datetime end_time
124 +# output:
125 + # 성공시 : tuple형태로 반환 => ( (1, "전자정보대학", "B01호", 40, "강의실", "빔프로젝터", ), )
126 + # 순서 : facility_id, location, location_detail, capacity, facility_type, equipment
127 + # 사용가능한 시설물이 없을 시 : result[0][0]="NoAvailableFacilites"인 tuple 반환 => ( ("NoAvailableFacilites", ), )
128 +
129 + # 실패시 : result[0][0]="SQL Error!"인 tuple 반환 => ( ("SQL Error!", ), )
130 +# 사용예:
131 + # tstart = datetime.datetime(2018,12,5,12,11,12)
132 + # tend = datetime.datetime(2018,12,5,14,20,12)
133 + # result = getAvailableFacilities("전자정보대학", 40, tstart, tend)
134 + # facility_id = result[0][0]
135 +
136 +def getAvailableFacilities(location, capacity, start_time, end_time):
137 + try:
138 + db = pymysql.connect(host='1.201.139.92', port=3306, user=user_info.user_id, password=user_info.user_passwd, db='FRS', charset='utf8')
139 + curs = db.cursor()
140 +
141 + curs.execute("select facility_id, location, location_detail, capacity, facility_type, equipment from facilities where (facility_id not in(select facility_id from reservations where (start_time<=%s AND %s<end_time) OR (start_time<%s AND %s<=end_time) OR (%s<=start_time AND end_time<=%s))) AND location=%s AND %s<=capacity;",
142 + (start_time, start_time, end_time, end_time, start_time, end_time, location, capacity))
143 +
144 + result = curs.fetchall()
145 + print("Fetch Success!")
146 +
147 + # 사용 가능한 시설물이 있을 경우
148 + if(len(result)!=0):
149 + return result
150 + # 사용 가능한 시설물이 없을 경우
151 + else:
152 + print("No Available Facilities")
153 + return (("NoAvailableFacilites",),)
154 +
155 + except:
156 + print("SQL Error!")
157 + return (("SQL Error!",),)
158 +
159 + finally:
160 + db.close()
......