Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ai
/
ai-workflow
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 55832ee4
authored
Jul 17, 2026
by
sunqi02
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
refactor(customer_info): 以group_id为唯一键并增加删除对账逻辑
1 parent
e2191f82
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
13 deletions
workspace/agent-admin/backend/app/customer_info/model.py
workspace/agent-admin/backend/app/customer_info/service.py
workspace/agent-admin/backend/app/infrastructure/schema_patch.py
workspace/agent-admin/backend/docs/superpowers/sunqi/plans/2026-07-15-customer-info-push-type-impl.md
workspace/agent-admin/backend/tests/test_customer_info_service.py
workspace/agent-admin/backend/app/customer_info/model.py
View file @
55832ee
...
...
@@ -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"
),
)
workspace/agent-admin/backend/app/customer_info/service.py
View file @
55832ee
...
...
@@ -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
]:
...
...
workspace/agent-admin/backend/app/infrastructure/schema_patch.py
View file @
55832ee
...
...
@@ -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
...
...
workspace/agent-admin/backend/docs/superpowers/sunqi/plans/2026-07-15-customer-info-push-type-impl.md
0 → 100644
View file @
55832ee
This diff is collapsed.
Click to expand it.
workspace/agent-admin/backend/tests/test_customer_info_service.py
View file @
55832ee
...
...
@@ -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
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment