base_from_icpi.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type BaseFromIcpiIndex struct {
  7. BaseFromIcpiIndexId int `orm:"column(base_from_icpi_index_id);pk"`
  8. BaseFromIcpiClassifyId int `description:"分类id"`
  9. IndexCode string `description:"指标编码"`
  10. IndexName string `description:"指标名称"`
  11. Frequency string `description:"频度"`
  12. Unit string `description:"单位"`
  13. StartDate time.Time `description:"开始日期"`
  14. EndDate time.Time `description:"结束日期"`
  15. CreateTime time.Time `description:"创建时间"`
  16. ModifyTime time.Time `description:"修改时间"`
  17. LatestValue float64 `description:"最新值"`
  18. }
  19. type BaseFromIcpiData struct {
  20. BaseFromIcpiDataId int `orm:"column(base_from_icpi_data_id);pk"`
  21. BaseFromIcpiIndexId int `description:"指标id"`
  22. IndexCode string `description:"指标编码"`
  23. DataTime string `description:"日期"`
  24. Value string `description:"值"`
  25. CreateTime time.Time `description:"创建时间"`
  26. ModifyTime time.Time `description:"修改时间"`
  27. }
  28. type BaseFromIcpiClassify struct {
  29. BaseFromIcpiClassifyId int `orm:"column(base_from_icpi_classify_id);pk"`
  30. ClassifyName string `description:"分类名称"`
  31. ClassifyNameEn string `description:"英文名称"`
  32. ParentId int `description:"上级id"`
  33. CreateTime time.Time `description:"创建时间"`
  34. ModifyTime time.Time `description:"修改时间"`
  35. }
  36. func (obj *BaseFromIcpiIndex) GetBaseFromIcpiIndexAll() (list []*BaseFromIcpiIndex, err error) {
  37. o := orm.NewOrmUsingDB("data")
  38. sql := `SELECT * FROM base_from_icpi_index`
  39. _, err = o.Raw(sql).QueryRows(&list)
  40. return
  41. }
  42. func (m *BaseFromIcpiIndex) Add() (err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. id, err := o.Insert(m)
  45. if err != nil {
  46. return
  47. }
  48. m.BaseFromIcpiIndexId = int(id)
  49. return
  50. }
  51. func (m *BaseFromIcpiIndex) AddClassify(classify *BaseFromIcpiClassify) (err error) {
  52. o := orm.NewOrmUsingDB("data")
  53. id, err := o.Insert(classify)
  54. if err != nil {
  55. return
  56. }
  57. m.BaseFromIcpiIndexId = int(id)
  58. return
  59. }
  60. func (obj *BaseFromIcpiIndex) GetBaseFromIcpiData(indexCode string, limit int) (list []*BaseFromIcpiData, err error) {
  61. o := orm.NewOrmUsingDB("data")
  62. sql := `SELECT * FROM base_from_icpi_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?`
  63. _, err = o.Raw(sql, indexCode, limit).QueryRows(&list)
  64. return
  65. }
  66. func (m *BaseFromIcpiIndex) AddData(dataItem *BaseFromIcpiData) (err error) {
  67. o := orm.NewOrmUsingDB("data")
  68. _, err = o.Insert(dataItem)
  69. if err != nil {
  70. return
  71. }
  72. return
  73. }
  74. func (m *BaseFromIcpiIndex) ModifyData(indexCode, dataTime, value string) (err error) {
  75. o := orm.NewOrmUsingDB("data")
  76. sql := ` UPDATE base_from_icpi_data SET value=? WHERE index_code=? AND data_time=? `
  77. _, err = o.Raw(sql, value, indexCode, dataTime).Exec()
  78. return
  79. }
  80. // EdbInfoMaxAndMinInfo 指标最新数据记录结构体
  81. type IndexMaxAndMinInfo struct {
  82. MinDate string `description:"最小日期"`
  83. MaxDate string `description:"最大日期"`
  84. MinValue float64 `description:"最小值"`
  85. MaxValue float64 `description:"最大值"`
  86. LatestValue float64 `description:"最新值"`
  87. LatestDate string `description:"实际数据最新日期"`
  88. }
  89. // 获取指标的最新数据记录信息
  90. func (m *BaseFromIcpiIndex) GetBaseFromIcpiIndexMaxAndMinInfo(indexCode string) (item *IndexMaxAndMinInfo, err error) {
  91. o := orm.NewOrmUsingDB("data")
  92. 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 base_from_icpi_data WHERE index_code=? `
  93. err = o.Raw(sql, indexCode).QueryRow(&item)
  94. if err != nil {
  95. return
  96. }
  97. // 获取最新日期的值
  98. var latestValue float64
  99. sql = ` SELECT value FROM base_from_icpi_data WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
  100. err = o.Raw(sql, indexCode).QueryRow(&latestValue)
  101. if err != nil {
  102. return
  103. }
  104. item.LatestValue = latestValue
  105. return
  106. }
  107. // 修改指标的最新数据信息
  108. func (m *BaseFromIcpiIndex) ModifyEdbInfoMaxAndMinInfo(indexCode string, item *IndexMaxAndMinInfo) (err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` UPDATE base_from_icpi_index SET start_date=?,end_date=?,modify_time=NOW(),latest_value = ? WHERE index_code=? `
  111. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  112. return
  113. }
  114. type IcpiClassify struct {
  115. Id interface{} `json:"id"`
  116. Category string `json:"category"`
  117. Percentage interface{} `json:"percentage"`
  118. Categoryenglish string `json:"categoryenglish"`
  119. }
  120. func (obj *BaseFromIcpiIndex) GetBaseFromIcpiClassifyAll() (list []*BaseFromIcpiClassify, err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. sql := `SELECT * FROM base_from_icpi_classify `
  123. _, err = o.Raw(sql).QueryRows(&list)
  124. return
  125. }