123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package excel
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/models/data_manage"
- "eta_gn/eta_api/utils"
- "fmt"
- "time"
- )
- type ExcelChartData struct {
- ExcelChartDataId int `orm:"column(excel_chart_data_id);pk" gorm:"primaryKey" `
- ExcelInfoId int `description:"表格id"`
- ExcelChartEdbId int `description:"指标ID"`
- ChartInfoId int `description:"图表id"`
- DataTime string
- Value float64
- ModifyTime time.Time `description:"修改时间"`
- CreateTime time.Time `description:"创建时间"`
- DataTimestamp int64
- }
- func (e *ExcelChartData) TableName() string {
- return "excel_chart_data"
- }
- // 新增
- func (e *ExcelChartData) Add() (err error) {
- err = global.DmSQL["data"].Create(e).Error
- return
- }
- // 修改
- func (e *ExcelChartData) Update(cols []string) (err error) {
- err = global.DmSQL["data"].Select(cols).Updates(e).Error
- return
- }
- // 删除
- func (e *ExcelChartData) Delete() (err error) {
- sql := ` DELETE FROM excel_chart_data WHERE excel_chart_data_id=? `
- err = global.DmSQL["data"].Exec(sql, e.ExcelChartDataId).Error
- return
- }
- // 查询
- func GetExcelChartDataByExcelInfoId(excelInfoId int) (list []*ExcelChartData, err error) {
- sql := ` SELECT *
- FROM excel_chart_data
- WHERE excel_info_id=?
- ORDER BY excel_chart_edb_id ASC, data_time desc, excel_chart_data_id ASC `
- err = global.DmSQL["data"].Raw(sql, excelInfoId).Find(&list).Error
- return
- }
- func BatchUpdateChartEdbData(excelInfoId int, excelEdbMap map[int]*ExcelChartEdb, excelDataMap map[int][]*data_manage.EdbDataList) (err error) {
- to := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- sql := `delete from excel_chart_data where excel_info_id = ?`
- err = to.Exec(sql, excelInfoId).Error
- if err != nil {
- return
- }
- // 图表指标数据入库
- addList := make([]*ExcelChartData, 0)
- for chartEdbId, dataList := range excelDataMap {
- chartEdb, ok := excelEdbMap[chartEdbId]
- if !ok {
- err = fmt.Errorf("chartEdbId:%d not exist", chartEdbId)
- return
- }
- for _, v := range dataList {
- chartData := &ExcelChartData{
- ExcelInfoId: chartEdb.ExcelInfoId,
- ExcelChartEdbId: chartEdb.ExcelChartEdbId,
- ChartInfoId: chartEdb.ChartInfoId,
- DataTime: v.DataTime,
- Value: v.Value,
- DataTimestamp: v.DataTimestamp,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- addList = append(addList, chartData)
- // data信息入库
- if len(addList) > 1000 {
- err = to.CreateInBatches(addList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- addList = addList[:0]
- }
- }
- }
- // data信息入库
- if len(addList) > 0 {
- err = to.CreateInBatches(addList, utils.MultiAddNum).Error
- if err != nil {
- return
- }
- }
- return
- }
- func GetExcelChartDataByChartInfoId(chartInfoId int) (list []*ExcelChartData, err error) {
- sql := ` SELECT *
- FROM excel_chart_Data
- WHERE chart_info_id=?
- ORDER BY excel_chart_edb_id ASC `
- err = global.DmSQL["data"].Raw(sql, chartInfoId).Find(&list).Error
- return
- }
|