package models import ( "eta_gn/eta_index_lib/global" "eta_gn/eta_index_lib/utils" "fmt" "strings" ) var ( EdbSourceIdMap map[int]*EdbSource // 指标来源 EdbSourceExtendIdMap map[string]int // 指标来源字符串对应来源ID ) type EdbSource struct { EdbSourceId int `gorm:"primaryKey;autoIncrement;column:edb_source_id" description:"指标来源ID"` SourceName string `gorm:"column:source_name;not null;default:'';type:varchar(128)" description:"指标来源名称"` TableName1 string `gorm:"column:table_name;not null;default:'';type:varchar(128)" description:"数据表名"` EdbAddMethod string `gorm:"column:edb_add_method;not null;default:'';type:varchar(255)" description:"指标新增接口"` EdbRefreshMethod string `gorm:"column:edb_refresh_method;not null;default:'';type:varchar(255)" description:"指标刷新接口"` IsBase int `gorm:"column:is_base;not null;default:0" description:"是否为基础指标: 0-否; 1-是"` FromBridge int `gorm:"column:from_bridge;not null;default:0" description:"是否来源于桥接服务: 0-否; 1-是"` BridgeFlag string `gorm:"column:bridge_flag;not null;default:'';type:varchar(128)" description:"桥接服务对象标识"` SourceExtend string `gorm:"column:source_extend;not null;default:'';type:varchar(255)" description:"扩展字段做查询用"` EdbCodeRequired int `gorm:"column:edb_code_required;not null;default:0" description:"指标编码是否必填: 0-否; 1-是"` } // TableName 获取表名 func (e *EdbSource) TableName() string { return "edb_source" } // GetEdbSourceItemsByCondition 获取指标来源列表 func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY edb_source_id ASC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&items).Error return } // InitEdbSource 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务 func InitEdbSource() { EdbSourceIdMap = make(map[int]*EdbSource) EdbSourceExtendIdMap = make(map[string]int) sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "") if e != nil { utils.FileLog.Info("init source table err: %s", e.Error()) return } for _, v := range sources { EdbSourceIdMap[v.EdbSourceId] = v if v.SourceExtend != "" { arr := strings.Split(v.SourceExtend, ",") if len(arr) == 0 { continue } for _, s := range arr { EdbSourceExtendIdMap[s] = v.EdbSourceId } } } } // GetEdbSourceItemsSourceId // @Description: 根据来源id获取指标来源 // @param sourceId // @return item // @return err func GetEdbSourceItemsSourceId(sourceId int) (item *EdbSource, err error) { sql := `SELECT * FROM edb_source WHERE 1=1 AND edb_source_id = ? ` err = global.DEFAULT_DmSQL.Raw(sql, sourceId).First(&item).Error return } // GetEdbSourceBySourceId // @Description: 根据来源id获取指标来源 // @param sourceId // @return sourceItem func GetEdbSourceBySourceId(sourceId int) (sourceItem *EdbSource) { sourceItem, ok := EdbSourceIdMap[sourceId] if !ok { item, err := GetEdbSourceItemsSourceId(sourceId) if err != nil { return } if item.EdbSourceId > 0 { sourceItem = item // 写入到内存中 EdbSourceIdMap[sourceId] = sourceItem } } return }