regular_task.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package task
  2. import (
  3. "context"
  4. "go.uber.org/zap"
  5. "log-server/global"
  6. "log-server/model/rentComputer"
  7. )
  8. type ServiceRegularTask struct {
  9. }
  10. const OnlinePcCode = "Online:PcCode"
  11. func (s *ServiceRegularTask) OnlinePcCodeUpdateCache() {
  12. ctx := context.Background()
  13. isNull, err := global.GVA_REDIS.Exists(ctx, OnlinePcCode).Result()
  14. mps := map[string]string{}
  15. if isNull != 0 {
  16. mps, err = global.GVA_REDIS.HGetAll(ctx, OnlinePcCode).Result()
  17. if err != nil {
  18. global.GVA_LOG.Error("OnlinePcCodeUpdateCache获取表失败", zap.Error(err))
  19. return
  20. }
  21. }
  22. var computers []*rentComputer.RentComputer
  23. err = global.GVA_DB.Where("is_off_shelf = ?", 0).Find(&computers).Error
  24. if err != nil {
  25. global.GVA_LOG.Error("OnlinePcCodeUpdateCache获取表失败", zap.Error(err))
  26. return
  27. }
  28. if len(computers) == 0 {
  29. return
  30. }
  31. var mp = map[string]string{}
  32. for _, pc := range computers {
  33. mp[pc.PcNum] = pc.DirectorName
  34. if _, ok := mps[pc.PcNum]; ok {
  35. delete(mps, pc.PcNum)
  36. }
  37. }
  38. err = global.GVA_REDIS.HSet(ctx, OnlinePcCode, mp).Err()
  39. if err != nil {
  40. global.GVA_LOG.Error("OnlinePcCodeUpdateCache更新缓存失败", zap.Error(err))
  41. return
  42. }
  43. if mps != nil {
  44. for code, _ := range mps {
  45. global.GVA_REDIS.HDel(ctx, OnlinePcCode, code)
  46. }
  47. }
  48. return
  49. }