123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package data_manage
- import (
- "eta/eta_task/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"`
- RelationCode string `description:"引用标识"`
- ParentRelationId int `description:"间接引用关联的直接引用的ID"`
- }
- func (e *EdbInfoRelation) TableName() string {
- return "edb_info_relation"
- }
- func AddEdbInfoRelationMulti(relationList []*EdbInfoRelation) (err error) {
- o, err := orm.NewOrmUsingDB("data").Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = o.Rollback()
- return
- }
- _ = o.Commit()
- }()
- relationCodesMap := make(map[string]struct{}, 0)
- if len(relationList) > 0 {
- for _, relation := range relationList {
- if relation.RelationType == 1 {
- relationCodesMap[relation.RelationCode] = struct{}{}
- }
- }
- _, err = o.InsertMulti(len(relationList), relationList)
- if err != nil {
- return
- }
- }
- if len(relationList) > 0 {
- // 更新间接引用指标的关联ID
- relationCodes := make([]string, 0)
- for relationCode := range relationCodesMap {
- relationCodes = append(relationCodes, relationCode)
- }
- if len(relationCodes) > 0 {
- sql := ` UPDATE edb_info_relation e1
- JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
- SET e1.parent_relation_id = e2.edb_info_relation_id
- WHERE
- e1.relation_type = 1
- AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
- _, err = o.Raw(sql, relationCodes).Exec()
- if err != nil {
- return
- }
- }
- }
- return
- }
- // GetEdbInfoRelationByEdbInfoIds 查询引用的指标ID
- func GetEdbInfoRelationByEdbInfoIds(edbInfoIds []int) (edbIds []int, err error) {
- o := orm.NewOrmUsingDB("data")
- msql := ` SELECT edb_info_id FROM edb_info_relation WHERE edb_info_id in (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) GROUP BY edb_info_id `
- _, err = o.Raw(msql, edbInfoIds).QueryRows(&edbIds)
- if err != nil {
- return
- }
- return
- }
- // GetEdbInfoRelationByReferObjectIds 查询引用的指标ID
- func GetEdbInfoRelationByReferObjectIds(referObjectIds []int, referObjectType int) (items []*EdbInfoRelation, err error) {
- o := orm.NewOrmUsingDB("data")
- msql := ` SELECT * FROM edb_info_relation WHERE refer_object_id in (` + utils.GetOrmInReplace(len(referObjectIds)) + `) AND refer_object_type=? AND relation_type=0`
- _, err = o.Raw(msql, referObjectIds, referObjectType).QueryRows(&items)
- return
- }
|