-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
321 lines (300 loc) · 8.54 KB
/
time.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package OrgTime
import (
"database/sql"
"errors"
"fmt"
CoreFilter "github.com/fotomxq/weeekj_core/v5/core/filter"
CoreSQL "github.com/fotomxq/weeekj_core/v5/core/sql"
CoreSQLPages "github.com/fotomxq/weeekj_core/v5/core/sql/pages"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
"github.com/lib/pq"
"time"
)
// ArgsGetList 获取列表参数
type ArgsGetList struct {
//分页
Pages CoreSQLPages.ArgsDataList `json:"pages"`
//组织ID
// 必填
OrgID int64 `json:"orgID" check:"id"`
//搜索
Search string `json:"search" check:"search" empty:"true"`
}
// GetList 获取列表
func GetList(args *ArgsGetList) (dataList []FieldsWorkTime, dataCount int64, err error) {
where := "org_id = :org_id"
maps := map[string]interface{}{
"org_id": args.OrgID,
}
if args.Search != "" {
where = where + " AND (name ILIKE '%' || :search || '%')"
maps["search"] = args.Search
}
var rawList []FieldsWorkTime
dataCount, err = CoreSQL.GetListPageAndCount(
Router2SystemConfig.MainDB.DB,
&rawList,
"org_work_time",
"id",
"SELECT id FROM org_work_time WHERE "+where,
where,
maps,
&args.Pages,
[]string{"id", "create_at", "update_at", "expire_at", "name"},
)
if err != nil {
return
}
for _, v := range rawList {
vData := getConfigByID(v.ID)
if vData.ID < 1 {
continue
}
dataList = append(dataList, vData)
}
return
}
// ArgsGetAllByBind 获取组织分组或成员参加的所有考勤安排参数
type ArgsGetAllByBind struct {
//组织分组
GroupIDs pq.Int64Array `json:"groupIDs" check:"ids"`
//组织成员
BindID int64 `json:"bindID" check:"id"`
}
// GetAllByBind 获取组织分组或成员参加的所有考勤安排
func GetAllByBind(args *ArgsGetAllByBind) (dataList []FieldsWorkTime, err error) {
var rawList []FieldsWorkTime
if err = Router2SystemConfig.MainDB.Select(&rawList, "SELECT id FROM org_work_time WHERE (expire_at > NOW() OR expire_at < to_timestamp(1000000)) AND ($1 && groups OR $2 = ANY(binds));", args.GroupIDs, args.BindID); err != nil {
return
}
if len(rawList) < 1 {
err = errors.New("no data")
return
}
for _, v := range rawList {
vData := getConfigByID(v.ID)
if vData.ID < 1 {
continue
}
dataList = append(dataList, vData)
}
return
}
// ArgsGetOne 获取某一个数据参数
type ArgsGetOne struct {
//ID
ID int64 `json:"id" check:"id"`
//组织ID
// 用于验证
OrgID int64 `json:"orgID" check:"id" empty:"true"`
}
// GetOne 获取某一个数据
func GetOne(args *ArgsGetOne) (data FieldsWorkTime, err error) {
data = getConfigByID(args.ID)
if data.ID < 1 || !CoreFilter.EqID2(args.OrgID, data.OrgID) {
err = errors.New("no data")
return
}
return
}
// ArgsCreate 创建新的数据参数
type ArgsCreate struct {
//过期时间
// 过期后自动失效,用于排临时班
ExpireAt time.Time `db:"expire_at" json:"expireAt" check:"isoTime" empty:"true"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id"`
//分组ID列
Groups pq.Int64Array `db:"groups" json:"groups" check:"ids" empty:"true"`
//绑定人列
Binds pq.Int64Array `db:"binds" json:"binds" check:"ids" empty:"true"`
//名称
Name string `db:"name" json:"name" check:"name"`
//时间配置组
Configs FieldsConfigs `db:"configs" json:"configs"`
//轮动任务
RotConfig FieldsConfigRot `db:"rot_config" json:"rotConfig"`
}
// Create 创建新的数据
func Create(args *ArgsCreate) (data FieldsWorkTime, err error) {
if args.OrgID < 1 {
err = errors.New("org id is error")
return
}
if err = checkMonth(args.Configs.Month); err != nil {
return
}
if err = checkMonthWeek(args.Configs.MonthWeek); err != nil {
return
}
if err = checkMonthDay(args.Configs.MonthDay); err != nil {
return
}
if err = checkWeek(args.Configs.Week); err != nil {
return
}
if err = checkTime(args.Configs.WorkTime); err != nil {
return
}
if err = checkTime(args.RotConfig.WorkTime); err != nil {
return
}
//写入数据
err = CoreSQL.CreateOneAndData(Router2SystemConfig.MainDB.DB, "org_work_time", "INSERT INTO org_work_time (expire_at, org_id, groups, binds, name, is_work, configs, rot_config) VALUES (:expire_at, :org_id, :groups, :binds, :name, false, :configs, :rot_config)", args, &data)
if err != nil {
return
}
//记录是否编辑过数据
needUpdateConfigMemData = true
//反馈
return
}
// ArgsUpdateByID 修改数据参数
type ArgsUpdateByID struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//过期时间
// 过期后自动失效,用于排临时班
ExpireAt time.Time `db:"expire_at" json:"expireAt" check:"isoTime" empty:"true"`
//分组ID列
Groups pq.Int64Array `db:"groups" json:"groups" check:"ids" empty:"true"`
//绑定人列
Binds pq.Int64Array `db:"binds" json:"binds" check:"ids" empty:"true"`
//名称
Name string `db:"name" json:"name" check:"name"`
//时间配置组
Configs FieldsConfigs `db:"configs" json:"configs"`
//轮动任务
RotConfig FieldsConfigRot `db:"rot_config" json:"rotConfig"`
}
// UpdateByID 修改数据
func UpdateByID(args *ArgsUpdateByID) (err error) {
if err = checkMonth(args.Configs.Month); err != nil {
return
}
if err = checkMonthWeek(args.Configs.MonthWeek); err != nil {
return
}
if err = checkMonthDay(args.Configs.MonthDay); err != nil {
return
}
if err = checkWeek(args.Configs.Week); err != nil {
return
}
if err = checkTime(args.Configs.WorkTime); err != nil {
return
}
if err = checkTime(args.RotConfig.WorkTime); err != nil {
return
}
var result sql.Result
result, err = CoreSQL.UpdateOne(Router2SystemConfig.MainDB.DB, "UPDATE org_work_time SET update_at = NOW(), expire_at = :expire_at, groups = :groups, binds = :binds, name = :name, configs = :configs, rot_config = :rot_config WHERE id = :id AND org_id = :org_id", args)
if err != nil {
return
}
aff, _ := result.RowsAffected()
if aff < 1 {
err = errors.New("aff is not exist")
return
}
//记录是否编辑过数据
needUpdateConfigMemData = true
//删除缓冲
deleteConfigCache(args.ID)
//反馈
return
}
// ArgsDeleteByID 删除数据参数
type ArgsDeleteByID struct {
//ID
ID int64 `db:"id" json:"id" check:"id"`
//组织ID
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
}
// DeleteByID 删除数据
func DeleteByID(args *ArgsDeleteByID) (err error) {
//执行操作
_, err = CoreSQL.DeleteAll(Router2SystemConfig.MainDB.DB, "org_work_time", "id = :id AND org_id = :org_id", args)
if err != nil {
return
}
//记录是否编辑过数据
needUpdateConfigMemData = true
//删除缓冲
deleteConfigCache(args.ID)
//反馈
return
}
// checkMonth 检查月份是否正确
func checkMonth(month []int) error {
for _, v := range month {
if v < 1 || v > 12 {
return errors.New(fmt.Sprint("month is error, month: ", v))
}
}
return nil
}
// checkMonthWeek 检查月份周
func checkMonthWeek(week []int) error {
for _, v := range week {
if v < 1 || v > 6 {
return errors.New(fmt.Sprint("month week is error, month week: ", v))
}
}
return nil
}
// checkMonthDay 检查月份天
func checkMonthDay(day []int) error {
for _, v := range day {
if v < 1 || v > 31 {
return errors.New(fmt.Sprint("month day is error, month day: ", v))
}
}
return nil
}
// checkWeek 检查周合法性
func checkWeek(week []int) error {
for _, v := range week {
if v < 0 || v > 7 {
return errors.New(fmt.Sprint("week is error, week: ", v))
}
}
return nil
}
// checkTime 检查工作时间的安排
func checkTime(data []FieldsWorkTimeTime) error {
for _, v := range data {
if v.StartHour < 0 || v.StartMinute < 0 || v.EndHour < 0 || v.EndMinute < 0 {
return errors.New("task time is error")
}
if v.StartHour > 24 || v.StartMinute > 60 || v.EndHour > 24 || v.EndMinute > 60 {
return errors.New("task time is error")
}
//if v.StartHour+v.StartMinute >= v.EndHour+v.EndMinute {
// return errors.New("task time is error")
//}
}
return nil
}
// 获取配置
func getConfigByID(id int64) (data FieldsWorkTime) {
cacheMark := getConfigCacheMark(id)
if err := Router2SystemConfig.MainCache.GetStruct(cacheMark, &data); err == nil && data.ID > 0 {
return
}
_ = Router2SystemConfig.MainDB.Get(&data, "SELECT id, create_at, update_at, expire_at, org_id, groups, binds, name, is_work, configs, rot_config FROM org_work_time WHERE id = $1", id)
if data.ID < 1 {
return
}
Router2SystemConfig.MainCache.SetStruct(cacheMark, data, 1800)
return
}
// 缓存
func getConfigCacheMark(id int64) string {
return fmt.Sprint("org:time:config:id:", id)
}
func deleteConfigCache(id int64) {
Router2SystemConfig.MainCache.DeleteMark(getConfigCacheMark(id))
}