...
|
...
|
@@ -11,6 +11,7 @@ import ( |
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
|
|
|
"strconv"
|
|
|
"sync"
|
...
|
...
|
@@ -29,6 +30,57 @@ func (deviceCollectionService *DeviceCollectionService) CreateDeviceCollection(c |
|
|
if err := createDeviceCollectionCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
newDeviceCollection := &domain.DeviceCollection{
|
|
|
//DeviceCollectionId: createDeviceCollectionCommand.DeviceCollectionId,
|
|
|
WorkShopName: createDeviceCollectionCommand.WorkShopName,
|
|
|
DeviceType: createDeviceCollectionCommand.DeviceType,
|
|
|
StartupStatus: createDeviceCollectionCommand.StartupStatus,
|
|
|
DeviceSn: createDeviceCollectionCommand.DeviceSn,
|
|
|
ComStatus: createDeviceCollectionCommand.ComStatus,
|
|
|
CollectionTime: createDeviceCollectionCommand.CollectionTime,
|
|
|
Values: createDeviceCollectionCommand.Values,
|
|
|
}
|
|
|
var deviceCollectionRepository domain.DeviceCollectionRepository
|
|
|
if value, err := factory.CreateDeviceCollectionRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
deviceCollectionRepository = value
|
|
|
}
|
|
|
if deviceCollection, err := deviceCollectionRepository.Save(newDeviceCollection); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
//err = domainService.SendWorkshopDeviceData(deviceCollection)
|
|
|
//if err != nil {
|
|
|
// log.Logger.Error("车间设备数据加入redis失败:" + err.Error())
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
//}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return map[string]interface{}{
|
|
|
"deviceCollection": deviceCollection,
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 创建
|
|
|
func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createDeviceCollectionCommand *command.CreateDeviceCollectionCommand) (interface{}, error) {
|
|
|
if err := createDeviceCollectionCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
newDeviceCollection := &domain.DeviceCollection{
|
|
|
//DeviceCollectionId: createDeviceCollectionCommand.DeviceCollectionId,
|
...
|
...
|
@@ -73,34 +125,31 @@ func (deviceCollectionService *DeviceCollectionService) CreateDeviceCollection(c |
|
|
} else {
|
|
|
deviceCollectionRepository = value
|
|
|
}
|
|
|
deviceCollection, err := deviceCollectionRepository.Save(newDeviceCollection)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
//处理设备数据
|
|
|
//计算区间的产能
|
|
|
//switch deviceCollection.DeviceType {
|
|
|
//case domain.DeviceTypeBaoXianJi, domain.DeviceTypeChuanChuanJi, domain.DeviceTypeFengKouJi, domain.DeviceTypeFengXiangJi:
|
|
|
if v, ok := deviceCollection.Values["count"]; ok {
|
|
|
curCount, errCurCount := strconv.Atoi(fmt.Sprintf("%v", v))
|
|
|
v, ok = lastDeviceCollectionRecord.Values["count"]
|
|
|
if ok {
|
|
|
lastCount, errLastCount := strconv.Atoi(fmt.Sprintf("%v", v))
|
|
|
if errLastCount == nil && errCurCount == nil && lastCount <= curCount {
|
|
|
deviceCollection.Values["count"] = curCount - lastCount
|
|
|
if v, ok := newDeviceCollection.Values["Count"]; ok {
|
|
|
newDeviceCollection.Values["total"] = v // 记录原始值
|
|
|
newDeviceCollection.Values["Count"] = 0
|
|
|
curCount, errCurCount := strconv.Atoi(utils.AssertString(v))
|
|
|
lastCount, errLastCount := strconv.Atoi(utils.AssertString(lastDeviceCollectionRecord.Values["Count"]))
|
|
|
if errLastCount == nil && errCurCount == nil && lastCount <= curCount {
|
|
|
if lastCount <= curCount {
|
|
|
newDeviceCollection.Values["Count"] = curCount - lastCount
|
|
|
} else {
|
|
|
deviceCollection.Values["count"] = 0
|
|
|
/*
|
|
|
设备统计的数量超过一定范围会重置为0,特殊处理0操作
|
|
|
*/
|
|
|
newDeviceCollection.Values["Count"] = 0
|
|
|
/*设备统计的数量超过一定范围会重置为0,特殊处理0操作*/
|
|
|
if lastCount > 10000000 && curCount < 1000 {
|
|
|
deviceCollection.Values["count"] = curCount
|
|
|
newDeviceCollection.Values["Count"] = curCount
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
deviceCollection.Values["count"] = 0
|
|
|
}
|
|
|
}
|
|
|
deviceCollection, err := deviceCollectionRepository.Save(newDeviceCollection)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
// break
|
|
|
//}
|
|
|
err = domainService.SendWorkshopDeviceData(deviceCollection)
|
...
|
...
|
|