dtp/function/prepUser.go

162 lines
3.8 KiB
Go

package function
import (
"bufio"
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/Leufolium/test/dbstruct"
"github.com/Leufolium/test/idgenerator"
"github.com/Leufolium/test/mongo"
goproto "google.golang.org/protobuf/proto"
)
func PrepUser() {
fmt.Println("Start importing...")
client, err := mongo.NewMongo()
if err != nil {
fmt.Printf("mongo client init fail : %v", err)
return
}
infilePath := "/app/dataprep/file/outfile.txt"
infoPath := "/app/dataprep/file/infile.txt"
infile, err := os.Open(infilePath)
if err != nil {
fmt.Printf("Open File Err : %v", err)
}
infofile, err1 := os.Open(infoPath)
if err1 != nil {
fmt.Printf("Open File Err : %v", err)
}
defer infile.Close()
reader := bufio.NewReader(infile)
inforeader := bufio.NewReader(infofile)
_map := make(map[int64]int64)
for {
str, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("ReadString fail : %v", err)
return
}
strs := strings.Split(str, ":")
seqStr := strs[0]
userIdStr := strs[1]
userIdStr = strings.ReplaceAll(userIdStr, "\n", "")
userIdStr = strings.ReplaceAll(userIdStr, "\r", "")
seq, err := strconv.Atoi(seqStr)
if err != nil {
fmt.Printf("atoi fail : %v", err)
return
}
userId, err := strconv.Atoi(userIdStr)
if err != nil {
fmt.Printf("atoi fail : %v", err)
return
}
_map[int64(seq)] = int64(userId)
}
imageIds := []int64{1}
offset := 0
for {
str, err := inforeader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("ReadString fail : %v", err)
return
}
strs := strings.Split(str, " ")
name := strs[0]
//mobilephone := strs[1]
phonehash := strs[2]
aes := strs[3]
aes = strings.ReplaceAll(aes, "\n", "")
aes = strings.ReplaceAll(aes, "\r", "")
accountSeqId, err := client.GetAndUpdateAccountIdSeq(context.Background())
if err != nil {
fmt.Printf("GetAndUpdateAccountIdSeq err : %v", err)
}
userSeqId, err := client.GetAndUpdateUserIdSeq(context.Background())
if err != nil {
fmt.Printf("GetAndUpdateUserIdSeq err : %v", err)
}
account := &dbstruct.Account{
Mid: goproto.Int64(int64(accountSeqId.Seq)),
Name: goproto.String(name),
UserId: goproto.Int64(_map[userSeqId.Seq]),
Avatar: &dbstruct.MediaComponent{
ImageIds: &imageIds,
},
MobilePhone: goproto.String(aes),
PhoneHash: goproto.String(phonehash),
RegionCode: goproto.String("86"),
Level: goproto.Int64(1),
Role: goproto.Int64(0),
CurrentExp: goproto.Int64(0),
CurrentLevelExp: goproto.Int64(100),
IsDndModeEnabled: goproto.Int64(0),
GoldNum: goproto.Int64(0),
DiamondNum: goproto.Int64(0),
Ct: goproto.Int64(time.Now().Unix()),
Ut: goproto.Int64(time.Now().Unix()),
DelFlag: goproto.Int64(0),
}
err = client.CreateAccount(context.Background(), account)
if err != nil {
fmt.Printf("CreateAccount err : %v", err)
}
login := &dbstruct.Login{
Id: goproto.Int64(idgenerator.GenLoginId() + int64(offset)),
Mid: account.Mid,
Password: goproto.String("472642e65f4132e33a4b916f82165e1a"),
PhoneHash: goproto.String(phonehash),
RegionCode: goproto.String("86"),
IsLogined: goproto.Int64(0),
LastLoginTime: goproto.Int64(0),
WrongPswdTimes: goproto.Int64(0),
IsEnabled: goproto.Int64(1),
IsLocked: goproto.Int64(0),
IsBanned: goproto.Int64(0),
Ct: goproto.Int64(time.Now().Unix()),
Ut: goproto.Int64(time.Now().Unix()),
DelFlag: goproto.Int64(0),
}
err = client.CreateLogin(context.Background(), login)
if err != nil {
fmt.Printf("CreateLogin err : %v", err)
}
offset++
}
fmt.Println("End importing...")
}