computer.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package log
  2. import (
  3. "context"
  4. "log-server/global"
  5. )
  6. type Computer struct {
  7. Id uint `json:"id"`
  8. PcCode string `json:"pc_code"`
  9. User string `json:"user"`
  10. Status int `json:"status"`
  11. CreateTime string `json:"create_time"` // 创建时间
  12. UpdateTime string `json:"update_time"` // 更新时间
  13. Supplier string `json:"supplier"` //电脑供应商
  14. }
  15. func (Computer) TableName() string {
  16. return "computer"
  17. }
  18. const OnlinePcCode = "Online:PcCode"
  19. func (c *Computer) OnlinePcCodeCache() (mps map[string]string, err error) {
  20. ctx := context.Background()
  21. isNull, _ := global.GVA_REDIS.Exists(ctx, OnlinePcCode).Result()
  22. if isNull != 0 {
  23. mps, err = global.GVA_REDIS.HGetAll(ctx, OnlinePcCode).Result()
  24. return
  25. }
  26. var computers []*Computer
  27. err = global.GVA_DB.Where("status = ?", 1).Find(&computers).Error
  28. if err != nil {
  29. return
  30. }
  31. if len(computers) == 0 {
  32. return
  33. }
  34. var mp = map[string]string{}
  35. for _, pc := range computers {
  36. mp[pc.PcCode] = pc.User
  37. }
  38. err = global.GVA_REDIS.HSet(ctx, OnlinePcCode, mp).Err()
  39. if err != nil {
  40. return
  41. }
  42. return mp, err
  43. }
  44. // 更新单条缓存数据
  45. func (c *Computer) UpdateOnlinePcCodeCache(pcCode, user string) (err error) {
  46. ctx := context.Background()
  47. err = global.GVA_REDIS.HSet(ctx, OnlinePcCode, pcCode, user).Err()
  48. return
  49. }
  50. // 删除单条缓存数据
  51. func (c *Computer) DelOnlinePcCodeCache(pcCode string) (err error) {
  52. ctx := context.Background()
  53. err = global.GVA_REDIS.HDel(ctx, OnlinePcCode, pcCode).Err()
  54. return
  55. }
  56. // 清空缓存数据
  57. func (c *Computer) DelAllOnlinePcCodeCache() (err error) {
  58. ctx := context.Background()
  59. keys, _ := global.GVA_REDIS.HKeys(ctx, OnlinePcCode).Result()
  60. err = global.GVA_REDIS.HDel(ctx, OnlinePcCode, keys...).Err()
  61. return
  62. }
  63. func (c *Computer) ExistsPcCodeCache(pcCode string) bool {
  64. ctx := context.Background()
  65. b, _ := global.GVA_REDIS.HExists(ctx, OnlinePcCode, pcCode).Result()
  66. return b
  67. }