edb_info_relation.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type EdbInfoRelation struct {
  9. EdbInfoRelationId int `gorm:"primaryKey;autoIncrement;column:edb_info_relation_id"`
  10. EdbInfoId int `gorm:"column:edb_info_id" description:"指标id"`
  11. Source int `gorm:"column:source" description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,9:手工指标,10:隆众"`
  12. EdbName string `gorm:"column:edb_name" description:"指标名称"`
  13. EdbCode string `gorm:"column:edb_code" description:"指标编码"`
  14. ReferObjectId int `gorm:"column:refer_object_id" description:"引用对象ID(图表ID,ETA逻辑ID等)"`
  15. ReferObjectType int `gorm:"column:refer_object_type" description:"引用对象ID类型(1.图表,2.ETA逻辑)"`
  16. ReferObjectSubType int `gorm:"column:refer_object_sub_type" description:"引用对象子类"`
  17. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  18. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  19. RelationTime time.Time `gorm:"column:relation_time" description:"引用时间"`
  20. RelationType int `gorm:"column:relation_type" description:"引用类型,0:直接引用,1间接引用"`
  21. RootEdbInfoId int `gorm:"column:root_edb_info_id" description:"间接引用时,关联的直接引用的指标ID"`
  22. ChildEdbInfoId int `gorm:"column:child_edb_info_id" description:"间接引用时,计算指标直接关联的指标ID"`
  23. RelationCode string `gorm:"column:relation_code" description:"引用标识"`
  24. ParentRelationId int `gorm:"column:parent_relation_id" description:"间接引用关联的直接引用的ID"`
  25. }
  26. func (e *EdbInfoRelation) TableName() string {
  27. return "edb_info_relation"
  28. }
  29. // GetEdbInfoRelationByChildEdbInfoId 查询引用的指标ID
  30. func GetEdbInfoRelationByChildEdbInfoId(edbInfoId int) (item *EdbInfoRelation, err error) {
  31. msql := ` SELECT * FROM edb_info_relation WHERE child_edb_info_id = ? or edb_info_id=?`
  32. err = global.DEFAULT_DmSQL.Raw(msql, edbInfoId, edbInfoId).First(&item).Error
  33. return
  34. }
  35. // GetEdbInfoRelationListByChildEdbInfoId 根据间接引用中的的计算指标ID查询引用列表
  36. func GetEdbInfoRelationListByChildEdbInfoId(edbInfoId int) (items []*EdbInfoRelation, err error) {
  37. msql := ` SELECT * FROM edb_info_relation WHERE relation_type=1 AND child_edb_info_id=?`
  38. err = global.DEFAULT_DmSQL.Raw(msql, edbInfoId).Scan(&items).Error
  39. return
  40. }
  41. // GetEdbInfoRelationListByParentRelationId 根据间接引用中的的父级ID查询
  42. func GetEdbInfoRelationEdbIdsByParentRelationId(relationId, edbInfoId int) (items []int, err error) {
  43. msql := ` SELECT edb_info_id FROM edb_info_relation WHERE parent_relation_id=? AND child_edb_info_id=?`
  44. err = global.DEFAULT_DmSQL.Raw(msql, relationId, edbInfoId).Scan(&items).Error
  45. return
  46. }
  47. // GetEdbInfoRelationByRelationIds 查询引用的指标ID
  48. func GetEdbInfoRelationByRelationIds(ids []int) (items []*EdbInfoRelation, err error) {
  49. msql := ` SELECT * FROM edb_info_relation WHERE edb_info_relation_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
  50. err = global.DEFAULT_DmSQL.Raw(msql, ids).Scan(&items).Error
  51. return
  52. }
  53. // UpdateSecondRelationEdbInfoId 更新指标替换后的间接引用记录
  54. func UpdateSecondRelationEdbInfoId(edbRelationIds []int, relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
  55. o, err := orm.NewOrm().Begin()
  56. if err != nil {
  57. return
  58. }
  59. defer func() {
  60. if err != nil {
  61. _ = o.Rollback()
  62. return
  63. }
  64. _ = o.Commit()
  65. }()
  66. // 删除相关的间接引用
  67. sql := ` DELETE FROM edb_info_relation WHERE relation_type=1 and parent_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
  68. err = global.DEFAULT_DmSQL.Exec(sql, edbRelationIds).Error
  69. if err != nil {
  70. return
  71. }
  72. // 新增间接引用
  73. relationCodesMap := make(map[string]struct{}, 0)
  74. if len(relationList) > 0 {
  75. for _, relation := range relationList {
  76. if relation.RelationType == 1 {
  77. relationCodesMap[relation.RelationCode] = struct{}{}
  78. }
  79. }
  80. _, err = o.InsertMulti(len(relationList), relationList)
  81. if err != nil {
  82. return
  83. }
  84. }
  85. if len(refreshEdbInfoIds) > 0 {
  86. // todo 更新指标的刷新状态
  87. sql = ` UPDATE edb_info SET no_update = 0 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
  88. err = global.DEFAULT_DmSQL.Exec(sql, refreshEdbInfoIds).Error
  89. if err != nil {
  90. return
  91. }
  92. }
  93. //更新数据源钢联化工指标
  94. if len(indexCodeList) > 0 {
  95. // 更改数据源的更新状态
  96. sql = ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
  97. err = global.DEFAULT_DmSQL.Exec(sql, indexCodeList).Error
  98. if err != nil {
  99. return
  100. }
  101. }
  102. if len(relationList) > 0 {
  103. // 更新间接引用指标的关联ID
  104. relationCodes := make([]string, 0)
  105. for relationCode := range relationCodesMap {
  106. relationCodes = append(relationCodes, relationCode)
  107. }
  108. if len(relationCodes) > 0 {
  109. sql := ` UPDATE edb_info_relation e1
  110. JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
  111. SET e1.parent_relation_id = e2.edb_info_relation_id
  112. WHERE
  113. e1.relation_type = 1
  114. AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
  115. err = global.DEFAULT_DmSQL.Exec(sql, relationCodes).Error
  116. if err != nil {
  117. return
  118. }
  119. }
  120. }
  121. return
  122. }