device_item.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package model
  4. import (
  5. "github.com/jinzhu/gorm"
  6. "sync"
  7. )
  8. type TDeviceTypeItem struct {
  9. ID int64 `gorm:"column:ID" json:"id" form:"id"`
  10. DeviceType string `gorm:"column:DeviceType" json:"device_type" form:"device_type"`
  11. DeviceCode int32 `gorm:"column:DeviceCode" json:"device_code" form:"device_code"`
  12. }
  13. func (TDeviceTypeItem) TableName() string {
  14. return "DeviceTypeItem"
  15. }
  16. var codeMap = map[int32]string{}
  17. var nameMap = map[string]int32{}
  18. var mux sync.Mutex
  19. func (p *TDeviceTypeItem) AllMap(db *gorm.DB) (map[int32]string, map[string]int32, error) {
  20. mux.Lock()
  21. defer mux.Unlock()
  22. if len(codeMap) > 0 && len(nameMap) > 0 {
  23. return codeMap, nameMap, nil
  24. }
  25. list := []TDeviceTypeItem{}
  26. result := db.Table(p.TableName()).Find(&list)
  27. ret1 := map[int32]string{}
  28. ret2 := map[string]int32{}
  29. for _, v := range list {
  30. if v.DeviceCode == DeviceTypeDust ||
  31. v.DeviceCode == DeviceTypeVedio ||
  32. v.DeviceCode == DeviceTypeAttendance ||
  33. v.DeviceCode == DeviceTypeTower ||
  34. v.DeviceCode == DeviceTypeLift {
  35. ret1[v.DeviceCode] = v.DeviceType
  36. ret2[v.DeviceType] = v.DeviceCode
  37. }
  38. }
  39. codeMap = ret1
  40. nameMap = ret2
  41. return ret1, ret2, result.Error
  42. }