mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
🐛 批量修改不起效
This commit is contained in:
parent
a7bfebd485
commit
bd0baa72cd
@ -77,7 +77,6 @@ class ApiDataSource:
|
||||
db_plugin.block_type = param.block_type
|
||||
db_plugin.status = param.block_type != BlockType.ALL
|
||||
await db_plugin.save()
|
||||
# 配置项
|
||||
if param.configs and (configs := Config.get(param.module)):
|
||||
for key in param.configs:
|
||||
if c := configs.configs.get(key):
|
||||
@ -98,48 +97,48 @@ class ApiDataSource:
|
||||
返回:
|
||||
dict: 更新结果, 例如 {'success': True, 'updated_count': 5, 'errors': []}
|
||||
"""
|
||||
# 分开处理,避免 bulk_update 对 CharEnumField 的潜在问题
|
||||
plugins_to_update_other_fields = []
|
||||
plugins_to_update_other_fields = []
|
||||
other_update_fields = set()
|
||||
updated_count = 0
|
||||
errors = []
|
||||
|
||||
# 收集需要更新的插件和字段
|
||||
for item in params.updates:
|
||||
try:
|
||||
db_plugin = await DbPluginInfo.get(module=item.module)
|
||||
plugin_changed_other = False
|
||||
plugin_changed_block = False
|
||||
|
||||
# 处理 block_type 和 status (单独保存)
|
||||
if db_plugin.block_type != item.block_type:
|
||||
db_plugin.block_type = item.block_type
|
||||
db_plugin.status = item.block_type != BlockType.ALL # 同时更新 status
|
||||
db_plugin.status = item.block_type != BlockType.ALL
|
||||
plugin_changed_block = True
|
||||
|
||||
# 处理 menu_type (准备批量更新)
|
||||
|
||||
if item.menu_type is not None and db_plugin.menu_type != item.menu_type:
|
||||
db_plugin.menu_type = item.menu_type
|
||||
other_update_fields.add("menu_type")
|
||||
plugin_changed_other = True
|
||||
|
||||
# 处理 default_status (准备批量更新)
|
||||
if item.default_status is not None and db_plugin.default_status != item.default_status:
|
||||
if (
|
||||
item.default_status is not None
|
||||
and db_plugin.default_status != item.default_status
|
||||
):
|
||||
db_plugin.default_status = item.default_status
|
||||
other_update_fields.add("default_status")
|
||||
plugin_changed_other = True
|
||||
|
||||
# 单独保存 block_type 和 status 的更改
|
||||
|
||||
if plugin_changed_block:
|
||||
try:
|
||||
await db_plugin.save(update_fields=["block_type", "status"])
|
||||
updated_count += 1 # 每次成功保存计为一个更新
|
||||
updated_count += 1
|
||||
except Exception as e_save:
|
||||
errors.append({"module": item.module, "error": f"Save block_type failed: {str(e_save)}"})
|
||||
# 如果保存失败,则不将其他字段加入批量更新,避免数据不一致
|
||||
plugin_changed_other = False
|
||||
errors.append(
|
||||
{
|
||||
"module": item.module,
|
||||
"error": f"Save block_type failed: {str(e_save)}",
|
||||
}
|
||||
)
|
||||
plugin_changed_other = False
|
||||
|
||||
# 如果其他字段有更改且 block_type 保存成功,则加入批量更新列表
|
||||
if plugin_changed_other:
|
||||
plugins_to_update_other_fields.append(db_plugin)
|
||||
|
||||
@ -148,19 +147,24 @@ class ApiDataSource:
|
||||
except Exception as e:
|
||||
errors.append({"module": item.module, "error": str(e)})
|
||||
|
||||
# 执行其他字段的批量更新
|
||||
bulk_updated_count = 0
|
||||
if plugins_to_update_other_fields and other_update_fields:
|
||||
try:
|
||||
await DbPluginInfo.bulk_update(plugins_to_update_other_fields, list(other_update_fields))
|
||||
# 注意:这里的 updated_count 可能需要调整,取决于是否将 bulk_update 的成功也计入
|
||||
# 为简单起见,我们只计算了上面单独 save 的次数
|
||||
# updated_count += len(plugins_to_update_other_fields) # 如果需要合并计数
|
||||
await DbPluginInfo.bulk_update(
|
||||
plugins_to_update_other_fields, list(other_update_fields)
|
||||
)
|
||||
bulk_updated_count = len(plugins_to_update_other_fields)
|
||||
except Exception as e_bulk:
|
||||
errors.append({"module": "batch_update_other", "error": f"Bulk update failed: {str(e_bulk)}"})
|
||||
errors.append(
|
||||
{
|
||||
"module": "batch_update_other",
|
||||
"error": f"Bulk update failed: {str(e_bulk)}",
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"success": len(errors) == 0, # 只要没有错误就算成功
|
||||
"updated_count": updated_count, # 只计算 block_type 成功更新的数量
|
||||
"success": len(errors) == 0,
|
||||
"updated_count": updated_count + bulk_updated_count,
|
||||
"errors": errors,
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user