edb_info_relation.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package data_manage
  2. import (
  3. "eta_gn/eta_task/global"
  4. "eta_gn/eta_task/utils"
  5. "time"
  6. )
  7. type EdbInfoRelation struct {
  8. EdbInfoRelationId int `gorm:"column:edb_info_relation_id;primaryKey"` // `orm:"column(edb_info_relation_id);pk"`
  9. EdbInfoId int `description:"指标id"`
  10. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,9:手工指标,10:隆众"`
  11. EdbName string `description:"指标名称"`
  12. EdbCode string `description:"指标编码"`
  13. ReferObjectId int `description:"引用对象ID(图表ID,ETA逻辑ID等)"`
  14. ReferObjectType int `description:"引用对象ID类型(1.图表,2.ETA逻辑)"`
  15. ReferObjectSubType int `description:"引用对象子类"`
  16. CreateTime time.Time `description:"创建时间"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. RelationTime time.Time `description:"引用时间"`
  19. RelationType int `description:"引用类型,0:直接饮用,1间接引用"`
  20. RootEdbInfoId int `description:"间接引用时,关联的直接引用的指标ID"`
  21. ChildEdbInfoId int `description:"间接引用时,计算指标直接关联的指标ID"`
  22. RelationCode string `description:"引用标识"`
  23. ParentRelationId int `description:"间接引用关联的直接引用的ID"`
  24. }
  25. func (e *EdbInfoRelation) TableName() string {
  26. return "edb_info_relation"
  27. }
  28. func AddEdbInfoRelationMulti(relationList []*EdbInfoRelation) (err error) {
  29. //o, err := orm.NewOrmUsingDB("data").Begin()
  30. o := global.DmSQL["data"].Begin()
  31. if err != nil {
  32. return
  33. }
  34. defer func() {
  35. if err != nil {
  36. _ = o.Rollback()
  37. return
  38. }
  39. _ = o.Commit()
  40. }()
  41. relationCodesMap := make(map[string]struct{}, 0)
  42. if len(relationList) > 0 {
  43. for _, relation := range relationList {
  44. if relation.RelationType == 1 {
  45. relationCodesMap[relation.RelationCode] = struct{}{}
  46. }
  47. }
  48. //_, err = o.InsertMulti(len(relationList), relationList)
  49. err = o.CreateInBatches(relationList, utils.MultiAddNum).Error
  50. if err != nil {
  51. return
  52. }
  53. }
  54. if len(relationList) > 0 {
  55. // 更新间接引用指标的关联ID
  56. relationCodes := make([]string, 0)
  57. for relationCode := range relationCodesMap {
  58. relationCodes = append(relationCodes, relationCode)
  59. }
  60. if len(relationCodes) > 0 {
  61. sql := ` UPDATE edb_info_relation e1
  62. JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
  63. SET e1.parent_relation_id = e2.edb_info_relation_id
  64. WHERE
  65. e1.relation_type = 1
  66. AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
  67. //_, err = o.Raw(sql, relationCodes).Exec()
  68. err = o.Exec(sql, relationCodes).Error
  69. if err != nil {
  70. return
  71. }
  72. }
  73. }
  74. return
  75. }
  76. // GetEdbInfoRelationByEdbInfoIds 查询引用的指标ID
  77. func GetEdbInfoRelationByEdbInfoIds(edbInfoIds []int) (edbIds []int, err error) {
  78. //o := orm.NewOrmUsingDB("data")
  79. msql := ` SELECT edb_info_id FROM edb_info_relation WHERE edb_info_id in (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) GROUP BY edb_info_id `
  80. //_, err = o.Raw(msql, edbInfoIds).QueryRows(&edbIds)
  81. err = global.DmSQL["data"].Raw(msql, edbInfoIds).Find(&edbIds).Error
  82. if err != nil {
  83. return
  84. }
  85. return
  86. }
  87. // GetEdbInfoRelationByReferObjectIds 查询引用的指标ID
  88. func GetEdbInfoRelationByReferObjectIds(referObjectIds []int, referObjectType int) (items []*EdbInfoRelation, err error) {
  89. //o := orm.NewOrmUsingDB("data")
  90. msql := ` SELECT * FROM edb_info_relation WHERE refer_object_id in (` + utils.GetOrmInReplace(len(referObjectIds)) + `) AND refer_object_type=? AND relation_type=0`
  91. //_, err = o.Raw(msql, referObjectIds, referObjectType).QueryRows(&items)
  92. err = global.DmSQL["data"].Raw(msql, referObjectIds, referObjectType).Find(&items).Error
  93. return
  94. }