act_fantastic_bonus.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package service
  2. import (
  3. "fmt"
  4. "sort"
  5. "crazy-fox-backend-api/config"
  6. "crazy-fox-backend-api/model"
  7. "crazy-fox-backend-api/utils"
  8. "crazy-fox-backend-api/utils/uniqid"
  9. "github.com/jmoiron/sqlx/types"
  10. jsoniter "github.com/json-iterator/go"
  11. "github.com/pkg/errors"
  12. )
  13. type actFantasticBonus struct{}
  14. // 展示数据处理
  15. func (This *actFantasticBonus) handleActShow(confInfo *model.ActConfInfo) (json types.JSONText, err error) {
  16. var repoConf map[int64]model.FantasticBonusRepoConf
  17. if err = jsoniter.Unmarshal(confInfo.Reward, &repoConf); err != nil {
  18. return nil, errors.Wrap(err, "翻倍礼包 奖励Json解析失败")
  19. }
  20. // 数据转换
  21. showConf := This.convertIntoShowConf(repoConf)
  22. if json, err = jsoniter.Marshal(showConf); err != nil {
  23. return json, errors.Wrap(err, "翻倍礼包 奖励序列化失败")
  24. }
  25. return
  26. }
  27. // 保存数据处理
  28. func (This *actFantasticBonus) handleActSave(confInfo *model.ActConfInfo) (err error) {
  29. var showConf []model.FantasticBonusShowConf
  30. if err = jsoniter.Unmarshal(confInfo.ActShowConf, &showConf); err != nil {
  31. return errors.Wrap(err, "翻倍礼包 奖励Json解析失败")
  32. }
  33. rewardMap := This.convertIntoRepoConf(showConf)
  34. if confInfo.Reward, err = jsoniter.Marshal(rewardMap); err != nil {
  35. return errors.Wrap(err, "翻倍礼包 奖励数据序列化失败")
  36. }
  37. return
  38. }
  39. // 分层数据处理
  40. func (This *actFantasticBonus) handleActRewardSection(confInfo *model.ActConfInfo, maps ...map[string]any) error {
  41. var rewardRepoConfMap map[int64]model.FantasticBonusRepoConf
  42. if err := jsoniter.Unmarshal(confInfo.Reward, &rewardRepoConfMap); err != nil {
  43. return errors.Wrap(err, "翻倍礼包奖励解析失败")
  44. }
  45. conditionMap, rewardMap := maps[0], maps[1]
  46. for idx1, item1 := range rewardRepoConfMap {
  47. for idx2, item2 := range item1.Stages {
  48. stageKey := fmt.Sprintf("%d_%d", idx1, idx2)
  49. conditionMap[stageKey] = Activity.buildStageCondition(
  50. model.LvRange{LvStart: item1.Start, LvEnd: item1.End},
  51. item2.SsGroup,
  52. )
  53. rewardMap[stageKey] = map[string][]model.FantasticBonusStageReward{"stages": item2.Stages}
  54. }
  55. }
  56. return nil
  57. }
  58. // 导入数据处理
  59. func (This *actFantasticBonus) handleActImport(_ *model.ActDetailInfo, excelInfo [][][]string) (json types.JSONText, err error) {
  60. if len(excelInfo) < 3 {
  61. return json, errors.New("工作表数量不匹配")
  62. }
  63. var showConf []model.FantasticBonusShowConf
  64. if showConf, err = This.sheetResolver(excelInfo); err != nil {
  65. return json, errors.Wrap(err, "翻倍礼包 工作表解析失败")
  66. }
  67. if json, err = jsoniter.Marshal(showConf); err != nil {
  68. return json, errors.Wrap(err, "翻倍礼包 配置序列化失败")
  69. }
  70. return
  71. }
  72. func (This *actFantasticBonus) convertIntoShowConf(repoConfMap map[int64]model.FantasticBonusRepoConf) (showConfArr []model.FantasticBonusShowConf) {
  73. for lvId, lvStage := range repoConfMap {
  74. for ssGroupId, ssGroupStage := range lvStage.Stages {
  75. showConfArr = append(showConfArr, model.FantasticBonusShowConf{
  76. TwoLayerLvSsGroup: model.TwoLayerLvSsGroup{
  77. Id: Activity.buildLayerOrderId(lvId, ssGroupId),
  78. LvRange: model.LvRange{LvStart: lvStage.Start, LvEnd: lvStage.End},
  79. SsGroup: ssGroupStage.SsGroup,
  80. },
  81. Stages: ssGroupStage.Stages,
  82. })
  83. }
  84. }
  85. sort.SliceStable(showConfArr, func(i, j int) bool { return showConfArr[i].Id < showConfArr[j].Id })
  86. return
  87. }
  88. func (This *actFantasticBonus) convertIntoRepoConf(showConfArr []model.FantasticBonusShowConf) (repoConfMap map[int64]model.FantasticBonusRepoConf) {
  89. var unique = uniqid.GetUniqId()
  90. defer uniqid.DelMapByUniqId(unique)
  91. repoConfMap = map[int64]model.FantasticBonusRepoConf{}
  92. for i := 0; i < len(showConfArr); i++ {
  93. oneRowInfo := showConfArr[i]
  94. idxes := uniqid.GetStageIdx(unique, oneRowInfo.LvRange, oneRowInfo.SsGroup)
  95. lvIdx, ssIdx := idxes[0], idxes[1]
  96. // 等级分组
  97. if _, Ok := repoConfMap[lvIdx]; !Ok {
  98. repoConfMap[lvIdx] = model.FantasticBonusRepoConf{
  99. StartEnd: model.StartEnd{Start: oneRowInfo.LvStart, End: oneRowInfo.LvEnd},
  100. Stages: map[int64]model.FantasticBonusSSGroupStage{},
  101. }
  102. }
  103. for j := 0; j < len(oneRowInfo.Stages); j++ {
  104. oneStage := &oneRowInfo.Stages[j]
  105. oneStage.Gid = int64(j + 1210001)
  106. }
  107. // 数数分组
  108. if _, Ok := repoConfMap[lvIdx].Stages[ssIdx]; !Ok {
  109. repoConfMap[lvIdx].Stages[ssIdx] = model.FantasticBonusSSGroupStage{
  110. SsGroup: oneRowInfo.SsGroup,
  111. Stages: oneRowInfo.Stages,
  112. }
  113. }
  114. }
  115. return
  116. }
  117. func (This *actFantasticBonus) sheetResolver(excelInfo [][][]string) (showConf []model.FantasticBonusShowConf, err error) {
  118. sheet2Array, sheet3Array := excelInfo[1], excelInfo[2]
  119. // 解析分组ID配置项
  120. var stageConf []model.TwoLayerLvSsGroup
  121. if stageConf, err = Activity.parseTwoLayerLvSsGroupConf(sheet2Array); err != nil {
  122. return showConf, errors.WithStack(err)
  123. }
  124. // 解析奖励配置项
  125. var rewardMap map[int64][]model.FantasticBonusStageReward
  126. if rewardMap, err = This.parseRewardSheet(sheet3Array); err != nil {
  127. return showConf, errors.WithStack(err)
  128. }
  129. if showConf, err = This.packageShowConf(stageConf, rewardMap); err != nil {
  130. return showConf, errors.WithStack(err)
  131. }
  132. return
  133. }
  134. func (This *actFantasticBonus) parseRewardSheet(sheet [][]string) (map[int64][]model.FantasticBonusStageReward, error) {
  135. var err error
  136. stageRewardMap := map[int64][]model.FantasticBonusStageReward{}
  137. for rowIdx := 1; rowIdx < len(sheet); rowIdx++ {
  138. var (
  139. id, layer int64
  140. one model.FantasticBonusStageReward
  141. bonusRewardArr = make([]model.ExcelPrize, 5)
  142. normalRewardArr = make([]model.ExcelPrize, 5)
  143. )
  144. // 解析行配置
  145. if err = utils.DestructAssign(sheet[rowIdx], &id, &layer, &one.Price, &one.Discount, &bonusRewardArr, &normalRewardArr); err != nil {
  146. return nil, errors.WithStack(err)
  147. }
  148. // 转换奖励格式
  149. if one.BonusPrize, err = Props.MultiParseActPrize(bonusRewardArr); err != nil {
  150. return nil, errors.WithStack(err)
  151. }
  152. if one.NormalPrize, err = Props.MultiParseActPrize(normalRewardArr); err != nil {
  153. return nil, errors.WithStack(err)
  154. }
  155. if one.Sku = config.PriceSkuMap[one.Price]; utils.IsEmpty(one.Sku) {
  156. return nil, errors.New(fmt.Sprintf("第%d行,购买价格:%.2f 无对应sku", rowIdx, one.Price))
  157. }
  158. stageRewardMap[id] = append(stageRewardMap[id], one)
  159. }
  160. return stageRewardMap, nil
  161. }
  162. func (This *actFantasticBonus) packageShowConf(layers []model.TwoLayerLvSsGroup, rewardMap map[int64][]model.FantasticBonusStageReward) (showConf []model.FantasticBonusShowConf, err error) {
  163. showConf = make([]model.FantasticBonusShowConf, 0, len(layers))
  164. for _, layer := range layers {
  165. reward, Ok := rewardMap[layer.Id]
  166. if !Ok {
  167. return showConf, errors.New("未配置奖励项")
  168. }
  169. showConf = append(showConf, model.FantasticBonusShowConf{
  170. TwoLayerLvSsGroup: layer,
  171. Stages: reward,
  172. })
  173. }
  174. return
  175. }