Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
f1e47291
Commit
f1e47291
authored
Dec 27, 2025
by
shaw
Browse files
fix: 修复账号更新时分组绑定操作顺序导致的数据不一致问题
原逻辑先执行 Update 再验证 GroupIDs,如果验证失败会导致账号已更新但返回错误。 现改为先验证分组是否存在,再执行 Update 和 BindGroups。
parent
d7e9ae38
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/internal/service/account_service.go
View file @
f1e47291
...
@@ -208,20 +208,23 @@ func (s *AccountService) Update(ctx context.Context, id int64, req UpdateAccount
...
@@ -208,20 +208,23 @@ func (s *AccountService) Update(ctx context.Context, id int64, req UpdateAccount
account
.
Status
=
*
req
.
Status
account
.
Status
=
*
req
.
Status
}
}
if
err
:=
s
.
accountRepo
.
Update
(
ctx
,
account
);
err
!=
nil
{
// 先验证分组是否存在(在任何写操作之前)
return
nil
,
fmt
.
Errorf
(
"update account: %w"
,
err
)
}
// 更新分组绑定
if
req
.
GroupIDs
!=
nil
{
if
req
.
GroupIDs
!=
nil
{
// 验证分组是否存在
for
_
,
groupID
:=
range
*
req
.
GroupIDs
{
for
_
,
groupID
:=
range
*
req
.
GroupIDs
{
_
,
err
:=
s
.
groupRepo
.
GetByID
(
ctx
,
groupID
)
_
,
err
:=
s
.
groupRepo
.
GetByID
(
ctx
,
groupID
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"get group: %w"
,
err
)
return
nil
,
fmt
.
Errorf
(
"get group: %w"
,
err
)
}
}
}
}
}
// 执行更新
if
err
:=
s
.
accountRepo
.
Update
(
ctx
,
account
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"update account: %w"
,
err
)
}
// 绑定分组
if
req
.
GroupIDs
!=
nil
{
if
err
:=
s
.
accountRepo
.
BindGroups
(
ctx
,
account
.
ID
,
*
req
.
GroupIDs
);
err
!=
nil
{
if
err
:=
s
.
accountRepo
.
BindGroups
(
ctx
,
account
.
ID
,
*
req
.
GroupIDs
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"bind groups: %w"
,
err
)
return
nil
,
fmt
.
Errorf
(
"bind groups: %w"
,
err
)
}
}
...
...
backend/internal/service/admin_service.go
View file @
f1e47291
...
@@ -652,11 +652,20 @@ func (s *adminServiceImpl) UpdateAccount(ctx context.Context, id int64, input *U
...
@@ -652,11 +652,20 @@ func (s *adminServiceImpl) UpdateAccount(ctx context.Context, id int64, input *U
account
.
Status
=
input
.
Status
account
.
Status
=
input
.
Status
}
}
// 先验证分组是否存在(在任何写操作之前)
if
input
.
GroupIDs
!=
nil
{
for
_
,
groupID
:=
range
*
input
.
GroupIDs
{
if
_
,
err
:=
s
.
groupRepo
.
GetByID
(
ctx
,
groupID
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"get group: %w"
,
err
)
}
}
}
if
err
:=
s
.
accountRepo
.
Update
(
ctx
,
account
);
err
!=
nil
{
if
err
:=
s
.
accountRepo
.
Update
(
ctx
,
account
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
//
更新分组
绑定
// 绑定
分组
if
input
.
GroupIDs
!=
nil
{
if
input
.
GroupIDs
!=
nil
{
if
err
:=
s
.
accountRepo
.
BindGroups
(
ctx
,
account
.
ID
,
*
input
.
GroupIDs
);
err
!=
nil
{
if
err
:=
s
.
accountRepo
.
BindGroups
(
ctx
,
account
.
ID
,
*
input
.
GroupIDs
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
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 comment