Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
cf725231
Commit
cf725231
authored
Feb 08, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
domain也ok了
parent
96755b19
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
0 deletions
+77
-0
ctx.go
domain/ctx.go
+13
-0
event.go
domain/event.go
+9
-0
model.go
domain/model.go
+55
-0
No files found.
domain/ctx.go
0 → 100644
View file @
cf725231
package
domain
import
(
"git.hilo.cn/hilo-common/mycontext"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"
)
type
CtxAndDb
struct
{
Db
*
gorm
.
DB
*
mycontext
.
MyContext
Redis
*
redis
.
Client
}
domain/event.go
0 → 100644
View file @
cf725231
package
domain
//异步执行的接口
type
AsyncEvent
interface
{
AsyncDo
(
model
*
Model
,
eventData
interface
{},
n
int
)
error
AsyncSize
()
int
AsyncNoTxDo
(
model
*
Model
,
eventData
interface
{},
n
int
)
error
AsyncNoTxSize
()
int
}
domain/model.go
0 → 100644
View file @
cf725231
package
domain
import
(
"git.hilo.cn/hilo-common/mycontext"
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/resource/redisCli"
"gorm.io/gorm"
)
type
Model
struct
{
*
CtxAndDb
`gorm:"-"`
}
func
CreateModel
(
ctxAndDb
*
CtxAndDb
)
*
Model
{
return
&
Model
{
CtxAndDb
:
ctxAndDb
}
}
func
CreateModelContext
(
myContext
*
mycontext
.
MyContext
)
*
Model
{
return
&
Model
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
myContext
,
Redis
:
redisCli
.
GetRedis
(),
},
}
}
func
CreateModelNil
()
*
Model
{
return
&
Model
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
mycontext
.
CreateMyContext
(
nil
),
Redis
:
redisCli
.
GetRedis
(),
},
}
}
func
(
m
*
Model
)
DB
()
*
gorm
.
DB
{
return
m
.
Db
.
WithContext
(
m
)
}
// 包装事务
// 注意:需要使用新的model
func
(
m
*
Model
)
Transaction
(
f
func
(
*
Model
)
error
)
error
{
// 公用context
// 新的db
txModel
:=
CreateModelContext
(
m
.
MyContext
)
txModel
.
Db
=
m
.
Db
.
Begin
()
.
WithContext
(
m
)
err
:=
f
(
txModel
)
if
err
!=
nil
{
txModel
.
Db
.
Rollback
()
return
err
}
return
txModel
.
Db
.
Commit
()
.
Error
}
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