fix: DBPools(config.databases) instead of DBPools() for subprocess context

This commit is contained in:
yumoqing 2026-05-08 14:49:20 +08:00
parent fd0c9f4aeb
commit 2f68b617c7
2 changed files with 43 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import uuid
from ahserver.serverenv import ServerEnv from ahserver.serverenv import ServerEnv
from appPublic.worker import awaitify from appPublic.worker import awaitify
from sqlor.dbpools import DBPools from sqlor.dbpools import DBPools
from appPublic.jsonConfig import getConfig
async def create_opportunity( async def create_opportunity(
@ -18,7 +19,9 @@ async def create_opportunity(
region: str = None region: str = None
) -> Dict: ) -> Dict:
"""创建商机""" """创建商机"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
opportunity_id = str(uuid.uuid4()).replace('-', '') opportunity_id = str(uuid.uuid4()).replace('-', '')
@ -70,7 +73,9 @@ async def update_opportunity_stage(
notes: str = None notes: str = None
) -> Dict: ) -> Dict:
"""更新商机阶段""" """更新商机阶段"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]}) opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0: if not opportunity_records or len(opportunity_records) == 0:
@ -122,7 +127,9 @@ async def assign_opportunity(
new_owner_id: str new_owner_id: str
) -> Dict: ) -> Dict:
"""分配商机给销售人员""" """分配商机给销售人员"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]}) opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0: if not opportunity_records or len(opportunity_records) == 0:
@ -163,7 +170,9 @@ async def get_opportunity_funnel(
region: str = None region: str = None
) -> Dict: ) -> Dict:
"""获取销售漏斗数据""" """获取销售漏斗数据"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
filters = [] filters = []
if start_date and end_date: if start_date and end_date:
@ -210,7 +219,9 @@ async def get_sales_performance(
owner_id: str = None owner_id: str = None
) -> Dict: ) -> Dict:
"""获取销售业绩数据""" """获取销售业绩数据"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
# 查询已关闭的商机(赢单) # 查询已关闭的商机(赢单)
won_filters = [ won_filters = [

View File

@ -23,7 +23,9 @@ async def create_opportunity(
region: str = None region: str = None
) -> Dict: ) -> Dict:
"""创建商机""" """创建商机"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
opportunity_id = str(uuid.uuid4()).replace('-', '') opportunity_id = str(uuid.uuid4()).replace('-', '')
@ -75,7 +77,9 @@ async def update_opportunity_stage(
notes: str = None notes: str = None
) -> Dict: ) -> Dict:
"""更新商机阶段""" """更新商机阶段"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]}) opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0: if not opportunity_records or len(opportunity_records) == 0:
@ -127,7 +131,9 @@ async def assign_opportunity(
new_owner_id: str new_owner_id: str
) -> Dict: ) -> Dict:
"""分配商机给销售人员""" """分配商机给销售人员"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]}) opportunity_records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
if not opportunity_records or len(opportunity_records) == 0: if not opportunity_records or len(opportunity_records) == 0:
@ -168,7 +174,9 @@ async def get_opportunity_funnel(
region: str = None region: str = None
) -> Dict: ) -> Dict:
"""获取销售漏斗数据""" """获取销售漏斗数据"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
filters = [] filters = []
if start_date and end_date: if start_date and end_date:
@ -215,7 +223,9 @@ async def get_sales_performance(
owner_id: str = None owner_id: str = None
) -> Dict: ) -> Dict:
"""获取销售业绩数据""" """获取销售业绩数据"""
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
# 查询已关闭的商机(赢单) # 查询已关闭的商机(赢单)
won_filters = [ won_filters = [
@ -272,7 +282,9 @@ async def predict_revenue(start_date: str, end_date: str):
return {"predicted_revenue": performance["weighted_pipeline_value"]} return {"predicted_revenue": performance["weighted_pipeline_value"]}
async def update_opportunity(opportunity_id: str, **updates): async def update_opportunity(opportunity_id: str, **updates):
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
updates["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") updates["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
await sor.U( await sor.U(
@ -283,7 +295,9 @@ async def update_opportunity(opportunity_id: str, **updates):
return {"opportunity_id": opportunity_id, "updated": True} return {"opportunity_id": opportunity_id, "updated": True}
async def delete_opportunity(opportunity_id: str): async def delete_opportunity(opportunity_id: str):
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
await sor.D( await sor.D(
"opportunities", "opportunities",
@ -292,13 +306,17 @@ async def delete_opportunity(opportunity_id: str):
return {"opportunity_id": opportunity_id, "deleted": True} return {"opportunity_id": opportunity_id, "deleted": True}
async def get_opportunity_by_id(opportunity_id: str): async def get_opportunity_by_id(opportunity_id: str):
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]}) records = await sor.R("opportunities", {"filters": [{"field": "id", "op": "=", "value": opportunity_id}]})
return records[0] if records else None return records[0] if records else None
async def list_opportunities(**filters): async def list_opportunities(**filters):
db = DBPools() config = getConfig()
db = DBPools(config.databases)
async with db.sqlorContext('opportunity_management') as sor: async with db.sqlorContext('opportunity_management') as sor:
filter_list = [] filter_list = []
for field, value in filters.items(): for field, value in filters.items():