device_item.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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:device_type" json:"device_type" form:"device_type"`
  11. DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
  12. }
  13. func (TDeviceTypeItem) TableName() string {
  14. return "t_device_type_item"
  15. }
  16. var codeMap = map[int32]string{}
  17. var nameMap = map[string]int32{}
  18. var idMap = map[int32]int64{}
  19. var mux sync.Mutex
  20. func (p *TDeviceTypeItem) AllMap(db *gorm.DB) (map[int32]string, map[string]int32, error) {
  21. mux.Lock()
  22. defer mux.Unlock()
  23. if len(codeMap) > 0 && len(nameMap) > 0 {
  24. return codeMap, nameMap, nil
  25. }
  26. list := []TDeviceTypeItem{}
  27. result := db.Table(p.TableName()).Find(&list)
  28. ret1 := map[int32]string{}
  29. ret2 := map[string]int32{}
  30. ret3 := map[int32]int64{}
  31. for _, v := range list {
  32. ret1[v.DeviceCode] = v.DeviceType
  33. ret2[v.DeviceType] = v.DeviceCode
  34. ret3[v.DeviceCode] = v.ID
  35. }
  36. codeMap = ret1
  37. nameMap = ret2
  38. idMap = ret3
  39. return ret1, ret2, result.Error
  40. }
  41. func GetDeviceIdMap() map[int32]int64 {
  42. return idMap
  43. }