File size: 1,629 Bytes
85494ee
 
 
 
 
 
 
 
 
 
 
 
 
bc186bd
 
85494ee
 
 
 
 
 
bc186bd
85494ee
 
 
 
 
 
 
 
bc186bd
85494ee
 
bc186bd
85494ee
bc186bd
 
 
 
 
85494ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# models_sql.py
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Float
from sqlalchemy.orm import declarative_base
import datetime

Base = declarative_base()

class Wallet(Base):
    """用户钱包表"""
    __tablename__ = "wallets"
    
    account = Column(String, primary_key=True, index=True)
    balance = Column(Integer, default=0)         # 用于消费的余额 (充值获得)
    earn_balance = Column(Integer, default=0)    # 创作者销售收益余额 (别人购买获得)
    tip_balance = Column(Integer, default=0)     # 【新增】创作者打赏收益余额 (粉丝赞助获得)
    frozen_balance = Column(Integer, default=0)  # 提现审核冻结中的余额
    
    # 乐观锁版本号,防止并发扣款被击穿
    version = Column(Integer, default=1)

class Ownership(Base):
    """资源所有权表"""
    __tablename__ = "ownerships"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    account = Column(String, index=True)
    item_id = Column(String, index=True)
    purchased_at = Column(DateTime, default=datetime.datetime.utcnow)

class Transaction(Base):
    """交易流水表"""
    __tablename__ = "transactions"
    
    tx_id = Column(String, primary_key=True)
    account = Column(String, index=True)
    tx_type = Column(String)                  
    amount = Column(Integer)                  
    target_id = Column(String, nullable=True) 
    prev_hash = Column(String, nullable=True) 
    tx_hash = Column(String)                  
    created_at = Column(DateTime, default=datetime.datetime.utcnow)