You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.8 KiB

package common_models
import (
"encoding/json"
"log"
)
type Factor struct {
Id int `json:"id"`
Name string `json:"name"`
ProtoCode string `json:"protoCode"`
ProtoName string `json:"protoName"`
Items []ProtoItem `json:"items"`
Units map[string]string `json:"units"`
}
// redis序列化
func (f *Factor) MarshalBinary() (data []byte, err error) {
return json.Marshal(f)
}
// redis序列化
func (f *Factor) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, f)
}
type ProtoItem struct {
Id int `json:"id"`
Name string `json:"name"`
FieldName string `json:"field_name"`
UnitName string `json:"unit_name"`
Precision int `json:"precision"`
}
type Proto struct {
Code string `json:"code"`
Name string `json:"name"`
Items []ProtoItem `json:"items"`
}
func (f *Factor) GetProtoItem(fieldName string) *ProtoItem {
for _, item := range f.Items {
if item.FieldName == fieldName {
return &item
}
}
log.Printf("无匹配字段 %s 的ProtoItem", fieldName)
return &ProtoItem{}
}
// GetFieldNames 获取监测原型监测项
func (f *Factor) GetFieldNames() []string {
var names []string
for _, protoItem := range f.Items {
names = append(names, protoItem.FieldName)
}
return names
}
func (p *Proto) GetProtoItem(fieldName string) *ProtoItem {
if fieldName == "" {
return &ProtoItem{}
}
for _, item := range p.Items {
if item.FieldName == fieldName {
return &item
}
}
log.Printf("无匹配字段 %s 的ProtoItem", fieldName)
return &ProtoItem{}
}
// redis序列化
func (p *Proto) MarshalBinary() (data []byte, err error) {
return json.Marshal(p)
}
// redis序列化
func (p *Proto) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, p)
}