edb_info_calculate_mapping.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package data_manage
  2. import (
  3. "eta/eta_task/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type EdbInfoCalculateMapping struct {
  9. EdbInfoCalculateMappingId int `orm:"column(edb_info_calculate_mapping_id);pk"`
  10. EdbInfoId int `description:"计算指标id"`
  11. Source int `description:"计算指标来源"`
  12. SourceName string `description:"计算指标来源名称"`
  13. EdbCode string `description:"计算指标编码"`
  14. FromEdbInfoId int `description:"基础指标id"`
  15. FromEdbCode string `description:"基础指标编码"`
  16. FromEdbName string `description:"基础指标名称"`
  17. FromSource int `description:"基础指标来源"`
  18. FromSourceName string `description:"基础指标来源名称"`
  19. FromTag string `description:"来源指标标签"`
  20. Sort int `description:"计算指标名称排序"`
  21. CreateTime time.Time `description:"创建时间"`
  22. ModifyTime time.Time `description:"修改时间"`
  23. }
  24. func AddEdbInfoCalculateMappingMulti(items []*EdbInfoCalculateMapping) (err error) {
  25. o := orm.NewOrm()
  26. _, err = o.InsertMulti(1, items)
  27. return
  28. }
  29. func FixEdbInfoCalculateMapping() {
  30. o := orm.NewOrm()
  31. tx, err := o.Begin()
  32. if err != nil {
  33. return
  34. }
  35. defer func() {
  36. if err != nil {
  37. tx.Rollback()
  38. } else {
  39. tx.Commit()
  40. }
  41. }()
  42. edbInfoList := make([]*EdbInfoList, 0)
  43. sql := `SELECT * FROM edb_info WHERE edb_type=2 `
  44. _, err = o.Raw(sql).QueryRows(&edbInfoList)
  45. if err != nil {
  46. return
  47. }
  48. for k, v := range edbInfoList {
  49. fmt.Println(k, v.EdbInfoId)
  50. calculateList := make([]*EdbInfoCalculateDetail, 0)
  51. calculateTableName := GetEdbInfoCalculateTableName(v.Source)
  52. sql := ` SELECT * FROM %s
  53. WHERE edb_info_id=? ORDER BY sort ASC `
  54. sql = fmt.Sprintf(sql, calculateTableName)
  55. _, err = tx.Raw(sql, v.EdbInfoId).QueryRows(&calculateList)
  56. if err != nil {
  57. fmt.Println("query calculate err:" + err.Error())
  58. return
  59. }
  60. for _, cv := range calculateList {
  61. //关联关系
  62. calculateMappingItem := new(EdbInfoCalculateMapping)
  63. calculateMappingItem.CreateTime = time.Now()
  64. calculateMappingItem.ModifyTime = time.Now()
  65. calculateMappingItem.Sort = 1
  66. calculateMappingItem.EdbCode = v.EdbCode
  67. calculateMappingItem.EdbInfoId = v.EdbInfoId
  68. calculateMappingItem.FromEdbInfoId = cv.FromEdbInfoId
  69. calculateMappingItem.FromEdbCode = cv.FromEdbCode
  70. calculateMappingItem.FromEdbName = cv.FromEdbName
  71. calculateMappingItem.FromSource = cv.FromSource
  72. calculateMappingItem.FromSourceName = cv.FromSourceName
  73. calculateMappingItem.FromTag = cv.FromTag
  74. calculateMappingItem.Source = v.Source
  75. calculateMappingItem.SourceName = v.SourceName
  76. tx.Insert(calculateMappingItem)
  77. }
  78. }
  79. }
  80. type EdbInfoCalculateMappingView struct {
  81. EdbInfoCalculateMappingId int `orm:"column(edb_info_calculate_mapping_id);pk"`
  82. EdbInfoId int `description:"计算指标id"`
  83. Source int `description:"计算指标来源"`
  84. SourceName string `description:"计算指标来源名称"`
  85. EdbCode string `description:"计算指标编码"`
  86. FromEdbInfoId int `description:"基础指标id"`
  87. FromEdbCode string `description:"基础指标编码"`
  88. FromEdbName string `description:"基础指标名称"`
  89. FromSource int `description:"基础指标来源"`
  90. FromSourceName string `description:"基础指标来源名称"`
  91. FromTag string `description:"来源指标标签"`
  92. Sort int `description:"计算指标名称排序"`
  93. CreateTime time.Time `description:"创建时间"`
  94. ModifyTime time.Time `description:"修改时间"`
  95. StartDate string `description:"开始日期"`
  96. EndDate string `description:"结束日期"`
  97. CalculateFormula string `description:"N值"`
  98. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  99. MoveFrequency string `description:"移动频度"`
  100. }
  101. func GetEdbInfoCalculateMappingDetail(edbInfoId int) (item *EdbInfoCalculateMappingView, err error) {
  102. o := orm.NewOrm()
  103. 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
  104. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  105. WHERE a.edb_info_id=? `
  106. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  107. return
  108. }
  109. // GetAllCalculateEdbIdsByEdbInfoIds 所依赖计算指标
  110. func GetAllCalculateEdbIdsByEdbInfoIds(edbInfoIds []int) (edbIds []int, err error) {
  111. o := orm.NewOrmUsingDB("data")
  112. msql := ` SELECT edb_info_id FROM edb_info_calculate_mapping WHERE from_edb_info_id in (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) GROUP BY edb_info_id `
  113. _, err = o.Raw(msql, edbInfoIds).QueryRows(&edbIds)
  114. if err != nil {
  115. return
  116. }
  117. return
  118. }
  119. // EdbInfoCalculateMappingInfo
  120. // @Description: 计算指标与基础指标关系表
  121. type EdbInfoCalculateMappingInfo struct {
  122. EdbInfoCalculateMappingId int `orm:"column(edb_info_calculate_mapping_id);pk"`
  123. EdbInfoId int `description:"计算指标id"`
  124. Source int `description:"计算指标来源"`
  125. SourceName string `description:"计算指标来源名称"`
  126. EdbCode string `description:"计算指标编码"`
  127. FromEdbInfoId int `description:"基础指标id"`
  128. FromEdbCode string `description:"基础指标编码"`
  129. FromEdbName string `description:"基础指标名称"`
  130. FromSource int `description:"基础指标来源"`
  131. FromSourceName string `description:"基础指标来源名称"`
  132. MoveValue int `description:"领先值"`
  133. FromTag string `description:"来源指标标签"`
  134. Sort int `description:"计算指标名称排序"`
  135. CreateTime time.Time `description:"创建时间"`
  136. ModifyTime time.Time `description:"修改时间"`
  137. FromEdbType int `description:"来源指标类型:1:基础指标,2:计算指标"`
  138. FromEdbInfoType int `description:"来源指标类型: 0-基础指标; 1-预测指标"`
  139. FromClassifyId int `description:"来源指标分类ID"`
  140. FromUniqueCode string `description:"来源指标唯一编码"`
  141. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  142. }
  143. // GetEdbInfoCalculateMappingListByEdbInfoId 根据生成的指标id获取来源的指标id列表
  144. func GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId int) (items []*EdbInfoCalculateMappingInfo, err error) {
  145. o := orm.NewOrmUsingDB("data")
  146. sql := ` SELECT a.*,b.edb_type as from_edb_type,b.edb_info_type as from_edb_info_type, b.unique_code AS from_unique_code, b.classify_id AS from_classify_id,b.no_update FROM edb_info_calculate_mapping AS a
  147. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  148. WHERE a.edb_info_id=? `
  149. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  150. return
  151. }
  152. // GetEdbInfoCalculateMappingListByEdbInfoIds 根据生成的指标id获取来源的指标id列表
  153. func GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds []int) (items []*EdbInfoCalculateMappingInfo, err error) {
  154. o := orm.NewOrmUsingDB("data")
  155. sql := ` SELECT a.*,b.edb_type as from_edb_type,b.edb_info_type as from_edb_info_type, b.unique_code AS from_unique_code, b.classify_id AS from_classify_id,b.no_update FROM edb_info_calculate_mapping AS a
  156. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  157. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) `
  158. _, err = o.Raw(sql, edbInfoIds).QueryRows(&items)
  159. return
  160. }