2024-11-19 12:08:21 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
# @Time : 2024/11/19 下午8:04
|
|
|
|
|
|
# @FileName: manage_tanant.py
|
|
|
|
|
|
# @Software: PyCharm
|
|
|
|
|
|
from fastapi import HTTPException, Response, Depends, APIRouter
|
|
|
|
|
|
from typing import Optional, Annotated
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
from jose import JWTError, jwt
|
|
|
|
|
|
|
|
|
|
|
|
from sqlmodel import select
|
|
|
|
|
|
|
|
|
|
|
|
from models import Tenant, User, Project
|
|
|
|
|
|
from dependencies import *
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
2024-11-19 13:38:36 +00:00
|
|
|
|
# 推送修改用注释
|
|
|
|
|
|
# @router.get(...)
|
|
|
|
|
|
# def example():
|
|
|
|
|
|
# return "hello"
|
|
|
|
|
|
|
|
|
|
|
|
# 列举所有租户
|
2024-11-20 10:31:35 +00:00
|
|
|
|
from fastapi import HTTPException, Response
|
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
from models import Tenant, User # 假设你已导入 Tenant 和 User 模型
|
|
|
|
|
|
from dependencies import SessionDep # 假设 SessionDep 是数据库会话的依赖
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#列举所有租户
|
|
|
|
|
|
from fastapi import HTTPException, Response
|
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
from models import Tenant, User # 假设你已导入 Tenant 和 User 模型
|
|
|
|
|
|
from dependencies import SessionDep # 假设 SessionDep 是数据库会话的依赖
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-19 13:38:36 +00:00
|
|
|
|
@router.get("/api/s1/tenant")
|
|
|
|
|
|
async def get_tenant(response: Response, session: SessionDep):
|
2024-11-20 10:31:35 +00:00
|
|
|
|
tenants = session.query(Tenant).all() # 获取所有租户
|
|
|
|
|
|
if not tenants:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="No tenants found")
|
|
|
|
|
|
|
|
|
|
|
|
tenant_data = []
|
|
|
|
|
|
for tenant in tenants:
|
|
|
|
|
|
# 获取该租户中 role=1 的第一个用户(如果存在)
|
|
|
|
|
|
tenant_user = next((user for user in tenant.users if user.role == 1), None)
|
|
|
|
|
|
|
|
|
|
|
|
# 获取该租户中除了 role=1 以外的用户数量
|
|
|
|
|
|
user_num = len([user for user in tenant.users if user.role != 1])
|
|
|
|
|
|
|
|
|
|
|
|
# 构建租户信息
|
|
|
|
|
|
tenant_info = {
|
|
|
|
|
|
"name": tenant.name,
|
|
|
|
|
|
"username": tenant_user.username if tenant_user else None, # 如果找到 role=1 的用户,返回其 username
|
|
|
|
|
|
"user_num": user_num # 除去 role=1 的用户数量
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tenant_data.append(tenant_info)
|
|
|
|
|
|
|
|
|
|
|
|
return {"tenants": tenant_data}
|
2024-11-19 13:38:36 +00:00
|
|
|
|
|
|
|
|
|
|
# 新增和修改租户
|
2024-11-20 10:31:35 +00:00
|
|
|
|
@router.post("/api/s1/tenant")
|
|
|
|
|
|
async def create_tenant(data: dict, session: SessionDep):
|
|
|
|
|
|
tenant_id = data.get("tenant_id")
|
|
|
|
|
|
name = data["name"]
|
|
|
|
|
|
username = data["username"]
|
|
|
|
|
|
password = data["password"]
|
|
|
|
|
|
|
|
|
|
|
|
# 验证是否缺少必要参数
|
|
|
|
|
|
if not name:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="Need to provide name")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 更新租户还是新增租户
|
|
|
|
|
|
if tenant_id:
|
|
|
|
|
|
# 查找现有租户
|
|
|
|
|
|
tenant = session.get(Tenant, tenant_id)
|
|
|
|
|
|
if not tenant:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Tenant not found")
|
|
|
|
|
|
|
|
|
|
|
|
# 更新项目内容
|
|
|
|
|
|
tenant.name = name
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 新增项目
|
|
|
|
|
|
project = Project(
|
|
|
|
|
|
name=name,
|
|
|
|
|
|
requirement=requirement,
|
|
|
|
|
|
start_time=start_time,
|
|
|
|
|
|
deadline=deadline,
|
|
|
|
|
|
owner_id=1 # 假设owner_id是1,之后应该是通过token获取owner_id吧
|
|
|
|
|
|
)
|
|
|
|
|
|
session.add(project)
|
|
|
|
|
|
|
|
|
|
|
|
# 处理项目和用户的关联
|
|
|
|
|
|
# 先清除现有的关联
|
|
|
|
|
|
# 生成删除语句并执行
|
|
|
|
|
|
print(project_id) #测试用
|
|
|
|
|
|
stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project.id)
|
|
|
|
|
|
session.execute(stmt)
|
|
|
|
|
|
session.commit() # 提交事务
|
|
|
|
|
|
|
|
|
|
|
|
# 重新建立与评估员和审核员的关系
|
|
|
|
|
|
for username in estimators:
|
|
|
|
|
|
user = next((user for user in users_estimators if user.username == username), None)
|
|
|
|
|
|
if user:
|
|
|
|
|
|
project_user_link = ProjectUserLink(project_id=project.id, user_id=user.id)
|
|
|
|
|
|
session.add(project_user_link)
|
|
|
|
|
|
|
|
|
|
|
|
for username in auditors:
|
|
|
|
|
|
user = next((user for user in users_auditors if user.username == username), None)
|
|
|
|
|
|
if user:
|
|
|
|
|
|
project_user_link = ProjectUserLink(project_id=project.id, user_id=user.id)
|
|
|
|
|
|
session.add(project_user_link)
|
|
|
|
|
|
|
|
|
|
|
|
# 提交事务
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
session.refresh(project)
|
|
|
|
|
|
|
|
|
|
|
|
return {"message": "Added or updated successfully",
|
|
|
|
|
|
"information": project,
|
|
|
|
|
|
}
|