Quellcode durchsuchen

电脑状态更新

wangbin vor 2 Jahren
Ursprung
Commit
4aef643396
3 geänderte Dateien mit 77 neuen und 0 gelöschten Zeilen
  1. 17 0
      model/log/computer_status.go
  2. 36 0
      service/log/log_statistics.go
  3. 24 0
      service/log/loging/logical_log.go

+ 17 - 0
model/log/computer_status.go

@@ -0,0 +1,17 @@
+package log
+
+import (
+	"time"
+)
+
+type ComputerStatus struct {
+	Id         uint      `json:"id"`
+	PcCode     string    `json:"pc_code"`
+	Status     int       `json:"status"`
+	UpdateTime time.Time `json:"update_time"` // 更新时间
+	CreateDate time.Time `json:"create_date"`
+}
+
+func (ComputerStatus) TableName() string {
+	return "computer_status"
+}

+ 36 - 0
service/log/log_statistics.go

@@ -1663,3 +1663,39 @@ func (s *ServiceStatisticsLog) GetDeviceContrastInfo(ctx context.Context, api lo
 	}
 	return
 }
+
+func (s *ServiceStatisticsLog) ComputerUpdateStatus() {
+	var computer log.Computer
+	logComputers, err := computer.OnlinePcCodeCache()
+	if err != nil {
+		global.GVA_LOG.Error("ComputerUpdateStatus LogComputer 获取失败!", zap.Error(err))
+		return
+	}
+	date := s.CurrentDate()
+	ctx := context.Background()
+	for pcCode, _ := range logComputers {
+		// 检测5分钟内电脑是否上报了数据
+		num := s.LogicalLog.CheckPcReportingLog(ctx, pcCode, 60*1000*8)
+		status := -1
+		if num != 0 {
+			status = 1
+		}
+		var computerStatus log.ComputerStatus
+		result := global.GVA_DB.Where("create_date = ?", date).Where("pc_code = ?", pcCode).First(&computerStatus)
+		if result.Error != nil {
+			if result.Error == gorm.ErrRecordNotFound {
+				// 数据不存在,执行创建操作
+				computerStatus.Status = status
+				computerStatus.CreateDate = time.Now()
+				computerStatus.PcCode = pcCode
+				result = global.GVA_DB.Create(&computerStatus)
+			} else {
+				// 其他错误
+				global.GVA_LOG.Error("ComputerUpdateStatus computerStatus 获取失败!", zap.Error(err))
+				return
+			}
+		} else {
+			global.GVA_DB.Model(&log.ComputerStatus{}).Where("create_date = ?", date).Where("pc_code = ?", pcCode).Update("status", status)
+		}
+	}
+}

+ 24 - 0
service/log/loging/logical_log.go

@@ -783,6 +783,30 @@ func (s *LogicalLog) GetPcReportingLog(ctx context.Context, pcCode string) (num
 	return
 }
 
+// 获取电脑上报的次数
+func (s *LogicalLog) CheckPcReportingLog(ctx context.Context, pcCode string, timeInterval int64) (num int) {
+	key := fmt.Sprintf(PcReportingLog, s.CurrentDate(), pcCode)
+	z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result()
+	if err != nil {
+		return
+	}
+	if len(z) == 0 {
+		return
+	}
+	end := time.Now().UnixNano()/1e6 + 60*1000*2
+	start := end - timeInterval
+	op := redis.ZRangeBy{
+		Min: strconv.Itoa(int(start)),
+		Max: strconv.Itoa(int(end)),
+	}
+	i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result()
+	if err != nil {
+		return
+	}
+	num = len(i)
+	return
+}
+
 // 添加电脑定时上报记录
 func (s *LogicalLog) SetPcReportingLog(ctx context.Context, pcCode string, operator string) {
 	key := fmt.Sprintf(PcReportingLog, s.CurrentDate(), pcCode)