from utils.rdbmsUtil.databaseUtil import DatabaseUtil
from utils.rdbmsUtil.modelBase import Base
from utils.rdbmsUtil.modelUser import User, user_factory  # user_factory 确保工厂注册代码被执行
from utils.rdbmsUtil.modelUserOperations import UserOperations
# company_factory 确保工厂注册代码被执行
from utils.rdbmsUtil.modelCompany import Company, company_factory
from utils.rdbmsUtil.modelCompanyOperations import CompanyOperations


db = DatabaseUtil('sqlite:///mydatabase.db')
Base.metadata.create_all(db.engine)

companys = [Company(id=1, name="DS-Tech"), Company(id=2, name="ZN-Tech")]
users = [User(name="Alice", age=31, companyid=1),
         User(name="Bob", age=25, companyid=2)]


with db.session_scope() as session:
    company_ops = CompanyOperations()

    user_ops = UserOperations()
    
    company_ops.upsert_companys(session, companys)
    user_ops.upsert_users(session, users)
    # session.flush()  # 确保所有的 SQL 操作都被推送到数据库

    user_ops.update(session, name="Alice", age=32)

    result = user_ops.get_user(session, name="Alice", age=32)
    for user in result:
        print(user)
