12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- // Copyright 2019 githup.com. All rights reserved.
- // Use of this source code is governed by githup.com.
- package model
- import (
- "github.com/jinzhu/gorm"
- "go.uber.org/zap"
- "sync"
- "github.com/jaryhe/gopkgs/logger"
- "smart-site-management/errors"
- )
- type TDeviceTypeItem struct {
- ID int64 `gorm:"column:id" json:"id" form:"id"`
- DeviceType string `gorm:"column:device_type" json:"device_type" form:"device_type"`
- DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
- }
- func (TDeviceTypeItem) TableName() string {
- return "t_device_type_item"
- }
- var codeMap = map[int32]string{}
- var nameMap = map[string]int32{}
- var mux sync.Mutex
- func (p *TDeviceTypeItem) AllMap(db *gorm.DB) (map[int32]string, map[string]int32, error) {
- mux.Lock()
- defer mux.Unlock()
- if len(codeMap) > 0 && len(nameMap) > 0 {
- return codeMap, nameMap, nil
- }
- list := []TDeviceTypeItem{}
- ret1 := map[int32]string{}
- ret2 := map[string]int32{}
- err := db.Table(p.TableName()).Find(&list).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "select * from t_device_type_item"),
- zap.String("error", err.Error()))
- return ret1, ret2, errors.DataBaseError
- }
- for _, v := range list {
- ret1[v.DeviceCode] = v.DeviceType
- ret2[v.DeviceType] = v.DeviceCode
- }
- codeMap = ret1
- nameMap = ret2
- return ret1, ret2, nil
- }
|