device_daily_running_record.go
3.6 KB
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
package redis
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"time"
)
// 获取每日设备运行数据
func GetDeviceDailyRunningRecord(t time.Time, deviceCode string) (*domain.DeviceDailyRunningRecord, error) {
client := GetRedis()
key := DeviceDailyRunningRecordKey(t, deviceCode)
record, err := getDeviceDailyRunningRecord(client, key)
//log.Logger.Debug(fmt.Sprintf("Redis Device:%v GET Key:%v Value:%v", deviceCode, key, record))
return record, err
}
func getDeviceDailyRunningRecord(client *redis.Client, key string) (*domain.DeviceDailyRunningRecord, error) {
result := client.Get(key)
data, err := result.Bytes()
if err == redis.Nil {
return nil, domain.ErrorNotFound
}
var record = &domain.DeviceDailyRunningRecord{}
record.RedisKey = key
if err = json.Unmarshal(data, record); err != nil {
return nil, err
}
return record, nil
}
// 保存每日设备运行数据
func SaveDeviceDailyRunningRecord(t time.Time, record *domain.DeviceDailyRunningRecord) error {
client := GetRedis()
key := DeviceDailyRunningRecordKey(t, record.DeviceCode)
recordData, err := json.Marshal(record)
result := client.Set(key, recordData, time.Hour*24*5)
_, err = result.Result()
return err
}
// 保存每日设备运行数据 - 按键值
func SaveDeviceDailyRunningRecordByKey(key string, record *domain.DeviceDailyRunningRecord) error {
client := GetRedis()
recordData, err := json.Marshal(record)
if err != nil {
return err
}
//log.Logger.Debug(fmt.Sprintf("Redis Device:%v SET Key:%v Value:%v", record.DeviceCode, key, record))
result := client.Set(key, recordData, time.Hour*24*5)
_, err = result.Result()
return err
}
// 保存每日设备运行数据
func RemoveDeviceDailyRunningRecord(t time.Time, deviceCode string) error {
client := GetRedis()
key := DeviceDailyRunningRecordKey(t, deviceCode)
result := client.Del(key)
_, err := result.Result()
return err
}
// 保存每日设备运行数据
func RemoveDeviceDailyRunningRecordByKey(key string) error {
client := GetRedis()
//key := DeviceDailyRunningRecordKey(t, deviceCode)
result := client.Del(key)
_, err := result.Result()
return err
}
func DeviceDailyRunningRecordKey(t time.Time, deviceCode string) string {
str := fmt.Sprintf("%v:device-daily-record:%v-%v:%v:%v", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, t.Local().Format("2006-01-02"), deviceCode)
return str
}
func DeviceDailyRunningRecordKeyByTimeStr(timeStr string, deviceCode string) string {
str := fmt.Sprintf("%v:device-daily-record:%v-%v:%v:%v", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, timeStr, deviceCode)
return str
}
// 获取设备每日所有数据记录
func GetDeviceDailyAllRecord(t time.Time) ([]*domain.DeviceDailyRunningRecord, error) {
client := GetRedis()
sliceResult := client.Keys(DeviceDailyAllRecordKey(t))
keys, err := sliceResult.Result()
var records = make([]*domain.DeviceDailyRunningRecord, 0)
if err != nil {
return nil, err
}
for _, v := range keys {
record, err := getDeviceDailyRunningRecord(client, v)
if err != nil {
return nil, err
}
record.RedisKey = v
records = append(records, record)
}
return records, nil
}
func DeviceDailyAllRecordKey(t time.Time) string {
str := fmt.Sprintf("%v:device-daily-record:%v-%v:%v:*", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, t.Format("2006-01-02"))
return str
}