edb_info_calculate_mapping.go 8.5 KB

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