Skip to content

Commit

Permalink
demo环境
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 14, 2023
1 parent 7791c17 commit 8d49bd2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
25 changes: 25 additions & 0 deletions cmd/haobase/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assets
import (
"time"

"github.com/shopspring/decimal"
"github.com/yzimhao/trading_engine/utils"
)

Expand Down Expand Up @@ -65,3 +66,27 @@ func FindSymbol(user_id string, symbol string) *Assets {
db_engine.Table(new(Assets)).Where("user_id=? and symbol=?", user_id, symbol).Get(&row)
return &row
}

func BalanceOfTotal(user_id, symbol string) decimal.Decimal {
row := FindSymbol(user_id, symbol)
if row.Id > 0 {
return utils.D(row.Total)
}
return decimal.Zero
}

func BalanceOfFreeze(user_id, symbol string) decimal.Decimal {
row := FindSymbol(user_id, symbol)
if row.Id > 0 {
return utils.D(row.Freeze)
}
return decimal.Zero
}

func BalanceOfAvailable(user_id, symbol string) decimal.Decimal {
row := FindSymbol(user_id, symbol)
if row.Id > 0 {
return utils.D(row.Available)
}
return decimal.Zero
}
12 changes: 12 additions & 0 deletions cmd/haobase/www/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package www

import (
"github.com/yzimhao/trading_engine/cmd/haobase/base/symbols"
"github.com/yzimhao/trading_engine/utils/app"
)

func demoBaseData() {
if app.RunMode == app.ModeDemo {
symbols.DemoData()
}
}
17 changes: 16 additions & 1 deletion cmd/haobase/www/middle/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package middle

import (
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
"github.com/yzimhao/trading_engine/cmd/haobase/assets"
"github.com/yzimhao/trading_engine/utils"
"github.com/yzimhao/trading_engine/utils/app"
)
Expand All @@ -10,11 +12,24 @@ func CheckLogin() gin.HandlerFunc {
return func(c *gin.Context) {
user_id := ""
if app.RunMode == app.ModeDemo {
user_id = c.GetHeader("UserId")
//自动为demo用户充值三种货币
user_id = c.GetHeader("User-Id")
if user_id != "" {
if assets.BalanceOfTotal(user_id, "usd").Equal(decimal.Zero) {
assets.SysRecharge(user_id, "usd", "10000.00", "sys_recharge")
}
if assets.BalanceOfTotal(user_id, "jpy").Equal(decimal.Zero) {
assets.SysRecharge(user_id, "jpy", "10000.00", "sys_recharge")
}
if assets.BalanceOfTotal(user_id, "eur").Equal(decimal.Zero) {
assets.SysRecharge(user_id, "eur", "10000.00", "sys_recharge")
}
}
}

if user_id == "" {
utils.ResponseFailJson(c, "需要登录")
c.Abort()
return
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/haobase/www/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func Run() {
demoBaseData()

g := gin.New()
router(g)
g.Run(viper.GetString("haobase.http.host"))
Expand Down

0 comments on commit 8d49bd2

Please sign in to comment.