123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package data_manage
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "strings"
- )
- var (
- EdbSourceIdMap map[int]*EdbSource // 指标来源
- )
- // EdbSource 指标来源表
- type EdbSource struct {
- EdbSourceId int `orm:"column(edb_source_id);pk" gorm:"primaryKey" `
- SourceName string `description:"指标来源名称"`
- SourceNameEn string `description:"指标来源名称英文"`
- TableName string `description:"数据表名"`
- EdbAddMethod string `description:"指标新增接口"`
- EdbRefreshMethod string `description:"指标刷新接口"`
- IsBase int `description:"是否为基础指标: 0-否; 1-是"`
- FromBridge int `description:"是否来源于桥接服务: 0-否; 1-是"`
- BridgeFlag string `description:"桥接服务对象标识"`
- SourceExtend string `description:"扩展字段做查询用"`
- EdbCodeRequired int `description:"指标编码是否必填: 0-否; 1-是"`
- IndexTableName string `description:"数据源指标表名"`
- }
- // GetEdbSourceItemsByCondition 获取指标来源列表
- func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
- o := global.DmSQL["data"]
- 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 = o.Raw(sql, pars...).Find(&items).Error
- return
- }
- // EdbSourceChild 指标来源表
- type EdbSourceChild struct {
- EdbSourceId int `orm:"column(edb_source_id);pk" gorm:"primaryKey" `
- SourceName string `description:"指标来源名称"`
- TableName string `description:"数据表名"`
- IsBase int `description:"是否为基础指标: 2-否; 1-是"`
- Child []EdbSourceChild
- }
- // GetEdbSourceItemByCondition 获取指标来源
- func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
- sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
- err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
- return
- }
- // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
- func InitEdbSourceVar() {
- EdbSourceIdMap = make(map[int]*EdbSource)
- 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
- }
- }
- // 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.DmSQL["data"].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
- }
|