package data_manage

import (
	"eta_gn/eta_chart_lib/global"
	"eta_gn/eta_chart_lib/utils"
	"fmt"
	"strings"
)

var (
	EdbSourceIdMap map[int]*EdbSource // 指标来源
)


type EdbSource struct {
	EdbSourceId      int    `gorm:"column:edb_source_id;primaryKey" description:"指标来源ID" orm:"column(edb_source_id);pk"`
	SourceName       string `gorm:"column:source_name" description:"指标来源名称"`
	TableName        string `gorm:"column:table_name" description:"数据表名"`
	EdbAddMethod     string `gorm:"column:edb_add_method" description:"指标新增接口"`
	EdbRefreshMethod string `gorm:"column:edb_refresh_method" description:"指标刷新接口"`
	IsBase           int    `gorm:"column:is_base" description:"是否为基础指标: 0-否; 1-是"`
	FromBridge       int    `gorm:"column:from_bridge" description:"是否来源于桥接服务: 0-否; 1-是"`
	BridgeFlag       string `gorm:"column:bridge_flag" description:"桥接服务对象标识"`
	SourceExtend     string `gorm:"column:source_extend" description:"扩展字段做查询用"`
	EdbCodeRequired  int    `gorm:"column:edb_code_required" description:"指标编码是否必填: 0-否; 1-是"`
}

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...).Scan(&items).Error
	return
}


type EdbSourceChild struct {
	EdbSourceId int              `gorm:"column:edb_source_id;primaryKey" description:"指标来源ID" orm:"column(edb_source_id);pk"`
	SourceName  string           `gorm:"column:source_name" description:"指标来源名称"`
	TableName   string           `gorm:"column:table_name" description:"数据表名"`
	IsBase      int              `gorm:"column:is_base" description:"是否为基础指标: 0-否; 1-是"`
	Child       []EdbSourceChild `description:"子源"`
}

func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
	o := global.DmSQL["data"]
	sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
	err = o.Raw(sql, pars...).First(&item).Error
	return
}

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
	}
}

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
}

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
}