checkin.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package model
  2. import (
  3. "errors"
  4. "math/rand"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/setting/operation_setting"
  8. "gorm.io/gorm"
  9. )
  10. // Checkin 签到记录
  11. type Checkin struct {
  12. Id int `json:"id" gorm:"primaryKey;autoIncrement"`
  13. UserId int `json:"user_id" gorm:"not null;uniqueIndex:idx_user_checkin_date"`
  14. CheckinDate string `json:"checkin_date" gorm:"type:varchar(10);not null;uniqueIndex:idx_user_checkin_date"` // 格式: YYYY-MM-DD
  15. QuotaAwarded int `json:"quota_awarded" gorm:"not null"`
  16. CreatedAt int64 `json:"created_at" gorm:"bigint"`
  17. }
  18. // CheckinRecord 用于API返回的签到记录(不包含敏感字段)
  19. type CheckinRecord struct {
  20. CheckinDate string `json:"checkin_date"`
  21. QuotaAwarded int `json:"quota_awarded"`
  22. }
  23. func (Checkin) TableName() string {
  24. return "checkins"
  25. }
  26. // GetUserCheckinRecords 获取用户在指定日期范围内的签到记录
  27. func GetUserCheckinRecords(userId int, startDate, endDate string) ([]Checkin, error) {
  28. var records []Checkin
  29. err := DB.Where("user_id = ? AND checkin_date >= ? AND checkin_date <= ?",
  30. userId, startDate, endDate).
  31. Order("checkin_date DESC").
  32. Find(&records).Error
  33. return records, err
  34. }
  35. // HasCheckedInToday 检查用户今天是否已签到
  36. func HasCheckedInToday(userId int) (bool, error) {
  37. today := time.Now().Format("2006-01-02")
  38. var count int64
  39. err := DB.Model(&Checkin{}).
  40. Where("user_id = ? AND checkin_date = ?", userId, today).
  41. Count(&count).Error
  42. return count > 0, err
  43. }
  44. // UserCheckin 执行用户签到
  45. // MySQL 和 PostgreSQL 使用事务保证原子性
  46. // SQLite 不支持嵌套事务,使用顺序操作 + 手动回滚
  47. func UserCheckin(userId int) (*Checkin, error) {
  48. setting := operation_setting.GetCheckinSetting()
  49. if !setting.Enabled {
  50. return nil, errors.New("签到功能未启用")
  51. }
  52. // 检查今天是否已签到
  53. hasChecked, err := HasCheckedInToday(userId)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if hasChecked {
  58. return nil, errors.New("今日已签到")
  59. }
  60. // 计算随机额度奖励
  61. quotaAwarded := setting.MinQuota
  62. if setting.MaxQuota > setting.MinQuota {
  63. quotaAwarded = setting.MinQuota + rand.Intn(setting.MaxQuota-setting.MinQuota+1)
  64. }
  65. today := time.Now().Format("2006-01-02")
  66. checkin := &Checkin{
  67. UserId: userId,
  68. CheckinDate: today,
  69. QuotaAwarded: quotaAwarded,
  70. CreatedAt: time.Now().Unix(),
  71. }
  72. // 根据数据库类型选择不同的策略
  73. if common.UsingSQLite {
  74. // SQLite 不支持嵌套事务,使用顺序操作 + 手动回滚
  75. return userCheckinWithoutTransaction(checkin, userId, quotaAwarded)
  76. }
  77. // MySQL 和 PostgreSQL 支持事务,使用事务保证原子性
  78. return userCheckinWithTransaction(checkin, userId, quotaAwarded)
  79. }
  80. // userCheckinWithTransaction 使用事务执行签到(适用于 MySQL 和 PostgreSQL)
  81. func userCheckinWithTransaction(checkin *Checkin, userId int, quotaAwarded int) (*Checkin, error) {
  82. err := DB.Transaction(func(tx *gorm.DB) error {
  83. // 步骤1: 创建签到记录
  84. // 数据库有唯一约束 (user_id, checkin_date),可以防止并发重复签到
  85. if err := tx.Create(checkin).Error; err != nil {
  86. return errors.New("签到失败,请稍后重试")
  87. }
  88. // 步骤2: 在事务中增加用户额度
  89. if err := tx.Model(&User{}).Where("id = ?", userId).
  90. Update("quota", gorm.Expr("quota + ?", quotaAwarded)).Error; err != nil {
  91. return errors.New("签到失败:更新额度出错")
  92. }
  93. return nil
  94. })
  95. if err != nil {
  96. return nil, err
  97. }
  98. // 事务成功后,异步更新缓存
  99. go func() {
  100. _ = cacheIncrUserQuota(userId, int64(quotaAwarded))
  101. }()
  102. return checkin, nil
  103. }
  104. // userCheckinWithoutTransaction 不使用事务执行签到(适用于 SQLite)
  105. func userCheckinWithoutTransaction(checkin *Checkin, userId int, quotaAwarded int) (*Checkin, error) {
  106. // 步骤1: 创建签到记录
  107. // 数据库有唯一约束 (user_id, checkin_date),可以防止并发重复签到
  108. if err := DB.Create(checkin).Error; err != nil {
  109. return nil, errors.New("签到失败,请稍后重试")
  110. }
  111. // 步骤2: 增加用户额度
  112. // 使用 db=true 强制直接写入数据库,不使用批量更新
  113. if err := IncreaseUserQuota(userId, quotaAwarded, true); err != nil {
  114. // 如果增加额度失败,需要回滚签到记录
  115. DB.Delete(checkin)
  116. return nil, errors.New("签到失败:更新额度出错")
  117. }
  118. return checkin, nil
  119. }
  120. // GetUserCheckinStats 获取用户签到统计信息
  121. func GetUserCheckinStats(userId int, month string) (map[string]interface{}, error) {
  122. // 获取指定月份的所有签到记录
  123. startDate := month + "-01"
  124. endDate := month + "-31"
  125. records, err := GetUserCheckinRecords(userId, startDate, endDate)
  126. if err != nil {
  127. return nil, err
  128. }
  129. // 转换为不包含敏感字段的记录
  130. checkinRecords := make([]CheckinRecord, len(records))
  131. for i, r := range records {
  132. checkinRecords[i] = CheckinRecord{
  133. CheckinDate: r.CheckinDate,
  134. QuotaAwarded: r.QuotaAwarded,
  135. }
  136. }
  137. // 检查今天是否已签到
  138. hasCheckedToday, _ := HasCheckedInToday(userId)
  139. // 获取用户所有时间的签到统计
  140. var totalCheckins int64
  141. var totalQuota int64
  142. DB.Model(&Checkin{}).Where("user_id = ?", userId).Count(&totalCheckins)
  143. DB.Model(&Checkin{}).Where("user_id = ?", userId).Select("COALESCE(SUM(quota_awarded), 0)").Scan(&totalQuota)
  144. return map[string]interface{}{
  145. "total_quota": totalQuota, // 所有时间累计获得的额度
  146. "total_checkins": totalCheckins, // 所有时间累计签到次数
  147. "checkin_count": len(records), // 本月签到次数
  148. "checked_in_today": hasCheckedToday, // 今天是否已签到
  149. "records": checkinRecords, // 本月签到记录详情(不含id和user_id)
  150. }, nil
  151. }