Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b3a1948a
authored
Aug 08, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: enhance AddAbilities and BatchInsertChannels to support transaction handling
parent
9682c46c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
21 deletions
+43
-21
model/ability.go
+8
-3
model/channel.go
+35
-18
No files found.
model/ability.go
View file @
b3a1948a
...
...
@@ -142,7 +142,7 @@ func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel,
return
&
channel
,
err
}
func
(
channel
*
Channel
)
AddAbilities
()
error
{
func
(
channel
*
Channel
)
AddAbilities
(
tx
*
gorm
.
DB
)
error
{
models_
:=
strings
.
Split
(
channel
.
Models
,
","
)
groups_
:=
strings
.
Split
(
channel
.
Group
,
","
)
abilitySet
:=
make
(
map
[
string
]
struct
{})
...
...
@@ -169,8 +169,13 @@ func (channel *Channel) AddAbilities() error {
if
len
(
abilities
)
==
0
{
return
nil
}
// choose DB or provided tx
useDB
:=
DB
if
tx
!=
nil
{
useDB
=
tx
}
for
_
,
chunk
:=
range
lo
.
Chunk
(
abilities
,
50
)
{
err
:=
DB
.
Clauses
(
clause
.
OnConflict
{
DoNothing
:
true
})
.
Create
(
&
chunk
)
.
Error
err
:=
use
DB
.
Clauses
(
clause
.
OnConflict
{
DoNothing
:
true
})
.
Create
(
&
chunk
)
.
Error
if
err
!=
nil
{
return
err
}
...
...
@@ -321,7 +326,7 @@ func FixAbility() (int, int, error) {
}
// Then add new abilities
for
_
,
channel
:=
range
chunk
{
err
=
channel
.
AddAbilities
()
err
=
channel
.
AddAbilities
(
nil
)
if
err
!=
nil
{
common
.
SysError
(
fmt
.
Sprintf
(
"Add abilities for channel %d failed: %s"
,
channel
.
Id
,
err
.
Error
()))
failCount
++
...
...
model/channel.go
View file @
b3a1948a
...
...
@@ -13,6 +13,7 @@ import (
"strings"
"sync"
"github.com/samber/lo"
"gorm.io/gorm"
)
...
...
@@ -337,38 +338,54 @@ func GetChannelById(id int, selectAll bool) (*Channel, error) {
}
func
BatchInsertChannels
(
channels
[]
Channel
)
error
{
var
err
error
err
=
DB
.
Create
(
&
channels
)
.
Error
if
err
!=
nil
{
if
len
(
channels
)
==
0
{
return
nil
}
tx
:=
DB
.
Begin
()
if
tx
.
Error
!=
nil
{
return
tx
.
Error
}
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
tx
.
Rollback
()
}
}()
for
_
,
chunk
:=
range
lo
.
Chunk
(
channels
,
50
)
{
if
err
:=
tx
.
Create
(
&
chunk
)
.
Error
;
err
!=
nil
{
tx
.
Rollback
()
return
err
}
for
_
,
channel_
:=
range
channels
{
err
=
channel_
.
AddAbilities
()
if
err
!=
nil
{
for
_
,
channel_
:=
range
chunk
{
if
err
:=
channel_
.
AddAbilities
(
tx
);
err
!=
nil
{
tx
.
Rollback
()
return
err
}
}
return
nil
}
return
tx
.
Commit
()
.
Error
}
func
BatchDeleteChannels
(
ids
[]
int
)
error
{
//使用事务 删除channel表和channel_ability表
if
len
(
ids
)
==
0
{
return
nil
}
// 使用事务 分批删除channel表和abilities表
tx
:=
DB
.
Begin
()
err
:=
tx
.
Where
(
"id in (?)"
,
ids
)
.
Delete
(
&
Channel
{})
.
Error
if
err
!=
nil
{
// 回滚事务
if
tx
.
Error
!=
nil
{
return
tx
.
Error
}
for
_
,
chunk
:=
range
lo
.
Chunk
(
ids
,
200
)
{
if
err
:=
tx
.
Where
(
"id in (?)"
,
chunk
)
.
Delete
(
&
Channel
{})
.
Error
;
err
!=
nil
{
tx
.
Rollback
()
return
err
}
err
=
tx
.
Where
(
"channel_id in (?)"
,
ids
)
.
Delete
(
&
Ability
{})
.
Error
if
err
!=
nil
{
// 回滚事务
if
err
:=
tx
.
Where
(
"channel_id in (?)"
,
chunk
)
.
Delete
(
&
Ability
{})
.
Error
;
err
!=
nil
{
tx
.
Rollback
()
return
err
}
// 提交事务
tx
.
Commit
()
return
err
}
return
tx
.
Commit
()
.
Error
}
func
(
channel
*
Channel
)
GetPriority
()
int64
{
...
...
@@ -412,7 +429,7 @@ func (channel *Channel) Insert() error {
if
err
!=
nil
{
return
err
}
err
=
channel
.
AddAbilities
()
err
=
channel
.
AddAbilities
(
nil
)
return
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