mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 10:10:25 +00:00
make lint happy
This commit is contained in:
@@ -6,12 +6,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/logoove/sqlite"
|
||||
_ "github.com/logoove/sqlite" // use sql
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// SleepDB 睡眠数据库
|
||||
type SleepDB gorm.DB
|
||||
|
||||
// Initialize 初始化
|
||||
func Initialize(dbpath string) *SleepDB {
|
||||
var err error
|
||||
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
|
||||
@@ -30,6 +32,7 @@ func Initialize(dbpath string) *SleepDB {
|
||||
return (*SleepDB)(gdb)
|
||||
}
|
||||
|
||||
// Open 打开
|
||||
func Open(dbpath string) (*SleepDB, error) {
|
||||
db, err := gorm.Open("sqlite3", dbpath)
|
||||
if err != nil {
|
||||
@@ -39,24 +42,27 @@ func Open(dbpath string) (*SleepDB, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func (sdb *SleepDB) Close() error {
|
||||
db := (*gorm.DB)(sdb)
|
||||
return db.Close()
|
||||
}
|
||||
|
||||
// SleepManage 睡眠信息
|
||||
type SleepManage struct {
|
||||
gorm.Model
|
||||
GroupId int64 `gorm:"column:group_id"`
|
||||
UserId int64 `gorm:"column:user_id"`
|
||||
GroupID int64 `gorm:"column:group_id"`
|
||||
UserID int64 `gorm:"column:user_id"`
|
||||
SleepTime time.Time `gorm:"column:sleep_time"`
|
||||
}
|
||||
|
||||
// TableName 表名
|
||||
func (SleepManage) TableName() string {
|
||||
return "sleep_manage"
|
||||
}
|
||||
|
||||
// 更新睡眠时间
|
||||
func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.Duration) {
|
||||
// Sleep 更新睡眠时间
|
||||
func (sdb *SleepDB) Sleep(gid, uid int64) (position int, awakeTime time.Duration) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
now := time.Now()
|
||||
var today time.Time
|
||||
@@ -66,11 +72,11 @@ func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.D
|
||||
today = now.Add(-time.Hour*time.Duration(3+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
|
||||
}
|
||||
st := SleepManage{
|
||||
GroupId: groupId,
|
||||
UserId: userId,
|
||||
GroupID: gid,
|
||||
UserID: uid,
|
||||
SleepTime: now,
|
||||
}
|
||||
if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).First(&st).Error; err != nil {
|
||||
if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).First(&st).Error; err != nil {
|
||||
// error handling...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
db.Debug().Model(&SleepManage{}).Create(&st) // newUser not user
|
||||
@@ -78,26 +84,26 @@ func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.D
|
||||
} else {
|
||||
log.Println("sleeptime为", st)
|
||||
awakeTime = now.Sub(st.SleepTime)
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).Update(
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).Update(
|
||||
map[string]interface{}{
|
||||
"sleep_time": now,
|
||||
})
|
||||
}
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", groupId, now, today).Count(&position)
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", gid, now, today).Count(&position)
|
||||
return position, awakeTime
|
||||
}
|
||||
|
||||
// 更新起床时间
|
||||
func (sdb *SleepDB) GetUp(groupId, userId int64) (position int, sleepTime time.Duration) {
|
||||
// GetUp 更新起床时间
|
||||
func (sdb *SleepDB) GetUp(gid, uid int64) (position int, sleepTime time.Duration) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
now := time.Now()
|
||||
today := now.Add(-time.Hour*time.Duration(-6+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
|
||||
st := SleepManage{
|
||||
GroupId: groupId,
|
||||
UserId: userId,
|
||||
GroupID: gid,
|
||||
UserID: uid,
|
||||
SleepTime: now,
|
||||
}
|
||||
if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).First(&st).Error; err != nil {
|
||||
if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).First(&st).Error; err != nil {
|
||||
// error handling...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
db.Debug().Model(&SleepManage{}).Create(&st) // newUser not user
|
||||
@@ -105,11 +111,11 @@ func (sdb *SleepDB) GetUp(groupId, userId int64) (position int, sleepTime time.D
|
||||
} else {
|
||||
log.Println("sleeptime为", st)
|
||||
sleepTime = now.Sub(st.SleepTime)
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).Update(
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).Update(
|
||||
map[string]interface{}{
|
||||
"sleep_time": now,
|
||||
})
|
||||
}
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", groupId, now, today).Count(&position)
|
||||
db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", gid, now, today).Count(&position)
|
||||
return position, sleepTime
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user