tank/rest/preference_dao.go
2019-04-04 03:14:47 +08:00

65 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package rest
import (
"github.com/nu7hatch/gouuid"
"time"
)
type PreferenceDao struct {
BaseDao
}
//按照Id查询偏好设置
func (this *PreferenceDao) Fetch() *Preference {
// Read
var preference = &Preference{}
db := CONTEXT.DB.First(preference)
if db.Error != nil {
if db.Error.Error() == DB_ERROR_NOT_FOUND {
preference.Name = "蓝眼云盘"
preference.ShowAlien = true
this.Create(preference)
return preference
} else {
return nil
}
}
return preference
}
//创建
func (this *PreferenceDao) Create(preference *Preference) *Preference {
timeUUID, _ := uuid.NewV4()
preference.Uuid = string(timeUUID.String())
preference.CreateTime = time.Now()
preference.UpdateTime = time.Now()
preference.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(preference)
this.PanicError(db.Error)
return preference
}
//修改一个偏好设置
func (this *PreferenceDao) Save(preference *Preference) *Preference {
preference.UpdateTime = time.Now()
db := CONTEXT.DB.Save(preference)
this.PanicError(db.Error)
return preference
}
//执行清理操作
func (this *PreferenceDao) Cleanup() {
this.logger.Info("[PreferenceDao]执行清理清除数据库中所有Preference记录。")
db := CONTEXT.DB.Where("uuid is not null").Delete(Preference{})
this.PanicError(db.Error)
}