excel_chart_data.go 3.1 KB

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