Ver Fonte

计算指标变更时同步变更指标引用记录

xyxie há 8 meses atrás
pai
commit
d91e13cf55

+ 8 - 0
controllers/base_from_calculate.go

@@ -336,6 +336,8 @@ func (this *CalculateController) Edit() {
 		EdbInfoId:  edbInfoDetail.EdbInfoId,
 		UniqueCode: edbInfoDetail.UniqueCode,
 	}
+	// 重置计算指标中的引用关系
+	go services.ResetEdbRelation(edbInfoDetail.EdbInfoId)
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "保存成功"
@@ -1208,6 +1210,9 @@ func (this *CalculateController) BatchEdit() {
 		return
 	}
 
+	// 重置计算指标中的引用关系
+	go services.ResetEdbRelation(edbInfoId)
+
 	resp := models.AddEdbInfoResp{
 		EdbInfoId:  edbInfo.EdbInfoId,
 		UniqueCode: edbInfo.UniqueCode,
@@ -1767,6 +1772,9 @@ func (this *CalculateController) SaveAdjust() {
 		EdbInfoId:  edbInfo.EdbInfoId,
 		UniqueCode: edbInfo.UniqueCode,
 	}
+
+	// 重置计算指标中的引用关系
+	go services.ResetEdbRelation(edbInfo.EdbInfoId)
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "保存成功"

+ 3 - 0
controllers/base_from_predict_calculate.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"eta/eta_index_lib/logic"
 	"eta/eta_index_lib/models"
+	"eta/eta_index_lib/services"
 	"eta/eta_index_lib/utils"
 	"fmt"
 	"strconv"
@@ -506,6 +507,8 @@ func editPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSa
 		br.ErrMsg = err.Error()
 		return
 	}
+	// 重置计算指标中的引用关系
+	go services.ResetEdbRelation(edbInfo.EdbInfoId)
 	resp := models.AddEdbInfoResp{
 		EdbInfoId:  edbInfo.EdbInfoId,
 		UniqueCode: edbInfo.UniqueCode,

+ 1 - 0
models/db.go

@@ -57,6 +57,7 @@ func init() {
 		new(EdbDataInsertConfig),
 		new(EdbAdjustConf), // 数据调整的配置
 		new(BaseFromMysteelChemicalClassify),
+		new(EdbInfoRelation), //指标引用记录
 	)
 
 	// 注册期货数据 数据表

+ 97 - 0
models/edb_info_relation.go

@@ -0,0 +1,97 @@
+package models
+
+import (
+	"eta/eta_index_lib/utils"
+	"github.com/beego/beego/v2/client/orm"
+	"time"
+)
+
+type EdbInfoRelation struct {
+	EdbInfoRelationId  int       `orm:"column(edb_info_relation_id);pk"`
+	EdbInfoId          int       `description:"指标id"`
+	Source             int       `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,9:手工指标,10:隆众"`
+	EdbName            string    `description:"指标名称"`
+	EdbCode            string    `description:"指标编码"`
+	ReferObjectId      int       `description:"引用对象ID(图表ID,ETA逻辑ID等)"`
+	ReferObjectType    int       `description:"引用对象ID类型(1.图表,2.ETA逻辑)"`
+	ReferObjectSubType int       `description:"引用对象子类"`
+	CreateTime         time.Time `description:"创建时间"`
+	ModifyTime         time.Time `description:"修改时间"`
+	RelationTime       time.Time `description:"引用时间"`
+	RelationType       int       `description:"引用类型,0:直接饮用,1间接引用"`
+	RootEdbInfoId      int       `description:"间接引用时,关联的直接引用的指标ID"`
+	ChildEdbInfoId     int       `description:"间接引用时,计算指标直接关联的指标ID"`
+}
+
+func (e *EdbInfoRelation) TableName() string {
+	return "edb_info_relation"
+}
+
+// GetEdbInfoRelationByChildEdbInfoId 查询引用的指标ID
+func GetEdbInfoRelationByChildEdbInfoId(edbInfoId int) (item *EdbInfoRelation, err error) {
+	o := orm.NewOrm()
+	msql := ` SELECT * FROM edb_info_relation WHERE child_edb_info_id = ?`
+	err = o.Raw(msql, edbInfoId).QueryRow(&item)
+	return
+}
+
+// GetEdbInfoRelationListByChildEdbInfoId 根据间接引用中的的计算指标ID查询引用列表
+func GetEdbInfoRelationListByChildEdbInfoId(edbInfoId int) (items []*EdbInfoRelation, err error) {
+	o := orm.NewOrm()
+	msql := ` SELECT * FROM edb_info_relation WHERE relation_type=1 AND child_edb_info_id=? `
+	_, err = o.Raw(msql, edbInfoId).QueryRows(&items)
+	return
+}
+
+// 新增记录
+func AddOrUpdateEdbInfoRelationByChildEdbInfoId(relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string, deleteRelationIds []int) (err error) {
+	o, err := orm.NewOrm().Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		if err != nil {
+			_ = o.Rollback()
+			return
+		}
+		_ = o.Commit()
+	}()
+
+	if len(relationList) > 0 {
+		_, err = o.InsertMulti(len(relationList), relationList)
+		if err != nil {
+			return
+		}
+	}
+
+	if len(deleteRelationIds) > 0 {
+		// 删除对应的记录
+		sql := ` DELETE FROM edb_info_relation WHERE edb_info_relation_id in (` + utils.GetOrmInReplace(len(deleteRelationIds)) + `)`
+		_, err = o.Raw(sql, deleteRelationIds).Exec()
+		if err != nil {
+			return
+		}
+	}
+
+	if len(refreshEdbInfoIds) > 0 {
+		// todo 更新指标的刷新状态
+		sql := ` UPDATE edb_info SET no_update = 0 WHERE source in (?, ?) AND edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
+		_, err = o.Raw(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, refreshEdbInfoIds).Exec()
+		if err != nil {
+			return
+		}
+	}
+
+	//更新数据源钢联化工指标
+	if len(indexCodeList) > 0 {
+		// 更改数据源的更新状态
+		sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
+		_, err = o.Raw(sql, indexCodeList).Exec()
+		if err != nil {
+			return
+		}
+	}
+
+	// todo 由此被禁用的计算指标是否能恢复刷新
+	return
+}

+ 3 - 0
services/base_from_calculate.go

@@ -856,6 +856,9 @@ func EdbCalculateBatchEdit(req models.EdbInfoCalculateBatchEditReq) (edbInfo *mo
 
 	// 更新ES
 	go logic.UpdateEs(edbInfo.EdbInfoId)
+
+	// 重置计算指标中的引用关系
+	go ResetEdbRelation(edbInfoId)
 	return
 }
 

+ 121 - 0
services/edb_info_relation.go

@@ -0,0 +1,121 @@
+package services
+
+import (
+	"eta/eta_index_lib/models"
+	"eta/eta_index_lib/services/alarm_msg"
+	"eta/eta_index_lib/utils"
+	"fmt"
+	"strconv"
+	"strings"
+	"time"
+)
+
+// 重置单个计算指标中的引用关系
+func ResetEdbRelation(edbInfoId int) {
+	var err error
+	defer func() {
+		if err != nil {
+			msg := fmt.Sprintf("重置单个计算指标中的引用关系失败,错误信息:%s", err.Error())
+			go alarm_msg.SendAlarmMsg(msg, 3)
+		}
+	}()
+	//查询与该计算指标相关的间接引用或者间接引用关系,如果记录不存在,则不处理
+	_, err = models.GetEdbInfoRelationByChildEdbInfoId(edbInfoId)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			err = nil
+			return
+		}
+		err = fmt.Errorf("查询与该计算指标相关的间接引用或者间接引用关系失败,错误信息:%s", err.Error())
+		return
+	}
+
+	//查询当前计算指标最新的引用指标列表
+	newMappingList, err := models.GetEdbInfoCalculateDetailList(edbInfoId)
+	if err != nil {
+		err = fmt.Errorf("查询当前计算指标最新的指标列表失败,错误信息:%s", err.Error())
+		return
+	}
+	//整理关联的来源指标ID
+	newEdbIdList := make([]int, 0)
+	newMappingListMap := make(map[int]*models.EdbInfoCalculateDetail)
+	for _, v := range newMappingList {
+		newEdbIdList = append(newEdbIdList, v.FromEdbInfoId)
+		newMappingListMap[v.FromEdbInfoId] = v
+	}
+	// 排序
+	//二者匹配一下,如果相同,则不处理,如果不同,先查询所有旧的间接引用记录,整理并分组,则删除旧的间接引用记录,新增新的间接引用记录,
+	relationList, err := models.GetEdbInfoRelationListByChildEdbInfoId(edbInfoId)
+	if err != nil {
+		err = fmt.Errorf("查询当前计算指标的间接引用关系失败,错误信息:%s", err.Error())
+		return
+	}
+	deleteRelationIds := make([]int, 0)
+	// 根据引用对象和直接引用指标 进行分组
+	groupMap := make(map[string]map[int]*models.EdbInfoRelation)
+	// 遍历每组内容,
+	for _, v := range relationList {
+		//如果指标ID不在新的
+		if !utils.InArrayByInt(newEdbIdList, v.EdbInfoId) {
+			deleteRelationIds = append(deleteRelationIds, v.EdbInfoRelationId)
+		}
+		name := fmt.Sprintf("%d_%d_%d_%d", v.ReferObjectId, v.ReferObjectType, v.ReferObjectSubType, v.RootEdbInfoId)
+		childMap, ok := groupMap[name]
+		if !ok {
+			childMap = make(map[int]*models.EdbInfoRelation, 0)
+		}
+		childMap[v.EdbInfoId] = v
+		groupMap[name] = childMap
+	}
+	// 遍历每组内容,如果新ID不在组内,则添加
+	addList := make([]*models.EdbInfoRelation, 0)
+	indexCodeList := make([]string, 0)
+	refreshEdbIds := make([]int, 0)
+	nowTime := time.Now()
+	for name, childMap := range groupMap {
+		for _, edbId := range newEdbIdList {
+			if _, ok := childMap[edbId]; !ok {
+				// 新增记录
+				childEdb, ok1 := newMappingListMap[edbId]
+				if !ok1 {
+					continue
+				}
+				// 获取引用时间
+				relationObjectInfo := strings.Split(name, "_")
+				objectId, _ := strconv.Atoi(relationObjectInfo[0])
+				objectType, _ := strconv.Atoi(relationObjectInfo[1])
+				objectSubType, _ := strconv.Atoi(relationObjectInfo[2])
+				rootEdbId, _ := strconv.Atoi(relationObjectInfo[3])
+				tmp1 := &models.EdbInfoRelation{
+					ReferObjectId:      objectId,
+					ReferObjectType:    objectType,
+					ReferObjectSubType: objectSubType,
+					EdbInfoId:          childEdb.FromEdbInfoId,
+					EdbName:            childEdb.FromEdbName,
+					Source:             childEdb.FromSource,
+					EdbCode:            childEdb.FromEdbCode,
+					CreateTime:         nowTime,
+					ModifyTime:         nowTime,
+					RelationTime:       nowTime,
+					RelationType:       1,
+					RootEdbInfoId:      rootEdbId,
+					ChildEdbInfoId:     edbInfoId,
+				}
+				addList = append(addList, tmp1)
+				refreshEdbIds = append(refreshEdbIds, childEdb.FromEdbInfoId)
+				if childEdb.FromSource == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
+					indexCodeList = append(indexCodeList, childEdb.FromEdbCode)
+				}
+			}
+		}
+	}
+
+	if len(addList) > 0 || len(deleteRelationIds) > 0 {
+		err = models.AddOrUpdateEdbInfoRelationByChildEdbInfoId(addList, refreshEdbIds, indexCodeList, deleteRelationIds)
+		if err != nil {
+			err = fmt.Errorf("新增引用关系失败,err:%v", err)
+			return
+		}
+	}
+	return
+}