i18n翻译

This commit is contained in:
hu xiaotong
2025-07-14 17:14:58 +08:00
parent 6061508ec9
commit b8c9817cb3
5 changed files with 151 additions and 146 deletions

View File

@@ -30,7 +30,6 @@ func NewThirdPartyDataSync() *ThirdPartyDataSync {
}
}
// SyncHeroData 同步英雄数据
func (t *ThirdPartyDataSync) SyncHeroData(ctx context.Context) error {
g.Log().Info(ctx, "开始同步英雄数据...")
@@ -130,7 +129,6 @@ func (t *ThirdPartyDataSync) fetchArtifactDataFromAPI(ctx context.Context) (stri
return string(content), nil
}
// processAndSaveHeroData 处理并保存英雄数据
func (t *ThirdPartyDataSync) processAndSaveHeroData(ctx context.Context, data []byte) error {
// 使用 gjson 解析
j := gjson.New(data)
@@ -147,13 +145,82 @@ func (t *ThirdPartyDataSync) processAndSaveHeroData(ctx context.Context, data []
g.Log().Info(ctx, "解析到", len(heroes), "个英雄数据")
// 一次性查出所有数据库英雄构建map
var dbHeroes []*entity.EpicHeroInfo
err := dao.EpicHeroInfo.Ctx(ctx).Scan(&dbHeroes)
if err != nil {
return fmt.Errorf("查询数据库英雄失败: %v", err)
}
heroMap := make(map[string]*entity.EpicHeroInfo, len(dbHeroes))
for _, h := range dbHeroes {
heroMap[h.HeroCode] = h
}
// 遍历 map设置 Name 字段,并保存
for name, hero := range heroes {
hero.Name = name // 将 map 的 key 作为 Name 字段
if err := t.saveHeroData(ctx, hero); err != nil {
g.Log().Error(ctx, "保存英雄数据失败:", err)
dbHero, exists := heroMap[hero.Code]
if exists && dbHero.RawJson != "" {
// 已有且rawJson有值跳过
continue
}
if exists && dbHero.RawJson == "" {
// 只更新 rawJson 字段
rawJsonBytes, err := json.Marshal(hero)
if err != nil {
g.Log().Error(ctx, "序列化英雄数据失败:", err)
continue
}
rawJson := string(rawJsonBytes)
_, err = dao.EpicHeroInfo.Ctx(ctx).
Where(dao.EpicHeroInfo.Columns().HeroCode, hero.Code).
Data(g.Map{
dao.EpicHeroInfo.Columns().RawJson: rawJson,
dao.EpicHeroInfo.Columns().UpdateTime: gtime.Now(),
}).
Update()
if err != nil {
g.Log().Error(ctx, "更新英雄rawJson失败:", err)
continue
}
g.Log().Debug(ctx, "更新英雄rawJson:", hero.Code)
continue
}
// 新增逻辑保持原样
status60 := hero.CalculatedStatus.Lv60SixStarFullyAwakened
status60json, _ := gjson.EncodeString(status60)
heroJson, _ := gjson.EncodeString(hero)
zhHeroName := i18n.GetZh(ctx, hero.Name)
//zhRole := i18n.GetZh(ctx, hero.Role)
//zhAttribute := i18n.GetZh(ctx, hero.Attribute)
fmt.Println(hero.Assets.Image)
newHero := &entity.EpicHeroInfo{
Id: 0,
HeroName: zhHeroName,
HeroCode: hero.Code,
HeroAttrLv60: status60json,
Creator: "",
CreateTime: gtime.Now(),
Updater: "",
UpdateTime: gtime.Now(),
Deleted: false,
//NickName: "",
Rarity: strconv.Itoa(hero.Rarity),
Role: hero.Role,
//Zodiac: "",
HeadImgUrl: "",
Attribute: hero.Attribute,
Remark: "",
RawJson: heroJson,
}
_, err = dao.EpicHeroInfo.Ctx(ctx).Data(newHero).Insert()
if err != nil {
g.Log().Error(ctx, "插入新英雄失败:", err)
continue
}
g.Log().Debug(ctx, "插入新英雄:", hero.Code)
}
return nil
@@ -210,8 +277,7 @@ func (t *ThirdPartyDataSync) saveHeroData(ctx context.Context, hero *dto.ThirdPa
_, err = dao.EpicHeroInfo.Ctx(ctx).
Where(dao.EpicHeroInfo.Columns().HeroCode, hero.Code).
Data(g.Map{
dao.EpicHeroInfo.Columns().RawJson: rawJson,
// 可选:更新时间
dao.EpicHeroInfo.Columns().RawJson: rawJson,
dao.EpicHeroInfo.Columns().UpdateTime: gtime.Now(),
}).
Update()
@@ -222,13 +288,13 @@ func (t *ThirdPartyDataSync) saveHeroData(ctx context.Context, hero *dto.ThirdPa
}
// 已有 rawJson不做处理
} else {
status60 := hero.CalculatedStatus["Lv60SixStarFullyAwakened"]
status60 := hero.CalculatedStatus.Lv60SixStarFullyAwakened
status60json, _ := gjson.EncodeString(status60)
heroJson, _ := gjson.EncodeString(hero)
// 使用i18n服务转换字段
zhHeroName := i18n.GetZh(ctx, hero.Name)
zhRole := i18n.GetZh(ctx, hero.Role)
zhAttribute := i18n.GetZh(ctx, hero.Attribute)
//zhRole := i18n.GetZh(ctx, hero.Role)
//zhAttribute := i18n.GetZh(ctx, hero.Attribute)
newHero := &entity.EpicHeroInfo{
Id: 0,
@@ -236,18 +302,18 @@ func (t *ThirdPartyDataSync) saveHeroData(ctx context.Context, hero *dto.ThirdPa
HeroCode: hero.Code,
HeroAttrLv60: status60json,
Creator: "",
CreateTime: nil,
CreateTime: gtime.Now(),
Updater: "",
UpdateTime: nil,
UpdateTime: gtime.Now(),
Deleted: false,
NickName: "",
Rarity: strconv.Itoa(hero.Rarity),
Role: zhRole,
Zodiac: "",
HeadImgUrl: "",
Attribute: zhAttribute,
Remark: "",
RawJson: heroJson,
//NickName: nil,
Rarity: strconv.Itoa(hero.Rarity),
Role: hero.Role,
Zodiac: "",
HeadImgUrl: "",
Attribute: hero.Attribute,
Remark: "",
RawJson: heroJson,
}
// 没查到,插入新记录,字段按 DO 结构体补全