123456789101112131415161718192021222324252627282930313233343536373839404142 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package model
- import (
- "github.com/jinzhu/gorm"
- "sync"
- )
- 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{}
- result := db.Table(p.TableName()).Find(&list)
- ret1 := map[int32]string{}
- ret2 := map[string]int32{}
- for _, v := range list {
- ret1[v.DeviceCode] = v.DeviceType
- ret2[v.DeviceType] = v.DeviceCode
- }
- codeMap = ret1
- nameMap = ret2
- return ret1, ret2, result.Error
- }
|