2024-09-01 08:38:09 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
2024-09-01 08:38:09 +00:00
|
|
|
"gorm.io/gorm"
|
2024-09-08 02:39:08 +00:00
|
|
|
"math"
|
2024-09-01 08:38:09 +00:00
|
|
|
"math/rand"
|
2024-09-01 08:51:13 +00:00
|
|
|
"time"
|
2024-09-01 08:38:09 +00:00
|
|
|
)
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
func CheckCanCheckIn(user models.Account) error {
|
2024-09-02 14:48:06 +00:00
|
|
|
probe := time.Now().Format("2006-01-02")
|
2024-09-01 08:51:13 +00:00
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
var record models.CheckInRecord
|
2024-10-30 16:17:53 +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")
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
func GetTodayCheckIn(user models.Account) (models.CheckInRecord, error) {
|
2024-09-02 14:48:06 +00:00
|
|
|
probe := time.Now().Format("2006-01-02")
|
2024-09-02 12:07:19 +00:00
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
var record models.CheckInRecord
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
|
2024-09-02 12:07:19 +00:00
|
|
|
return record, fmt.Errorf("unable get daliy sign record: %v", err)
|
|
|
|
}
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
func CheckIn(user models.Account) (models.CheckInRecord, error) {
|
2024-09-01 08:38:09 +00:00
|
|
|
tier := rand.Intn(5)
|
2024-11-27 13:57:10 +00:00
|
|
|
record := models.CheckInRecord{
|
2024-09-01 08:38:09 +00:00
|
|
|
ResultTier: tier,
|
2024-09-08 02:39:08 +00:00
|
|
|
ResultExperience: rand.Intn(int(math.Max(float64(tier), 1)*100)+1-100) + 100,
|
2024-10-30 16:17:53 +00:00
|
|
|
AccountID: user.ID,
|
2024-09-01 08:38:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
if err := CheckCanCheckIn(user); err != nil {
|
2024-09-01 08:38:09 +00:00
|
|
|
return record, fmt.Errorf("today already signed")
|
|
|
|
}
|
|
|
|
|
2024-09-02 12:07:19 +00:00
|
|
|
var profile models.AccountProfile
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := database.C.Where("account_id = ?", user.ID).First(&profile).Error; err != nil {
|
2024-09-02 12:07:19 +00:00
|
|
|
return record, fmt.Errorf("unable get account profile: %v", err)
|
|
|
|
} else {
|
|
|
|
profile.Experience += uint64(record.ResultExperience)
|
|
|
|
database.C.Save(&profile)
|
|
|
|
}
|
|
|
|
|
2024-09-01 08:38:09 +00:00
|
|
|
if err := database.C.Save(&record).Error; err != nil {
|
|
|
|
return record, fmt.Errorf("unable do daliy sign: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return record, nil
|
|
|
|
}
|