edb_info_relation.go 4.9 KB

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