package service import ( "fmt" "iot_manager_service/app/system/dao" "iot_manager_service/util" "strconv" ) // 字典管理服务 var DictService = new(dictService) type dictService struct{} const ( ControlTypeKey = "%d:dict:control_type:%d" ControlType = "control_type" ModuleType = "module_his_type" SexNameKey = "%d:sys:sex_name:%d" ModuleTypeKey = "%d:dict:module_type:%d" Sex = "sex" ) func (s *dictService) GetByCode(code string) ([]dao.Dict, error) { dict := &dao.Dict{Code: code} return dict.GetByCode() } func (s *dictService) GetControlType(tenantId int, id int) string { var name string key := fmt.Sprintf(ControlTypeKey, tenantId, id) name, err := util.Redis.Get(key).Result() if err != nil { dict := &dao.Dict{Code: ControlType, DictKey: strconv.Itoa(id)} if err := dict.GetByCodeAndKey(); err != nil { fmt.Printf("GetByCodeAndKey err = %d \n", err) } else { name = dict.DictValue util.Redis.Set(key, name, -1) } } return name } func (s *dictService) GetModuleName(tenantId int, id int) string { var name string key := fmt.Sprintf(ModuleTypeKey, tenantId, id) name, err := util.Redis.Get(key).Result() if err != nil { dict := &dao.Dict{Code: ModuleType, DictKey: strconv.Itoa(id)} if err := dict.GetByCodeAndKey(); err != nil { fmt.Printf("GetByCodeAndKey err = %d \n", err) } else { name = dict.DictValue util.Redis.Set(key, name, -1) } } return name } func (s *dictService) GetSexName(tenantId int, id int) string { var name string key := fmt.Sprintf(SexNameKey, tenantId, id) name, err := util.Redis.Get(key).Result() if err != nil { dict := &dao.Dict{Code: Sex, DictKey: strconv.Itoa(id)} if err := dict.GetByCodeAndKey(); err != nil { fmt.Printf("GetByCodeAndKey err = %d \n", err) } else { name = dict.DictValue util.Redis.Set(key, name, -1) } } return name }