2024-09-01 08:38:09 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"math/rand"
|
2024-09-01 08:51:13 +00:00
|
|
|
"time"
|
2024-09-01 08:38:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CheckDailyCanSign(user models.Account) error {
|
2024-09-01 08:51:13 +00:00
|
|
|
probe := time.Now().Format("YYYY-MM-DD")
|
|
|
|
|
2024-09-01 08:38:09 +00:00
|
|
|
var record models.SignRecord
|
2024-09-01 08:51:13 +00:00
|
|
|
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
|
2024-09-01 08:38:09 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unable check daliy sign record: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("daliy sign record exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func DailySign(user models.Account) (models.SignRecord, error) {
|
|
|
|
tier := rand.Intn(5)
|
|
|
|
record := models.SignRecord{
|
|
|
|
ResultTier: tier,
|
|
|
|
ResultExperience: rand.Intn(tier*100) + 100,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := CheckDailyCanSign(user); err != nil {
|
|
|
|
return record, fmt.Errorf("today already signed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Save(&record).Error; err != nil {
|
|
|
|
return record, fmt.Errorf("unable do daliy sign: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return record, nil
|
|
|
|
}
|