device_item.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.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 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. ret1[v.DeviceCode] = v.DeviceType
  31. ret2[v.DeviceType] = v.DeviceCode
  32. }
  33. codeMap = ret1
  34. nameMap = ret2
  35. return ret1, ret2, result.Error
  36. }