edb_info_relation.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type EdbInfoRelation struct {
  8. EdbInfoRelationId int `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. // GetEdbInfoRelationByChildEdbInfoId 查询引用的指标ID
  29. func GetEdbInfoRelationByChildEdbInfoId(edbInfoId int) (item *EdbInfoRelation, err error) {
  30. o := orm.NewOrm()
  31. msql := ` SELECT * FROM edb_info_relation WHERE child_edb_info_id = ?`
  32. err = o.Raw(msql, edbInfoId).QueryRow(&item)
  33. return
  34. }
  35. // GetEdbInfoRelationListByChildEdbInfoId 根据间接引用中的的计算指标ID查询引用列表
  36. func GetEdbInfoRelationListByChildEdbInfoId(edbInfoId int) (items []*EdbInfoRelation, err error) {
  37. o := orm.NewOrm()
  38. msql := ` SELECT * FROM edb_info_relation WHERE relation_type=1 AND child_edb_info_id=? `
  39. _, err = o.Raw(msql, edbInfoId).QueryRows(&items)
  40. return
  41. }
  42. // 新增记录
  43. func AddOrUpdateEdbInfoRelationByChildEdbInfoId(relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string, deleteRelationIds []int) (err error) {
  44. o, err := orm.NewOrm().Begin()
  45. if err != nil {
  46. return
  47. }
  48. defer func() {
  49. if err != nil {
  50. _ = o.Rollback()
  51. return
  52. }
  53. _ = o.Commit()
  54. }()
  55. relationCodesMap := make(map[string]struct{}, 0)
  56. if len(relationList) > 0 {
  57. for _, relation := range relationList {
  58. if relation.RelationType == 1 {
  59. relationCodesMap[relation.RelationCode] = struct{}{}
  60. }
  61. }
  62. _, err = o.InsertMulti(len(relationList), relationList)
  63. if err != nil {
  64. return
  65. }
  66. }
  67. if len(deleteRelationIds) > 0 {
  68. // 删除对应的记录
  69. sql := ` DELETE FROM edb_info_relation WHERE edb_info_relation_id in (` + utils.GetOrmInReplace(len(deleteRelationIds)) + `)`
  70. _, err = o.Raw(sql, deleteRelationIds).Exec()
  71. if err != nil {
  72. return
  73. }
  74. }
  75. if len(refreshEdbInfoIds) > 0 {
  76. // todo 更新指标的刷新状态
  77. sql := ` UPDATE edb_info SET no_update = 0 WHERE source in (?, ?) AND edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
  78. _, err = o.Raw(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, refreshEdbInfoIds).Exec()
  79. if err != nil {
  80. return
  81. }
  82. }
  83. //更新数据源钢联化工指标
  84. if len(indexCodeList) > 0 {
  85. // 更改数据源的更新状态
  86. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
  87. _, err = o.Raw(sql, indexCodeList).Exec()
  88. if err != nil {
  89. return
  90. }
  91. }
  92. if len(relationList) > 0 {
  93. // 更新间接引用指标的关联ID
  94. relationCodes := make([]string, 0)
  95. for relationCode := range relationCodesMap {
  96. relationCodes = append(relationCodes, relationCode)
  97. }
  98. if len(relationCodes) > 0 {
  99. sql := ` UPDATE edb_info_relation e1
  100. JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
  101. SET e1.parent_relation_id = e2.edb_info_relation_id
  102. WHERE
  103. e1.relation_type = 1
  104. AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
  105. _, err = o.Raw(sql, relationCodes).Exec()
  106. if err != nil {
  107. return
  108. }
  109. }
  110. }
  111. return
  112. }