Browse Source

Merge branch 'feature/jiayue_index' into debug

hsun 1 year ago
parent
commit
20354ffce5

+ 9 - 12
controllers/base_from_jiayue.go

@@ -6,6 +6,7 @@ import (
 	"eta/eta_index_lib/models"
 	"eta/eta_index_lib/services"
 	"eta/eta_index_lib/utils"
+	"fmt"
 	"strconv"
 	"time"
 )
@@ -62,13 +63,10 @@ func (this *JiaYueController) Add() {
 	}()
 
 	// 校验数据源
-	cond := ` AND edb_source_id = ?`
-	pars := make([]interface{}, 0)
-	pars = append(pars, req.Source)
-	sourceItem, e := models.GetEdbSourceItemByCondition(cond, pars)
-	if e != nil {
+	sourceItem := models.EdbSourceIdMap[req.Source]
+	if sourceItem == nil {
 		br.Msg = "数据源有误"
-		br.ErrMsg = "获取数据源失败, Err: " + e.Error()
+		br.ErrMsg = fmt.Sprintf("数据源有误, Source: %d", req.Source)
 		return
 	}
 
@@ -78,6 +76,7 @@ func (this *JiaYueController) Add() {
 	params.SourceExtend = sourceItem.SourceExtend
 	params.StartDate = utils.BASE_START_DATE
 	params.EndDate = utils.BASE_END_DATE
+	params.IndexCodeRequired = sourceItem.EdbCodeRequired
 	indexData, e := services.GetJiaYueIndexDataFromBridge(params)
 	if e != nil {
 		br.Msg = "获取指标失败"
@@ -165,13 +164,10 @@ func (this *JiaYueController) Refresh() {
 		br.ErrMsg = "查询指标信息失败, Err: " + e.Error()
 		return
 	}
-	cond := ` AND edb_source_id = ?`
-	pars := make([]interface{}, 0)
-	pars = append(pars, req.Source)
-	sourceItem, e := models.GetEdbSourceItemByCondition(cond, pars)
-	if e != nil {
+	sourceItem := models.EdbSourceIdMap[req.Source]
+	if sourceItem == nil {
 		br.Msg = "数据源有误"
-		br.ErrMsg = "获取数据源失败, Err: " + e.Error()
+		br.ErrMsg = fmt.Sprintf("数据源有误, Source: %d", req.Source)
 		return
 	}
 
@@ -181,6 +177,7 @@ func (this *JiaYueController) Refresh() {
 	params.SourceExtend = sourceItem.SourceExtend
 	params.StartDate = startDate
 	params.EndDate = utils.BASE_END_DATE
+	params.IndexCodeRequired = sourceItem.EdbCodeRequired
 	indexData, e := services.GetJiaYueIndexDataFromBridge(params)
 	if e != nil {
 		br.Msg = "获取指标失败"

+ 5 - 4
models/base_from_jiayue.go

@@ -11,10 +11,11 @@ import (
 
 // BridgeJiaYueIndexDataParams 桥接服务-获取嘉悦指标数据入参
 type BridgeJiaYueIndexDataParams struct {
-	IndexCode    string `json:"index_code" form:"index_code" description:"指标编码"`
-	SourceExtend string `json:"source_extend" form:"source_extend" description:"来源"`
-	StartDate    string `json:"start_date" form:"start_date" description:"开始日期"`
-	EndDate      string `json:"end_date" form:"end_date" description:"结束日期"`
+	IndexCode         string `json:"index_code" form:"index_code" description:"指标编码"`
+	SourceExtend      string `json:"source_extend" form:"source_extend" description:"来源"`
+	StartDate         string `json:"start_date" form:"start_date" description:"开始日期"`
+	EndDate           string `json:"end_date" form:"end_date" description:"结束日期"`
+	IndexCodeRequired int    `json:"index_code_required" form:"index_code_required" description:"指标编码是否必填: 0-否; 1-是"`
 }
 
 // BridgeJiaYueResultIndexData 桥接服务-获取嘉悦指标数据响应体

+ 4 - 33
models/edb_data_table.go

@@ -2,40 +2,8 @@ package models
 
 import (
 	"eta/eta_index_lib/utils"
-	"strings"
 )
 
-var (
-	EdbDataTableNameMap  map[int]string // 指标来源对应数据表名
-	EdbSourceExtendIdMap map[string]int // 指标来源字符串对应来源ID
-	EdbSourceNameMap     map[int]string // 指标来源名称
-)
-
-// InitEdbSource 初始化时加载指标来源对应表名, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
-func InitEdbSource() {
-	EdbDataTableNameMap = make(map[int]string)
-	EdbSourceExtendIdMap = make(map[string]int)
-	EdbSourceNameMap = make(map[int]string)
-	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 {
-		EdbDataTableNameMap[v.EdbSourceId] = v.TableName
-		EdbSourceNameMap[v.EdbSourceId] = v.SourceName
-		if v.SourceExtend != "" {
-			arr := strings.Split(v.SourceExtend, ",")
-			if len(arr) == 0 {
-				continue
-			}
-			for _, s := range arr {
-				EdbSourceExtendIdMap[s] = v.EdbSourceId
-			}
-		}
-	}
-}
-
 // GetEdbDataTableName 指标数据->存储表
 func GetEdbDataTableName(source int) (tableName string) {
 	switch source {
@@ -186,7 +154,10 @@ func GetEdbDataTableName(source int) (tableName string) {
 	case utils.DATA_SOURCE_CALCULATE_RJZ: //日均值75
 		tableName = "edb_data_calculate_rjz"
 	default:
-		tableName = EdbDataTableNameMap[source] // 没有对应的从edb_source中取
+		edbSource := EdbSourceIdMap[source]
+		if edbSource != nil {
+			tableName = edbSource.TableName
+		}
 	}
 	return
 }

+ 4 - 1
models/edb_info.go

@@ -1071,7 +1071,10 @@ func EdbInfoAdd(req *AddEdbInfoParams, serverUrl string, sysUserId int, sysUserR
 
 	sourceName, ok := sourceNameMap[source]
 	if !ok {
-		sourceName = EdbSourceNameMap[source]
+		edbSource := EdbSourceIdMap[source]
+		if edbSource != nil {
+			sourceName = edbSource.SourceName
+		}
 		if sourceName == "" {
 			err = errors.New("指标来源异常")
 			return

+ 30 - 0
models/edb_source.go

@@ -1,11 +1,17 @@
 package models
 
 import (
+	"eta/eta_index_lib/utils"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"strings"
 )
 
+var (
+	EdbSourceIdMap       map[int]*EdbSource // 指标来源
+	EdbSourceExtendIdMap map[string]int     // 指标来源字符串对应来源ID
+)
+
 // EdbSource 指标来源表
 type EdbSource struct {
 	EdbSourceId      int    `orm:"column(edb_source_id);pk"`
@@ -17,6 +23,7 @@ type EdbSource struct {
 	FromBridge       int    `description:"是否来源于桥接服务: 0-否; 1-是"`
 	BridgeFlag       string `description:"桥接服务对象标识"`
 	SourceExtend     string `description:"扩展字段做查询用"`
+	EdbCodeRequired  int    `description:"指标编码是否必填: 0-否; 1-是"`
 }
 
 // GetEdbSourceItemsByCondition 获取指标来源列表
@@ -42,3 +49,26 @@ func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *Ed
 	err = o.Raw(sql, pars).QueryRow(&item)
 	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
+			}
+		}
+	}
+}

+ 19 - 3
services/base_from_jiayue.go

@@ -223,13 +223,28 @@ func SyncJiaYueNewIndex(item models.BridgeJiaYueIndexAndData, menus []models.Bri
 		utils.FileLog.Info(fmt.Sprintf("指标来源ID有误, 忽略同步, sourceId: %d; sourceType: %s", sourceId, item.SourceType))
 		return
 	}
-	sourceTableName := models.EdbDataTableNameMap[sourceId]
+	edbSource := models.EdbSourceIdMap[sourceId]
+	if edbSource == nil {
+		utils.FileLog.Info(fmt.Sprintf("指标来源有误, sourceId: %d", sourceId))
+		return
+	}
+	sourceTableName := edbSource.TableName
 	if sourceTableName == "" {
 		utils.FileLog.Info(fmt.Sprintf("指标数据表有误, 忽略同步, sourceId: %d", sourceId))
 		return
 	}
-	// 指标编码默认使用自增ID
-	indexCode := strconv.Itoa(item.Id)
+
+	// 指标编码
+	indexCode := ""
+	if edbSource.EdbCodeRequired == 1 {
+		indexCode = item.IndexCode
+	} else {
+		indexCode = strconv.Itoa(item.Id)
+	}
+	if indexCode == "" {
+		utils.FileLog.Info("指标编码不存在")
+		return
+	}
 
 	// 从桥接服务获取指标和数据
 	var params models.BridgeJiaYueIndexDataParams
@@ -237,6 +252,7 @@ func SyncJiaYueNewIndex(item models.BridgeJiaYueIndexAndData, menus []models.Bri
 	params.SourceExtend = item.SourceType
 	params.StartDate = utils.BASE_START_DATE
 	params.EndDate = utils.BASE_END_DATE
+	params.IndexCodeRequired = edbSource.EdbCodeRequired
 	indexData, e := GetJiaYueIndexDataFromBridge(params)
 	if e != nil {
 		err = fmt.Errorf("GetJiaYueIndexDataFromBridge err: %s", e.Error())