作者 yangfu

feat:设备源数据增加字段

... ... @@ -85,6 +85,7 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
ComStatus: createDeviceCollectionCommand.ComStatus,
CollectionTime: createDeviceCollectionCommand.CollectionTime,
Values: createDeviceCollectionCommand.Values,
LatestUpdateTime: time.Now(),
}
var lastDeviceCollectionRecord = &domain.DeviceCollection{}
var err error
... ... @@ -126,16 +127,19 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
newDeviceCollection.Values["Count"] = 0
curCount, errCurCount := strconv.Atoi(utils.AssertString(v))
lastCount, errLastCount := strconv.Atoi(utils.AssertString(lastDeviceCollectionRecord.Values["Count"]))
var count int
if errLastCount == nil && errCurCount == nil && lastCount <= curCount {
if lastCount <= curCount {
newDeviceCollection.Values["Count"] = curCount - lastCount
count = curCount - lastCount
} else {
newDeviceCollection.Values["Count"] = 0
count = 0
/*设备统计的数量超过一定范围会重置为0,特殊处理0操作*/
if lastCount > 10000000 && curCount < 1000 {
newDeviceCollection.Values["Count"] = curCount
count = curCount
}
}
newDeviceCollection.Values["Count"] = count
newDeviceCollection.Count = count
}
}
// TODO:测试假数据,后期注释掉
... ...
//+build !local
//go:build !local
// +build !local
package constant
... ...
... ... @@ -24,6 +24,10 @@ type DeviceCollection struct {
ComStatus int64 `json:"comStatus"`
// 设备数据值
Values map[string]interface{} `json:"values"`
// 数量(字库使用)
Count int `json:"count"`
// 最后跟新时间(字库使用)
LatestUpdateTime time.Time `json:"latest_update_time"`
}
var (
... ...
... ... @@ -26,4 +26,8 @@ type DeviceCollection struct {
UpdatedAt time.Time `comment:"更新时间" pg:",default:now()"`
// 删除时间
DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"`
// 数量(字库使用)
Count int `comment:"数量" json:"count"`
// 最后更新时间(字库使用)
LatestUpdateTime time.Time `comment:"最后更新时间" json:"latest_update_time"`
}
... ...
... ... @@ -15,5 +15,7 @@ func TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel *m
CollectionTime: deviceCollectionModel.CreatedAt,
ComStatus: deviceCollectionModel.ComStatus,
Values: deviceCollectionModel.Values,
Count: deviceCollectionModel.Count,
LatestUpdateTime: deviceCollectionModel.LatestUpdateTime,
}, nil
}
... ...
... ... @@ -35,6 +35,8 @@ func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.Devi
"startup_status",
"com_status",
"values",
"count",
"latest_update_time",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
... ... @@ -59,6 +61,8 @@ func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.Devi
&deviceCollection.StartupStatus,
&deviceCollection.ComStatus,
&deviceCollection.Values,
&deviceCollection.Count,
&deviceCollection.LatestUpdateTime,
),
fmt.Sprintf("INSERT INTO manufacture.device_collections (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
deviceCollection.DeviceCollectionId,
... ... @@ -69,6 +73,8 @@ func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.Devi
deviceCollection.StartupStatus,
deviceCollection.ComStatus,
deviceCollection.Values,
deviceCollection.Count,
deviceCollection.LatestUpdateTime,
); err != nil {
return deviceCollection, err
}
... ... @@ -83,6 +89,8 @@ func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.Devi
&deviceCollection.StartupStatus,
&deviceCollection.ComStatus,
&deviceCollection.Values,
&deviceCollection.Count,
&deviceCollection.LatestUpdateTime,
),
fmt.Sprintf("UPDATE manufacture.device_collections SET %s WHERE device_collection_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
//deviceCollection.DeviceCollectionId,
... ... @@ -93,6 +101,8 @@ func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.Devi
deviceCollection.StartupStatus,
deviceCollection.ComStatus,
deviceCollection.Values,
deviceCollection.Count,
deviceCollection.LatestUpdateTime,
deviceCollection.Identify(),
); err != nil {
return deviceCollection, err
... ...