h5video.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package h5video
  2. import (
  3. "active/internal/model/goldbrick"
  4. "active/tools"
  5. "fmt"
  6. "git.jiaxianghudong.com/webs/pkg/rds"
  7. "git.jiaxianghudong.com/webs/pkg/xgorm"
  8. "log"
  9. "time"
  10. )
  11. var (
  12. H5VideoConf = &H5VideoConfig{}
  13. RdH5video = "joyh5draw_%v"
  14. RdH5video2 = "joyh5draw%v_%v"
  15. )
  16. type H5VideoConfig struct {
  17. DataId int32 `json:"data_id" yaml:"data_id"`
  18. Awards []int64 `json:"awards" yaml:"awards"`
  19. Rate []int `json:"rate" yaml:"rate"`
  20. RateV2 []int `json:"rate_v2" yaml:"rate_v2"`
  21. IndexLimit map[int]int64 `json:"index_limit" yaml:"index_limit"`
  22. AwardsMap map[int][][]int64 `json:"awards_map" yaml:"awards_map"`
  23. AppId string `json:"app_id" yaml:"app_id"`
  24. PingUrl string `json:"ping_url" yaml:"ping_url"`
  25. AppSecret string `json:"app_secret" yaml:"app_secret"`
  26. }
  27. func PreConfig() {
  28. tools.ReloadYaml("h5_video.yaml", H5VideoConf)
  29. rate := 0
  30. for i := range H5VideoConf.Rate {
  31. rate += H5VideoConf.Rate[i]
  32. H5VideoConf.Rate[i] = rate
  33. }
  34. rate = 0
  35. for i := range H5VideoConf.RateV2 {
  36. rate += H5VideoConf.RateV2[i]
  37. H5VideoConf.RateV2[i] = rate
  38. }
  39. }
  40. func GetVideoDrawHistory(userId string, nickname string, add bool, tm string, v string) (*VideoDrawHistory, error) {
  41. videoDrawHistory := &VideoDrawHistory{}
  42. if userId == "" {
  43. return videoDrawHistory, nil
  44. }
  45. update := false
  46. xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
  47. tableName := VideoDrawHistory{}.TableName()
  48. if v != "" {
  49. tableName += v
  50. }
  51. err := xdb.Table(tableName).Where("user_id = ?", userId).First(videoDrawHistory).Error
  52. if (err != nil || videoDrawHistory.Id == 0) && add && userId != "" {
  53. videoDrawHistory.UserId = userId
  54. videoDrawHistory.Nickname = nickname
  55. update = true
  56. }
  57. if !judgeDrawTm(userId, tm, v) {
  58. update = true
  59. videoDrawHistory.Number = 0
  60. videoDrawHistory.NumberLimit = 11
  61. }
  62. if update {
  63. err = xdb.Table(tableName).Save(videoDrawHistory).Error
  64. }
  65. if userId == "oHdo261vjs61C7kOYeBqDN1HZlTA" {
  66. log.Println("joydebug", userId, update, videoDrawHistory, err)
  67. }
  68. return videoDrawHistory, err
  69. }
  70. func judgeDrawTm(userId string, tm string, v string) bool {
  71. demo := fmt.Sprintf(RdH5video, tm)
  72. if v != "" {
  73. demo = fmt.Sprintf(RdH5video2, v, tm)
  74. }
  75. val := rds.Redis.HGet(demo, userId).Val()
  76. if userId == "oHdo261vjs61C7kOYeBqDN1HZlTA" {
  77. log.Println("joydebug", userId, demo, val)
  78. }
  79. if val == "" {
  80. rds.Redis.HSet(demo, userId, "1").Val()
  81. rds.Redis.ExpireAt(demo, time.Now().Add(24*time.Hour))
  82. return false
  83. } else {
  84. return true
  85. }
  86. }
  87. func GetCodeList(userId string, v string) ([]*VideoCode, error) {
  88. xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
  89. videoCodeList := make([]*VideoCode, 0)
  90. tableName := VideoCode{}.TableName()
  91. if v != "" {
  92. tableName += v
  93. }
  94. err := xdb.Table(tableName).Where("user_id = ? and award_id>=0", userId).Find(&videoCodeList).Error
  95. return videoCodeList, err
  96. }
  97. func GetCodeAllList(userId string, v string) ([]*VideoCode, error) {
  98. xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
  99. videoCodeList := make([]*VideoCode, 0)
  100. tableName := VideoCode{}.TableName()
  101. if v != "" {
  102. tableName += v
  103. }
  104. var err error
  105. if userId != "" {
  106. xdb = xdb.Table(tableName).Where("user_id = ? and award_id >= 0", userId).Order("create_time desc")
  107. } else {
  108. xdb = xdb.Table(tableName).Order("create_time desc").Limit(20)
  109. }
  110. err = xdb.Find(&videoCodeList).Error
  111. for i := range videoCodeList {
  112. videoCodeList[i].AwardId = videoCodeList[i].AwardsId
  113. if userId == "" {
  114. videoCodeList[i].Code = ""
  115. videoCodeList[i].UserId = ""
  116. }
  117. }
  118. return videoCodeList, err
  119. }