feat: 添加导入原有积分功能。

main
NorthLan 3 years ago
parent f5194c7ef8
commit a894f97638

@ -2,6 +2,7 @@ package model
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc" "git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -19,8 +20,8 @@ type (
userIntegralModel userIntegralModel
Transact(ctx context.Context, tx *gorm.DB, fn func(tx *gorm.DB) error) error Transact(ctx context.Context, tx *gorm.DB, fn func(tx *gorm.DB) error) error
InsertTx(ctx context.Context, tx *gorm.DB, data *UserIntegral) error InsertTx(ctx context.Context, tx *gorm.DB, data *UserIntegral) error
UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, addon int64) error
FindIntegral(ctx context.Context, tx *gorm.DB, userId int64) (int64, error) FindIntegral(ctx context.Context, tx *gorm.DB, userId int64) (int64, error)
UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, addon int64) error
// ChangeIntegral 用户积分变动 // ChangeIntegral 用户积分变动
ChangeIntegral(ctx context.Context, tx *gorm.DB, userId int64, change int64) (int64, error) ChangeIntegral(ctx context.Context, tx *gorm.DB, userId int64, change int64) (int64, error)
} }
@ -110,6 +111,9 @@ func (m *customUserIntegralModel) ChangeIntegral(ctx context.Context, tx *gorm.D
} }
resp = integral + change resp = integral + change
return nil return nil
}, &sql.TxOptions{
Isolation: sql.LevelReadCommitted,
ReadOnly: false,
}) })
return resp, err return resp, err
} }

@ -0,0 +1,14 @@
package integral
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"live-service/app/user_center/rpc/internal/svc"
)
type IntegralTemp struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}

@ -0,0 +1,91 @@
package integral
import (
"bufio"
"context"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"live-service/app/user_center/model"
"log"
"os"
"regexp"
"strconv"
"strings"
"testing"
"time"
)
func TestA(t *testing.T) {
file, err := os.Open("C:\\Users\\NorthLan\\Desktop\\dmgame v2\\5月.txt")
if err != nil {
fmt.Println("读取文件失败")
return
}
defer file.Close()
regex, _ := regexp.Compile(`\d+`)
type jf struct {
name string
integral int64
}
jfs := make([]jf, 0)
scanner := bufio.NewScanner(file)
l := 0
var tmpJ jf
for scanner.Scan() {
l++
line := scanner.Text()
if l == 3 {
// 名字
tmpJ.name = strings.TrimSpace(line)
} else if l == 4 {
// 电池
battery, _ := strconv.ParseInt(regex.FindString(line), 10, 0)
tmpJ.integral = battery * 100
jfs = append(jfs, tmpJ)
//fmt.Println(battery)
l = 0
}
}
gormDb, err := gorm.Open(mysql.Open("root:root@tcp(127.0.0.1:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true"), &gorm.Config{
Logger: logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: 5 * time.Second,
LogLevel: logger.Info,
IgnoreRecordNotFoundError: true,
Colorful: true,
},
),
})
//UserPlatformModel := model.NewUserPlatformModel(gormDb)
UserIntegralModel := model.NewUserIntegralModel(gormDb)
gormDb.Transaction(func(tx *gorm.DB) error {
for _, tmpJ := range jfs {
var userId int64
if err := tx.Table("user_platform").
Select("user_id").
Where("p_uname = ?", tmpJ.name).Take(&userId).Error; err != nil {
continue
}
//fmt.Println(userId)
UserIntegralModel.ChangeIntegral(context.Background(), tx, userId, tmpJ.integral)
//tx.Table("user_integral").
// Where("user_id = ?", userId).
// Update("integral", gorm.Expr("integral + ?", tmpJ.integral))
}
return nil
})
if err := scanner.Err(); err != nil {
fmt.Println("err", err)
}
}
Loading…
Cancel
Save