package jobs //import ( // "encoding/json" // "errors" // "fmt" // "gadmin/internal/cron/jobs/types" // "github.com/hashicorp/go-version" // "github.com/sirupsen/logrus" // "io" // "io/fs" // "io/ioutil" // "leafstalk/covenant/constants" // "leafstalk/covenant/model" // "net/http" // "os" // "path" // "path/filepath" // "strconv" // "strings" // "sync" // "time" //) // //var SyncGoodsJson = new(JSyncGoodsJson) // //type JSyncGoodsJson struct { // sync.Mutex // TMap map[string]int // bc *model.BaseConfig // center []*types.GiftCenterItem // outPath string //} // //// 需要下载的json文件列表 //var downJsonFiles = []string{ // "base_data.json", // "Giftcenter.json", //礼包分类 // "shop.json", // "PropsStore.json", // "LimitGift.json", // // "DailyGift.json", // "RoleGift.json", // "NewerGift.json", // "Holiday9Gift.json", // "LanternFestivalGift.json", // "Holiday3_Gift.json", // "BuyOnceGetThreeDays.json", // "TombSweepingDayGift.json", // "LaborDayGift.json", // "DragonBoatFestivalGift.json", // "WeekendGift.json", // "QixiFestivalGift.json", // "MoonFestivalGift.json", // "EquipBoxGiftPack.json", // "EquipBoxBase.json", // "EndlessPassportBase.json", //} // //var passJsonFiles = map[string]struct{}{ // "base_data.json": {}, // "Giftcenter.json": {}, // "shop.json": {}, // "PropsStore.json": {}, // "LimitGift.json": {}, //} // //func NewSyncGoodsJson() *JSyncGoodsJson { // return &JSyncGoodsJson{ // TMap: make(map[string]int), // bc: &model.BaseConfig{}, // center: make([]*types.GiftCenterItem, 0), // outPath: "", // } //} // //func (j *JSyncGoodsJson) Run(jsonVersion string) { // logrus.Info("同步商品json文件...") // //本地存放json地址 // outPath := fmt.Sprintf("%s/%s", os.Getenv("JSON_PATH"), "shop") // j.outPath = outPath // // //默认下载地址 // downloadUrl := "https://gravecs.mg.xhgame.com/json/" + jsonVersion + "/server" // //获取下载地址 // if os.Getenv("GIN_MODE") == "release" && os.Getenv("ADMIN_PLATFORM") == "wechat" { // downloadUrl = "https://srgtw.mg.xhgame.com/grave/json/wx/" + jsonVersion + "/server" // } else if os.Getenv("GIN_MODE") == "release" && os.Getenv("ADMIN_PLATFORM") == "dy" { // downloadUrl = "https://srgtw.mg.xhgame.com/grave/json/tt/" + jsonVersion + "/server" // } else if os.Getenv("GIN_MODE") == "release" && os.Getenv("ADMIN_PLATFORM") == "mt" { // downloadUrl = "https://srgtw.mg.xhgame.com/grave/json/mt/" + jsonVersion + "/server" // } // j.Lock() // defer func() { // j.Unlock() // if err := recover(); err != nil { // logrus.Errorf("同步商品json失败:%v", err) // } // }() // //下载json文件到指定文件夹 // err := j.download(downloadUrl, outPath, downJsonFiles) // if err != nil { // logrus.Errorf("download err:%v", err) // return // } // logrus.Info("下载完成") // //获取giftCenter数据 // err = j.giftCenter(fmt.Sprintf("%s/%s", outPath, "GiftCenter.json")) // if err != nil { // logrus.Errorf("giftCenter err:%v", err) // return // } // logrus.Info("giftCenter加载完成") // // //获取baseConf // err = j.baseConf(fmt.Sprintf("%s/%s", outPath, "base_data.json")) // if err != nil { // logrus.Errorf("baseConf err:%v", err) // return // } // logrus.Info("baseConf加载完成") // //遍历json文件 // allData := make([]*model.SalesGoods, 0) // for _, file := range downJsonFiles { // if _, ok := passJsonFiles[file]; ok { // continue // } // filePath := fmt.Sprintf("%s/%s", outPath, file) // logrus.Infof("加载%s...", filePath) // //清洗数据 // data, err := j.cleanData(filePath) // if err != nil { // logrus.Errorf("cleanData err:%v", err) // return // } // logrus.Infof("加载%s完成...", filePath) // allData = append(allData, data...) // } // //组装数据 // err = j.ShopGoods(fmt.Sprintf("%s/%s", outPath, "shop.json"), fmt.Sprintf("%s/%s", outPath, "PropsStore.json"), fmt.Sprintf("%s/%s", outPath, "LimitGift.json"), allData) // if err != nil { // logrus.Errorf("ShopGoods err:%v", err) // return // } // logrus.Infof("Encoder success") //} // ///* //下载json文件 //downloadPath: 下载地址 //outPutPath: 输出地址 //downJsonFiles: 下载的json文件列表 //*/ //func (j *JSyncGoodsJson) download(downloadPath, outPutPath string, downJsonFiles []string) error { // //检查文件夹是否存在 // _, err := os.Stat(outPutPath) // if errors.Is(err, fs.ErrNotExist) { // //创建目标文件夹 // err = os.MkdirAll(outPutPath, 0755) // if err != nil { // return err // } // } // // // 下载文件 // for _, file := range downJsonFiles { // filePath := fmt.Sprintf("%s/%s", outPutPath, file) // logrus.Infof("下载文件:%s...", file) // err := j.downloadFile(filePath, downloadPath+"/"+file) // if err != nil { // logrus.Errorf("无法下载文件:%v", err) // } else { // logrus.Infof("下载文件:%s...", file) // } // } // return nil //} // //func (j *JSyncGoodsJson) downloadFile(outFilePath, url string) error { // logrus.Infof("outFile:%s; url:%s", outFilePath, url) // // 发送 GET 请求 // resp, err := http.Get(url) // if err != nil { // return err // } // defer resp.Body.Close() // // // 创建文件 // out, err := os.Create(outFilePath) // if err != nil { // return err // } // defer out.Close() // // // 将响应体内容写入文件 // _, err = io.Copy(out, resp.Body) // if err != nil { // return err // } // // return nil //} // //func (j *JSyncGoodsJson) giftCenter(filePath string) error { // talentFile, err := os.Open(filePath) // if err != nil { // logrus.Errorln("读取日志文件失败", err) // } // defer talentFile.Close() // var gifts []*types.GiftCenterItem // decoder := json.NewDecoder(talentFile) // err = decoder.Decode(&gifts) // if err != nil { // return err // } // for _, v := range gifts { // tm, _ := time.ParseInLocation("2006-01-02 15:04:05", v.St, time.Local) // v.StartTime = tm.Unix() // // tm, _ = time.ParseInLocation("2006-01-02 15:04:05", v.Et, time.Local) // v.EndTime = tm.Unix() // //整理礼包类型 // j.TMap[v.Title] = v.Type // } // j.center = gifts // return nil //} //func (j *JSyncGoodsJson) baseConf(baseJsonPath string) error { // // _, err := j.RowsToCols2(baseJsonPath, true) // if err != nil { // return err // } // // bc := new(model.BaseConfig) // _, err = j.loadJsonFile(fmt.Sprintf("%s", baseJsonPath), &bc) // if err != nil { // return err // } // j.bc = bc // return nil //} //func (j *JSyncGoodsJson) cleanData(filePath string) ([]*model.SalesGoods, error) { // ptrFile, err := os.Open(filePath) // if err != nil { // logrus.Errorf("读取日志文件%s失败:%v", filePath, err) // return nil, err // } // defer ptrFile.Close() // fileName := filepath.Base(filePath) // switch fileName { // case "DailyGift.json": // data, err := j.cleanDataGiftGoods(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "RoleGift.json": // data, err := j.cleanDataGiftGoods(ptrFile) // if err != nil { // return nil, err // } // for i, v := range data { // data[i].TotalTimes = v.AdDayTimes // data[i].AdDayTimes = 0 // } // return data, nil // case "NewerGift.json": // data, err := j.cleanNewerGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "Holiday9Gift.json": // data, err := j.cleanNewYearGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "LanternFestivalGift.json": // data, err := j.YearFifteenGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "Holiday3_Gift.json": // data, err := j.Holiday38Gift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "BuyOnceGetThreeDays.json": // data, err := j.SpecialGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "TombSweepingDayGift.json", "LaborDayGift.json", "DragonBoatFestivalGift.json": // data, err := j.TombSweepGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "WeekendGift.json": // data, err := j.WeekendGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "QixiFestivalGift.json": // data, err := j.QIXIGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "MoonFestivalGift.json": // data, err := j.MoonFestivalGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "EquipBoxGiftPack.json": // data, err := j.BoxKeyGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "EquipBoxBase.json": // data, err := j.EquipBoxBase(ptrFile) // if err != nil { // return nil, err // } // return data, nil // case "EndlessPassportBase.json": // data, err := j.EndlessPassGift(ptrFile) // if err != nil { // return nil, err // } // return data, nil // } // return nil, errors.New("未找到对应的json文件") //} // //func (j *JSyncGoodsJson) cleanDataGiftGoods(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidGiftItem // // fileName := strings.TrimSuffix(filepath.Base(ptrFile.Name()), filepath.Ext(ptrFile.Name())) // tNum := 0 // if typeNum, ok := j.TMap[fileName]; ok { // tNum = typeNum // } // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // var allJson []*model.SalesGoods // for _, v := range gifts { // if v.Show == 0 { // continue // } // // if v.Type == 0 { // v.Type = tNum // } // // gd := &model.SalesGoods{} // gd.ID = v.ID // gd.AdDayTimes = v.Limit // gd.GoodsType = v.Type // gd.Name = v.Name // dm := model.DropMaterial{ // ID: 0, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm = model.DropMaterial{} // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, dm) // } // // //时间 // if len(v.StartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.StartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.EndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) cleanNewerGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidNewerItem // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // fileName := strings.TrimSuffix(filepath.Base(ptrFile.Name()), filepath.Ext(ptrFile.Name())) // tNum := 0 // if typeNum, ok := j.TMap[fileName]; ok { // tNum = typeNum // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.TotalTimes = v.Limit // gd.GoodsType = tNum // gd.OpenLevel = v.Level // gd.Name = v.Name // dm := model.DropMaterial{ // ID: 0, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // // tp1 := v2[0] // if tp1 == 0 { // dm := new(model.DropMaterial) // dm.ID = v2[1] // dm.Count = int(v2[2]) // gd.Materials = append(gd.Materials, *dm) // } else { // id := v2[1] // count := int(v2[2]) // // for i := 0; i < count; i++ { // gd.Equipments = append(gd.Equipments, id) // } // } // } // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) cleanNewYearGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidNewYearGiftItem // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // if v.Show == 0 { // continue // } // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.GoodsType = v.Type // gd.AdDayTimes = v.Limit // gd.AdCoolDown = v.AdCoolDown // gd.Name = v.Name // costId := int64(0) // switch v.PriceType { // case 0: // 免费 或者没有该字段 // case 2, 3: // 广告 // costId = int64(model.ADTicket) // case 6: // 钻石 // costId = int64(constants.MaterilaId_Diamond) // case 7: // 现金 // costId = 0 // case 8: //金币 // costId = constants.MaterilaId_Coin // } // // dm := model.DropMaterial{ // ID: costId, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // //时间 // if len(v.StartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.StartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.EndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) YearFifteenGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidNewYearGiftItem // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // if v.Show == 0 { // continue // } // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.GoodsType = v.Type // gd.AdDayTimes = v.Limit // gd.AdCoolDown = v.AdCoolDown // gd.Name = v.Name // costId := int64(0) // switch v.PriceType { // case 0: // 免费 或者没有该字段 // case 2, 3: // 广告 // costId = int64(model.ADTicket) // case 6: // 钻石 // costId = int64(constants.MaterilaId_Diamond) // case 7: // 现金 // costId = 0 // case 8: //金币 // costId = constants.MaterilaId_Coin // } // // dm := model.DropMaterial{ // ID: costId, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // //时间 // if len(v.StartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.StartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.EndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) Holiday38Gift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidHoliday38Gift // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // if v.Show == 0 { // continue // } // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.AdDayTimes = v.Limit // gd.GoodsType = v.Type // gd.Name = v.Name // dm := model.DropMaterial{ // ID: 0, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // //时间 // if len(v.StartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.StartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.EndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) SpecialGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.SpecialGift // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // fileName := strings.TrimSuffix(filepath.Base(ptrFile.Name()), filepath.Ext(ptrFile.Name())) // tNum := 0 // if typeNum, ok := j.TMap[fileName]; ok { // tNum = typeNum // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.TotalTimes = 1 // 只能购买一次 // gd.GoodsType = tNum // gd.MaxLevel = v.MaxLevel // gd.Name = v.Name // dm := model.DropMaterial{ // ID: 0, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // tp1 := 0 // 材料 // if 10000000 < v2[0] { // tp1 = 1 // 装备 // } // if tp1 == 0 { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } else { // id := v2[0] // count := int(v2[1]) // // for i := 0; i < count; i++ { // gd.Equipments = append(gd.Equipments, id) // } // } // } // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) TombSweepGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidNewYearGiftItem // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // if v.Show == 0 { // continue // } // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.AdDayTimes = v.Limit // gd.GoodsType = v.Type // gd.AdCoolDown = v.AdCoolDown // gd.Name = v.Name // costType := int64(0) // switch v.PriceType { // case 0: // 免费 或者没有该字段 // case 2, 3: // 广告 // costType = int64(model.ADTicket) // case 6: // 钻石 // costType = int64(constants.MaterilaId_Diamond) // case 7: // 现金 // } // // dm := model.DropMaterial{ // ID: costType, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // //时间 // if len(v.StartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.StartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.EndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) WeekendGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.WeekendGift // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var ( // allJson []*model.SalesGoods // attrs = make(map[int64]*types.WeekendGiftAttr) // ) // for _, v := range gifts { // if v.ShowOrNot == 0 { // continue // } // // gd := new(model.SalesGoods) // gd.ID = v.GiftId // gd.AdDayTimes = v.GiftLimit // gd.GoodsType = v.Type // gd.Name = v.Name // id := v.PriceType // if v.PriceType == 2 { // id = 0 // } // // dm := model.DropMaterial{ // ID: id, // Count: v.GiftPrice, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.GiftAward { // dm := new(model.DropMaterial) // dm.ID = int64(v2[0]) // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // //时间 // if len(v.GiftStartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.GiftStartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.GiftEndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.GiftEndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // // allJson = append(allJson, gd) // // // 属性表 // attrs[v.GiftId] = &types.WeekendGiftAttr{ // ID: v.GiftId, // Name: v.Name, // Week: v.Week, // Weight: v.Weight, // LimitType: v.GiftLimitType[0], // LimitCount: v.GiftLimitType[1], // Cost: v.GiftPrice, // } // } // // //写文件 // writeFile := fmt.Sprintf("%s/%s", j.outPath, "WeekendGiftAttr.json") // writePtr, err := os.Create(writeFile) // if err != nil { // return nil, err // } // defer writePtr.Close() // encoder := json.NewEncoder(writePtr) // if err = encoder.Encode(&attrs); err != nil { // return nil, err // } // return allJson, nil //} //func (j *JSyncGoodsJson) QIXIGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidNewYearGiftItem // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // if v.Show == 0 { // continue // } // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.AdDayTimes = v.Limit // gd.GoodsType = v.Type // gd.Name = v.Name // dm := model.DropMaterial{ // ID: 0, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // //时间 // if len(v.StartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.StartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.EndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) MoonFestivalGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidNewYearGiftItem // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // if v.Show == 0 { // continue // } // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.AdDayTimes = v.Limit // gd.GoodsType = v.Type // gd.Name = v.Name // dm := model.DropMaterial{ // ID: 0, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // //时间 // if len(v.StartTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.StartTime, time.Local) // if err != nil { // return nil, err // } // gd.StartTime = tm.Unix() // } // if len(v.EndTime) > 0 { // tm, err := time.ParseInLocation("2006-01-02 15:04:05", v.EndTime, time.Local) // if err != nil { // return nil, err // } // gd.EndTime = tm.Unix() // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) BoxKeyGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidBoxKeyGiftItem // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.AdDayTimes = v.Limit // gd.GoodsType = v.Type // gd.PlatGroup = v.PlatGroup // gd.Name = v.Name // id := int64(0) // switch v.GetType { // case 0: // 免费 // case 2, 3: // 广告 // id = int64(model.ADTicket) // case 6: // 钻石 // id = int64(constants.MaterilaId_Diamond) // case 7: // 现金 // } // gd.AdCoolDown = v.AdCoolDown // dm := model.DropMaterial{ // ID: id, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // for _, v2 := range v.Materials { // dm := new(model.DropMaterial) // dm.ID = v2[0] // dm.Count = int(v2[1]) // gd.Materials = append(gd.Materials, *dm) // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) EquipBoxBase(ptrFile *os.File) ([]*model.SalesGoods, error) { // var items []types.MidEquipBoxBase // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&items) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range items { // gd := new(model.SalesGoods) // gd.ID = v.GoodsId // gd.GoodsType = v.BoxType // dm := model.DropMaterial{ // ID: v.Cost, // Count: 1, // } // gd.Cost = append(gd.Cost, dm) // gd.Name = v.Name // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) EndlessPassGift(ptrFile *os.File) ([]*model.SalesGoods, error) { // var gifts []types.MidEndlessPassGift // // decoder := json.NewDecoder(ptrFile) // err := decoder.Decode(&gifts) // if err != nil { // return nil, err // } // // var allJson []*model.SalesGoods // for _, v := range gifts { // // gd := new(model.SalesGoods) // gd.ID = v.ID // gd.AdDayTimes = 1 // gd.GoodsType = v.GoodsType // gd.Name = v.Name // id := int64(0) // switch v.GetType { // case 0: // 免费 // case 2, 3: // 广告 // id = int64(model.ADTicket) // case 6: // 钻石 // id = int64(constants.MaterilaId_Diamond) // case 7: // 现金 // } // // dm := model.DropMaterial{ // ID: id, // Count: v.Price, // } // gd.Cost = append(gd.Cost, dm) // // if v.AddPassportExp != 0 { // dm := new(model.DropMaterial) // //TODO model.EndLessExp 值 // dm.ID = model.EndLessExp // dm.Count = v.AddPassportExp // gd.Materials = append(gd.Materials, *dm) // } // // if v.ReturnDiamond != 0 { // dm := new(model.DropMaterial) // dm.ID = constants.MaterilaId_Diamond // dm.Count = v.ReturnDiamond // gd.Materials = append(gd.Materials, *dm) // } // // allJson = append(allJson, gd) // } // return allJson, nil //} //func (j *JSyncGoodsJson) ShopGoods(shopJsonPath, propsStoreJsonPath, limitGiftPath string, allJson []*model.SalesGoods) error { // ptfFile, err := os.Open(shopJsonPath) // if err != nil { // logrus.Errorln("读取日志文件失败", err) // } // defer ptfFile.Close() // fileName := filepath.Base(shopJsonPath) // // var materials []types.MidSalesSalesGoods // // decoder := json.NewDecoder(ptfFile) // err = decoder.Decode(&materials) // if err != nil { // logrus.Errorf("%v Decoder failed %v", fileName, err.Error()) // return err // } // // allBox := make(map[int64]*model.SalesGoods) // logrus.Infof("初始化allBox...") // // for _, v := range materials { // n := new(model.SalesGoods) // n.ID = v.ID // n.AdCoolDown = v.AdCoolDown // n.AdDayTimes = v.AdDayTimes // n.AdDayType = v.AdDayType // n.PlatGroup = v.PlatGroup // n.GoodsType = v.Type // n.Name = v.Name // if len(v.GiftNum) == 2 { // dm := model.DropMaterial{ // ID: v.GiftNum[0], // Count: int(v.GiftNum[1]), // } // n.Gift = append(n.Gift, dm) // } // // // 当数组大于1时,表示有多种购买方式 // priceCount := len(v.Price) // switch priceCount { // case 0: // case 1: // dm := model.DropMaterial{} // price := v.Price[0] // if price[0] > 0 { // dm.ID = price[0] // } // dm.Count = int(price[1]) // n.Cost = append(n.Cost, dm) // case 2, 3: // dm := model.DropMaterial{} // price := v.Price[0] // if price[0] > 0 { // dm.ID = price[0] // } // dm.Count = int(price[1]) // n.Cost = append(n.Cost, dm) // // contents := model.ExtractMaterials(v.Content) // n.Materials = append(n.Materials, *contents[0]) // // // 第二、三种支付方式 // // for k, p1 := range v.Price { // if k == 0 { // continue // } // // gs := new(model.SalesGoods2) // // dm2 := model.DropMaterial{} // dm2.ID = p1[0] // dm2.Count = int(p1[1]) // gs.Cost = append(gs.Cost, dm2) // // gs.Materials = append(gs.Materials, *contents[k]) // // n.OtherGoods = append(n.OtherGoods, gs) // } // // default: // logrus.Errorf("foud shop error priceCount != 1 2\n", n) // } // // if v.Double == 1 { // n.FirstDouble = 1 // } // // var ms []*model.DropMaterial // var es []int64 // // if len(v.Content[0]) == 2 { // ms = model.ExtractMaterials(v.Content) // } else { // ms, es = model.ExtractMerialEquips(v.Content) // } // // if priceCount < 2 { // for _, v := range ms { // n.Materials = append(n.Materials, *v) // } // n.Equipments = es // } // // allBox[n.ID] = n // // //监测配置数值 // if n.ID <= 0 || n.GoodsType <= 0 || // (len(n.Cost) == 0 && n.GoodsType != 7 && n.GoodsType != 14) || // (len(n.Materials) == 0 && len(n.Equipments) == 0) { // logrus.Errorf("foud shop error1 %v\n", n) // } // for _, v := range n.Cost { // if v.ID > 100000 { // logrus.Errorf("foud shop error2 %v\n", v) // } // } // for _, v := range n.Gift { // if v.ID > 100000 { // logrus.Errorf("foud shop error3 %v\n", v) // } // } // } // logrus.Infof("初始化allBox完成") // // logrus.Infof("allBox加载otherGoods...") // otherGoods, err := ExtractPropStore(propsStoreJsonPath) // if err != nil { // logrus.Errorf("ExtractPropStore err: %v", err.Error()) // return err // } // for k, v := range otherGoods { // allBox[k] = v // } // logrus.Infof("allBox加载otherGoods完成") // // logrus.Infof("allBox加载allJson...") // for _, v := range allJson { // allBox[v.ID] = v // } // logrus.Infof("allBox加载allJson完成") // // logrus.Infof("allBox加载limitGoods...") // // 导入需要RMB支付的限时礼包 // limitGoods, err := ExtractLimitGiftGoods(limitGiftPath, int(j.bc.LimitgiftPerDayMaximum)) // if err != nil { // logrus.Errorf("ExtractLimitGiftGoods err: %v", err.Error()) // return err // } // for k, v := range limitGoods { // allBox[k] = v // } // logrus.Infof("allBox加载limitGoods完成") // // logrus.Infof("allBox转化购买等级限制...") // // 商品上限购买等级不限制时转化为一个非常大的数 // for _, v := range allBox { // if v.MaxLevel == 0 { // v.MaxLevel = 1000000 // } // } // logrus.Infof("allBox转化购买等级限制完成") // // //保存到指定文件夹内 // lastDir, err := j.getLatestVersion() // if err != nil { // logrus.Errorf("getLatestVersion err: %v", err) // } // logrus.Infof("最新的文件夹:%s/%s", os.Getenv("JSON_PATH"), lastDir) // writeFile := fmt.Sprintf("%s/%s/%s", os.Getenv("JSON_PATH"), lastDir, "product.json") // writePtr, err := os.Create(writeFile) // if err != nil { // logrus.Errorln("写日志文件失败", err) // return err // } // defer writePtr.Close() // encoder := json.NewEncoder(writePtr) // err = encoder.Encode(&allBox) // if err != nil { // logrus.Errorln("Encoder failed", err.Error()) // return err // } // return nil //} // //func (j *JSyncGoodsJson) getLatestVersion() (string, error) { // // 读取指定目录下的所有文件夹名称 // folders, err := ioutil.ReadDir(os.Getenv("JSON_PATH")) // if err != nil { // logrus.Errorf("Failed to read directory:%v", err) // return "", err // } // // var latestVersion *version.Version // // // 遍历文件夹列表,比较版本号 // for _, folder := range folders { // if folder.IsDir() { // versionStr := folder.Name() // logrus.Infof("folderName:%s", versionStr) // if strings.HasPrefix(versionStr, "v") { // v, err := version.NewVersion(versionStr[1:]) // 移除前缀 "v" 并创建版本对象 // if err != nil { // fmt.Println("Failed to parse version:", err) // continue // } // // if latestVersion == nil || v.GreaterThan(latestVersion) { // latestVersion = v // } // } // } // } // // if latestVersion != nil { // return fmt.Sprintf("v%s", latestVersion.String()), nil // } // return "", errors.New("no version folders found") //} // //func (j *JSyncGoodsJson) RowsToCols2(filePath string, save bool) (map[string]interface{}, error) { // fileName := filepath.Base(filePath) // // ext := path.Ext(fileName) // // 去除后缀,获取文件名 // name := fileName[:len(fileName)-len(ext)] // //writeFile := filepath.Base(filePath) // writeFilePath := fmt.Sprintf("%s/%s2.json", j.outPath, name) // _, err := os.Stat(writeFilePath) // if !errors.Is(err, fs.ErrNotExist) { // logrus.Infof("%s已存在", writeFilePath) // return nil, nil // } // // 读数据 // talentFile, err := os.Open(filePath) // if err != nil { // logrus.Errorln("读取日志文件失败", err) // return nil, err // } // defer talentFile.Close() // // var items []*types.MidCol // // decoder := json.NewDecoder(talentFile) // err = decoder.Decode(&items) // if err != nil { // logrus.Errorf("%v Decoder failed, %v", fileName, err.Error()) // return nil, err // } // // // KV // allObjects := make(map[string]interface{}) // for _, v := range items { // // switch v.Type { // case "STRING": // allObjects[v.Key] = v.Value // case "INT": // allObjects[v.Key], _ = strconv.ParseInt(v.Value, 10, 64) // case "FLOAT": // allObjects[v.Key], _ = strconv.ParseFloat(v.Value, 64) // case "STRINGARR": // var val []string // if err = json.Unmarshal([]byte(v.Value), &val); err != nil { // logrus.Errorf("%v Unmarshal STRINGARR error:%+v", fileName, v.Value) // return nil, err // } // allObjects[v.Key] = val // case "INTARR", "ARR": // var val []int64 // if err = json.Unmarshal([]byte(v.Value), &val); err != nil { // logrus.Errorf("%v Unmarshal STRINGARR error:%+v", fileName, v.Value) // return nil, err // } // allObjects[v.Key] = val // case "FLOATARR": // var val []float64 // if err = json.Unmarshal([]byte(v.Value), &val); err != nil { // logrus.Errorf("%v Unmarshal STRINGARR error:%+v", fileName, v.Value) // return nil, err // } // allObjects[v.Key] = val // case "STRINGARRS": // var val [][]string // if err = json.Unmarshal([]byte(v.Value), &val); err != nil { // logrus.Errorf("%v Unmarshal STRINGARR error:%+v", fileName, v.Value) // return nil, err // } // allObjects[v.Key] = val // case "INTARRS", "ARRS": // var val [][]int64 // if err = json.Unmarshal([]byte(v.Value), &val); err != nil { // logrus.Errorf("%v Unmarshal STRINGARR error:%+v", fileName, v.Value) // return nil, err // } // allObjects[v.Key] = val // case "FLOATARRS": // var val [][]float64 // if err = json.Unmarshal([]byte(v.Value), &val); err != nil { // logrus.Errorf("%v Unmarshal STRINGARR error:%+v", fileName, v.Value) // return nil, err // } // allObjects[v.Key] = val // } // } // // if !save { // return allObjects, nil // } // // // 保存 // logrus.Infof("RowsToCols2 base2:%s", writeFilePath) // writePtr, err := os.Create(writeFilePath) // if err != nil { // logrus.Errorf("%v RowsToCols2 CreateFile failed. %v", fileName, err) // return nil, err // } // defer writePtr.Close() // encoder2 := json.NewEncoder(writePtr) // err = encoder2.Encode(&allObjects) // if err != nil { // logrus.Errorf("%v RowsToCols2 save failed. %v", fileName, err.Error()) // return nil, err // } // return allObjects, nil //} //func (j *JSyncGoodsJson) loadJsonFile(filePath string, items interface{}) (interface{}, error) { // fileName := filepath.Base(filePath) // ext := path.Ext(fileName) // // 去除后缀,获取文件名 // name := fileName[:len(fileName)-len(ext)] // writeFilePath := fmt.Sprintf("%s/%s2.json", j.outPath, name) // logrus.Infof("loadJsonFile base2:%s", writeFilePath) // // roleFile, err := os.Open(writeFilePath) // if err != nil { // logrus.Errorln("读取日志文件失败", err) // } // defer roleFile.Close() // // //var roles []MidRoleAttr // decoder := json.NewDecoder(roleFile) // err = decoder.Decode(&items) // if err != nil { // logrus.Errorf("%v Decoder failed. %v", fileName, err.Error()) // return nil, err // } // return items, nil //} //func ExtractPropStore(filePath string) (map[int64]*model.SalesGoods, error) { // ptfFile, err := os.Open(filePath) // if err != nil { // logrus.Errorln("读取日志文件失败", err) // } // defer ptfFile.Close() // fileName := filepath.Base(filePath) // // var materials []types.MidPropStoreGoods // // decoder := json.NewDecoder(ptfFile) // err = decoder.Decode(&materials) // if err != nil { // logrus.Errorf("%v Decoder failed", fileName, err.Error()) // return nil, err // } // // allBox := make(map[int64]*model.SalesGoods) // for _, v := range materials { // n := new(model.SalesGoods) // n.ID = v.ID // n.GoodsType = v.Type // // if len(v.Price) == 2 { // dm := model.DropMaterial{} // if v.Price[0] > 0 { // dm.ID = v.Price[0] // } // dm.Count = int(v.Price[1]) // n.Cost = append(n.Cost, dm) // } // n.Name = v.Name // // dm := model.DropMaterial{ID: v.Content[0], Count: int(v.Content[1])} // n.Materials = append(n.Materials, dm) // // allBox[n.ID] = n // // //监测配置数值 // if n.ID <= 0 || n.GoodsType <= 0 || // (len(n.Cost) == 0) || // (len(n.Materials) == 0 && len(n.Equipments) == 0) { // logrus.Errorf("foud shop error5 %v\n", n) // } // } // // return allBox, nil //} //func ExtractLimitGiftGoods(filePath string, openLevel int) (map[int64]*model.SalesGoods, error) { // ptfFile, err := os.Open(filePath) // if err != nil { // logrus.Errorln("读取日志文件失败", err) // } // defer ptfFile.Close() // fileName := filepath.Base(filePath) // // var goods []types.LimitGiftGoods // // decoder := json.NewDecoder(ptfFile) // err = decoder.Decode(&goods) // if err != nil { // logrus.Errorf("%v Decoder failed", fileName, err.Error()) // return nil, err // } // // allBox := make(map[int64]*model.SalesGoods) // for _, v := range goods { // n := new(model.SalesGoods) // n.ID = v.Id // n.GoodsType = v.Type // n.OpenLevel = openLevel // n.Name = v.Name // if len(v.Price) >= 2 { // var id int64 = 0 // 0金币,1钻石,2RMB // if v.Price[0] == 0 { // id = 1001 // } // if v.Price[0] == 1 { // id = 1002 // } // dm := model.DropMaterial{} // dm.ID = id // dm.Count = int(v.Price[1]) // n.Cost = append(n.Cost, dm) // } // // var ( // material []model.DropMaterial // equipment []int64 // ) // // for _, award := range v.Award { // if len(award) == 0 { // 没有奖励内容 // continue // } // // if 10000000 < award[0] { // // 装备 // for i := 0; i < int(award[1]); i++ { // equipment = append(equipment, award[0]) // } // } else { // // 材料、金币、钻石 // material = append(material, model.DropMaterial{ // ID: award[0], // Count: int(award[1]), // }) // } // } // // if len(material) == 0 && len(equipment) == 0 { // continue // } // // n.Materials = material // n.Equipments = equipment // allBox[n.ID] = n // // //监测配置数值 // if n.ID <= 0 || n.GoodsType <= 0 || // (len(n.Cost) == 0) || // (len(n.Materials) == 0 && len(n.Equipments) == 0) { // logrus.Errorf("foud limitGiftGoods error5 %v\n", n) // } // } // // return allBox, nil //}