mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 20:27:23 +00:00
36 lines
739 B
Go
36 lines
739 B
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"nysoure/server/model"
|
|
)
|
|
|
|
func GetStatistic(key string) int64 {
|
|
statistic := &model.Statistic{}
|
|
if err := db.Where(&model.Statistic{
|
|
Key: key,
|
|
}).First(statistic).Error; err != nil {
|
|
return 0
|
|
}
|
|
return statistic.Value
|
|
}
|
|
|
|
func UpdateStatistic(key string, offset int64) error {
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
statistic := &model.Statistic{}
|
|
if err := tx.Where(&model.Statistic{
|
|
Key: key,
|
|
}).First(statistic).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
statistic.Key = key
|
|
statistic.Value = offset
|
|
return tx.Create(statistic).Error
|
|
}
|
|
return err
|
|
}
|
|
statistic.Value += offset
|
|
return tx.Save(statistic).Error
|
|
})
|
|
}
|