class Movie(db.Model, Model): mid = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True, nullable=False) language = db.Column(db.String(32)) subtitle = db.Column(db.String(32)) # 字幕 show_date = db.Column(db.Date) vision = db.Column(db.String(16)) model = db.Column(db.String(16)) screen_size = db.Column(db.String(16)) introduction = db.Column(db.Text) status = db.Column(db.Integer, default=0, nullable=False, index=True) @classmethod def create_test_data(cls, num=10): for i in range(1, num + 1): m = Movie() m.mid = i m.sn = str(i).zfill(10) m.name = '电影名称%s' % i m.language = '英文' m.subtitle = '中文' # m.show_date = m.mode = '数字' m.vision = '2D' m.screen_size = 'IMAX' m.introduction = 'blahblah哈哈' m.status = 1 db.session.add(m) db.session.commit() current_app.logger.info('movie test data done!')
class Hall(db.Model, Model): hid = db.Column(db.Integer, primary_key=True) cid = db.Column(db.Integer, index=True) name = db.Column(db.String(64), nullable=False) screen_type = db.Column(db.String(32)) audio_type = db.Column(db.String(32)) seats_num = db.Column(db.Integer, default=0, nullable=False) status = db.Column(db.Integer, nullable=False, index=True)
class Play(db.Model, Model): pid = db.Column(db.Integer, primary_key=True) cid = db.Column(db.Integer) hid = db.Column(db.Integer) mid = db.Column(db.Integer) start_time = db.Column(db.DateTime, nullable=False) duration = db.Column(db.Integer, default=0, nullable=False) # 时长 price_type = db.Column(db.Integer) price = db.Column(db.Integer) market_price = db.Column(db.Integer) lowest_price = db.Column(db.Integer) # sever_default,让数据库给个默认值 created_time = db.Column(db.DateTime, server_default=text('CURRENT_TIMESTAMP')) updated_time = db.Column(db.DateTime, onupdate=func.now()) status = db.Column(db.Integer, default=0, nullable=False, index=True)
class PlaySeat(db.Model, Model): psid = db.Column(db.Integer, primary_key=True) orderno = db.Column(db.String(32), index=True) cid = db.Column(db.Integer) hid = db.Column(db.Integer) pid = db.Column(db.Integer) sid = db.Column(db.Integer) x = db.Column(db.Integer) y = db.Column(db.Integer) row = db.Column(db.String(16)) column = db.Column(db.String(16)) area = db.Column(db.String(16)) seat_type = db.Column(db.String(16)) love_seats = db.Column(db.String(16)) status = db.Column(db.Integer, default=0, nullable=False, index=True) lock_time = db.Column(db.DateTime) created_time = db.Column(db.DateTime, server_default=text('CURRENT_TIMESTAMP')) def copy(self, seat): self.sid = seat.sid self.cid = seat.cid self.hid = seat.hid self.x = seat.x self.y = seat.y self.row = seat.row self.column = seat.column self.area = seat.area self.seat_type = seat.seat_type self.love_seats = seat.love_seats self.status = seat.status @classmethod def lock(cls, orderno, pid, sid_list): session = db.create_scoped_session() # 要操作这张表 rows = session.query(PlaySeat).filter( PlaySeat.pid == pid, PlaySeat.status == SeatStatus.ok.value, PlaySeat.sid.in_(sid_list), ).update( { 'orderno': orderno, 'status': SeatStatus.locked.value, 'lock_time': datetime.now() }, synchronize_session=False) # 变成同步的session # 座位的范围如果越界,那么锁定的数量会和上传的sid长度对不上,保证了座位范围 if rows != len(sid_list): session.rollback() return 0 session.commit() return rows @classmethod def unlock(cls, orderno, pid, sid_list): session = db.create_scoped_session() rows = session.query(PlaySeat).filter_by( orderno=orderno, status=SeatStatus.locked.value).update( { 'orderno': None, 'status': SeatStatus.ok.value, }, synchronize_session=False) # 取消订单时,保证同一订单的座位数量及id和生成该订单时一致 if rows != len(sid_list): session.rollback() return 0 # 出现不一致就会回滚,发送解锁失败 session.commit() return rows @classmethod def buy(cls, orderno, pid, sid_list): session = db.create_scoped_session() rows = session.query(PlaySeat).filter_by( orderno=orderno, status=SeatStatus.locked.value, ).update({ 'status': SeatStatus.sold.value, }, synchronize_session=False) if rows != len(sid_list): session.rollback() return 0 session.commit() return rows @classmethod def refund(cls, orderno, pid, sid_list): session = db.create_scoped_session() rows = session.query(PlaySeat).filter_by( orderno=orderno, status=SeatStatus.sold.value, ).update({ 'status': SeatStatus.ok.value, 'orderno': None, }, synchronize_session=False) if rows != len(sid_list): session.rollback() return 0 session.commit() return rows @classmethod def print_tickets(cls, orderno, pid, sid_list): session = db.create_scoped_session() rows = session.query(PlaySeat).filter_by( orderno=orderno, status=SeatStatus.sold.value, ).update({ 'status': SeatStatus.printed.value, }, synchronize_session=False) if rows != len(sid_list): session.rollback() return 0 session.commit() return rows @classmethod def getby_orderno(cls, orderno): return cls.query.filter_by(orderno=orderno).all()
class Seat(db.Model, Model): sid = db.Column(db.Integer, primary_key=True) cid = db.Column(db.Integer) hid = db.Column(db.Integer) x = db.Column(db.Integer) y = db.Column(db.Integer) row = db.Column(db.String(16)) column = db.Column(db.String(16)) area = db.Column(db.String(16)) seat_type = db.Column(db.String(16)) love_seats = db.Column(db.String(16)) status = db.Column(db.Integer, default=0, nullable=False, index=True)
class Cinema(db.Model, Model): cid = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True, nullable=False) address = db.Column(db.String(256), nullable=False) halls = db.Column(db.Integer, default=0, nullable=False) handle_fee = db.Column(db.Integer, default=0, nullable=False) buy_limit = db.Column(db.Integer, default=0, nullable=False) status = db.Column(db.Integer, nullable=False, index=True) @classmethod def create_test_data(cls, cinema_num=10, hall_num=10, play_num=10): start_time = time.time() from FPproject.models.hall import Hall from FPproject.models.movie import Movie from FPproject.models.play import Play from FPproject.models.seat import Seat, PlaySeat from FPproject.models.order import Order f = faker.Faker('zh_CN') screen_type = ['普通', 'IMAX'] audio_type = ['普通', '杜比环绕'] 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() for n in range(1, hall_num + 1): hall = Hall() hall.cid = cinema.cid hall.name = '%s号厅' % n hall.screen_type = random.choice(screen_type) hall.audio_type = random.choice(audio_type) hall.seats_num = 25 hall.status = 1 hall.save() # 根据seats——num生成座椅记录 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 = math.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('creat test data done! cost %s seconds' % (time.time() - start_time))
class Order(db.Model, Model): __tablename__ = 'orders' # 订单id,我们自己的订单号 oid = db.Column(db.String(32), primary_key=True) # 销售方订单号 seller_order_no = db.Column(db.String(32), index=True) cid = db.Column(db.Integer) pid = db.Column(db.Integer) sid = db.Column(db.String(50)) # 取票码 ticket_flag = db.Column(db.String(64)) tickets_num = db.Column(db.Integer) amount = db.Column(db.Integer) paid_time = db.Column(db.DateTime) # 支付时间 printed_time = db.Column(db.DateTime) # 取票时间案 refund_time = db.Column(db.DateTime) # 退款时间 created_time = db.Column(db.DateTime, server_default=text('CURRENT_TIMESTAMP')) # 创建时间 # 数据更新时候,将这个字段设置成当前时间 updated_time = db.Column(db.DateTime, onupdate=func.now()) # 最后更新时间 status = db.Column(db.Integer, default=0, nullable=False, index=True) # 创建订单,并且锁座 @classmethod def create(cls, cid, pid, sid): order = cls() order.oid = '%s%s%s' % (tetime.now(), randint(100000, 999999), pid) order.cid = cid order.pid = pid # order.sid = sid if type(sid) == list: order.sid = ','.join(str(i) for i in sid) else: order.sid = sid return order @classmethod def getby_orderno(cls, orderno): return Order.query.filter_by(seller_order_no=orderno).first() def gen_ticket_flag(self): self.ticket_flag = ''.join( [str(randint(1000, 9999)) for i in range(8)]) def validate(self, ticket_flag): return self.ticket_flag == ticket_flag @classmethod def getby_ticket_flag(cls, ticket_flag): return cls.query.filter_by(ticket_flag=ticket_flag).first()