142 lines
5.4 KiB
Python
142 lines
5.4 KiB
Python
from flask import render_template, request, g, redirect, url_for, session, jsonify
|
|
from .config import db
|
|
import pymysql
|
|
import csv
|
|
import io
|
|
|
|
def index():
|
|
if request.method == 'GET':
|
|
if not g.user:
|
|
return redirect(url_for("login"))
|
|
flightID = request.args.get('flightID')
|
|
|
|
if flightID:
|
|
conn = pymysql.connect(**db)
|
|
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
|
|
|
search_sql = """SELECT * FROM Flights WHERE ID = %s"""
|
|
cursor.execute(search_sql, (flightID, ))
|
|
flight = cursor.fetchone()
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
return render_template(
|
|
'index.html',
|
|
flight=flight,
|
|
username=g.user
|
|
)
|
|
else:
|
|
return render_template('index.html', username=g.user)
|
|
|
|
def modify():
|
|
if request.method == 'POST':
|
|
flight_id = request.form.get('flightID')
|
|
first_class_change = int(request.form.get('first_class_change', 0))
|
|
business_class_change = int(request.form.get('business_class_change', 0))
|
|
economy_class_change = int(request.form.get('economy_class_change', 0))
|
|
|
|
first_class_price = float(request.form.get('first_class_price', 0))
|
|
business_class_price = float(request.form.get('business_class_price', 0))
|
|
economy_class_price = float(request.form.get('economy_class_price', 0))
|
|
|
|
status = request.form.get('status', '未知')
|
|
|
|
conn = pymysql.connect(**db)
|
|
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
|
|
|
try:
|
|
# 查询当前座位数
|
|
cursor.execute("SELECT First_class_seats_remaining, Business_class_seats_remaining, Economy_class_seats_remaining FROM Flights WHERE ID = %s", (flight_id,))
|
|
current_seats = cursor.fetchone()
|
|
|
|
new_first_class_seats = current_seats['First_class_seats_remaining'] + first_class_change
|
|
new_business_class_seats = current_seats['Business_class_seats_remaining'] + business_class_change
|
|
new_economy_class_seats = current_seats['Economy_class_seats_remaining'] + economy_class_change
|
|
|
|
# 检查余座数是否为负值
|
|
if new_first_class_seats < 0 or new_business_class_seats < 0 or new_economy_class_seats < 0:
|
|
return jsonify({'message': '座位变化后余座数不能为负值'}), 400
|
|
|
|
# 更新座位数和价格
|
|
update_sql = """
|
|
UPDATE Flights
|
|
SET First_class_seats_remaining = %s, Business_class_seats_remaining = %s, Economy_class_seats_remaining = %s,
|
|
First_class_price = %s, Business_class_price = %s, Economy_class_price = %s, Status = %s
|
|
WHERE ID = %s
|
|
"""
|
|
cursor.execute(update_sql, (new_first_class_seats, new_business_class_seats, new_economy_class_seats,
|
|
first_class_price, business_class_price, economy_class_price, status, flight_id))
|
|
conn.commit()
|
|
except Exception as e:
|
|
print(e)
|
|
conn.rollback()
|
|
return jsonify({'message': '数据库错误,请稍后再试'}), 500
|
|
finally:
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
return jsonify({'message': '座位数、价格和状态更新成功'}), 200
|
|
|
|
|
|
def delete_flight():
|
|
if request.method == 'DELETE':
|
|
flight_id = request.args.get('flightID')
|
|
|
|
conn = pymysql.connect(**db)
|
|
cursor = conn.cursor()
|
|
|
|
try:
|
|
delete_sql = "DELETE FROM Flights WHERE ID = %s"
|
|
cursor.execute(delete_sql, (flight_id,))
|
|
conn.commit()
|
|
except Exception as e:
|
|
print(e)
|
|
conn.rollback()
|
|
return jsonify({'message': '数据库错误,请稍后再试', 'success': False}), 500
|
|
finally:
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
return jsonify({'message': '航班删除成功', 'success': True}), 200
|
|
|
|
from flask import flash, redirect, url_for, jsonify, request
|
|
import pymysql
|
|
import csv
|
|
import io
|
|
from .config import db
|
|
|
|
def upload_csv():
|
|
if request.method == 'POST':
|
|
file = request.files['file']
|
|
if not file:
|
|
flash('没有文件上传', 'error')
|
|
return redirect(url_for('index'))
|
|
|
|
conn = pymysql.connect(**db)
|
|
cursor = conn.cursor()
|
|
|
|
try:
|
|
stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
|
|
csv_input = csv.reader(stream)
|
|
next(csv_input) # 跳过表头
|
|
|
|
for row in csv_input:
|
|
cursor.execute("""
|
|
INSERT INTO Flights (ID, Airline, Departure_airport, Arrival_airport, Departure_time, Arrival_time,
|
|
First_class_seats_remaining, Business_class_seats_remaining, Economy_class_seats_remaining,
|
|
First_class_price, Business_class_price, Economy_class_price, Status)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
|
""", row)
|
|
conn.commit()
|
|
flash('航班批量添加成功', 'success')
|
|
except Exception as e:
|
|
print(e)
|
|
conn.rollback()
|
|
flash(f'文件处理错误:{e}', 'error')
|
|
finally:
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
return redirect(url_for('index'))
|
|
|