package models import ( "eta/eta_forum_hub/utils" "fmt" "github.com/beego/beego/v2/client/orm" "time" ) type ChartSeries struct { ChartSeriesId int `orm:"column(chart_series_id);pk"` SeriesName string `description:"系列名称"` SeriesNameEn string `description:"系列英文名称"` ChartInfoId int `description:"图表ID"` ChartStyle string `description:"图表类型"` ChartColor string `description:"颜色"` ChartWidth int `description:"线条大小"` IsPoint int `description:"是否用数据点展示(0 否,1是)"` IsNumber int `description:"是否用数值展示(0 否,1是)"` IsAxis int `description:"1:左轴,0:右轴"` MaxData float64 `description:"上限"` MinData float64 `description:"下限"` IsOrder bool `description:"true:正序,false:逆序"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` } func (c *ChartSeries) TableName() string { return "chart_series" } func GetChartSeriesByChartInfoId(chartInfoId int) (items []*ChartSeries, err error) { o := orm.NewOrm() sql := "SELECT * FROM chart_series WHERE chart_info_id = ?" _, err = o.Raw(sql, chartInfoId).QueryRows(&items) return } // EditChartSeriesAndEdbMapping func EditChartSeriesAndEdbMapping(seriesList []*ChartSeries, seriesEdbInfoList []*ChartSeriesEdbMapping, chartInfoId int, newEdbInfoMap map[int]int) (err error) { o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() // 获取已经存在的系列 series, err := GetChartSeriesByChartInfoId(chartInfoId) if err != nil { return } //整理成系列map seriesMap := make(map[string]int) seriesDeleteMap := make(map[string]struct{}) for _, v := range series { seriesMap[v.SeriesName] = v.ChartSeriesId seriesDeleteMap[v.SeriesName] = struct{}{} } // 删除所有的指标映射 _, err = o.Raw("DELETE FROM chart_series_edb_mapping WHERE chart_info_id = ?", chartInfoId).Exec() if err != nil { return } seriesEdbMap := make(map[int][]*ChartSeriesEdbMapping) for _, v := range seriesEdbInfoList { seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], v) } for _, v := range seriesList { tmp := &ChartSeries{ SeriesName: v.SeriesName, SeriesNameEn: v.SeriesNameEn, ChartInfoId: chartInfoId, ChartStyle: v.ChartStyle, ChartColor: v.ChartColor, ChartWidth: v.ChartWidth, IsPoint: v.IsPoint, IsNumber: v.IsNumber, IsAxis: v.IsAxis, MaxData: v.MaxData, MinData: v.MinData, ModifyTime: time.Now(), } seriesId, ok := seriesMap[v.SeriesName] if !ok { //新增 tmp.CreateTime = time.Now() seriesIdTmp, e := to.Insert(tmp) if e != nil { err = fmt.Errorf("AddChartSeries Err:" + e.Error()) return } seriesId = int(seriesIdTmp) } else { //编辑 tmp.ChartSeriesId = seriesId delete(seriesDeleteMap, v.SeriesName) _, e := to.Update(tmp) if e != nil { err = fmt.Errorf("UpdateChartSeries Err:" + e.Error()) return } } addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0) mappingList, ok := seriesEdbMap[v.ChartSeriesId] if ok { for _, edbItem := range mappingList { newEdbInfoId, ok1 := newEdbInfoMap[edbItem.EdbInfoId] if ok1 { edbTmp := &ChartSeriesEdbMapping{ ChartSeriesId: seriesId, //todo 系列ID ChartInfoId: chartInfoId, //todo 表图ID EdbInfoId: newEdbInfoId, //EdbAliasName: "", //EdbAliasNameEn: "", DateConfName: edbItem.DateConfName, DateConf: edbItem.DateConf, DateConfType: edbItem.DateConfType, CreateTime: time.Now(), ModifyTime: time.Now(), } addSeriesEdbList = append(addSeriesEdbList, edbTmp) } } if len(addSeriesEdbList) > 0 { _, err = to.InsertMulti(len(addSeriesEdbList), addSeriesEdbList) if err != nil { err = fmt.Errorf("AddChartSeries Err:" + err.Error()) return } } } } //删除旧的系列和ID seriesNames := make([]string, 0) for name, _ := range seriesDeleteMap { seriesNames = append(seriesNames, name) } if len(seriesNames) > 0 { sql := `DELETE FROM chart_series WHERE series_name IN (` + utils.GetOrmInReplace(len(seriesNames)) + `) and chart_info_id=?` _, err = to.Raw(sql, seriesNames, chartInfoId).Exec() if err != nil { err = fmt.Errorf("删除系列失败 Err:" + err.Error()) return } } return } func AddChartSeriesAndEdbMapping(seriesList []*ChartSeries, seriesEdbInfoList []*ChartSeriesEdbMapping, chartInfoId int, newEdbInfoMap map[int]int) (err error) { o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() seriesEdbMap := make(map[int][]*ChartSeriesEdbMapping) for _, v := range seriesEdbInfoList { seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], v) } for _, v := range seriesList { tmp := &ChartSeries{ SeriesName: v.SeriesName, SeriesNameEn: v.SeriesNameEn, ChartInfoId: chartInfoId, ChartStyle: v.ChartStyle, ChartColor: v.ChartColor, ChartWidth: v.ChartWidth, IsPoint: v.IsPoint, IsNumber: v.IsNumber, IsAxis: v.IsAxis, MaxData: v.MaxData, MinData: v.MinData, CreateTime: time.Now(), ModifyTime: time.Now(), } seriesIdTmp, e := to.Insert(tmp) if e != nil { err = fmt.Errorf("AddChartSeries Err:" + e.Error()) return } seriesId := int(seriesIdTmp) addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0) mappingList, ok := seriesEdbMap[v.ChartSeriesId] if ok { for _, edbItem := range mappingList { newEdbInfoId, ok1 := newEdbInfoMap[edbItem.EdbInfoId] if ok1 { edbTmp := &ChartSeriesEdbMapping{ ChartSeriesId: seriesId, //todo 系列ID ChartInfoId: chartInfoId, //todo 表图ID EdbInfoId: newEdbInfoId, //EdbAliasName: "", //EdbAliasNameEn: "", DateConfName: edbItem.DateConfName, DateConfType: edbItem.DateConfType, DateConf: edbItem.DateConf, CreateTime: time.Now(), ModifyTime: time.Now(), } addSeriesEdbList = append(addSeriesEdbList, edbTmp) } } if len(addSeriesEdbList) > 0 { _, e = to.InsertMulti(len(addSeriesEdbList), addSeriesEdbList) if e != nil { err = fmt.Errorf("AddChartSeries Err:" + e.Error()) return } } } } return } func DeleteChartSeriesAndEdbMapping(chartInfoId int) (err error) { o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() sql := ` DELETE FROM chart_series WHERE chart_info_id=? ` _, err = to.Raw(sql, chartInfoId).Exec() if err != nil { return } sql = ` DELETE FROM chart_series_edb_mapping WHERE chart_info_id=? ` _, err = to.Raw(sql, chartInfoId).Exec() return }