def lock(self): # 获取当前所有的需要的属性 pid = request.params["pid"] sid = request.params["sid"] price = request.params["price"] orderno = request.params["orderno"] # 从Play中获取目标对象 play = Play.get(pid) # 判断目标排期是否存在 if not play: return Code.play_does_not_exist, request.params # 票价判断:不能低于最低价 if price < play.lowest_price: return Code.prcice_less_than_the_lowest_price, request.params # 座位锁定操作 locked_seat_num = PlaySeat.lock(orderno, pid, sid) # 如果没有完成坐定操作,报错 if not locked_seat_num: return Code.seat_lock_failed, {} # 表单创建 order = Order.create(play.cid, pid, sid) # 修改第三方的订单号 order.sell_order_no = orderno # 修改订单的状态 order.status = OrderStatus.locked.value # 修改订单锁定的座位数 order.ticket_num = locked_seat_num # 执行保存操作 order.save() # 返回锁定的座位数目 return {"locked_seats_num": locked_seat_num}
def print_ticket(self): orderno = request.params['orderno'] ticket_flag = request.params['ticket_flag'] seats = request.params['sid'] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, {'orderno': orderno} # 订单是否已打印 if order.status == OrderStatus.printed.value: return Code.ticket_printed_already, {} # 订单是否支付 if order.status != OrderStatus.paid.value: return Code.order_not_paid_yet, {} # 核对订单码 if not order.validate(ticket_flag): return Code.ticket_flag_error, {'ticket_flag': ticket_flag} printed_num = PlaySeat.print_tickets(order.seller_order_no, order.pid, seats) if not printed_num: return Code.ticket_print_failed.value, {} order.status = OrderStatus.printed.value order.printed_time = datetime.now() order.save() return {'printed_num': printed_num}
def print_ticket(self): '''取票接口''' orderno = request.params['orderno'] ticket_flag = request.params['ticket_flag'] seats = request.params['sid'] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, {'orderno': orderno} if order.status == OrderStatus.printed.value: return Code.ticket_printed_already, {} if order.status != OrderStatus.paid.value: return Code.order_not_paid_yet, {} if not order.validate(ticket_flag): return Code.ticket_flag_error, {'ticket_flag': ticket_flag} # 修改座位状态为已取票 printed_num = PlaySeat.print_tickets(orderno, order.pid, seats) if not printed_num: return Code.ticket_print_failed, {} # 设置订单状态为已取票 order.status = OrderStatus.printed.value # 设置取票时间 order.printed_time = datetime.now() order.save() return {'printed_num': printed_num}
def buy(self): # pid = request.params["pid"] # sid = request.params["sid"] seats = request.params['seats'] orderno = request.params["orderno"] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, {'orderno': orderno} if order.status != OrderStatus.locked.value: return Code.order_status_error, {} sid_list = [] for sid, handle_fee, price in seats: order.amount += handle_fee + price sid_list.append(sid) bought_seats_num = PlaySeat.buy(orderno, order.pid, sid_list) if not bought_seats_num: return Code.seat_buy_failed, {} order.paid_time = datetime.now() order.status = OrderStatus.paid.value order.gen_ticket_flag() order.save() return { 'bought_seats_num': bought_seats_num, 'ticket_flag': order.ticket_flag, }
def lock(self): # 使用Request.Params["id"]来获取参数是一种比较有效的途径。 pid = request.params['pid'] sid = request.params['sid'] price = request.params['price'] orderno = request.params['orderno'] # 获取到场次 play = Play.get(pid) if not play: return Code.play_does_not_exist, request.params if price < play.lowest_price: return Code.prcice_less_than_the_lowest_price, request.params # lock 是定义这个类,创建的而方法 locked_seats_num = PlaySeat.lock(orderno, pid, sid) # 如果没有锁定数量 if not locked_seats_num: # 为啥返回的是空字典 return Code.seat_lock_failed, {} # 定义了一个create方法,创造订单 # play.cid影院的排期,排期id,座位id order = Order.create(play.cid, pid, sid) order.seller_order_no = orderno # OrderStatus 是自定义的方法,锁定的值为1 order.status = OrderStatus.locked.value # 订单的票数 = 自定义的 lock方法的返回值 是那个操作数 order.ticket_num = locked_seats_num # 这个save 是自定义了一个类,然后多继承 order.save() return {'locked_seats_num': locked_seats_num}
def unlock(self): # 获取对应的值 pid = request.params["pid"] sid = request.params["sid"] orderno = request.params["orderno"] # 获取目标对象 play = Play.get(pid) # 判断是否存在 if not play: return Code.play_does_not_exist, request.params # 获取目标对象 order = Order.getby_orderno(orderno) # 判断是否存在 if not order: return Code.order_does_not_exist, request.params # 解锁的座位数目 unlock_seats_num = PlaySeat.unlock(orderno, pid, sid) # 判断解锁操作是否执行 if not unlock_seats_num: return Code.seat_unlock_failed, {} # 修改状态 order.status = OrderStatus.unlocked.value # 执行修改保存 order.save() return {"unlock_seats_num": unlock_seats_num}
def print_ticket(self): orderno = request.params["orderno"] ticket_flag = request.params["ticket_flag"] seats = request.params["sid"] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, {"orderno": orderno} # 判断是否出票 if order.status == OrderStatus.printed.value: return Code.ticket_printed_already, {} # 订单是否支付 if order.status != OrderStatus.paid.value: return Code.order_not_paid_yet, {} # 判断取票码 if not order.validate(ticket_flag): return Code.ticket_flag_error, {"ticket_flag": ticket_flag} printed_num = PlaySeat.print_ticket(order.sell_order_no, order.pid, seats) if not printed_num: return Code.ticket_print_failed, {} order.status = OrderStatus.printed.value order.print_time = datetime.now() order.save() return { "printed_num": printed_num, }
def ticket_info(self): orderno = request.params['orderno'] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, {'orderno': orderno} order.play = Play.get(order.pid) order.movie = Movie.get(order.play.mid) order.tickets = PlaySeat.getby_orderno(orderno) return order
def unlock(self): pid = request.params['pid'] sid = request.params['sid'] orderno = request.params['orderno'] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist if order.status != OrderStatus.locked.value: return Code.order_status_error unlock_seats_num = PlaySeat.unlock(orderno, pid, sid) if not unlock_seats_num: return Code.seat_unlock_failed order.status = OrderStatus.unlocked.value order.save() return {'unlocked_seats_num': unlock_seats_num}
def unlock(self): pid = request.params["pid"] sid = request.params["sid"] orderno = request.params["orderno"] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, {'orderno': orderno} if order.status != OrderStatus.locked.value: return Code.order_status_error, {} unlocked_seats_num = PlaySeat.unlock(orderno, pid, sid) if not unlocked_seats_num: return Code.seat_unlock_failed, {} order.status = OrderStatus.unlocked.value order.save() return {'unlocked_seats_num': unlocked_seats_num}
def unlock(self): pid = request.params['pid'] sid = request.params['sid'] orderno = request.params['orderno'] play = Play.get(pid) if not play: return Code.play_does_not_exist, request.params order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, request.params unlock_seats_num = PlaySeat.unlock(orderno, pid, sid) if not unlock_seats_num: return Code.seat_lock_failed, {} order.status = OrderStatus.unlocked.value order.save() return {'unlock_seats_num': unlock_seats_num}
def lock(self): pid = request.params['pid'] sid = request.params['sid'] price = request.params['price'] orderno = request.params['orderno'] play = Play.get(pid) if not play: return Code.play_does_not_exist if price < play.lowest_price: return Code.prcice_less_than_the_lowest_price locked_seats_num = PlaySeat.lock(orderno, pid, sid) if not locked_seats_num: return Code.seat_lock_failed order = Order.create(play.cid, pid, sid) order.seller_order_no = orderno order.status = OrderStatus.locked.value order.tickets_num = locked_seats_num order.save() return {'locked_seats_num': locked_seats_num}
def lock(self): pid = request.params["pid"] sid = request.params["sid"] price = request.params["price"] orderno = request.params["orderno"] play = Play.get(pid) if not play: return Code.play_does_not_exist, {"pid": pid} if price < play.lowest_price: return Code.prcice_less_than_the_lowest_price, {"price": price} locked_seat_num = PlaySeat.lock(orderno, pid, sid) if not locked_seat_num: return Code.seat_lock_failed, {"lock_seat_num": locked_seat_num} order = Order.create(play.cid, pid, sid) order.seller_order_no = orderno order.status = OrderStatus.locked.value order.save() return {"locked_seat_num": locked_seat_num}
def refund_ticket(self): orderno = request.params['orderno'] ticket_flag = request.params['ticket_flag'] seats = request.params['sid'] order = Order.getby_orderno(orderno) if not order: return Code.order_does_not_exist, {'orderno': orderno} if order.status == OrderStatus.printed.value: return Code.ticket_printed_already, {} if order.status != OrderStatus.paid.value: return Code.order_not_paid_yet, {} if not order.validate(ticket_flag): return Code.ticket_flag_error, {'ticket_flag': ticket_flag} refund_num = PlaySeat.refund(orderno, order.pid, seats) if not refund_num: return Code.ticket_refund_failed, {} order.status = OrderStatus.refund.value order.refund_time = datetime.now() order.save() return {'refund_num': refund_num}
def buy(self): seats = request.params["seats"] orderno = request.params['orderno'] order = Order.getby_orderno(orderno) # 判断目标order是否存在 if not order: return Code.order_does_not_exist, request.params # 判断订单状态是否位锁定 if order.status != OrderStatus.locked.value: # 不是锁定状态报错 return Code.order_status_error, { "orderno": orderno, "status": order.status, } # 修改订单 order.sell_order_no = request.params["orderno"] order.amount = order.amount or 0 # 座位id sid_list = [] # 计算价钱 for sid, handle_fee, price in seats: sid_list.append(sid) order.amount += handle_fee + price # 座位购买数量 bought_seats_num = PlaySeat.buy(orderno, order.pid, sid_list) # 判断操作是否成功 if not bought_seats_num: return Code.seat_buy_failed, {} order.ticket_num = len(seats) order.status = OrderStatus.paid.value order.paid_time = datetime.now() order.gen_ticket_flag() order.save() return { "bought_seats_num": bought_seats_num, "ticket_flag": order.ticket_flag, }
def create_test_data(cls, cinema_num=10, hall_num=10, play_num=10): # from tigereye.models.cinerma import Cinema from tigereye.models.hall import Hall from tigereye.models.movie import Movie from tigereye.models.seat import Seat, PlaySeat from tigereye.models.play import Play from tigereye.models.order import Order f = faker.Faker('zh_CN') screen_types = ['普通', 'IMAX'] audio_types = ['普通', '杜比环绕'] cinemas = [] for i in range(1, cinema_num + 1): cinema = Cinema() cinema.cid = i cinema.name = '%s影城' % f.name() cinema.address = f.address() cinema.status = 1 # cinema.save() cinema.put() cinemas.append(cinema) Cinema.commit() halls = [] for cinema in cinemas: for n in range(1, hall_num + 1): hall = Hall() hall.cid = cinema.cid hall.name = '%s号厅' % n hall.screen_type = random.choice(screen_types) hall.audio_type = random.choice(audio_types) hall.seats_num = 25 hall.status = 1 hall.put() halls.append(hall) Hall.commit() plays = [] for hall in halls: hall.seats = [] for s in range(1, hall.seats_num + 1): seat = Seat() seat.cid = cinema.cid seat.hid = hall.hid seat.x = s % 5 % 5 seat.y = ceil(s / 5) seat.row = seat.x seat.column = seat.y seat.seat_type = 1 seat.put() hall.seats.append(seat) Seat.commit() for p in range(1, play_num + 1): play = Play() playcid = cinema.cid play.hid = hall.hid play.mid = p play.start_time = datetime.now() play.duration = 3600 play.price_type = 1 play.price = 7000 play.market_price = 5000 for n in range(1, hall_num + 1): hall = Hall() hall.cid = cinema.cid hall.name = '%s号厅' % n hall.screen_type = random.choice(screen_types) hall.audio_type = random.choice(audio_types) hall.seats_num = 25 # hall.seats = '' hall.status = 1 hall.save() seats = [] for s in range(1, hall.seats_num + 1): seat = Seat() seat.cid = cinema.cid seat.hid = hall.hid seat.x = s % 5 or 5 seat.y = ceil(s / 5) seat.row = seat.x seat.column = seat.y seat.seat_type = 1 seat.put() seats.append(seat) Seat.commit() for p in range(1, play_num + 1): play = Play() play.cid = cinema.cid play.hid = hall.hid play.mid = p play.start_time = datetime.now() play.duration = 3600 play.price_type = 1 play.price = 7000 play.market_price = 5000 play.lowest_price = 3000 play.status = 1 play.save() for seat in seats: ps = PlaySeat() ps.pid = play.pid ps.copy(seat) ps.put() PlaySeat.commit()
def create_test_data(cls, cinema_num=10, hall_num=10, play_num=10): HALL_SEATS_NUM = 25 start_time = time.time() from tigereye.models.hall import Hall from tigereye.models.movie import Movie from tigereye.models.play import Play from tigereye.models.seat import Seat, PlaySeat from tigereye.models.order import Order f = faker.Faker('zh_CN') screen_types = ['普通', 'IMAX'] audio_types = ['普通', '杜比环绕'] cinemas = [] for i in range(1, cinema_num + 1): cinema = Cinema() cinema.cid = i cinema.name = '%s影城' % f.name() cinema.address = f.address() cinema.status = 1 cinema.put() cinemas.append(cinema) Cinema.commit() halls = [] plays = [] seats = [] for cinema in cinemas: for n in range(1, hall_num + 1): hall = Hall() hall.cid = cinema.cid hall.name = '%s号厅' % n hall.screen_type = random.choice(screen_types) hall.audio_type = random.choice(audio_types) hall.seats_num = HALL_SEATS_NUM hall.status = 1 hall.put() halls.append(hall) Hall.commit() for hall in halls: hall.seats = [] for s in range(1, hall.seats_num + 1): seat = Seat() seat.cid = hall.cid seat.hid = hall.hid seat.x = s % 5 or 5 seat.y = math.ceil(s / 5) seat.row = seat.x seat.column = seat.y seat.seat_type = 1 seat.put() hall.seats.append(seat) Seat.commit() for p in range(1, play_num + 1): play = Play() play.cid = cinema.cid play.hid = hall.hid play.mid = p play.start_time = datetime.now() play.duration = 3600 play.price_type = 1 play.price = 7000 play.market_price = 5000 play.lowest_price = 3000 play.status = 1 play.put() play.hall = hall plays.append(play) Play.commit() for play in plays: for seat in play.hall.seats: ps = PlaySeat() ps.pid = play.pid ps.copy(seat) ps.put() PlaySeat.commit() current_app.logger.info('create test data done! cost %.2f seconds' % (time.time() - start_time))
def create_test_data(cls, cinema_num=10, hall_num=10, play_num=10): from tigereye.models.hall import Hall from tigereye.models.movie import Movie from tigereye.models.order import Order from tigereye.models.seat import Seat, PlaySeat from tigereye.models.play import Play time1 = time.time() f = faker.Faker('zh_CN') screen_types = ['普通', 'IMAX'] audio_types = ['普通', '杜比环绕'] cm_name = ['影院', '影城', '国际影城', '国际影院'] for i in range(1, cinema_num + 1): cinema = Cinema() cinema.cid = i cinema.name = f.company()[:-6] + random.choice(cm_name) cinema.address = f.address() cinema.status = 1 print(cinema.name) cinema.save() for n in range(1, hall_num + 1): hall = Hall() hall.cid = cinema.cid hall.name = '%s号厅' % n hall.screen_type = random.choice(screen_types) hall.audio_type = random.choice(audio_types) hall.seats_num = 25 hall.status = 1 hall.save() seats = [] for s in range(1, hall.seats_num + 1): seat = Seat() seat.cid = cinema.cid seat.hid = hall.hid seat.x = s % 5 or 5 seat.y = ceil(s / 5) seat.row = seat.x seat.column = seat.y seat.seat_type = 1 seat.put() seats.append(seat) Seat.commit() for p in range(1, play_num + 1): play = Play() play.cid = cinema.cid play.hid = hall.hid play.mid = p play.start_time = datetime.now() play.duration = 3600 play.price_type = 1 play.price = 7000 play.market_price = 5000 play.lowest_price = 3000 play.status = 1 play.save() for seat in seats: ps = PlaySeat() ps.pid = play.pid ps.copy(seat) ps.put() PlaySeat.commit() current_app.logger.info('create test data done! %.2f s' % (time.time() - time1))
def create_test_data(cls,cinema_num = 3,hall_num = 3,play_num = 10): import time from tigereye.models.hall import Hall from tigereye.models.seat import Seat,PlaySeat,SeatType from tigereye.models.movie import Movie from tigereye.models.cinema import Cinema from tigereye.models.play import Play from faker import Faker import math from datetime import datetime start_time = time.time() HALL_SEATS_NUM = 25 faker = Faker('zh_CN') cinemas = [] for i in range(1,cinema_num+1): cinema = Cinema() cinema.name = '%s影城' % faker.name() cinema.address = faker.address() cinema.status = 1 cinema.put() cinemas.append(cinema) Cinema.commit() halls = [] plays = [] for cinema in cinemas: for n in range(1,hall_num+1): hall = Hall() hall.cid = cinema.cid hall.name = '%s 号厅' % n hall.screen_type = "IMAX" hall.audio_type = '杜比环绕' hall.seats_num = HALL_SEATS_NUM hall.status = 1 hall.put() halls.append(hall) Hall.commit() for hall in halls: hall.seats = [] for x in range(1,HALL_SEATS_NUM+1): seat = Seat() seat.cid = hall.cid seat.hid = hall.hid seat.x = x % 5 or 5 seat.y = math.ceil(x / 5) seat.row = '%s 排' % seat.x seat.column = '%s 列' % seat.y seat.seat_type = SeatType.single.value seat.status = SeatStatus.ok.value seat.put() hall.seats.append(seat) Seat.commit() for p in range(1,play_num+1): play = Play() play.cid = hall.cid play.hid = hall.hid play.mid = p play.start_time = datetime.now() play.duration = 3600 play.price_type = 1 play.price = 70000 play.market_price = 5000 play.lowest_price = 3000 play.status = 1 play.put() play.hall = hall plays.append(play) Play.commit() for play in plays: for seat in play.hall.seats: ps = PlaySeat() ps.pid = play.pid ps.copy(seat) ps.put() PlaySeat.commit() for i in range(10): m = Movie() m.name = '电影名称 %s' % (i +1) m.language = '英文' m.subtitle = '中文' m.mode = '数字' m.vision = '3D' m.screen_size = 'IMAX' m.introduction = '哈哈哈' m.status = 1 m.save() print('create test data done! cost %s seconds' % (time.time() - start_time))
def create_test_data(cls, cinema_num=10, hall_num=10, play_num=10): start_time = time.time() from tigereye.models.seat import Seat, PlaySeat from tigereye.models.play import Play from tigereye.models.hall import Hall f = faker.Faker("zh_CN") screen_types = ["普通", "IMAX"] audio_types = ["普通", "杜比音效"] cinemas = [] for i in range(1, cinema_num + 1): cinema = Cinema() cinema.cid = i cinema.name = "%s影院" % f.name() cinema.address = f.address() cinema.status = 1 cinema.put() cinemas.append(cinema) Cinema.commit() halls = [] for cinema in cinemas: for n in range(1, hall_num + 1): hall = Hall() hall.cid = cinema.cid hall.name = "%s号厅" % n hall.screen_type = random.choice(screen_types) hall.audio_type = random.choice(audio_types) hall.seats_num = 25 hall.status = 1 hall.put() halls.append(hall) Hall.commit() plays = [] for hall in halls: hall.seats = [] for s in range(1, hall.seats_num + 1): seat = Seat() seat.cid = hall.cid seat.hid = hall.hid seat.x = s % 5 or 5 seat.y = ceil(s / 5) seat.row = seat.x seat.column = seat.y seat.seat_type = 1 seat.put() hall.seats.append(seat) Seat.commit() for p in range(1, play_num + 1): play = Play() play.cid = cinema.cid play.hid = hall.hid play.mid = p play.start_time = datetime.now() play.duration = 3600 play.price_type = 1 play.price = 7000 play.market_price = 5000 play.lowest_price = 3000 play.status = 1 play.hall = hall play.put() plays.append(play) Play.commit() for play in plays: for seat in play.hall.seats: ps = PlaySeat() ps.pid = play.pid ps.copy(seat) ps.put() PlaySeat.commit() current_app.logger.info("screate test data done: %f" % (time.time() - start_time))