/
query.go
264 lines (263 loc) 路 6 KB
/
query.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package repo
// import (
//
// "context"
// "database/sql"
// "encoding/base64"
// "errors"
//
// "github.com/ItsNotGoodName/ipcmanview/internal/models"
// "github.com/ItsNotGoodName/ipcmanview/internal/types"
// "github.com/ItsNotGoodName/ipcmanview/pkg/pagination"
// "github.com/ItsNotGoodName/ipcmanview/pkg/ssq"
// sq "github.com/Masterminds/squirrel"
//
// )
//
// type ListDahuaEventParams struct {
// pagination.Page
// Code []string
// Action []string
// DeviceID []int64
// Start types.Time
// End types.Time
// Ascending bool
// }
//
// type ListDahuaEventResult struct {
// pagination.PageResult
// Data []ListDahuaEvent
// }
//
// type ListDahuaEvent struct {
// DeviceName string
// DahuaEvent
// }
//
// func (db DB) ListDahuaEvent(ctx context.Context, arg ListDahuaEventParams) (ListDahuaEventResult, error) {
// where := sq.And{}
//
// eq := sq.Eq{}
// if len(arg.Code) != 0 {
// eq["code"] = arg.Code
// }
// if len(arg.Action) != 0 {
// eq["action"] = arg.Action
// }
// if len(arg.DeviceID) != 0 {
// eq["device_id"] = arg.DeviceID
// }
// where = append(where, eq)
//
// and := sq.And{}
// if !arg.Start.IsZero() {
// and = append(and, sq.GtOrEq{"created_at": arg.Start})
// }
// if !arg.End.IsZero() {
// and = append(and, sq.Lt{"created_at": arg.End})
// }
// where = append(where, and)
//
// order := "created_at DESC"
// if arg.Ascending {
// order = "created_at ASC"
// }
//
// var res []ListDahuaEvent
// err := ssq.Query(ctx, db, &res, sq.
// Select("e.*, d.name as device_name").
// From("dahua_events AS e").
// Where(where).
// OrderBy(order).
// Limit(uint64(arg.Page.Limit())).
// Offset(uint64(arg.Page.Offset())).
// LeftJoin("dahua_devices AS d ON d.id = e.device_id"))
// if err != nil {
// return ListDahuaEventResult{}, err
// }
//
// var count int
// err = ssq.QueryOne(ctx, db, &count, sq.
// Select("COUNT(*)").
// From("dahua_events").
// Where(where))
// if err != nil {
// return ListDahuaEventResult{}, err
// }
//
// return ListDahuaEventResult{
// PageResult: arg.Page.Result(count),
// Data: res,
// }, nil
// }
//
// type DahuaFileFilter struct {
// Type []string
// DeviceID []int64
// Start types.Time
// End types.Time
// Ascending bool
// Storage []models.Storage
// }
//
// func (arg DahuaFileFilter) where(where sq.And) sq.And {
// eq := sq.Eq{}
// if len(arg.Type) != 0 {
// eq["type"] = arg.Type
// }
// if len(arg.DeviceID) != 0 {
// eq["device_id"] = arg.DeviceID
// }
// if len(arg.Storage) != 0 {
// eq["storage"] = arg.Storage
// }
// where = append(where, eq)
//
// and := sq.And{}
// if !arg.Start.IsZero() {
// and = append(and, sq.GtOrEq{"start_time": arg.Start})
// }
// if !arg.End.IsZero() {
// and = append(and, sq.Lt{"start_time": arg.End})
// }
// where = append(where, and)
//
// return where
// }
//
// func (arg DahuaFileFilter) order() string {
// if arg.Ascending {
// return "start_time ASC"
// } else {
// return "start_time DESC"
// }
// }
//
// type ListDahuaFileParams struct {
// pagination.Page
// DahuaFileFilter
// }
//
// type ListDahuaFileResult struct {
// pagination.PageResult
// Data []DahuaFile
// }
//
// func (db DB) ListDahuaFile(ctx context.Context, arg ListDahuaFileParams) (ListDahuaFileResult, error) {
// where := arg.where(sq.And{})
// order := arg.order()
//
// var res []DahuaFile
// err := ssq.Query(ctx, db, &res, sq.
// Select("*").
// From("dahua_files").
// Where(where).
// OrderBy(order).
// Limit(uint64(arg.Page.Limit())).
// Offset(uint64(arg.Page.Offset())))
// if err != nil {
// return ListDahuaFileResult{}, err
// }
//
// var count int
// err = ssq.QueryOne(ctx, db, &count, sq.
// Select("COUNT(*)").
// From("dahua_files").
// Where(where))
// if err != nil {
// return ListDahuaFileResult{}, err
// }
//
// return ListDahuaFileResult{
// PageResult: arg.Page.Result(count),
// Data: res,
// }, nil
// }
//
// type CursorListDahuaFileParams struct {
// Cursor string
// PerPage int
// DahuaFileFilter
// }
//
// type CursorListDahuaFileResult struct {
// Cursor string
// HasMore bool
// Data []DahuaFile
// }
//
// func (db DB) CursorListDahuaFile(ctx context.Context, arg CursorListDahuaFileParams) (CursorListDahuaFileResult, error) {
// where := arg.where(sq.And{})
// if arg.Cursor != "" {
// b, err := base64.URLEncoding.DecodeString(arg.Cursor)
// if err != nil {
// return CursorListDahuaFileResult{}, err
// }
//
// var startTime types.Time
//
// err = startTime.UnmarshalBinary(b)
// if err != nil {
// return CursorListDahuaFileResult{}, err
// }
//
// if arg.Ascending {
// where = append(where, sq.GtOrEq{"start_time": startTime})
// } else {
// where = append(where, sq.LtOrEq{"start_time": startTime})
// }
// }
//
// order := arg.order()
// limit := arg.PerPage + 1
//
// var res []DahuaFile
// err := ssq.Query(ctx, db, &res, sq.
// Select("*").
// From("dahua_files").
// Where(where).
// OrderBy(order).
// Limit(uint64(limit)))
// if err != nil {
// return CursorListDahuaFileResult{}, err
// }
// length := len(res)
//
// if length == 0 || length != limit {
// return CursorListDahuaFileResult{
// Cursor: "",
// HasMore: false,
// Data: res,
// }, nil
// }
//
// data, last := res[:length-1], res[length-1]
//
// var cursor string
// {
// b, err := last.StartTime.MarshalBinary()
// if err != nil {
// return CursorListDahuaFileResult{}, nil
// }
//
// cursor = base64.URLEncoding.EncodeToString(b)
// }
//
// return CursorListDahuaFileResult{
// Cursor: cursor,
// HasMore: true,
// Data: data,
// }, nil
// }
//
//
// type ListDahuaDeviceByFeatureRow = listDahuaDeviceByFeatureRow
//
// func (db DB) ListDahuaDeviceByFeature(ctx context.Context, features ...models.DahuaFeature) ([]ListDahuaDeviceByFeatureRow, error) {
// var feature models.DahuaFeature
// for _, f := range features {
// feature = feature | f
// }
// return db.listDahuaDeviceByFeature(ctx, feature)
// }
//