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
eee21ef5
Commit
eee21ef5
authored
Aug 21, 2023
by
JiebinHu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:事务操作账号明细
明早细细调,用游戏服务
parent
d9e63096
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
113 additions
and
0 deletions
+113
-0
diamond.go
txop/diamond_tx/diamond.go
+113
-0
No files found.
txop/diamond_tx/diamond.go
View file @
eee21ef5
...
@@ -6,8 +6,11 @@ import (
...
@@ -6,8 +6,11 @@ import (
"git.hilo.cn/hilo-common/internal/enum/diamond_e"
"git.hilo.cn/hilo-common/internal/enum/diamond_e"
"git.hilo.cn/hilo-common/internal/enum/msg_e"
"git.hilo.cn/hilo-common/internal/enum/msg_e"
"git.hilo.cn/hilo-common/internal/model/diamond_m"
"git.hilo.cn/hilo-common/internal/model/diamond_m"
"git.hilo.cn/hilo-common/myerr"
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/txop/msg"
"git.hilo.cn/hilo-common/txop/msg"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
)
// 下发钻石
// 下发钻石
...
@@ -51,3 +54,113 @@ func SendDiamondPink(model *domain.Model, userId mysql.ID, opt diamond_e.Operate
...
@@ -51,3 +54,113 @@ func SendDiamondPink(model *domain.Model, userId mysql.ID, opt diamond_e.Operate
}
}
return
nil
return
nil
}
}
// 事务操作账号明细
// 注意:外层不能再加事务了
// 注意:DiamondOperateSet限流的不能用
// 账号冻结/账号余额:属于业务逻辑,不在事务做
// 函数步骤
// 1. 非事务前置判断: 获取账号信息,加减,封号等
// 事务
// 2. diamondAccount表原子操作, 判断更新条数
// 3. 更新明细表
// param extraTxFunc: 外部函数,可能会影响到事务回滚的函数
func
TxAddReduceDiamondAccount
(
model
*
domain
.
Model
,
userId
mysql
.
ID
,
operateType
diamond_e
.
OperateType
,
diamondNum
mysql
.
Num
,
originId
mysql
.
ID
,
operateIds
mysql
.
Str
,
extraTxFunc
...
func
()
error
)
error
{
var
diamondOperateSet
diamond_m
.
DiamondOperateSet
var
err
error
if
err
=
model
.
DB
()
.
Where
(
&
diamond_m
.
DiamondOperateSet
{
Type
:
operateType
,
Status
:
mysql
.
USER
,
DiamondType
:
diamond_e
.
DiamondYellow
,
})
.
First
(
&
diamondOperateSet
)
.
Error
;
err
!=
nil
{
return
err
}
// logs
model
.
Log
=
model
.
Log
.
WithFields
(
logrus
.
Fields
{
"func"
:
"TxAddReduceDiamondAccount"
,
"uid"
:
userId
,
"ot"
:
operateType
,
"num"
:
diamondNum
,
"oid"
:
originId
,
"oids"
:
operateIds
,
"ar"
:
diamondOperateSet
.
AddReduce
,
})
var
diamondAccount
=
new
(
diamond_m
.
DiamondAccount
)
if
err
:=
model
.
DB
()
.
Model
(
diamond_m
.
DiamondAccount
{})
.
Where
(
"user_id = ?"
,
userId
)
.
First
(
diamondAccount
)
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"TxAddReduceDiamondAccount fail :%v"
,
err
)
return
err
}
if
diamondOperateSet
.
AddReduce
==
mysql
.
REDUCE
&&
diamondAccount
.
DiamondNum
<
diamondNum
{
return
fmt
.
Errorf
(
"bizerr.DiamondAccountNotEnough"
)
}
if
diamondAccount
.
Status
==
diamond_e
.
Frozen
&&
diamondOperateSet
.
AddReduce
==
mysql
.
REDUCE
{
return
fmt
.
Errorf
(
"bizerr.DiamondAccountFrozen"
)
}
return
model
.
Transaction
(
func
(
model
*
domain
.
Model
)
error
{
// 更新diamondAccount表
var
db
*
gorm
.
DB
if
diamondOperateSet
.
AddReduce
==
mysql
.
ADD
{
//增加
db
=
model
.
DB
()
.
Model
(
diamond_m
.
DiamondAccount
{})
.
UpdateColumn
(
"diamond_num"
,
gorm
.
Expr
(
"diamond_num + ?"
,
diamondNum
))
}
else
if
diamondOperateSet
.
AddReduce
==
mysql
.
REDUCE
{
//减少,保证不能扣成负数
db
=
model
.
DB
()
.
Model
(
diamond_m
.
DiamondAccount
{})
.
Where
(
"diamond_num >= ?"
,
diamondNum
)
.
UpdateColumn
(
"diamond_num"
,
gorm
.
Expr
(
"diamond_num - ?"
,
diamondNum
))
}
else
{
return
myerr
.
NewSysErrorF
(
"addReduce 枚举错误 value:"
,
diamondOperateSet
.
AddReduce
)
}
// 错误判断
if
db
==
nil
{
model
.
Log
.
Errorf
(
"db nil"
)
return
myerr
.
NewSysError
(
"db nil"
)
}
if
err
:=
db
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"db err:%v"
,
err
)
return
myerr
.
WrapErr
(
err
)
}
if
db
.
RowsAffected
==
0
{
model
.
Log
.
Errorf
(
"gorm condition update.RowsAffected = 0"
)
return
myerr
.
NewWaring
(
"gorm condition update.RowsAffected = 0"
)
}
// 写后读(写后缓存都会丢失,多读一次不影响)
newAccount
:=
new
(
diamond_m
.
DiamondAccount
)
if
err
:=
model
.
DB
()
.
Model
(
diamond_m
.
DiamondAccount
{})
.
Where
(
"id = ?"
,
diamondAccount
.
ID
)
.
First
(
newAccount
)
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"newAccount err:%v"
,
err
)
return
err
}
if
newAccount
.
DiamondNum
<
0
{
model
.
Log
.
Errorf
(
"newAccount 0"
)
return
myerr
.
NewSysError
(
"newAccount 0"
)
}
// 更新明细
befNum
:=
mysql
.
Num
(
0
)
if
diamondOperateSet
.
AddReduce
==
mysql
.
ADD
{
befNum
=
newAccount
.
DiamondNum
-
diamondNum
}
if
diamondOperateSet
.
AddReduce
==
mysql
.
REDUCE
{
befNum
=
newAccount
.
DiamondNum
+
diamondNum
}
if
err
:=
model
.
DB
()
.
Table
(
diamond_m
.
DiamondAccountDetail
{}
.
TableName
())
.
Create
(
&
diamond_m
.
DiamondAccountDetail
{
UserId
:
userId
,
DiamondAccountId
:
newAccount
.
ID
,
OperateId
:
diamondOperateSet
.
ID
,
OperateType
:
operateType
,
OriginId
:
originId
,
AddReduce
:
diamondOperateSet
.
AddReduce
,
Num
:
diamondNum
,
Remark
:
diamondOperateSet
.
Name
,
BefNum
:
befNum
,
AftNum
:
newAccount
.
DiamondNum
,
})
.
Error
;
err
!=
nil
{
model
.
Log
.
Errorf
(
"add detail fail:%v"
,
err
)
return
err
}
for
i
,
f
:=
range
extraTxFunc
{
if
err
:=
f
();
err
!=
nil
{
model
.
Log
.
Errorf
(
"extraFunc i:%v fail:%v"
,
i
,
err
)
return
err
}
}
return
nil
})
}
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