edb_info_calculate_mapping.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type EdbInfoCalculateMapping struct {
  8. EdbInfoCalculateMappingId int `orm:"column(edb_info_calculate_mapping_id);pk"`
  9. EdbInfoId int `description:"计算指标id"`
  10. Source int `description:"计算指标来源"`
  11. SourceName string `description:"计算指标来源名称"`
  12. EdbCode string `description:"计算指标编码"`
  13. FromEdbInfoId int `description:"基础指标id"`
  14. FromEdbCode string `description:"基础指标编码"`
  15. FromEdbName string `description:"基础指标名称"`
  16. FromSource int `description:"基础指标来源"`
  17. FromSourceName string `description:"基础指标来源名称"`
  18. FromTag string `description:"来源指标标签"`
  19. Sort int `description:"计算指标名称排序"`
  20. CreateTime time.Time `description:"创建时间"`
  21. ModifyTime time.Time `description:"修改时间"`
  22. }
  23. func AddEdbInfoCalculateMappingMulti(items []*EdbInfoCalculateMapping) (err error) {
  24. o := orm.NewOrmUsingDB("data")
  25. _, err = o.InsertMulti(1, items)
  26. return
  27. }
  28. func FixEdbInfoCalculateMapping() {
  29. o := orm.NewOrmUsingDB("data")
  30. tx,err:=o.Begin()
  31. if err!=nil {
  32. return
  33. }
  34. defer func() {
  35. if err != nil {
  36. tx.Rollback()
  37. } else {
  38. tx.Commit()
  39. }
  40. }()
  41. edbInfoList := make([]*EdbInfoList, 0)
  42. sql := `SELECT * FROM edb_info WHERE edb_type=2 `
  43. _, err = o.Raw(sql).QueryRows(&edbInfoList)
  44. if err != nil {
  45. return
  46. }
  47. for k, v := range edbInfoList {
  48. fmt.Println(k, v.EdbInfoId)
  49. calculateList := make([]*EdbInfoCalculateDetail, 0)
  50. calculateTableName := GetEdbInfoCalculateTableName(v.Source)
  51. sql := ` SELECT * FROM %s
  52. WHERE edb_info_id=? ORDER BY sort ASC `
  53. sql = fmt.Sprintf(sql, calculateTableName)
  54. _, err = tx.Raw(sql, v.EdbInfoId).QueryRows(&calculateList)
  55. if err != nil {
  56. fmt.Println("query calculate err:" + err.Error())
  57. return
  58. }
  59. for _, cv := range calculateList {
  60. //关联关系
  61. calculateMappingItem := new(EdbInfoCalculateMapping)
  62. calculateMappingItem.CreateTime = time.Now()
  63. calculateMappingItem.ModifyTime = time.Now()
  64. calculateMappingItem.Sort = 1
  65. calculateMappingItem.EdbCode = v.EdbCode
  66. calculateMappingItem.EdbInfoId = v.EdbInfoId
  67. calculateMappingItem.FromEdbInfoId = cv.FromEdbInfoId
  68. calculateMappingItem.FromEdbCode = cv.FromEdbCode
  69. calculateMappingItem.FromEdbName = cv.FromEdbName
  70. calculateMappingItem.FromSource = cv.FromSource
  71. calculateMappingItem.FromSourceName = cv.FromSourceName
  72. calculateMappingItem.FromTag = cv.FromTag
  73. calculateMappingItem.Source = v.Source
  74. calculateMappingItem.SourceName = v.SourceName
  75. tx.Insert(calculateMappingItem)
  76. }
  77. }
  78. }
  79. type EdbInfoCalculateMappingView struct {
  80. EdbInfoCalculateMappingId int `orm:"column(edb_info_calculate_mapping_id);pk"`
  81. EdbInfoId int `description:"计算指标id"`
  82. Source int `description:"计算指标来源"`
  83. SourceName string `description:"计算指标来源名称"`
  84. EdbCode string `description:"计算指标编码"`
  85. FromEdbInfoId int `description:"基础指标id"`
  86. FromEdbCode string `description:"基础指标编码"`
  87. FromEdbName string `description:"基础指标名称"`
  88. FromSource int `description:"基础指标来源"`
  89. FromSourceName string `description:"基础指标来源名称"`
  90. FromTag string `description:"来源指标标签"`
  91. Sort int `description:"计算指标名称排序"`
  92. CreateTime time.Time `description:"创建时间"`
  93. ModifyTime time.Time `description:"修改时间"`
  94. StartDate string `description:"开始日期"`
  95. EndDate string `description:"结束日期"`
  96. CalculateFormula string `description:"N值"`
  97. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  98. MoveFrequency string `description:"移动频度"`
  99. }
  100. func GetEdbInfoCalculateMappingDetail(edbInfoId int) (item *EdbInfoCalculateMappingView, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT a.*,b.start_date,b.end_date,b.calculate_formula,b.move_type,b.move_frequency FROM edb_info_calculate_mapping AS a
  103. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  104. WHERE a.edb_info_id=? `
  105. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  106. return
  107. }