'''
Author: your name
Date: 2021-07-15 09:33:39
LastEditTime: 2021-07-16 11:48:39
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\models\base.py
'''
#!/usr/bin/env python
# -*- coding: utf_8 -*-

# Documention: https://docs.sqlalchemy.org/en/14/orm/declarative_mixins.html

import uuid
from datetime import datetime
from application.app import db
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr

def generate_uuid():
    return uuid.uuid1().hex

class BaseModel(db.Model):
    # Flask-SQLAlchemy创建table时,如何声明基类（这个类不会创建表,可以被继承）
    # 方法就是把__abstract__这个属性设置为True,这个类为基类，不会被创建为表！
    __abstract__ = True
    create_at = db.Column(db.DateTime, default=datetime.now)
    update_at = db.Column(db.DateTime, default=datetime.now)
    remarks = db.Column(db.String(255), default="")
    is_delete = db.Column(db.Boolean, default=0)

    @declared_attr
    def create_by(cls):
        return db.Column(db.Integer, ForeignKey("evm_user.id"))

    @declared_attr
    def update_by(cls):
        return db.Column(db.Integer, ForeignKey("evm_user.id"))

    # __table_args__ = (UniqueConstraint('machine_id', ts, value),
    #                   Index('system_machine_id_index', 'machine_id'),
    #                   Index('system_ts_index', ts))

class PrimaryModel(BaseModel):
    __abstract__ = True
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uuid = db.Column(db.String(64), primary_key=False, default=generate_uuid)

class AutoBaseModel(BaseModel):
    __abstract__ = True
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    sort = db.Column(db.Integer, default=0)

class UuidBaseModel(BaseModel):
    __abstract__ = True
    id = db.Column(db.String(64), primary_key=True, default=generate_uuid)
    sort = db.Column(db.Integer, default=0)

class SortBaseModel(BaseModel):
    __abstract__ = True
    sort = db.Column(db.Integer, default=0)