123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package service
- import (
- "fmt"
- "sort"
- "crazy-fox-backend-api/config"
- "crazy-fox-backend-api/model"
- "crazy-fox-backend-api/utils"
- "crazy-fox-backend-api/utils/uniqid"
- "github.com/jmoiron/sqlx/types"
- jsoniter "github.com/json-iterator/go"
- "github.com/pkg/errors"
- )
- type actFantasticBonus struct{}
- // 展示数据处理
- func (This *actFantasticBonus) handleActShow(confInfo *model.ActConfInfo) (json types.JSONText, err error) {
- var repoConf map[int64]model.FantasticBonusRepoConf
- if err = jsoniter.Unmarshal(confInfo.Reward, &repoConf); err != nil {
- return nil, errors.Wrap(err, "翻倍礼包 奖励Json解析失败")
- }
- // 数据转换
- showConf := This.convertIntoShowConf(repoConf)
- if json, err = jsoniter.Marshal(showConf); err != nil {
- return json, errors.Wrap(err, "翻倍礼包 奖励序列化失败")
- }
- return
- }
- // 保存数据处理
- func (This *actFantasticBonus) handleActSave(confInfo *model.ActConfInfo) (err error) {
- var showConf []model.FantasticBonusShowConf
- if err = jsoniter.Unmarshal(confInfo.ActShowConf, &showConf); err != nil {
- return errors.Wrap(err, "翻倍礼包 奖励Json解析失败")
- }
- rewardMap := This.convertIntoRepoConf(showConf)
- if confInfo.Reward, err = jsoniter.Marshal(rewardMap); err != nil {
- return errors.Wrap(err, "翻倍礼包 奖励数据序列化失败")
- }
- return
- }
- // 分层数据处理
- func (This *actFantasticBonus) handleActRewardSection(confInfo *model.ActConfInfo, maps ...map[string]any) error {
- var rewardRepoConfMap map[int64]model.FantasticBonusRepoConf
- if err := jsoniter.Unmarshal(confInfo.Reward, &rewardRepoConfMap); err != nil {
- return errors.Wrap(err, "翻倍礼包奖励解析失败")
- }
- conditionMap, rewardMap := maps[0], maps[1]
- for idx1, item1 := range rewardRepoConfMap {
- for idx2, item2 := range item1.Stages {
- stageKey := fmt.Sprintf("%d_%d", idx1, idx2)
- conditionMap[stageKey] = Activity.buildStageCondition(
- model.LvRange{LvStart: item1.Start, LvEnd: item1.End},
- item2.SsGroup,
- )
- rewardMap[stageKey] = map[string][]model.FantasticBonusStageReward{"stages": item2.Stages}
- }
- }
- return nil
- }
- // 导入数据处理
- func (This *actFantasticBonus) handleActImport(_ *model.ActDetailInfo, excelInfo [][][]string) (json types.JSONText, err error) {
- if len(excelInfo) < 3 {
- return json, errors.New("工作表数量不匹配")
- }
- var showConf []model.FantasticBonusShowConf
- if showConf, err = This.sheetResolver(excelInfo); err != nil {
- return json, errors.Wrap(err, "翻倍礼包 工作表解析失败")
- }
- if json, err = jsoniter.Marshal(showConf); err != nil {
- return json, errors.Wrap(err, "翻倍礼包 配置序列化失败")
- }
- return
- }
- func (This *actFantasticBonus) convertIntoShowConf(repoConfMap map[int64]model.FantasticBonusRepoConf) (showConfArr []model.FantasticBonusShowConf) {
- for lvId, lvStage := range repoConfMap {
- for ssGroupId, ssGroupStage := range lvStage.Stages {
- showConfArr = append(showConfArr, model.FantasticBonusShowConf{
- TwoLayerLvSsGroup: model.TwoLayerLvSsGroup{
- Id: Activity.buildLayerOrderId(lvId, ssGroupId),
- LvRange: model.LvRange{LvStart: lvStage.Start, LvEnd: lvStage.End},
- SsGroup: ssGroupStage.SsGroup,
- },
- Stages: ssGroupStage.Stages,
- })
- }
- }
- sort.SliceStable(showConfArr, func(i, j int) bool { return showConfArr[i].Id < showConfArr[j].Id })
- return
- }
- func (This *actFantasticBonus) convertIntoRepoConf(showConfArr []model.FantasticBonusShowConf) (repoConfMap map[int64]model.FantasticBonusRepoConf) {
- var unique = uniqid.GetUniqId()
- defer uniqid.DelMapByUniqId(unique)
- repoConfMap = map[int64]model.FantasticBonusRepoConf{}
- for i := 0; i < len(showConfArr); i++ {
- oneRowInfo := showConfArr[i]
- idxes := uniqid.GetStageIdx(unique, oneRowInfo.LvRange, oneRowInfo.SsGroup)
- lvIdx, ssIdx := idxes[0], idxes[1]
- // 等级分组
- if _, Ok := repoConfMap[lvIdx]; !Ok {
- repoConfMap[lvIdx] = model.FantasticBonusRepoConf{
- StartEnd: model.StartEnd{Start: oneRowInfo.LvStart, End: oneRowInfo.LvEnd},
- Stages: map[int64]model.FantasticBonusSSGroupStage{},
- }
- }
- for j := 0; j < len(oneRowInfo.Stages); j++ {
- oneStage := &oneRowInfo.Stages[j]
- oneStage.Gid = int64(j + 1210001)
- }
- // 数数分组
- if _, Ok := repoConfMap[lvIdx].Stages[ssIdx]; !Ok {
- repoConfMap[lvIdx].Stages[ssIdx] = model.FantasticBonusSSGroupStage{
- SsGroup: oneRowInfo.SsGroup,
- Stages: oneRowInfo.Stages,
- }
- }
- }
- return
- }
- func (This *actFantasticBonus) sheetResolver(excelInfo [][][]string) (showConf []model.FantasticBonusShowConf, err error) {
- sheet2Array, sheet3Array := excelInfo[1], excelInfo[2]
- // 解析分组ID配置项
- var stageConf []model.TwoLayerLvSsGroup
- if stageConf, err = Activity.parseTwoLayerLvSsGroupConf(sheet2Array); err != nil {
- return showConf, errors.WithStack(err)
- }
- // 解析奖励配置项
- var rewardMap map[int64][]model.FantasticBonusStageReward
- if rewardMap, err = This.parseRewardSheet(sheet3Array); err != nil {
- return showConf, errors.WithStack(err)
- }
- if showConf, err = This.packageShowConf(stageConf, rewardMap); err != nil {
- return showConf, errors.WithStack(err)
- }
- return
- }
- func (This *actFantasticBonus) parseRewardSheet(sheet [][]string) (map[int64][]model.FantasticBonusStageReward, error) {
- var err error
- stageRewardMap := map[int64][]model.FantasticBonusStageReward{}
- for rowIdx := 1; rowIdx < len(sheet); rowIdx++ {
- var (
- id, layer int64
- one model.FantasticBonusStageReward
- bonusRewardArr = make([]model.ExcelPrize, 5)
- normalRewardArr = make([]model.ExcelPrize, 5)
- )
- // 解析行配置
- if err = utils.DestructAssign(sheet[rowIdx], &id, &layer, &one.Price, &one.Discount, &bonusRewardArr, &normalRewardArr); err != nil {
- return nil, errors.WithStack(err)
- }
- // 转换奖励格式
- if one.BonusPrize, err = Props.MultiParseActPrize(bonusRewardArr); err != nil {
- return nil, errors.WithStack(err)
- }
- if one.NormalPrize, err = Props.MultiParseActPrize(normalRewardArr); err != nil {
- return nil, errors.WithStack(err)
- }
- if one.Sku = config.PriceSkuMap[one.Price]; utils.IsEmpty(one.Sku) {
- return nil, errors.New(fmt.Sprintf("第%d行,购买价格:%.2f 无对应sku", rowIdx, one.Price))
- }
- stageRewardMap[id] = append(stageRewardMap[id], one)
- }
- return stageRewardMap, nil
- }
- func (This *actFantasticBonus) packageShowConf(layers []model.TwoLayerLvSsGroup, rewardMap map[int64][]model.FantasticBonusStageReward) (showConf []model.FantasticBonusShowConf, err error) {
- showConf = make([]model.FantasticBonusShowConf, 0, len(layers))
- for _, layer := range layers {
- reward, Ok := rewardMap[layer.Id]
- if !Ok {
- return showConf, errors.New("未配置奖励项")
- }
- showConf = append(showConf, model.FantasticBonusShowConf{
- TwoLayerLvSsGroup: layer,
- Stages: reward,
- })
- }
- return
- }
|