Commit 55832ee4 by sunqi02

refactor(customer_info): 以group_id为唯一键并增加删除对账逻辑

1 parent e2191f82
......@@ -18,8 +18,8 @@ class CustomerGroupPhone(Base):
update_time = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"), onupdate=text("CURRENT_TIMESTAMP"), nullable=False)
__table_args__ = (
UniqueConstraint("customer_name", "group_id", name="uk_customer_name_group_id"),
Index("idx_group_id", "group_id"),
# 以 group_id 作为唯一键,它是群的稳定标识。
UniqueConstraint("group_id", name="uk_group_id"),
Index("idx_phone", "phone"),
Index("idx_push_candidates", "group_status", "phone", "last_pushed_at"),
)
......@@ -20,13 +20,14 @@ class CustomerInfoService:
self.db = db
def sync_from_customer_groups(self) -> int:
"""从 wecom_customer_groups 同步数据,返回受影响行数。
"""从 wecom_customer_groups 同步群状态与客户名到 customer_group_phones,返回受影响行数。
使用 INSERT ... ON DUPLICATE KEY UPDATE 实现 upsert。
只同步 is_deleted=0 AND group_id IS NOT NULL 的记录。
按 group_id 关联,使用 INSERT ... ON DUPLICATE KEY UPDATE 完成新增与更新。
"""
now = china_now()
result = self.db.execute(
# 新增 + 更新:group_status / customer_name 变化都能覆盖
upserted = self.db.execute(
text("""
INSERT INTO customer_group_phones (customer_name, group_id, group_status, create_time, update_time)
SELECT wcg.customer_name, wcg.group_id, wcg.group_status, :now, :now
......@@ -34,13 +35,27 @@ class CustomerInfoService:
WHERE wcg.is_deleted = 0 AND wcg.group_id IS NOT NULL
ON DUPLICATE KEY UPDATE
group_status = VALUES(group_status),
customer_name = VALUES(customer_name),
update_time = VALUES(update_time)
"""),
{"now": now},
)
).rowcount
# 对账:源表已逻辑删除(is_deleted=1)或不存在的群,标记目标行为 deleted
reconciled = self.db.execute(
text("""
UPDATE customer_group_phones cgp
LEFT JOIN wecom_customer_groups wcg
ON wcg.group_id = cgp.group_id AND wcg.is_deleted = 0 AND wcg.group_id IS NOT NULL
SET cgp.group_status = 'deleted', cgp.update_time = :now
WHERE wcg.group_id IS NULL AND cgp.group_status != 'deleted'
"""),
{"now": now},
).rowcount
self.db.commit()
affected = result.rowcount
logger.info("同步完成: affected=%s", affected)
affected = upserted + reconciled
logger.info("同步完成: upserted=%s reconciled=%s total=%s", upserted, reconciled, affected)
return affected
def bind_phone(self, group_id: str, phone: str) -> Optional[CustomerGroupPhone]:
......
......@@ -266,8 +266,7 @@ def _ensure_mysql_runtime_schema(conn) -> None:
last_pushed_at DATETIME DEFAULT NULL,
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uk_customer_name_group_id (customer_name, group_id),
KEY idx_group_id (group_id),
UNIQUE KEY uk_group_id (group_id),
KEY idx_phone (phone),
KEY idx_push_candidates (group_status, phone, last_pushed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
......
......@@ -168,7 +168,7 @@ class TestBindPhone:
db_session.commit()
service = CustomerInfoService(db_session)
result = service.bind_phone("绑定测试用户", "group_phone", "13800138000")
result = service.bind_phone("group_phone", "13800138000")
assert result is not None
assert result.phone == "13800138000"
......@@ -296,4 +296,4 @@ class TestCustomerGroupPhoneDDL:
"""验证唯一约束存在。"""
constraints = CustomerGroupPhone.__table_args__
names = [c.name for c in constraints if hasattr(c, 'name')]
assert "uk_customer_name_group_id" in names
assert "uk_group_id" in names
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!