data.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package comcode
  2. import (
  3. "active/constant"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "strconv"
  8. "time"
  9. "git.jiaxianghudong.com/webs/pkg/rds"
  10. "gorm.io/gorm"
  11. )
  12. type Comcode struct {
  13. Id int64 `json:"id" gorm:"column:id"`
  14. Code string `json:"code" gorm:"column:code"`
  15. AwardsId int `json:"awards_id" gorm:"column:awards_id"`
  16. CreateTime string `json:"create_time" gorm:"column:create_time"`
  17. OpenTime int64 `json:"open_time" gorm:"column:open_time"`
  18. EndTime int64 `json:"end_time" gorm:"column:end_time"`
  19. UserId int64 `json:"user_id" gorm:"column:user_id"`
  20. activeId string `gorm:"-"`
  21. }
  22. func (c *Comcode) GetActiveId() string {
  23. return c.activeId
  24. }
  25. func (this *Comcode) SetActiveId(activeId string) {
  26. this.activeId = activeId
  27. }
  28. func (this Comcode) TableName() string {
  29. return fmt.Sprintf("common_code_%v", this.activeId)
  30. }
  31. func (this *Comcode) OneComcode(code string, db *gorm.DB) error {
  32. return db.Table(this.TableName()).Where("code = ?", code).First(this).Error
  33. }
  34. func (this *Comcode) CreateOne(db *gorm.DB) error {
  35. var count int64
  36. db.Table(this.TableName()).Where("awards_id = ? and user_id=?", this.AwardsId, this.UserId).Count(&count)
  37. if count > 0 {
  38. return errors.New("count>0")
  39. }
  40. db.Table(this.TableName()).Create(this)
  41. return nil
  42. }
  43. func (this *Comcode) UpdateUserId(db *gorm.DB) error {
  44. return db.Table(this.TableName()).Where("code = ?", this.Code).Updates(map[string]interface{}{
  45. "user_id": this.UserId,
  46. "used_time": time.Now().Format("2006-01-02 15:04:05"),
  47. }).Error
  48. }
  49. func (this *Comcode) CountByUserid(db *gorm.DB, userid int64) (count int64) {
  50. db.Table(this.TableName()).Where("user_id = ? and awards_id = ?", userid, this.AwardsId).Count(&count)
  51. return count
  52. }
  53. func (this *Comcode) CountByUseridAndDate(db *gorm.DB, userid int64, date time.Time) (count int64) {
  54. db.Table(this.TableName()).Where("user_id = ? and awards_id = ? and DATE(used_time) = ?",
  55. userid, this.AwardsId, date.Format("2006-01-02")).Count(&count)
  56. return count
  57. }
  58. func GetLimitData(activeId string, userId string, tm string) int {
  59. data := rds.Redis.HGet(fmt.Sprintf(constant.ComCodeLimt, tm), fmt.Sprintf("%v%v", activeId, userId)).Val()
  60. if data == "" {
  61. return 0
  62. } else {
  63. number, err := strconv.Atoi(data)
  64. if err != nil {
  65. return 0
  66. }
  67. return number
  68. }
  69. }
  70. func SetLimitData(activeId string, userId string, tm string, data int) bool {
  71. rds.Redis.HSet(fmt.Sprintf(constant.ComCodeLimt, tm), fmt.Sprintf("%v%v", activeId, userId), fmt.Sprintf("%v", data))
  72. end, _ := time.Parse("2006-01-02", tm)
  73. expireAt := end.Add(25 * time.Hour)
  74. log.Printf("---更新领取兑换卷过期时间: expireAt:%v \n", expireAt)
  75. rds.Redis.ExpireAt(fmt.Sprintf(constant.ComCodeLimt, tm), expireAt)
  76. return true
  77. }
  78. func (this *Comcode) JudgeExp(timeUnix int64) bool {
  79. if this.OpenTime != 0 && timeUnix < this.OpenTime {
  80. return false
  81. }
  82. if this.EndTime != 0 && timeUnix > this.EndTime {
  83. return false
  84. }
  85. return true
  86. }