作者 yangfu

fix: 非法数据过滤(启动、通讯异常数据 、当次生产数据异常(超10000))

@@ -122,6 +122,7 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD @@ -122,6 +122,7 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
122 } 122 }
123 123
124 //计算区间的产能 124 //计算区间的产能
  125 +
125 if v, ok := newDeviceCollection.Values["Count"]; ok { 126 if v, ok := newDeviceCollection.Values["Count"]; ok {
126 newDeviceCollection.Values["total"] = v // 记录原始值 127 newDeviceCollection.Values["total"] = v // 记录原始值
127 newDeviceCollection.Values["Count"] = 0 128 newDeviceCollection.Values["Count"] = 0
@@ -142,6 +143,7 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD @@ -142,6 +143,7 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
142 newDeviceCollection.ProductCount = count 143 newDeviceCollection.ProductCount = count
143 } 144 }
144 } 145 }
  146 + _, valErr := newDeviceCollection.Valid()
145 // TODO:测试假数据,后期注释掉 147 // TODO:测试假数据,后期注释掉
146 //if createDeviceCollectionCommand.DeviceType == domain.DeviceTypeChuanChuanJi { 148 //if createDeviceCollectionCommand.DeviceType == domain.DeviceTypeChuanChuanJi {
147 // newDeviceCollection.Values["Count"] = rand.Intn(300) 149 // newDeviceCollection.Values["Count"] = rand.Intn(300)
@@ -150,7 +152,9 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD @@ -150,7 +152,9 @@ func (deviceCollectionService *DeviceCollectionService) DeviceCollection(createD
150 if err != nil { 152 if err != nil {
151 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 153 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
152 } 154 }
153 - err = domainService.SendWorkshopDeviceData(deviceCollection) 155 + if valErr == nil {
  156 + err = domainService.SendWorkshopDeviceData(deviceCollection)
  157 + }
154 if err != nil { 158 if err != nil {
155 log.Logger.Error("车间设备数据加入redis失败:" + err.Error()) 159 log.Logger.Error("车间设备数据加入redis失败:" + err.Error())
156 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 160 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -97,3 +97,21 @@ func (deviceCollection *DeviceCollection) Update(data map[string]interface{}) er @@ -97,3 +97,21 @@ func (deviceCollection *DeviceCollection) Update(data map[string]interface{}) er
97 func TaskDeviceCollection() string { 97 func TaskDeviceCollection() string {
98 return fmt.Sprintf("%v:task:device-collection:report", constant.CACHE_PREFIX) 98 return fmt.Sprintf("%v:task:device-collection:report", constant.CACHE_PREFIX)
99 } 99 }
  100 +
  101 +func (data *DeviceCollection) Valid() (bool, error) {
  102 + var (
  103 + result = false
  104 + )
  105 + if data.ProductCount > DeviceMaxSingleProductCount {
  106 + data.ProductCount = 0
  107 + data.Values["Count"] = 0
  108 + return result, fmt.Errorf("设备数据异常: 生产数量超过:%v", DeviceMaxSingleProductCount)
  109 + }
  110 + if data.StartupStatus == 0 {
  111 + return result, fmt.Errorf("设备数据异常: 启动0")
  112 + }
  113 + if data.ComStatus == 0 {
  114 + return result, fmt.Errorf("设备数据异常: 通讯0")
  115 + }
  116 + return true, nil
  117 +}
@@ -53,14 +53,18 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) Update(data map[string @@ -53,14 +53,18 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) Update(data map[string
53 return nil 53 return nil
54 } 54 }
55 55
56 -func (deviceDailyRunningRecord *DeviceDailyRunningRecord) AddDeviceRunningData(t time.Time, data *DeviceRunningData) { 56 +func (deviceDailyRunningRecord *DeviceDailyRunningRecord) AddDeviceRunningData(t time.Time, data *DeviceRunningData) bool {
57 deviceDailyRunningRecord.DeviceRunningRecordInfo.AddDeviceRunningData(t, data) 57 deviceDailyRunningRecord.DeviceRunningRecordInfo.AddDeviceRunningData(t, data)
58 now := time.Now().Unix() 58 now := time.Now().Unix()
  59 + if ok, _ := data.Valid(); !ok {
  60 + return false
  61 + }
59 if t.Unix() > (now-DefaultCollectionTimeSpan) && t.Unix() < (now+DefaultCollectionTimeSpan) { 62 if t.Unix() > (now-DefaultCollectionTimeSpan) && t.Unix() < (now+DefaultCollectionTimeSpan) {
60 deviceDailyRunningRecord.UpdatedAt = t 63 deviceDailyRunningRecord.UpdatedAt = t
61 - return 64 + return false
62 } 65 }
63 deviceDailyRunningRecord.UpdatedAt = time.Now() 66 deviceDailyRunningRecord.UpdatedAt = time.Now()
  67 + return true
64 } 68 }
65 69
66 func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string { 70 func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string {
1 package domain 1 package domain
2 2
3 -import "time" 3 +import (
  4 + "fmt"
  5 + "time"
  6 +)
  7 +
  8 +const DeviceMaxSingleProductCount = 10000
4 9
5 // 设备运行数据 10 // 设备运行数据
6 type DeviceRunningData struct { 11 type DeviceRunningData struct {
@@ -41,3 +46,20 @@ type DeviceRunningData struct { @@ -41,3 +46,20 @@ type DeviceRunningData struct {
41 // 单位数据 比如:1串/0.1kg weight = count * unitQuantity 46 // 单位数据 比如:1串/0.1kg weight = count * unitQuantity
42 UnitQuantity float64 `json:"unitQuantity"` 47 UnitQuantity float64 `json:"unitQuantity"`
43 } 48 }
  49 +
  50 +func (data *DeviceRunningData) Valid() (bool, error) {
  51 + var (
  52 + result = false
  53 + )
  54 + if data.Count > DeviceMaxSingleProductCount {
  55 + data.Count = 0
  56 + return result, fmt.Errorf("设备数据异常: 生产数量超过:%v", DeviceMaxSingleProductCount)
  57 + }
  58 + if data.StartupStatus == 0 {
  59 + return result, fmt.Errorf("设备数据异常: 启动0")
  60 + }
  61 + if data.ComStatus == 0 {
  62 + return result, fmt.Errorf("设备数据异常: 通讯0")
  63 + }
  64 + return true, nil
  65 +}
@@ -92,19 +92,20 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d @@ -92,19 +92,20 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
92 if _, err = deviceRunningRecordRepository.Save(deviceRunningRecord); err != nil { 92 if _, err = deviceRunningRecordRepository.Save(deviceRunningRecord); err != nil {
93 return nil, err 93 return nil, err
94 } 94 }
95 - // 2.保存设备生产记录 (统计车间、员工产能) 批次跟数量不为空  
96 - if record.DeviceType == domain.DeviceTypeChuanChuanJi && plan != nil && deviceRunningData.Count > 0 {  
97 95
98 - productRecord, _ := ptr.newProductRecord(companyId, orgId, workStation, device, deviceRunningData, plan)  
99 - //SendProductRecordStaticsJob(productRecord) 96 + // 2.更新 设备每日运行记录(汇总) - redis更新 十分钟异步刷库
  97 + if addSuccess := deviceDailyRecord.AddDeviceRunningData(deviceRunningData.CollectionTime, deviceRunningData); addSuccess {
  98 + // 3.保存设备生产记录 (统计车间、员工产能) 批次跟数量不为空
  99 + if record.DeviceType == domain.DeviceTypeChuanChuanJi && plan != nil && deviceRunningData.Count > 0 {
100 100
101 - productRecordService, _ := NewPGProductRecordService(ptr.transactionContext)  
102 - productRecordService.EmployeeProductStatics(productRecord)  
103 - productRecordService.WorkshopProductStatics(productRecord)  
104 - } 101 + productRecord, _ := ptr.newProductRecord(companyId, orgId, workStation, device, deviceRunningData, plan)
  102 + //SendProductRecordStaticsJob(productRecord)
105 103
106 - // 3.更新 设备每日运行记录(汇总) - redis更新 十分钟异步刷库  
107 - deviceDailyRecord.AddDeviceRunningData(deviceRunningData.CollectionTime, deviceRunningData) 104 + productRecordService, _ := NewPGProductRecordService(ptr.transactionContext)
  105 + productRecordService.EmployeeProductStatics(productRecord)
  106 + productRecordService.WorkshopProductStatics(productRecord)
  107 + }
  108 + }
108 if err = redis.SaveDeviceDailyRunningRecord(deviceDailyRecord); err != nil { 109 if err = redis.SaveDeviceDailyRunningRecord(deviceDailyRecord); err != nil {
109 //log.Logger.Error(err.Error()) 110 //log.Logger.Error(err.Error())
110 return nil, err 111 return nil, err