edb_info.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/eta_server/eta_chart_lib/utils"
  6. "time"
  7. )
  8. type EdbInfo struct {
  9. EdbInfoId int `orm:"column(edb_info_id);pk"`
  10. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  11. SourceName string `description:"来源名称"`
  12. Source int `description:"来源id"`
  13. EdbCode string `description:"指标编码"`
  14. EdbName string `description:"指标名称"`
  15. EdbNameSource string `description:"指标名称来源"`
  16. Frequency string `description:"频率"`
  17. Unit string `description:"单位"`
  18. StartDate string `description:"起始日期"`
  19. EndDate string `description:"终止日期"`
  20. ClassifyId int `description:"分类id"`
  21. SysUserId int
  22. SysUserRealName string
  23. UniqueCode string `description:"指标唯一编码"`
  24. CreateTime time.Time
  25. ModifyTime time.Time
  26. MinValue float64 `description:"指标最小值"`
  27. MaxValue float64 `description:"指标最大值"`
  28. CalculateFormula string `description:"计算公式"`
  29. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  30. Sort int `description:"排序字段"`
  31. LatestDate string `description:"数据最新日期"`
  32. LatestValue float64 `description:"数据最新值"`
  33. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  34. MoveFrequency string `description:"移动频度"`
  35. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  36. }
  37. type EdbInfoMaxAndMinInfo struct {
  38. MinDate string `description:"最小日期"`
  39. MaxDate string `description:"最大日期"`
  40. MinValue float64 `description:"最小值"`
  41. MaxValue float64 `description:"最大值"`
  42. LatestValue float64 `description:"最新值"`
  43. }
  44. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  45. o := orm.NewOrmUsingDB("data")
  46. sql := ``
  47. tableName := GetEdbDataTableName(source)
  48. sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM %s WHERE edb_code=? `
  49. sql = fmt.Sprintf(sql, tableName)
  50. err = o.Raw(sql, edbCode).QueryRow(&item)
  51. var latest_value float64
  52. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  53. sql = fmt.Sprintf(sql, tableName)
  54. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  55. item.LatestValue = latest_value
  56. return
  57. }
  58. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  59. o := orm.NewOrmUsingDB("data")
  60. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
  61. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  62. return
  63. }
  64. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  67. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  68. return
  69. }
  70. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  71. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  72. num := len(edbInfoIdList)
  73. if num <= 0 {
  74. return
  75. }
  76. o := orm.NewOrmUsingDB("data")
  77. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  78. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  79. return
  80. }
  81. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  82. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  83. if err != nil && err.Error() != utils.ErrNoRow() {
  84. return
  85. }
  86. for _, item := range calculateList {
  87. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  88. continue
  89. }
  90. if item.EdbType == 1 {
  91. baseEdbInfoArr = append(baseEdbInfoArr, item)
  92. } else {
  93. calculateInfoArr = append(calculateInfoArr, item)
  94. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  95. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  96. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  97. }
  98. }
  99. return
  100. }
  101. type EdbInfoSearchData struct {
  102. DataTime string `description:"数据日期"`
  103. Value float64 `description:"数据"`
  104. }
  105. // GetEdbDataListAll
  106. // order:1升序,其余值为降序
  107. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := ``
  110. tableName := GetEdbDataTableName(source)
  111. if tableName == "" {
  112. err = fmt.Errorf("指标来源有误, source: %d", source)
  113. return
  114. }
  115. sql = ` SELECT * FROM %s WHERE 1=1 `
  116. sql = fmt.Sprintf(sql, tableName)
  117. if condition != "" {
  118. sql += condition
  119. }
  120. if order == 1 {
  121. sql += ` ORDER BY data_time ASC `
  122. } else {
  123. sql += ` ORDER BY data_time DESC `
  124. }
  125. _, err = o.Raw(sql, pars).QueryRows(&item)
  126. return
  127. }