package service import ( "sort" "strings" "crazy-fox-backend-api/model" "crazy-fox-backend-api/repo" "crazy-fox-backend-api/utils" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" ) type actThemeSrc struct{} // GetActThemeList 获取活动主题资源列表 func (This *actThemeSrc) GetActThemeList() []model.ActThemeConf { imgNameList := repo.Search[model.TActiveImgNameConfig]("") filterList := make([]model.ActThemeConf, 0, len(imgNameList)) for _, oneImgInfo := range imgNameList { if oneImgInfo.Show == offline { continue } var oneTheme model.ActThemeConf oneTheme.ActImgNameConfBasic = oneImgInfo.ActImgNameConfBasic oneTheme.Content = utils.MapValues(oneImgInfo.Content, true) // 排序 sort.SliceStable(oneTheme.Content, func(i, j int) bool { return oneTheme.Content[i].Id < oneTheme.Content[j].Id }) filterList = append(filterList, oneTheme) } return filterList } // UpdateThemeConf 更新活动主题资源配置 func (This *actThemeSrc) UpdateThemeConf(themeConf []model.ActThemeConf) (err error) { existThemeList := repo.SearchOneFieldMulti[model.TActiveImgNameConfig, string]("name", "") var ( contentStr string curTime = utils.CurTimestamp() uniqueMap = make(map[string]bool, len(themeConf)) cacheMap = make(map[string]model.TActiveImgNameConfigCache, len(themeConf)) updateThemeList = make([]model.TActiveImgNameConfig, 0, len(themeConf)) insertThemeList = make([]model.TActiveImgNameConfig, 0, 5) ) for _, oneTheme := range themeConf { oneTheme.Name = strings.Trim(oneTheme.Name, " ") if _, OK := uniqueMap[oneTheme.Name]; OK { return errors.New("不能添加重复的类型名: " + oneTheme.Name) } uniqueMap[oneTheme.Name] = true oneDbTheme := model.TActiveImgNameConfig{ ActImgNameConfBasic: oneTheme.ActImgNameConfBasic, Content: make(map[int64]model.ActImgContent, len(oneTheme.Content)), } for j := 0; j < len(oneTheme.Content); j++ { oneDbTheme.Content[int64(j+1)] = model.ActImgContent{ Id: int64(j + 1), Name: strings.Trim(oneTheme.Content[j].Name, " "), Type: strings.ToLower(strings.Trim(oneTheme.Content[j].Type, " ")), } } if !utils.InSlice(oneDbTheme.Name, existThemeList) { oneDbTheme.Id = 0 oneDbTheme.AddTime = curTime insertThemeList = append(insertThemeList, oneDbTheme) } else { updateThemeList = append(updateThemeList, oneDbTheme) } if contentStr, err = jsoniter.MarshalToString(oneDbTheme.Content); err != nil { return errors.New("序列化Content失败的类型名: " + oneDbTheme.Name) } cacheMap[oneDbTheme.Name] = model.TActiveImgNameConfigCache{ ActImgNameConfBasic: oneDbTheme.ActImgNameConfBasic, Content: contentStr, } } // 新增 if len(insertThemeList) != 0 { if _, err = repo.Insert(insertThemeList); err != nil { return errors.WithStack(err) } } // 更新 if len(updateThemeList) != 0 { if err = repo.Update(updateThemeList, "id"); err != nil { return errors.WithStack(err) } } // 更新缓存 repo.Activity.HMSetActImgNameConfCache(cacheMap) return err } func (This *activityService) GetActThemeExtraConf(req *model.ImgExtraConfReq) []model.TActiveImgExtraConfig { extraConfs := repo.Search[model.TActiveImgExtraConfig]("Where `type` = ? And `skin` = ?", req.Type, req.Skin) if extraConfs == nil { extraConfs = []model.TActiveImgExtraConfig{} } return extraConfs } func (This *activityService) ThemeExtraConfUpdate(req *model.ImgExtraConfUpdateReq) error { for i := range req.Conf { req.Conf[i].Type = req.Type req.Conf[i].Skin = req.Skin } if _, err := repo.ReplaceStruct[model.TActiveImgExtraConfig](req.Conf, repo.WithKeys("id"), repo.WithWhere("Where `type` = ? And `skin` = ?", req.Type, req.Skin), ); err != nil { return err } return This.buildImgExtraConfJson() } func (This *activityService) buildImgExtraConfJson() error { list := repo.Search[model.TActiveImgExtraConfig]("") maps := map[string]map[string]map[string]string{} for _, config := range list { if _, Ok := maps[config.Type]; !Ok { maps[config.Type] = map[string]map[string]string{} } if _, Ok := maps[config.Type][config.Skin]; !Ok { maps[config.Type][config.Skin] = map[string]string{} } maps[config.Type][config.Skin][config.Key] = config.Val } return Config.SaveJson(maps, "ActSkinExtraConfig") } // GetActIconConfList 获取活动Icon列表 func (This *actThemeSrc) GetActIconConfList() model.ResActIconConfList { iconList := repo.Search[model.TRewardCenterConfig]("") retList := make([]model.TRewardCenterConfig, 0, len(iconList)) for _, config := range iconList { if utils.InSlice(config.Id, []int64{3, 4, 5, 6, 7, 8, 9, 10, 15, 19, 21, 22, 23, 32, 34, 42, 47}) { continue } retList = append(retList, config) } showLimit := repo.Activity.HGetActIconRightMaxShow() return model.ResActIconConfList{ List: retList, RightMaxShow: showLimit, } } // InsertIconConf 新增活动Icon配置 func (This *actThemeSrc) InsertIconConf(iconConf model.TRewardCenterConfig) error { if _, err := repo.Insert(&iconConf); err != nil { return errors.WithStack(err) } // 更新全部配置文件 if err := This.handleIconConfJson(); err != nil { return errors.WithStack(err) } return nil } // UpdateIconConf 更新活动Icon配置 func (This *actThemeSrc) UpdateIconConf(req model.ResActIconConfList) error { // 更新配置列表 if err := repo.Update(req.List, "id"); err != nil { return errors.WithStack(err) } // 更新缓存中的配置 repo.Activity.HSetActIconRightMaxShow(req.RightMaxShow) // 更新全部配置文件 if err := This.handleIconConfJson(); err != nil { return errors.WithStack(err) } return nil } // handleIconConfJson 处理活动Icon配置Json func (This *actThemeSrc) handleIconConfJson() error { iconConf := This.GetActIconConfList() return Config.SaveJson(iconConf, "IconConfig") }