-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstance.go
78 lines (71 loc) · 1.69 KB
/
Instance.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
package main
import (
"fmt"
"log"
"os"
"wobuzaixiaoyuan/pkg/common"
"wobuzaixiaoyuan/pkg/database"
"wobuzaixiaoyuan/pkg/wzxy"
"github.com/jinzhu/copier"
)
type Instance struct {
EventMap map[string]*struct {
Count int // <= 2
Status int // 0: success, 1: failed
}
Users []database.User
}
func NewInstance() (*Instance, error) {
users, err := database.GetUsers()
if err != nil {
return nil, err
}
return &Instance{
Users: users,
EventMap: make(map[string]*struct {
Count int // <= 2
Status int // 0: success, 1: failed
}),
}, nil
}
func (i *Instance) UpdateData() {
log.Printf("更新用户数据")
dataTmp, err := database.GetUsers()
if err != nil {
fmt.Println(err)
} else {
i.Users = dataTmp
}
}
func (i *Instance) CheckInTask() {
var checkinUsers []*wzxy.User
for _, user := range i.Users {
// 如果当前时间在用户设置的时间范围内,并且用户今天还没有打过卡
if !common.CompareTime(user.Start) && common.CompareTime(user.End) && i.EventMap[user.RealName].Count < 2 && i.EventMap[user.RealName].Status == 0 {
wzxyUser := &wzxy.User{}
copier.Copy(&wzxyUser, &user)
checkinUsers = append(checkinUsers, wzxyUser)
i.EventMap[user.RealName].Count += 1
}
}
if len(checkinUsers) == 0 {
log.SetOutput(os.Stdout)
log.Printf("没有需要打卡的用户")
}
if len(checkinUsers) != 0 {
res := wzxy.DoWork(checkinUsers)
for _, result := range res {
if result.Status == 0 {
i.EventMap[result.RealName].Status = 0
} else {
i.EventMap[result.RealName].Status = 1
}
}
}
}
func (i *Instance) ResetEventMap() {
log.Printf("重置打卡次数")
for _, user := range i.Users {
i.EventMap[user.RealName].Count = 0
}
}