device.go
5.3 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
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
package domain
import (
"fmt"
"time"
)
// 风险等级
const (
RiskLevelHigh = 1 // 1:高
RiskLevelMiddle = 2 // 2:中
RiskLevelLow = 3 // 3:低
)
const (
DeviceStatusNormal = 1 // 1:正常
DeviceStatusArchive = 2 // 2:封存
DeviceStatusScrapped = 3 // 3:报废
)
var (
mapRiskLevel = map[string]int{
"高": RiskLevelHigh,
"中": RiskLevelMiddle,
"低": RiskLevelLow,
}
mapDeviceStatus = map[string]int{
"正常": DeviceStatusNormal,
"封存": DeviceStatusArchive,
"报废": DeviceStatusScrapped,
}
)
// 设备
type Device struct {
// 设备Id
DeviceId int `json:"deviceId,omitempty"`
// 企业id
CompanyId int `json:"companyId,omitempty"`
// 组织ID
OrgId int `json:"orgId,omitempty"`
// 设备编号
DeviceCode string `json:"deviceCode,omitempty"`
// 设备名称
DeviceName string `json:"deviceName,omitempty"`
// 设备型号
DeviceModel string `json:"deviceModel,omitempty"`
// 设备类型
DeviceType string `json:"deviceType,omitempty"`
// 品牌
Brand string `json:"brand,omitempty"`
// 设备状态 1:正常 2:封存 3:报废
DeviceStatus int `json:"deviceStatus,omitempty"`
// 风险等级 1:高 2:中 3:低
RiskLevel int `json:"riskLevel,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
// 删除时间
DeletedAt time.Time `json:"deletedAt,omitempty"`
// 所属位置
WorkStation *WorkStation `json:"workStation,omitempty"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
}
type DeviceRepository interface {
Save(device *Device) (*Device, error)
Remove(device *Device) (*Device, error)
FindOne(queryOptions map[string]interface{}) (*Device, error)
Find(queryOptions map[string]interface{}) (int64, []*Device, error)
}
func (device *Device) Identify() interface{} {
if device.DeviceId == 0 {
return nil
}
return device.DeviceId
}
func (device *Device) Update(data map[string]interface{}) error {
if deviceCode, ok := data["deviceCode"]; ok {
device.DeviceCode = deviceCode.(string)
}
if deviceName, ok := data["deviceName"]; ok {
device.DeviceName = deviceName.(string)
}
if deviceModel, ok := data["deviceModel"]; ok {
device.DeviceModel = deviceModel.(string)
}
if deviceType, ok := data["deviceType"]; ok {
device.DeviceType = deviceType.(string)
}
if brand, ok := data["brand"]; ok {
device.Brand = brand.(string)
}
if deviceStatus, ok := data["deviceStatus"]; ok {
device.DeviceStatus = deviceStatus.(int)
}
if riskLevel, ok := data["riskLevel"]; ok {
device.RiskLevel = riskLevel.(int)
}
if device.Ext == nil {
device.Ext = &Ext{}
}
if device.Ext != nil && device.Ext.DeviceExt == nil {
device.Ext.DeviceExt = &DeviceExt{}
}
if orgName, ok := data["orgName"]; ok {
device.Ext.OrgName = orgName.(string)
}
if unitProductionSecTime, ok := data["unitProductionSecTime"]; ok {
device.Ext.DeviceExt.UnitProductionSecTime = unitProductionSecTime.(int)
}
device.UpdatedAt = time.Now()
return nil
}
func (device *Device) Valid() error {
if !(device.RiskLevel == RiskLevelHigh || device.RiskLevel == RiskLevelMiddle || device.RiskLevel == RiskLevelLow) {
return fmt.Errorf("风险等级参数有误")
}
if !(device.DeviceStatus == DeviceStatusNormal || device.DeviceStatus == DeviceStatusArchive || device.DeviceStatus == DeviceStatusScrapped) {
return fmt.Errorf("设备状态参数有误")
}
return nil
}
// 标记为生产设备
func (device *Device) MarkAsProductDevice() bool {
if device.Ext.DeviceExt.IsProductDevice == 1 {
return false
}
device.Ext.DeviceExt.IsProductDevice = 1
device.UpdatedAt = time.Now()
return true
}
// 导入设备数据体
type ImportDeviceItem struct {
// 设备编号
DeviceCode string `json:"deviceCode,omitempty"`
// 设备名称
DeviceName string `json:"deviceName,omitempty"`
// 设备型号
DeviceModel string `json:"deviceModel,omitempty"`
// 设备类型
DeviceType string `json:"deviceType,omitempty"`
// 品牌
Brand string `json:"brand,omitempty"`
// 设备状态 1:正常 2:封存 3:报废
Status string `json:"status,omitempty"`
// 风险等级 1:高 2:中 3:低
Level string `json:"level,omitempty"`
// 失败理由
FailReason string `json:"failReason"`
// 设备状态 1:正常 2:封存 3:报废
DeviceStatus int `json:"-"`
// 风险等级 1:高 2:中 3:低
RiskLevel int `json:"-"`
}
func (item *ImportDeviceItem) Valid() error {
if len(item.DeviceCode) == 0 {
return fmt.Errorf("设备编号不能为空")
}
if len(item.DeviceName) == 0 {
return fmt.Errorf("设备名称不能为空")
}
if len(item.DeviceModel) == 0 {
return fmt.Errorf("设备型号不能为空")
}
if len(item.DeviceType) == 0 {
return fmt.Errorf("设备类型不能为空")
}
if len(item.Status) == 0 {
return fmt.Errorf("设备状态不能为空")
}
if len(item.Level) == 0 {
return fmt.Errorf("风险等级不能为空")
}
if v, ok := mapDeviceStatus[item.Status]; !ok {
return fmt.Errorf("设备状态格式有误")
} else {
item.DeviceStatus = v
}
if v, ok := mapRiskLevel[item.Level]; !ok {
return fmt.Errorf("风险等级格式有误")
} else {
item.RiskLevel = v
}
return nil
}
type Devices []*Device
func (devices Devices) ToMap() map[string]*Device {
var resp = make(map[string]*Device)
for _, v := range devices {
resp[v.DeviceCode] = v
}
return resp
}