# 模型
from sqlalchemy import func
from .exts import  db

# 模型
class User(db.Model):
    # 表名
    __tablename__ = "tb_user"
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    role = db.Column(db.Integer, autoincrement=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(128), nullable=False)
# 论文管理
class Item(db.Model):
    # 表名
    __tablename__ = "tb_item"
    # 论文编号
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    # 论文标题
    name = db.Column(db.String(80), autoincrement=True)
    # 路径
    path = db.Column(db.String(255), unique=True, nullable=False)
    # 是否分析
    is_view = db.Column(db.Integer)
    # 阅读人数
    view = db.Column(db.Integer)
    # 上传用户
    user_id = db.Column(db.Integer, nullable=False)
    # 创建时间
    create_time = db.Column(db.String(100), nullable=False, default=func.now())
class Config(db.Model):
    # 表名
    __tablename__ = "tb_sysconfig"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 主键
    name = db.Column(db.String(50), nullable=False)
    key = db.Column(db.String(50), unique=True, nullable=False)  # 配置键
    value = db.Column(db.String(255), nullable=False)  # 配置值
    description = db.Column(db.String(255))  # 描述


# 定义金融交易模型
class FinancialTransaction(db.Model):
    __tablename__ = 'tb_financial_transactions'
    transaction_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_name = db.Column(db.String(50), nullable=False)
    transaction_amount = db.Column(db.Float, nullable=False)
    transaction_time = db.Column(db.DateTime, nullable=False)
    transaction_location = db.Column(db.String(255), nullable=True)
    transaction_status = db.Column(db.String(255), nullable=True)
    device_info = db.Column(db.String(255), nullable=True)
    ip_address = db.Column(db.String(45), nullable=True)
    browser_info = db.Column(db.String(255), nullable=True)
    mobile = db.Column(db.Integer, nullable=True)
    is_fraud = db.Column(db.Boolean, nullable=False)
    def __init__(self, user_name, transaction_amount, transaction_time, transaction_location, device_info, ip_address,transaction_status,mobile,
                 browser_info, is_fraud):
        self.user_name = user_name
        self.transaction_amount = transaction_amount
        self.transaction_time = transaction_time
        self.transaction_location = transaction_location
        self.device_info = device_info
        self.ip_address = ip_address
        self.browser_info = browser_info
        self.is_fraud = is_fraud
        self.transaction_status = transaction_status
        self.mobile = mobile

    def to_dict(self):
        return {
            'transaction_id': self.transaction_id,
            'user_name': self.user_name,
            'transaction_amount': self.transaction_amount,
            'transaction_time': self.transaction_time.strftime('%Y-%d-%m %H:%M:%S'),
            'transaction_location': self.transaction_location,
            'device_info': self.device_info,
            'ip_address': self.ip_address,
            'browser_info': self.browser_info,
            'is_fraud': self.is_fraud,
            'transaction_status': self.transaction_status,
            'mobile': self.mobile
        }