| package model |
|
|
| import ( |
| "errors" |
| "math/rand" |
| "time" |
|
|
| "github.com/QuantumNous/new-api/common" |
| "github.com/QuantumNous/new-api/setting/operation_setting" |
| "gorm.io/gorm" |
| ) |
|
|
| |
| type Checkin struct { |
| Id int `json:"id" gorm:"primaryKey;autoIncrement"` |
| UserId int `json:"user_id" gorm:"not null;uniqueIndex:idx_user_checkin_date"` |
| CheckinDate string `json:"checkin_date" gorm:"type:varchar(10);not null;uniqueIndex:idx_user_checkin_date"` |
| QuotaAwarded int `json:"quota_awarded" gorm:"not null"` |
| CreatedAt int64 `json:"created_at" gorm:"bigint"` |
| } |
|
|
| |
| type CheckinRecord struct { |
| CheckinDate string `json:"checkin_date"` |
| QuotaAwarded int `json:"quota_awarded"` |
| } |
|
|
| func (Checkin) TableName() string { |
| return "checkins" |
| } |
|
|
| |
| func GetUserCheckinRecords(userId int, startDate, endDate string) ([]Checkin, error) { |
| var records []Checkin |
| err := DB.Where("user_id = ? AND checkin_date >= ? AND checkin_date <= ?", |
| userId, startDate, endDate). |
| Order("checkin_date DESC"). |
| Find(&records).Error |
| return records, err |
| } |
|
|
| |
| func HasCheckedInToday(userId int) (bool, error) { |
| today := time.Now().Format("2006-01-02") |
| var count int64 |
| err := DB.Model(&Checkin{}). |
| Where("user_id = ? AND checkin_date = ?", userId, today). |
| Count(&count).Error |
| return count > 0, err |
| } |
|
|
| |
| |
| |
| func UserCheckin(userId int) (*Checkin, error) { |
| setting := operation_setting.GetCheckinSetting() |
| if !setting.Enabled { |
| return nil, errors.New("签到功能未启用") |
| } |
|
|
| |
| hasChecked, err := HasCheckedInToday(userId) |
| if err != nil { |
| return nil, err |
| } |
| if hasChecked { |
| return nil, errors.New("今日已签到") |
| } |
|
|
| |
| quotaAwarded := setting.MinQuota |
| if setting.MaxQuota > setting.MinQuota { |
| quotaAwarded = setting.MinQuota + rand.Intn(setting.MaxQuota-setting.MinQuota+1) |
| } |
|
|
| today := time.Now().Format("2006-01-02") |
| checkin := &Checkin{ |
| UserId: userId, |
| CheckinDate: today, |
| QuotaAwarded: quotaAwarded, |
| CreatedAt: time.Now().Unix(), |
| } |
|
|
| |
| if common.UsingSQLite { |
| |
| return userCheckinWithoutTransaction(checkin, userId, quotaAwarded) |
| } |
|
|
| |
| return userCheckinWithTransaction(checkin, userId, quotaAwarded) |
| } |
|
|
| |
| func userCheckinWithTransaction(checkin *Checkin, userId int, quotaAwarded int) (*Checkin, error) { |
| err := DB.Transaction(func(tx *gorm.DB) error { |
| |
| |
| if err := tx.Create(checkin).Error; err != nil { |
| return errors.New("签到失败,请稍后重试") |
| } |
|
|
| |
| if err := tx.Model(&User{}).Where("id = ?", userId). |
| Update("quota", gorm.Expr("quota + ?", quotaAwarded)).Error; err != nil { |
| return errors.New("签到失败:更新额度出错") |
| } |
|
|
| return nil |
| }) |
|
|
| if err != nil { |
| return nil, err |
| } |
|
|
| |
| go func() { |
| _ = cacheIncrUserQuota(userId, int64(quotaAwarded)) |
| }() |
|
|
| return checkin, nil |
| } |
|
|
| |
| func userCheckinWithoutTransaction(checkin *Checkin, userId int, quotaAwarded int) (*Checkin, error) { |
| |
| |
| if err := DB.Create(checkin).Error; err != nil { |
| return nil, errors.New("签到失败,请稍后重试") |
| } |
|
|
| |
| |
| if err := IncreaseUserQuota(userId, quotaAwarded, true); err != nil { |
| |
| DB.Delete(checkin) |
| return nil, errors.New("签到失败:更新额度出错") |
| } |
|
|
| return checkin, nil |
| } |
|
|
| |
| func GetUserCheckinStats(userId int, month string) (map[string]interface{}, error) { |
| |
| startDate := month + "-01" |
| endDate := month + "-31" |
|
|
| records, err := GetUserCheckinRecords(userId, startDate, endDate) |
| if err != nil { |
| return nil, err |
| } |
|
|
| |
| checkinRecords := make([]CheckinRecord, len(records)) |
| for i, r := range records { |
| checkinRecords[i] = CheckinRecord{ |
| CheckinDate: r.CheckinDate, |
| QuotaAwarded: r.QuotaAwarded, |
| } |
| } |
|
|
| |
| hasCheckedToday, _ := HasCheckedInToday(userId) |
|
|
| |
| var totalCheckins int64 |
| var totalQuota int64 |
| DB.Model(&Checkin{}).Where("user_id = ?", userId).Count(&totalCheckins) |
| DB.Model(&Checkin{}).Where("user_id = ?", userId).Select("COALESCE(SUM(quota_awarded), 0)").Scan(&totalQuota) |
|
|
| return map[string]interface{}{ |
| "total_quota": totalQuota, |
| "total_checkins": totalCheckins, |
| "checkin_count": len(records), |
| "checked_in_today": hasCheckedToday, |
| "records": checkinRecords, |
| }, nil |
| } |
|
|