device_item.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. "go.uber.org/zap"
  7. "sync"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "smart-site-management/errors"
  10. )
  11. type TDeviceTypeItem struct {
  12. ID int64 `gorm:"column:id" json:"id" form:"id"`
  13. DeviceType string `gorm:"column:device_type" json:"device_type" form:"device_type"`
  14. DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
  15. }
  16. func (TDeviceTypeItem) TableName() string {
  17. return "t_device_type_item"
  18. }
  19. var codeMap = map[int32]string{}
  20. var nameMap = map[string]int32{}
  21. var mux sync.Mutex
  22. func (p *TDeviceTypeItem) AllMap(db *gorm.DB) (map[int32]string, map[string]int32, error) {
  23. mux.Lock()
  24. defer mux.Unlock()
  25. if len(codeMap) > 0 && len(nameMap) > 0 {
  26. return codeMap, nameMap, nil
  27. }
  28. list := []TDeviceTypeItem{}
  29. ret1 := map[int32]string{}
  30. ret2 := map[string]int32{}
  31. err := db.Table(p.TableName()).Find(&list).Error
  32. if err != nil {
  33. logger.Error("mysql",
  34. zap.String("sql", "select * from t_device_type_item"),
  35. zap.String("error", err.Error()))
  36. return ret1, ret2, errors.DataBaseError
  37. }
  38. for _, v := range list {
  39. ret1[v.DeviceCode] = v.DeviceType
  40. ret2[v.DeviceType] = v.DeviceCode
  41. }
  42. codeMap = ret1
  43. nameMap = ret2
  44. return ret1, ret2, nil
  45. }