/
deviceInfo.go
145 lines (131 loc) · 3.86 KB
/
deviceInfo.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package relationDB
import (
"context"
"fmt"
"github.com/i-Things/things/shared/def"
"github.com/i-Things/things/shared/stores"
"github.com/i-Things/things/shared/utils"
"gorm.io/gorm"
)
type DeviceInfoRepo struct {
db *gorm.DB
}
type (
DeviceFilter struct {
ProductID string
AreaIDs []int64
DeviceName string
DeviceNames []string
Tags map[string]string
LastLoginTime struct {
Start int64
End int64
}
IsOnline int64
Range int64
Position stores.Point
DeviceAlias string
Versions []string
WithProduct bool
}
)
func NewDeviceInfoRepo(in any) *DeviceInfoRepo {
return &DeviceInfoRepo{db: stores.GetCommonConn(in)}
}
func (d DeviceInfoRepo) fmtFilter(ctx context.Context, f DeviceFilter) *gorm.DB {
db := d.db.WithContext(ctx)
if f.WithProduct {
db = db.Preload("ProductInfo")
}
//业务过滤条件
if f.ProductID != "" {
db = db.Where("product_id = ?", f.ProductID)
}
if len(f.AreaIDs) != 0 {
db = db.Where("area_id in ?", f.AreaIDs)
}
if len(f.Versions) != 0 {
db = db.Where("version = ?", f.AreaIDs)
}
if f.DeviceName != "" {
db = db.Where("device_name like ?", "%"+f.DeviceName+"%")
}
if len(f.DeviceNames) != 0 {
db = db.Where("device_name in ?", f.DeviceNames)
}
if f.DeviceAlias != "" {
db = db.Where("device_alias like ?", "%"+f.DeviceAlias+"%")
}
if f.Tags != nil {
for k, v := range f.Tags {
db = db.Where("JSON_CONTAINS(tags, JSON_OBJECT(?,?))",
k, v)
}
}
if f.LastLoginTime.Start != 0 {
db = db.Where("last_login >= ?", utils.ToYYMMddHHSS(f.LastLoginTime.Start*1000))
}
if f.LastLoginTime.End != 0 {
db = db.Where("last_login <= ?", utils.ToYYMMddHHSS(f.LastLoginTime.End*1000))
}
if f.IsOnline != 0 {
db = db.Where("is_online = ?", f.IsOnline)
}
if f.Range > 0 {
//f.Position 形如:point(116.393 39.905)
db = db.Where(fmt.Sprintf(
"round(st_distance_sphere(ST_GeomFromText(POINT(%v %v)), ST_GeomFromText(AsText(`position`))),2)>%d",
f.Position.Longitude, f.Position.Latitude, f.Range))
}
return db
}
func (d DeviceInfoRepo) FindOneByFilter(ctx context.Context, f DeviceFilter) (*DmDeviceInfo, error) {
var result DmDeviceInfo
db := d.fmtFilter(ctx, f)
err := db.First(&result).Error
if err != nil {
return nil, stores.ErrFmt(err)
}
return &result, nil
}
func (d DeviceInfoRepo) Update(ctx context.Context, data *DmDeviceInfo) error {
err := d.db.WithContext(ctx).Where("id = ?", data.ID).Save(data).Error
return stores.ErrFmt(err)
}
func (d DeviceInfoRepo) Delete(ctx context.Context, id int64) error {
err := d.db.WithContext(ctx).Where("id=?", id).Delete(&DmDeviceInfo{}).Error
return stores.ErrFmt(err)
}
func (d DeviceInfoRepo) Insert(ctx context.Context, data *DmDeviceInfo) error {
result := d.db.WithContext(ctx).Create(data)
return stores.ErrFmt(result.Error)
}
func (d DeviceInfoRepo) FindByFilter(ctx context.Context, f DeviceFilter, page *def.PageInfo) ([]*DmDeviceInfo, error) {
var results []*DmDeviceInfo
db := d.fmtFilter(ctx, f).Model(&DmDeviceInfo{})
db = page.ToGorm(db)
err := db.Find(&results).Error
if err != nil {
return nil, stores.ErrFmt(err)
}
return results, nil
}
func (d DeviceInfoRepo) CountByFilter(ctx context.Context, f DeviceFilter) (size int64, err error) {
db := d.fmtFilter(ctx, f).Model(&DmDeviceInfo{})
err = db.Count(&size).Error
return size, stores.ErrFmt(err)
}
type countModel struct {
CountKey string
Count int64
}
func (d DeviceInfoRepo) CountGroupByField(ctx context.Context, f DeviceFilter, columnName string) (map[string]int64, error) {
db := d.fmtFilter(ctx, f).Model(&DmDeviceInfo{})
countModelList := make([]*countModel, 0)
err := db.Select(fmt.Sprintf("%s as CountKey", columnName), "count(1) as count").Group(columnName).Find(&countModelList).Error
result := make(map[string]int64, 0)
for _, v := range countModelList {
result[v.CountKey] = v.Count
}
return result, stores.ErrFmt(err)
}