excel_chart_data.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package excel
  2. import (
  3. "eta/eta_mobile/models/data_manage"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type ExcelChartData struct {
  9. ExcelChartDataId int `orm:"column(excel_chart_data_id);pk"`
  10. ExcelInfoId int `description:"表格id"`
  11. ExcelChartEdbId int `description:"指标ID"`
  12. ChartInfoId int `description:"图表id"`
  13. DataTime string
  14. Value float64
  15. ModifyTime time.Time `description:"修改时间"`
  16. CreateTime time.Time `description:"创建时间"`
  17. DataTimestamp int64
  18. }
  19. func (e *ExcelChartData) TableName() string {
  20. return "excel_chart_data"
  21. }
  22. // 新增
  23. func (e *ExcelChartData) Add() (err error) {
  24. o := orm.NewOrmUsingDB("data")
  25. _, err = o.Insert(e)
  26. return
  27. }
  28. // 修改
  29. func (e *ExcelChartData) Update(cols []string) (err error) {
  30. o := orm.NewOrmUsingDB("data")
  31. _, err = o.Update(e, cols...)
  32. return
  33. }
  34. // 删除
  35. func (e *ExcelChartData) Delete() (err error) {
  36. o := orm.NewOrmUsingDB("data")
  37. _, err = o.Delete(e)
  38. return
  39. }
  40. // 查询
  41. func GetExcelChartDataByExcelInfoId(excelInfoId int) (list []*ExcelChartData, err error) {
  42. o := orm.NewOrmUsingDB("data")
  43. sql := ` SELECT *
  44. FROM excel_chart_data
  45. WHERE excel_info_id=?
  46. ORDER BY excel_chart_edb_id ASC, data_time desc, excel_chart_data_id ASC `
  47. _, err = o.Raw(sql, excelInfoId).QueryRows(&list)
  48. return
  49. }
  50. func BatchUpdateChartEdbData(excelInfoId int, excelEdbMap map[int]*ExcelChartEdb, excelDataMap map[int][]*data_manage.EdbDataList) (err error) {
  51. o, err := orm.NewOrmUsingDB("data").Begin()
  52. if err != nil {
  53. return
  54. }
  55. defer func() {
  56. if err != nil {
  57. _ = o.Rollback()
  58. } else {
  59. _ = o.Commit()
  60. }
  61. }()
  62. //如果有数据则删除所有的数据
  63. sql := `delete from excel_chart_data where excel_info_id = ?`
  64. _, err = o.Raw(sql, excelInfoId).Exec()
  65. if err != nil {
  66. return
  67. }
  68. // 图表指标数据入库
  69. addList := make([]*ExcelChartData, 0)
  70. for chartEdbId, dataList := range excelDataMap {
  71. chartEdb, ok := excelEdbMap[chartEdbId]
  72. if !ok {
  73. err = fmt.Errorf("chartEdbId:%d not exist", chartEdbId)
  74. return
  75. }
  76. for _, v := range dataList {
  77. chartData := &ExcelChartData{
  78. ExcelInfoId: chartEdb.ExcelInfoId,
  79. ExcelChartEdbId: chartEdb.ExcelChartEdbId,
  80. ChartInfoId: chartEdb.ChartInfoId,
  81. DataTime: v.DataTime,
  82. Value: v.Value,
  83. DataTimestamp: v.DataTimestamp,
  84. ModifyTime: time.Now(),
  85. CreateTime: time.Now(),
  86. }
  87. addList = append(addList, chartData)
  88. // data信息入库
  89. if len(addList) > 1000 {
  90. _, err = o.InsertMulti(len(addList), addList)
  91. if err != nil {
  92. return
  93. }
  94. addList = addList[:0]
  95. }
  96. }
  97. }
  98. // data信息入库
  99. if len(addList) > 0 {
  100. _, err = o.InsertMulti(len(addList), addList)
  101. if err != nil {
  102. return
  103. }
  104. }
  105. return
  106. }
  107. func GetExcelChartDataByChartInfoId(chartInfoId int) (list []*ExcelChartData, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := ` SELECT *
  110. FROM excel_chart_Data
  111. WHERE chart_info_id=?
  112. ORDER BY excel_chart_edb_id ASC `
  113. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  114. return
  115. }