edb_data_base.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/utils"
  6. "time"
  7. )
  8. //指标检索数据
  9. type EdbDataItem struct {
  10. EdbCode string `description:"指标编码"`
  11. StartDate string `description:"起始日期"`
  12. EndDate string `description:"终止日期"`
  13. EdbName string `description:"指标名称"`
  14. Unit string `description:"单位"`
  15. Frequency string `description:"频率"`
  16. DataList []*EdbData
  17. }
  18. type EdbData struct {
  19. EdbDataId int `orm:"column(edb_data_id);pk"`
  20. EdbInfoId int
  21. EdbCode string
  22. DataTime string
  23. Value string
  24. Status int
  25. CreateTime time.Time
  26. ModifyTime time.Time
  27. DataTimestamp int64
  28. }
  29. func GetEdbDataAllByEdbCode(edbCode string, source, limit int) (items []*EdbData, err error) {
  30. var pars []interface{}
  31. pars = append(pars, edbCode)
  32. o := orm.NewOrm()
  33. tableName := GetEdbDataTableName(source)
  34. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  35. if limit > 0 {
  36. sql += ` LIMIT ? `
  37. pars = append(pars, limit)
  38. }
  39. sql = fmt.Sprintf(sql, tableName)
  40. _, err = o.Raw(sql, pars).QueryRows(&items)
  41. return
  42. }
  43. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  44. nowStr := time.Now().Format(utils.FormatDateTime)
  45. addSql += "("
  46. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  47. "," + "'" + nowStr + "'" + "," + "1"
  48. addSql += "," + "'" + timestampStr + "'"
  49. addSql += "),"
  50. return
  51. }
  52. type EdbDataMaxAndMinInfo struct {
  53. MinDate string `description:"最小日期"`
  54. MaxDate string `description:"最大日期"`
  55. MinValue float64 `description:"最小值"`
  56. MaxValue float64 `description:"最大值"`
  57. LatestValue float64 `description:"最新值"`
  58. }
  59. func GetEdbDataMaxAndMinInfo(source int, edbCode string) (item *EdbDataMaxAndMinInfo, err error) {
  60. o := orm.NewOrm()
  61. sql := ``
  62. tableName := GetEdbDataTableName(source)
  63. 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=? `
  64. sql = fmt.Sprintf(sql, tableName)
  65. err = o.Raw(sql, edbCode).QueryRow(&item)
  66. var latest_value float64
  67. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  68. sql = fmt.Sprintf(sql, tableName)
  69. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  70. item.LatestValue = latest_value
  71. return
  72. }
  73. type AddEdbInfoReq struct {
  74. EdbCode string `description:"指标编码"`
  75. }