Files
dsProject/dsLightRag/Routes/TeachingModel/api/ThemeController.py
2025-09-11 15:10:25 +08:00

281 lines
14 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# routes/ThemeController.py
from fastapi import APIRouter, Depends
from Util.Database import *
from Util.ParseRequest import *
from Routes.TeachingModel.auth.dependencies import *
from Util.PageUtil import *
from Util.PersonUtil import get_person_info
from Util.TranslateUtil import *
# 创建一个路由实例,需要依赖get_current_user,登录后才能访问
router = APIRouter(dependencies=[Depends(get_current_user)])
# 功能【Theme-1】主题管理列表
# 作者Kalman.CHENG ☆
# 时间2025-07-14
# 备注:
@router.get("/list")
async def list(request: Request):
# 获取参数
person_id = await get_request_str_param(request, "person_id", True, True)
person_info = await get_person_info(person_id)
if person_info is None:
return {"success": False, "message": "用户不存在!"}
stage_id = await get_request_num_param(request, "stage_id", False, True, -1)
subject_id = await get_request_num_param(request, "subject_id", False, True, -1)
scope_type = await get_request_num_param(request, "scope_type", False, True, 0)
page_number = await get_request_num_param(request, "page_number", False, True,1)
page_size = await get_request_num_param(request, "page_size", False, True, 10)
theme_name = await get_request_str_param(request, "theme_name", False, True)
city_id = person_info["city_id"]
area_id = person_info["area_id"]
bureau_id = person_info["bureau_id"]
# 拼接查询SQL语句 # 修改列表获取逻辑,我能管理啥? 我自己创建的+共享给我管理的
select_theme_sql: str = " select * from t_ai_teaching_model_theme WHERE is_deleted = 0 "
# scope_type --> 0全部1共享给市2共享给区3共享给校4共享给人5本人创建
if scope_type == 0:
select_theme_sql += " and ((person_id = '" + person_id + "') or ( id in (select theme_id from t_ai_teaching_model_theme_scope where check_flag = 1 and is_deleted = 0 and permission_type = 1 and ((scope_type = 1 and scope_value = '" + city_id + "') or (scope_type = 2 and scope_value = '" + area_id + "') or (scope_type = 3 and scope_value = '" + bureau_id + "') or (scope_type = 4 and scope_value = '" + person_id + "')))))"
elif scope_type == 1:
select_theme_sql += " and id in (select theme_id from t_ai_teaching_model_theme_scope where check_flag = 1 and is_deleted = 0 and scope_type = 1 and scope_value = '" + city_id + "' and permission_type = 1)"
elif scope_type == 2:
select_theme_sql += " and id in (select theme_id from t_ai_teaching_model_theme_scope where check_flag = 1 and is_deleted = 0 and scope_type = 2 and scope_value = '" + area_id + "' and permission_type = 1)"
elif scope_type == 3:
select_theme_sql += " and id in (select theme_id from t_ai_teaching_model_theme_scope where check_flag = 1 and is_deleted = 0 and scope_type = 3 and scope_value = '" + bureau_id + "' and permission_type = 1)"
elif scope_type == 4:
select_theme_sql += " and id in (select theme_id from t_ai_teaching_model_theme_scope where check_flag = 1 and is_deleted = 0 and scope_type = 4 and scope_value = '" + person_id + "' and permission_type = 1)"
elif scope_type == 5:
select_theme_sql += " and person_id = '" + person_id + "'"
if stage_id != -1:
select_theme_sql += " and stage_id = " + str(stage_id)
if subject_id != -1:
select_theme_sql += " and subject_id = " + str(subject_id)
if theme_name != "":
select_theme_sql += " and theme_name like '%" + theme_name + "%'"
select_theme_sql += " ORDER BY create_time DESC"
print(select_theme_sql)
# 查询主题列表
page = await get_page_data_by_sql(select_theme_sql, page_number, page_size)
person_ids = ""
for item in page["list"]:
person_ids += "'" + item["person_id"] + "',"
if person_ids != "":
person_ids = person_ids[:-1]
else:
person_ids = "''"
person_map = await get_person_map(person_ids)
stage_map = await get_stage_map()
subject_map = await get_subject_map()
for item in page["list"]:
item["stage_name"] = stage_map.get(str(item["stage_id"]), "未知学段")
item["subject_name"] = subject_map.get(str(item["subject_id"]), "未知学科")
item["person_name"] = person_map.get(str(item["person_id"]), "未知姓名")
item["can_share"] = 1 if person_id == item['person_id'] else 0
return {"success": True, "message": "查询成功!", "data": page}
# 功能【Theme-2】保存主题管理
# 作者Kalman.CHENG ☆
# 时间2025-07-14
# 备注:
@router.post("/save")
async def save(request: Request):
# 获取参数
id = await get_request_num_param(request, "id", False, True, 0)
theme_name = await get_request_str_param(request, "theme_name", True, True)
if len(theme_name) > 50:
return {"success": False, "message": "主题名称不能超过50个字符"}
short_name = await get_request_str_param(request, "short_name", True, True)
if len(short_name) > 50:
return {"success": False, "message": "主题英文简称不能超过50个字符"}
theme_icon = await get_request_str_param(request, "theme_icon", False, True)
if len(theme_name) > 200:
return {"success": False, "message": "主题图标不能超过200个字符"}
stage_id = await get_request_num_param(request, "stage_id", True, True, None)
subject_id = await get_request_num_param(request, "subject_id", True, True, None)
person_id = await get_request_str_param(request, "person_id", True, True)
bureau_id = await get_request_str_param(request, "bureau_id", True, True)
# 校验参数
check_theme_sql = "SELECT theme_name FROM t_ai_teaching_model_theme WHERE is_deleted = 0 and bureau_id = '" + bureau_id + "' and theme_name = '" + theme_name + "'"
if id != 0:
check_theme_sql += " and id <> " + str(id)
print(check_theme_sql)
check_theme_result = await find_by_sql(check_theme_sql,())
if check_theme_result:
return {"success": False, "message": "该主题名称已存在!"}
check_short_name_sql = "SELECT short_name FROM t_ai_teaching_model_theme WHERE is_deleted = 0 and bureau_id = '" + bureau_id + "' and short_name = '" + short_name + "'"
if id != 0:
check_short_name_sql += " and id <> " + str(id)
print(check_short_name_sql)
check_short_name_result = await find_by_sql(check_short_name_sql,())
if check_short_name_result:
return {"success": False, "message": "该主题英文简称已存在!"}
# 组装参数
param = {"theme_name": theme_name,"short_name": short_name,"theme_icon": theme_icon,"stage_id": stage_id,"subject_id": subject_id,"person_id": person_id,"bureau_id": bureau_id}
# 保存数据
if id == 0:
param["search_flag"] = 0
param["train_flag"] = 0
param["quote_count"] = 0
# 插入数据
id = await insert("t_ai_teaching_model_theme", param, False)
return {"success": True, "message": "保存成功!", "data": {"insert_id" : id}}
else:
# 更新数据
await update("t_ai_teaching_model_theme", param, "id", id, False)
return {"success": True, "message": "更新成功!", "data": {"update_id" : id}}
# 功能【Theme-3】获取主题信息
# 作者Kalman.CHENG ☆
# 时间2025-07-14
# 备注:
@router.get("/get")
async def get(request: Request):
# 获取参数
id = await get_request_num_param(request, "id", True, True, None)
theme_obj = await find_by_id("t_ai_teaching_model_theme", "id", id)
if theme_obj is None:
return {"success": False, "message": "未查询到该主题信息!"}
stage_map = await get_stage_map()
subject_map = await get_subject_map()
theme_obj["stage_name"] = stage_map.get(str(theme_obj["stage_id"]), "未知学段")
theme_obj["subject_name"] = subject_map.get(str(theme_obj["subject_id"]), "未知学科")
theme_obj["person_name"] = await find_person_name_by_id(theme_obj["person_id"])
return {"success": True, "message": "查询成功!", "data": {"theme": theme_obj}}
@router.post("/delete")
async def delete(request: Request):
# 获取参数
id = await get_request_num_param(request, "id", True, True, None)
result = await delete_by_id("t_ai_teaching_model_theme", "id", id)
if not result:
return {"success": False, "message": "删除失败!"}
return {"success": True, "message": "删除成功!"}
# 功能【Theme-5】根据学段学科获取主题列表
# 作者Kalman.CHENG ☆
# 时间2025-07-31
# 备注:
@router.get("/getListByStageSubject")
async def get_list_by_stage_subject(request: Request):
# 获取参数
person_id = await get_request_str_param(request, "person_id", True, True)
stage_id = await get_request_num_param(request, "stage_id", False, True, -1)
subject_id = await get_request_num_param(request, "subject_id", False, True, -1)
person_info = await get_person_info(person_id)
if person_info is None:
return {"success": False, "message": "用户不存在!"}
city_id = person_info["city_id"]
area_id = person_info["area_id"]
bureau_id = person_info["bureau_id"]
# 拼接查询SQL语句
select_theme_sql: str = " select id as theme_id, theme_name from t_ai_teaching_model_theme where is_deleted = 0 "
# 不用加permission_type判断因为permission_type有两个选项管理和查看能管理的都能看so不用管permission_type
select_theme_sql += " and ((person_id = '" + person_id + "') or ( id in (select theme_id from t_ai_teaching_model_theme_scope where check_flag = 1 and is_deleted = 0 and ((scope_type = 1 and scope_value = '" + city_id + "') or (scope_type = 2 and scope_value = '" + area_id + "') or (scope_type = 3 and scope_value = '" + bureau_id + "') or (scope_type = 4 and scope_value = '" + person_id + "')))))"
if stage_id != -1:
select_theme_sql += " and stage_id = " + str(stage_id)
if subject_id != -1:
select_theme_sql += " and subject_id = " + str(subject_id)
print(select_theme_sql)
select_theme_result = await find_by_sql(select_theme_sql,())
if select_theme_result is None:
select_theme_result = []
return {"success": True, "message": "查询成功!", "data": {"theme_list": select_theme_result}}
@router.post("/share")
async def share(request: Request):
# 获取参数
theme_id = await get_request_num_param(request, "theme_id", True, True, None)
scope_type = await get_request_num_param(request, "scope_type", True, True, None)
scope_value = await get_request_str_param(request, "scope_value", True, True)
permission_type = await get_request_num_param(request, "permission_type", True, True, None)
person_id = await get_request_str_param(request, "person_id", True, True)
expires_at = await get_request_str_param(request, "expires_at", False, True)
now = datetime.datetime.now()
# 组装参数
param = {"theme_id": theme_id,"scope_type": scope_type,"scope_value": scope_value,"created_by": person_id,"created_at": now, "check_flag": 1, "checked_at": now, "checked_info": "默认审核通过", "permission_type": permission_type, "granted_at": now}
if expires_at != "":
param["expires_at"] = datetime.datetime.strptime(expires_at, "%Y-%m-%d")
print(param)
# 插入数据
id = await insert("t_ai_teaching_model_theme_scope", param, False)
return {"success": True, "message": "保存成功!", "data": {"insert_id": id}}
@router.get("/myShareList")
async def my_share_list(request: Request):
# 获取参数
person_id = await get_request_str_param(request, "person_id", True, True)
person_info = await get_person_info(person_id)
if person_info is None:
return {"success": False, "message": "用户不存在!"}
scope_type = await get_request_num_param(request, "scope_type", True, True, None)
stage_id = await get_request_num_param(request, "stage_id", False, True, -1)
subject_id = await get_request_num_param(request, "subject_id", False, True, -1)
theme_name = await get_request_str_param(request, "theme_name", False, True)
page_number = await get_request_num_param(request, "page_number", False, True, 1)
page_size = await get_request_num_param(request, "page_size", False, True, 10)
# 拼接查询SQL语句
column_str: str = "t1.id as theme_id, t1.theme_name, t1.short_name, t1.theme_icon, t1.stage_id, t1.subject_id, t1.quote_count, t1.search_flag, t1.train_flag, t2.id as scope_id, t2.scope_type, t2.scope_value, t2.created_by, t2.created_at, t2.check_flag, t2.permission_type, t2.granted_at, t2.expires_at "
select_theme_sql: str = f" select {column_str} from t_ai_teaching_model_theme t1, t_ai_teaching_model_theme_scope t2 where t1.is_deleted = 0 and t2.is_deleted = 0 and t1.id = t2.theme_id and t2.created_by = '{person_id}' "
# scope_type --> 0全部1共享给市2共享给区3共享给校4共享给人
if scope_type != 0:
select_theme_sql += " and t2.scope_type = " + str(scope_type)
if stage_id != -1:
select_theme_sql += " and t1.stage_id = " + str(stage_id)
if subject_id != -1:
select_theme_sql += " and t1.subject_id = " + str(subject_id)
if theme_name != "":
select_theme_sql += " and t1.theme_name like '%" + theme_name + "%' "
select_theme_sql += "ORDER BY t1.create_time DESC "
page = await get_page_data_by_sql(select_theme_sql, page_number, page_size)
person_name = person_info["person_name"]
stage_map = await get_stage_map()
subject_map = await get_subject_map()
for item in page["list"]:
item["stage_name"] = stage_map.get(str(item["stage_id"]), "未知学段")
item["subject_name"] = subject_map.get(str(item["subject_id"]), "未知学科")
item["person_name"] = person_name
return {"success": True, "message": "查询成功!", "data": page}
@router.post("/deleteShare")
async def delete_share(request: Request):
# 获取参数
scope_id = await get_request_num_param(request, "scope_id", True, True, None)
result = await delete_by_id("t_ai_teaching_model_theme_scope", "id", scope_id)
if not result:
return {"success": False, "message": "删除失败!"}
return {"success": True, "message": "删除成功!"}