base_from_icpi.go 4.5 KB

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