/
map_element.go
153 lines (135 loc) · 3.06 KB
/
map_element.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
146
147
148
149
150
151
152
153
package db
import (
"github.com/jinzhu/gorm"
"time"
"encoding/json"
. "github.com/e154/smart-home/common"
"fmt"
)
type MapElements struct {
Db *gorm.DB
}
type Prototype struct {
*MapImage
*MapText
*MapDevice
}
type MapElement struct {
Id int64 `gorm:"primary_key"`
Name string
Description string
PrototypeId int64
PrototypeType PrototypeType
Prototype Prototype
Map *Map
MapId int64
MapLayer *MapLayer
MapLayerId int64
GraphSettings json.RawMessage `gorm:"type:jsonb;not null"`
Status StatusType
Weight int64
CreatedAt time.Time
UpdatedAt time.Time
}
func (d *MapElement) TableName() string {
return "map_elements"
}
func (n MapElements) Add(v *MapElement) (id int64, err error) {
if err = n.Db.Create(&v).Error; err != nil {
return
}
id = v.Id
return
}
func (n MapElements) GetById(mapId int64) (v *MapElement, err error) {
v = &MapElement{Id: mapId}
if err = n.Db.First(&v).Error; err != nil {
return
}
if v.PrototypeId == 0 {
return
}
switch v.PrototypeType {
case PrototypeTypeText:
text := &MapText{}
err = n.Db.Model(&MapText{}).
Where("id = ?", v.PrototypeId).
First(&text).
Error
if err == nil {
v.Prototype = Prototype{
MapText: text,
}
}
case PrototypeTypeImage:
image := &MapImage{}
err = n.Db.Model(&MapImage{}).
Where("id = ?", v.PrototypeId).
First(&image).
Error
if err == nil {
v.Prototype = Prototype{
MapImage: image,
}
}
case PrototypeTypeDevice:
device := &MapDevice{}
err = n.Db.Model(&MapDevice{}).
Where("id = ?", v.PrototypeId).
Preload("Image").
Preload("States").
Preload("States.Image").
Preload("States.DeviceState").
Preload("Actions").
Preload("Actions.Image").
Preload("Actions.DeviceAction").
Preload("Device").
Preload("Device.States").
Preload("Device.Actions").
First(&device).
Error
if err == nil {
v.Prototype = Prototype{
MapDevice: device,
}
}
}
return
}
func (n MapElements) Update(m *MapElement) (err error) {
err = n.Db.Model(&MapElement{Id: m.Id}).Updates(map[string]interface{}{
"name": m.Name,
"description": m.Description,
"prototype_id": m.PrototypeId,
"prototype_type": m.PrototypeType,
"map_id": m.MapId,
"layer_id": m.MapLayerId,
"graph_settings": m.GraphSettings,
"status": m.Status,
"weight": m.Weight,
}).Error
return
}
func (n MapElements) Sort(m *MapElement) (err error) {
err = n.Db.Model(&MapElement{Id: m.Id}).Updates(map[string]interface{}{
"weight": m.Weight,
}).Error
return
}
func (n MapElements) Delete(mapId int64) (err error) {
err = n.Db.Delete(&MapElement{Id: mapId}).Error
return
}
func (n *MapElements) List(limit, offset int64, orderBy, sort string) (list []*MapElement, total int64, err error) {
if err = n.Db.Model(MapElement{}).Count(&total).Error; err != nil {
return
}
list = make([]*MapElement, 0)
err = n.Db.
Limit(limit).
Offset(offset).
Order(fmt.Sprintf("%s %s", sort, orderBy)).
Find(&list).
Error
return
}