uniqid.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package uniqid
  2. import (
  3. "crazy-fox-backend-api/config"
  4. "crazy-fox-backend-api/model"
  5. "crazy-fox-backend-api/utils"
  6. "github.com/google/uuid"
  7. "github.com/pkg/errors"
  8. )
  9. // uniqId=>stageType=>parentIndex=>typeKey=>idx
  10. var stageIdxMapping = make(map[string]map[string]map[string]map[string]int64)
  11. // getIdxByStageKey 获取当前层级Index
  12. func getIdxByStageKey(uniqId, stageType, val string, parentIdx ...int64) int64 {
  13. parentKey := utils.Ternary(len(parentIdx) == 0, "0", utils.Implode(parentIdx, "_"))
  14. if _, Ok := stageIdxMapping[uniqId]; !Ok {
  15. stageIdxMapping[uniqId] = map[string]map[string]map[string]int64{}
  16. }
  17. if _, Ok := stageIdxMapping[uniqId][stageType]; !Ok {
  18. stageIdxMapping[uniqId][stageType] = map[string]map[string]int64{}
  19. }
  20. if _, Ok := stageIdxMapping[uniqId][stageType][parentKey]; !Ok {
  21. stageIdxMapping[uniqId][stageType][parentKey] = map[string]int64{}
  22. }
  23. if stageIdxMapping[uniqId][stageType][parentKey][val] == 0 {
  24. stageIdxMapping[uniqId][stageType][parentKey][val] = int64(len(stageIdxMapping[uniqId][stageType][parentKey])) + 1
  25. }
  26. return stageIdxMapping[uniqId][stageType][parentKey][val]
  27. }
  28. // GetStageIdx 获取当前层级所有分组的Idx
  29. func GetStageIdx(uniqId string, stage ...any) []int64 {
  30. idxArr := make([]int64, 0, len(stage))
  31. for i := 0; i < len(stage); i++ {
  32. switch val := stage[i].(type) {
  33. case model.LvRange:
  34. key := utils.Implode([]int64{val.LvStart, val.LvEnd}, "_")
  35. idxArr = append(idxArr, getIdxByStageKey(uniqId, config.ActGroupTypeLv, key, idxArr...))
  36. case model.SpinsRange:
  37. key := utils.Implode([]int64{val.SpinsStart, val.SpinsEnd}, "_")
  38. idxArr = append(idxArr, getIdxByStageKey(uniqId, config.ActGroupTypeSpins, key, idxArr...))
  39. case model.VipPointRange:
  40. key := utils.Implode([]int64{val.VipPointStart, val.VipPointEnd}, "_")
  41. idxArr = append(idxArr, getIdxByStageKey(uniqId, config.ActGroupTypeVipPoint, key, idxArr...))
  42. case model.LostHourRange:
  43. key := utils.Implode([]int64{val.LostHourStart, val.LostHourEnd}, "_")
  44. idxArr = append(idxArr, getIdxByStageKey(uniqId, config.ActGroupTypeLostHour, key, idxArr...))
  45. case model.SsGroup:
  46. key := utils.Implode(val.Shushu, "_")
  47. idxArr = append(idxArr, getIdxByStageKey(uniqId, config.ActGroupTypeShushu, key, idxArr...))
  48. }
  49. }
  50. return idxArr
  51. }
  52. // DelMapByUniqId 通过唯一ID删除map元素
  53. func DelMapByUniqId(uniqId string) {
  54. delete(stageIdxMapping, uniqId)
  55. }
  56. // GetUniqId 获取唯一ID
  57. func GetUniqId() string {
  58. unique, err := uuid.NewUUID()
  59. if err != nil {
  60. panic(errors.Wrap(err, "获取唯一Id失败"))
  61. }
  62. return unique.String()
  63. }