Commit 5ebd584d by imwangxiaozhou

fix(wecom客户群运营服务): 优化新客户扫描逻辑并改进错误处理

- 过滤输入的bot_ids空值并统一转为字符串格式
- 调整默认机器人查询范围,仅获取绑定渠道活码且有效的机器人
- 将扫描过程中的异常抛出改为日志记录并跳过异常机器人,避免任务整体崩溃
1 parent 9bd4d3d3
......@@ -175,9 +175,20 @@ class WeComCustomerGroupOrchestrationService:
}
def scan_new_customers(self, *, bot_ids: Iterable[str] | None = None) -> list[WeComNewCustomerItem]:
resolved_bot_ids = list(bot_ids) if bot_ids else [
item.bot_id for item in self.db.query(WeComBot).order_by(WeComBot.id.asc()).all()
]
if bot_ids:
resolved_bot_ids = [str(item).strip() for item in bot_ids if str(item).strip()]
else:
resolved_bot_ids = [
bot.bot_id
for bot in (
self.db.query(WeComBot)
.join(QrcodeChannel, QrcodeChannel.wecom_bot_id == WeComBot.id)
.filter(WeComBot.bot_id.isnot(None))
.order_by(WeComBot.id.asc())
.all()
)
if bot.bot_id
]
items: list[WeComNewCustomerItem] = []
seen_contact_ids: set[str] = set()
......@@ -636,15 +647,18 @@ class WeComCustomerGroupOrchestrationService:
def _scan_new_customers_for_bot(self, *, bot_id: str) -> list[WeComNewCustomerItem]:
bot = self.db.query(WeComBot).filter(WeComBot.bot_id == bot_id).first()
if bot is None:
raise ValueError(f"??? bot_id={bot_id} ??????????")
logger.warning(format_job_log("跳过新客户扫描:bot_id=%s 不存在" % bot_id))
return []
qrcode = self.db.query(QrcodeChannel).filter(QrcodeChannel.wecom_bot_id == bot.id).first()
if qrcode is None:
raise ValueError(f"bot_id={bot_id} ???????????????")
logger.info(format_job_log("跳过新客户扫描:bot_id=%s 未绑定渠道活码" % bot_id))
return []
position = self.db.query(PositionTag).filter(PositionTag.id == qrcode.position_id).first()
if position is None:
raise ValueError(f"bot_id={bot_id} ????????????")
logger.warning(format_job_log("跳过新客户扫描:bot_id=%s 绑定的渠道活码缺少岗位" % bot_id))
return []
resolved_contact_type = WECOM_CUSTOMER_CONTACT_TYPE
candidates: list[WeComNewCustomerItem] = []
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!