class User(db.Model): __tablename__ = "User" username = db.Column(VARCHAR(20), primary_key=True) password = db.Column(VARCHAR(20)) email = db.Column(VARCHAR(20)) email_validated = db.Column(BOOLEAN) register_time = db.Column(DATETIME) role = db.Column(db.Enum(Role)) amount = db.Column(FLOAT, default=0.0) # 金额(初始资金) profit = db.Column(FLOAT, default=0.0) # 收益 risk = db.Column(FLOAT, default=0.0) # 风险 year = db.Column(INTEGER, default=0) # 年限 citi_username = db.Column(VARCHAR(50)) # 花旗用户名 citi_password = db.Column(VARCHAR(1000)) # 花旗密码 user_type = db.Column(INTEGER, default=1) # 用户的标签,默认是1,在系统推荐时要用 def __init__(self, username: str, password: str, email: str, register_time: datetime, role=Role.USER): self.username = username self.password = password self.email = email self.register_time = register_time self.role = role self.email_validated = False self.amount = 0 self.profit = 0 self.risk = 0 self.year = 0
class Advice(db.Model): """ 交易建议 通过里面的request_id和每个配置方案相挂钩 """ __tablename__ = 'Advice' code = db.Column(VARCHAR(100)) name = db.Column(VARCHAR(100)) price = db.Column(Float) # 买入价 quantity = db.Column(Integer) type = db.Column(db.Enum(Mode), default='STOCK') time = db.Column(DATETIME, default=datetime.today()) is_done = db.Column(BOOLEAN, default=False) request_id = db.Column(Integer, db.ForeignKey('InvestRequirement.id')) id = db.Column(Integer, primary_key=True) def __init__(self, code, quantity, request_id, type): self.code = code self.quantity = quantity if type == Mode.GOODS: self.price = GoodsDao().get_price_by_code(code) self.name = GoodsDao().get_goods_name(code) elif type == Mode.BOND: self.price = BondsDao().get_price_by_code(code) self.name = BondsDao().get_name_by_code(code) else: self.price = StockDao().get_stock_price(code) self.name = StockDao().get_stock_name(code) self.request_id = request_id self.type = type
class UserModel(db.Model): __tablename__ = "User" id = db.Column(Integer, primary_key=True) username = db.Column(VARCHAR(20)) password = db.Column(VARCHAR(20)) role = db.Column(db.Enum(Role), default=Role.USER) openid = db.Column(VARCHAR(100)) def __init__(self, **kwargs): pass
class TransactionRecord(db.Model): """ 交易记录 通过里面的request_id和每个配置方案相挂钩 """ __tablename__ = "TransactionRecord" id = db.Column(Integer, primary_key=True) quota_id = db.Column(VARCHAR(50)) quota_name = db.Column(VARCHAR(50)) time = db.Column(DATETIME) type = db.Column(db.Enum(Mode), default='STOCK') buy_or_sale = db.Column(db.Enum(SellMethod), default='BUY') quantity = db.Column(Integer) price = db.Column(Float) expense = db.Column(Float) # 手续费 matching = db.Column(Float, default=0) # 此产品占此类型的占比 lastMatching = db.Column(Float, default=0) # 上一次交易时候此产品占此类型的占比 request_id = db.Column(Integer, db.ForeignKey('InvestRequirement.id')) def __init__(self, code, price, quantity, time, expense, type, request_id): self.quota_id = code self.expense = expense self.price = price if quantity > 0: # 买入 self.quantity = quantity self.buy_or_sale = SellMethod.BUY elif quantity < 0: # 卖出 self.quantity = -quantity self.buy_or_sale = SellMethod.SELL self.time = time self.type = type if type == Mode.STOCK: self.quota_name = StockDao().get_stock_name(code) elif type == Mode.BOND: self.quota_name = BondsDao().get_name_by_code(code) else: self.quota_name = GoodsDao().get_goods_name(code) self.request_id = request_id self.matching = 0 self.lastMatching = 0
class MenuModel(db.Model): __tablename__ = 'menu' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) menu_type = db.Column(db.Enum(*MenuTypes.get_enum_labels()), default=MenuTypes.CUSTOMIZED.value) # menu type coffee_option = db.Column(db.Enum(*CoffeeOption.get_enum_labels()), default=CoffeeOption.CoffeeA.value) # coffee option owner_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) taste_level = db.Column(db.Enum(*TasteLevels.get_enum_labels()), default=TasteLevels.STANDARD.value) # taste level water_level = db.Column(db.Enum(*WaterLevels.get_enum_labels()), default=WaterLevels.STANDARD.value) # water level foam_level = db.Column(db.Enum(*FoamLevels.get_enum_labels()), default=FoamLevels.STANDARD.value) # foam level grind_size = db.Column(db.Enum(*SizeLevels.get_enum_labels()), default=SizeLevels.MEDIUM.value) # particle size create_date = db.Column(db.DATETIME, default=datetime.datetime.utcnow()) owner = db.relationship('UserModel', back_populates='menu_list') orders = db.relationship('AssociationModel', back_populates='menu') serials = db.relationship('SerialNumberModel', back_populates='menu') def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_user(cls, owner: UserModel): return cls.query.filter_by(owner_id=owner.id) @classmethod def get_by_owner_and_id(cls, menu_id: int, owner: UserModel): return cls.query.filter_by(id=menu_id, owner_id=owner.id).one_or_none() @classmethod def get_by_id(cls, menu_id: int): return cls.query.get(menu_id)
class UserModel(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) phone = db.Column(db.String(20), nullable=True) gender = db.Column(db.Enum(*Gender.get_enum_labels()), default=Gender.NONE.value) # gender birthday = db.Column(db.Date, nullable=True) email = db.Column(db.String(255), nullable=False, default="") menu_list = db.relationship('MenuModel', back_populates='owner', lazy=True) order_list = db.relationship('OrderModel', back_populates='owner', lazy=True) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def get_by_id(cls, user_id: int): return cls.query.get(user_id) @classmethod def return_all(cls): def to_json(x): return { 'username': x.username, 'email': x.email } return {'users': list(map(lambda x: to_json(x), UserModel.query.all()))} @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash_value): return sha256.verify(password, hash_value)
class InvestRequirement(db.Model): """ 用户的需求,每次用户需求都有一个id 第一次交易的时候要改is_bought参数 每次交易的时候要改累计收益和今日收益 """ __tablename__ = 'InvestRequirement' id = db.Column(Integer, primary_key=True) username = db.Column(VARCHAR(20), db.ForeignKey('User.username')) is_bought = db.Column(db.Enum(State), default='CONSIDERATION') # 生成、买入、赎回 amount = db.Column(Float) # 金额(初始资金) profit = db.Column(Float) # 预期收益 risk = db.Column(Float) # 预期风险 year = db.Column(Integer) # 预期年限 accu_revenue = db.Column(Float, default=0.0) # 累计收益 pred_revenue_year = db.Column(Float) # 计算预期年化收益 pred_risk_year = db.Column(Float) # 计算预期年化风险 today_revenue = db.Column(Float, default=0.0) # 今日收益 start_date = db.Column(DATETIME, default=datetime.today()) # 开始时间 # 7天收益率和7天波动率 seven_revenue = db.Column(Float, default=0.0) seven_risk = db.Column(Float, default=0.0) # 调整方案册策略 plan_tran_time = db.Column(Integer, default=0) # 计划交易时间 plan_remind_time = db.Column(Integer, default=1) # 在计划时间前多少小时通知有交易 min_confirmed_price = db.Column(Float, default=3000) # 需要手动确认的最小金额,单位为元 confirm_time = db.Column(Integer, default=1) # 有效的确认时间 defaultConfirm = db.Column(BOOLEAN, default=True) # 默认操作是否为确认交易 def __init__(self, profit, risk, amount, year, username, pred_revenue_year, pred_risk_year): self.profit = profit self.risk = risk self.amount = amount self.year = year self.username = username self.pred_revenue_year = pred_revenue_year self.pred_risk_year = pred_risk_year self.is_bought = State.CONSIDERATION # self.balance = amount pass
class UserModel(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key = True) access_level = db.Column(db.Enum("NOACCESS", "USER", "ADMIN", name="ACCESS_ENUM"), nullable=False) username = db.Column(db.String(120), unique = True, nullable = False) password = db.Column(db.String(120), nullable=False) def save_to_db(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash) @classmethod def find_by_username(cls, username): return cls.query.filter_by(username = username).first() @classmethod def return_all(cls): return {"users": [{"username": x.username for x in UserModel.query.all()}]} @classmethod def delete(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {"message": f"{num_rows_deleted} rows deleted"} except: return {"message": "An error occured while deleting users"}
class Position(db.Model): """ 持仓情况 通过里面的request_id和每个配置方案相挂钩 """ __tablename__ = "Position" # 数组里面的子项,一个账户里有多个 id = db.Column(Integer, primary_key=True) # 没用的主键 code = db.Column(VARCHAR(50)) # 股票ID name = db.Column(VARCHAR(50)) price = db.Column(Float) # 买入价 quantity = db.Column(Integer) # 买入数量 buy_time = db.Column(DATETIME) # 买入时间 type = db.Column(db.Enum(Mode), default='STOCK') request_id = db.Column(Integer, db.ForeignKey('InvestRequirement.id')) def __init__(self, code, price, quantity, buy_time, name): self.code = code self.price = price self.quantity = quantity self.buy_time = buy_time self.name = name