act_theme_src.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package service
  2. import (
  3. "sort"
  4. "strings"
  5. "crazy-fox-backend-api/model"
  6. "crazy-fox-backend-api/repo"
  7. "crazy-fox-backend-api/utils"
  8. jsoniter "github.com/json-iterator/go"
  9. "github.com/pkg/errors"
  10. )
  11. type actThemeSrc struct{}
  12. // GetActThemeList 获取活动主题资源列表
  13. func (This *actThemeSrc) GetActThemeList() []model.ActThemeConf {
  14. imgNameList := repo.Search[model.TActiveImgNameConfig]("")
  15. filterList := make([]model.ActThemeConf, 0, len(imgNameList))
  16. for _, oneImgInfo := range imgNameList {
  17. if oneImgInfo.Show == offline {
  18. continue
  19. }
  20. var oneTheme model.ActThemeConf
  21. oneTheme.ActImgNameConfBasic = oneImgInfo.ActImgNameConfBasic
  22. oneTheme.Content = utils.MapValues(oneImgInfo.Content, true)
  23. // 排序
  24. sort.SliceStable(oneTheme.Content, func(i, j int) bool {
  25. return oneTheme.Content[i].Id < oneTheme.Content[j].Id
  26. })
  27. filterList = append(filterList, oneTheme)
  28. }
  29. return filterList
  30. }
  31. // UpdateThemeConf 更新活动主题资源配置
  32. func (This *actThemeSrc) UpdateThemeConf(themeConf []model.ActThemeConf) (err error) {
  33. existThemeList := repo.SearchOneFieldMulti[model.TActiveImgNameConfig, string]("name", "")
  34. var (
  35. contentStr string
  36. curTime = utils.CurTimestamp()
  37. uniqueMap = make(map[string]bool, len(themeConf))
  38. cacheMap = make(map[string]model.TActiveImgNameConfigCache, len(themeConf))
  39. updateThemeList = make([]model.TActiveImgNameConfig, 0, len(themeConf))
  40. insertThemeList = make([]model.TActiveImgNameConfig, 0, 5)
  41. )
  42. for _, oneTheme := range themeConf {
  43. oneTheme.Name = strings.Trim(oneTheme.Name, " ")
  44. if _, OK := uniqueMap[oneTheme.Name]; OK {
  45. return errors.New("不能添加重复的类型名: " + oneTheme.Name)
  46. }
  47. uniqueMap[oneTheme.Name] = true
  48. oneDbTheme := model.TActiveImgNameConfig{
  49. ActImgNameConfBasic: oneTheme.ActImgNameConfBasic,
  50. Content: make(map[int64]model.ActImgContent, len(oneTheme.Content)),
  51. }
  52. for j := 0; j < len(oneTheme.Content); j++ {
  53. oneDbTheme.Content[int64(j+1)] = model.ActImgContent{
  54. Id: int64(j + 1),
  55. Name: strings.Trim(oneTheme.Content[j].Name, " "),
  56. Type: strings.ToLower(strings.Trim(oneTheme.Content[j].Type, " ")),
  57. }
  58. }
  59. if !utils.InSlice(oneDbTheme.Name, existThemeList) {
  60. oneDbTheme.Id = 0
  61. oneDbTheme.AddTime = curTime
  62. insertThemeList = append(insertThemeList, oneDbTheme)
  63. } else {
  64. updateThemeList = append(updateThemeList, oneDbTheme)
  65. }
  66. if contentStr, err = jsoniter.MarshalToString(oneDbTheme.Content); err != nil {
  67. return errors.New("序列化Content失败的类型名: " + oneDbTheme.Name)
  68. }
  69. cacheMap[oneDbTheme.Name] = model.TActiveImgNameConfigCache{
  70. ActImgNameConfBasic: oneDbTheme.ActImgNameConfBasic,
  71. Content: contentStr,
  72. }
  73. }
  74. // 新增
  75. if len(insertThemeList) != 0 {
  76. if _, err = repo.Insert(insertThemeList); err != nil {
  77. return errors.WithStack(err)
  78. }
  79. }
  80. // 更新
  81. if len(updateThemeList) != 0 {
  82. if err = repo.Update(updateThemeList, "id"); err != nil {
  83. return errors.WithStack(err)
  84. }
  85. }
  86. // 更新缓存
  87. repo.Activity.HMSetActImgNameConfCache(cacheMap)
  88. return err
  89. }
  90. func (This *activityService) GetActThemeExtraConf(req *model.ImgExtraConfReq) []model.TActiveImgExtraConfig {
  91. extraConfs := repo.Search[model.TActiveImgExtraConfig]("Where `type` = ? And `skin` = ?", req.Type, req.Skin)
  92. if extraConfs == nil {
  93. extraConfs = []model.TActiveImgExtraConfig{}
  94. }
  95. return extraConfs
  96. }
  97. func (This *activityService) ThemeExtraConfUpdate(req *model.ImgExtraConfUpdateReq) error {
  98. for i := range req.Conf {
  99. req.Conf[i].Type = req.Type
  100. req.Conf[i].Skin = req.Skin
  101. }
  102. if _, err := repo.ReplaceStruct[model.TActiveImgExtraConfig](req.Conf, repo.WithKeys("id"),
  103. repo.WithWhere("Where `type` = ? And `skin` = ?", req.Type, req.Skin),
  104. ); err != nil {
  105. return err
  106. }
  107. return This.buildImgExtraConfJson()
  108. }
  109. func (This *activityService) buildImgExtraConfJson() error {
  110. list := repo.Search[model.TActiveImgExtraConfig]("")
  111. maps := map[string]map[string]map[string]string{}
  112. for _, config := range list {
  113. if _, Ok := maps[config.Type]; !Ok {
  114. maps[config.Type] = map[string]map[string]string{}
  115. }
  116. if _, Ok := maps[config.Type][config.Skin]; !Ok {
  117. maps[config.Type][config.Skin] = map[string]string{}
  118. }
  119. maps[config.Type][config.Skin][config.Key] = config.Val
  120. }
  121. return Config.SaveJson(maps, "ActSkinExtraConfig")
  122. }
  123. // GetActIconConfList 获取活动Icon列表
  124. func (This *actThemeSrc) GetActIconConfList() model.ResActIconConfList {
  125. iconList := repo.Search[model.TRewardCenterConfig]("")
  126. retList := make([]model.TRewardCenterConfig, 0, len(iconList))
  127. for _, config := range iconList {
  128. if utils.InSlice(config.Id, []int64{3, 4, 5, 6, 7, 8, 9, 10, 15, 19, 21, 22, 23, 32, 34, 42, 47}) {
  129. continue
  130. }
  131. retList = append(retList, config)
  132. }
  133. showLimit := repo.Activity.HGetActIconRightMaxShow()
  134. return model.ResActIconConfList{
  135. List: retList,
  136. RightMaxShow: showLimit,
  137. }
  138. }
  139. // InsertIconConf 新增活动Icon配置
  140. func (This *actThemeSrc) InsertIconConf(iconConf model.TRewardCenterConfig) error {
  141. if _, err := repo.Insert(&iconConf); err != nil {
  142. return errors.WithStack(err)
  143. }
  144. // 更新全部配置文件
  145. if err := This.handleIconConfJson(); err != nil {
  146. return errors.WithStack(err)
  147. }
  148. return nil
  149. }
  150. // UpdateIconConf 更新活动Icon配置
  151. func (This *actThemeSrc) UpdateIconConf(req model.ResActIconConfList) error {
  152. // 更新配置列表
  153. if err := repo.Update(req.List, "id"); err != nil {
  154. return errors.WithStack(err)
  155. }
  156. // 更新缓存中的配置
  157. repo.Activity.HSetActIconRightMaxShow(req.RightMaxShow)
  158. // 更新全部配置文件
  159. if err := This.handleIconConfJson(); err != nil {
  160. return errors.WithStack(err)
  161. }
  162. return nil
  163. }
  164. // handleIconConfJson 处理活动Icon配置Json
  165. func (This *actThemeSrc) handleIconConfJson() error {
  166. iconConf := This.GetActIconConfList()
  167. return Config.SaveJson(iconConf, "IconConfig")
  168. }