edb_info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_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. }
  36. type EdbInfoMaxAndMinInfo struct {
  37. MinDate string `description:"最小日期"`
  38. MaxDate string `description:"最大日期"`
  39. MinValue float64 `description:"最小值"`
  40. MaxValue float64 `description:"最大值"`
  41. LatestValue float64 `description:"最新值"`
  42. }
  43. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  44. o := orm.NewOrmUsingDB("data")
  45. sql := ``
  46. tableName := GetEdbDataTableName(source)
  47. 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=? `
  48. sql = fmt.Sprintf(sql, tableName)
  49. err = o.Raw(sql, edbCode).QueryRow(&item)
  50. var latest_value float64
  51. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  52. sql = fmt.Sprintf(sql, tableName)
  53. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  54. item.LatestValue = latest_value
  55. return
  56. }
  57. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. 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=? `
  60. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  61. return
  62. }
  63. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  64. o := orm.NewOrmUsingDB("data")
  65. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  66. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  67. return
  68. }
  69. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  70. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  71. num := len(edbInfoIdList)
  72. if num <= 0 {
  73. return
  74. }
  75. o := orm.NewOrmUsingDB("data")
  76. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  77. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  78. return
  79. }
  80. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  81. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  82. if err != nil && err.Error() != utils.ErrNoRow() {
  83. return
  84. }
  85. for _, item := range calculateList {
  86. if item.EdbType == 1 {
  87. baseEdbInfoArr = append(baseEdbInfoArr, item)
  88. } else {
  89. calculateInfoArr = append(calculateInfoArr, item)
  90. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  91. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  92. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  93. }
  94. }
  95. return
  96. }
  97. type EdbInfoSearchData struct {
  98. DataTime string `description:"数据日期"`
  99. Value float64 `description:"数据"`
  100. }
  101. // GetEdbDataListAll
  102. // order:1升序,其余值为降序
  103. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  104. o := orm.NewOrmUsingDB("data")
  105. sql := ``
  106. tableName := GetEdbDataTableName(source)
  107. if tableName == "" {
  108. err = fmt.Errorf("指标来源有误, source: %d", source)
  109. return
  110. }
  111. sql = ` SELECT * FROM %s WHERE 1=1 `
  112. sql = fmt.Sprintf(sql, tableName)
  113. if condition != "" {
  114. sql += condition
  115. }
  116. if order == 1 {
  117. sql += ` ORDER BY data_time ASC `
  118. } else {
  119. sql += ` ORDER BY data_time DESC `
  120. }
  121. _, err = o.Raw(sql, pars).QueryRows(&item)
  122. return
  123. }