diff --git a/app/user_center/model/user_integral_model.go b/app/user_center/model/user_integral_model.go index 89c2f00..67fff5d 100644 --- a/app/user_center/model/user_integral_model.go +++ b/app/user_center/model/user_integral_model.go @@ -2,6 +2,7 @@ package model import ( "context" + "database/sql" "fmt" "git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc" "github.com/pkg/errors" @@ -19,8 +20,8 @@ type ( userIntegralModel Transact(ctx context.Context, tx *gorm.DB, fn func(tx *gorm.DB) error) 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) + UpdateIntegralTx(ctx context.Context, tx *gorm.DB, userId, addon int64) error // ChangeIntegral 用户积分变动 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 return nil + }, &sql.TxOptions{ + Isolation: sql.LevelReadCommitted, + ReadOnly: false, }) return resp, err } diff --git a/app/user_center/rpc/internal/logic/integral/integral_temp.go b/app/user_center/rpc/internal/logic/integral/integral_temp.go new file mode 100644 index 0000000..a648763 --- /dev/null +++ b/app/user_center/rpc/internal/logic/integral/integral_temp.go @@ -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 +} diff --git a/app/user_center/rpc/internal/logic/integral/integral_temp_test.go b/app/user_center/rpc/internal/logic/integral/integral_temp_test.go new file mode 100644 index 0000000..689c4b8 --- /dev/null +++ b/app/user_center/rpc/internal/logic/integral/integral_temp_test.go @@ -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) + } +}