zhangzengfei
2024-10-20 b916f7c68a3e16413eac4be13f0404267561f90c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package models
 
import "time"
 
type Positions struct {
    Id         uint   `gorm:"column:id;primary_key;auto_increment;" json:"id"`
    DeviceId   string `gorm:"column:device_id;index" json:"device_id"`
    Pos        string `gorm:"column:pos" json:"pos"`
    RunDir     int    `gorm:"column:run_dir" json:"run_dir"`
    CreateTime int64  `gorm:"column:create_time;index"`
    TimeString string `gorm:"column:time_string;"`
}
 
func (d *Positions) TableName() string {
    return "positions"
}
 
func (d *Positions) Save() error {
    return db.Table(d.TableName()).Save(d).Error
}
 
func (d *Positions) FindDevicePosition(devId string, timestamp int64) error {
    return db.Table(d.TableName()).Where("device_id = ? AND create_time <= ?", devId, timestamp).Order("create_time desc").First(&d).Error
}
 
func (d *Positions) FindMovePosition(timestamp int64, pos string) error {
    return db.Table(d.TableName()).Where("create_time >= ? AND pos != ?", timestamp, pos).First(&d).Error
}
 
func (d *Positions) FindPositionByTime(timestamp int64) error {
    return db.Table(d.TableName()).Where("create_time <= ?", timestamp).Order("create_time desc").First(&d).Error
}
 
func (d *Positions) Clean() error {
    timestamp := time.Now().Unix() - 3600
    return db.Table(d.TableName()).Where("create_time <= ?", timestamp).Delete(&d).Error
}