fix: DBPools Singleton - manually set db.databases after getting instance

This commit is contained in:
yumoqing 2026-05-08 15:17:20 +08:00
parent 2f68b617c7
commit bc7cb9676f
2 changed files with 42 additions and 14 deletions

View File

@ -21,7 +21,9 @@ async def create_opportunity(
"""创建商机"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
opportunity_id = str(uuid.uuid4()).replace('-', '')
@ -75,7 +77,9 @@ async def update_opportunity_stage(
"""更新商机阶段"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0:
@ -129,7 +133,9 @@ async def assign_opportunity(
"""分配商机给销售人员"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0:
@ -172,7 +178,9 @@ async def get_opportunity_funnel(
"""获取销售漏斗数据"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
filters = []
if start_date and end_date:
@ -221,7 +229,9 @@ async def get_sales_performance(
"""获取销售业绩数据"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
# 查询已关闭的商机(赢单)
won_filters = [

View File

@ -25,7 +25,9 @@ async def create_opportunity(
"""创建商机"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
opportunity_id = str(uuid.uuid4()).replace('-', '')
@ -79,7 +81,9 @@ async def update_opportunity_stage(
"""更新商机阶段"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0:
@ -133,7 +137,9 @@ async def assign_opportunity(
"""分配商机给销售人员"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0:
@ -176,7 +182,9 @@ async def get_opportunity_funnel(
"""获取销售漏斗数据"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
filters = []
if start_date and end_date:
@ -225,7 +233,9 @@ async def get_sales_performance(
"""获取销售业绩数据"""
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
# 查询已关闭的商机(赢单)
won_filters = [
@ -284,7 +294,9 @@ async def predict_revenue(start_date: str, end_date: str):
async def update_opportunity(opportunity_id: str, **updates):
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
updates["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
await sor.U(
@ -297,7 +309,9 @@ async def update_opportunity(opportunity_id: str, **updates):
async def delete_opportunity(opportunity_id: str):
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
await sor.D(
"opportunities",
@ -308,7 +322,9 @@ async def delete_opportunity(opportunity_id: str):
async def get_opportunity_by_id(opportunity_id: str):
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
return records[0] if records else None
@ -316,7 +332,9 @@ async def get_opportunity_by_id(opportunity_id: str):
async def list_opportunities(**filters):
config = getConfig()
db = DBPools(config.databases)
db = DBPools()
db.databases = config.databases
async with db.sqlorContext('opportunity_management') as sor:
filter_list = []
for field, value in filters.items():