device_item.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 || v.DeviceCode == DeviceTypeVedio || v.DeviceCode == DeviceTypeAttendance{
  31. ret1[v.DeviceCode] = v.DeviceType
  32. ret2[v.DeviceType] = v.DeviceCode
  33. }
  34. }
  35. codeMap = ret1
  36. nameMap = ret2
  37. return ret1, ret2, result.Error
  38. }