123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- package models
- import (
- "encoding/json"
- "eta/eta_chart_lib/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.NewOrmUsingDB("data")
- sql := "SELECT * FROM chart_series WHERE chart_info_id = ?"
- _, err = o.Raw(sql, chartInfoId).QueryRows(&items)
- return
- }
- // EditChartSeriesAndEdbMapping
- func EditChartSeriesAndEdbMapping(extraConfigStr string, chartInfoId int) (err error) {
- o := orm.NewOrmUsingDB("data")
- 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[int]struct{})
- seriesDeleteMap := make(map[int]struct{})
- for _, v := range series {
- seriesMap[v.ChartSeriesId] = struct{}{}
- seriesDeleteMap[v.ChartSeriesId] = struct{}{}
- }
- var sectionExtraConfig ChartSectionAllExtraConf
- err = json.Unmarshal([]byte(extraConfigStr), §ionExtraConfig)
- if err != nil {
- err = fmt.Errorf("截面组合图配置异常")
- return
- }
- // 删除所有的指标映射
- _, err = o.Raw("DELETE FROM chart_series_edb_mapping WHERE chart_info_id = ?", chartInfoId).Exec()
- if err != nil {
- return
- }
- seriesList := sectionExtraConfig.SeriesList
- for _, v := range seriesList {
- seriesId := v.ChartSeriesId
- tmp := &ChartSeries{
- ChartSeriesId: v.ChartSeriesId,
- 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(),
- }
- if _, ok := seriesMap[v.ChartSeriesId]; !ok {
- if seriesId > 0 {
- err = fmt.Errorf("系列ID错误")
- return
- }
- //新增
- tmp.CreateTime = time.Now()
- seriesIdTmp, e := to.Insert(tmp)
- if e != nil {
- err = fmt.Errorf("AddChartSeries Err:" + e.Error())
- return
- }
- seriesId = int(seriesIdTmp)
- } else {
- //编辑
- delete(seriesDeleteMap, v.ChartSeriesId)
- _, e := to.Update(tmp)
- if e != nil {
- err = fmt.Errorf("UpdateChartSeries Err:" + e.Error())
- return
- }
- }
- addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0)
- for _, edbItem := range v.EdbInfoList {
- dateConfStrByte, e := json.Marshal(edbItem.DateConf)
- if e != nil {
- err = e
- return
- }
- dateConfStr := string(dateConfStrByte)
- edbTmp := &ChartSeriesEdbMapping{
- ChartSeriesId: seriesId, //todo 系列ID
- ChartInfoId: chartInfoId, //todo 表图ID
- EdbInfoId: edbItem.EdbInfoId,
- //EdbAliasName: "",
- //EdbAliasNameEn: "",
- DateConfName: edbItem.DateConfName,
- DateConf: dateConfStr,
- 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
- seriesIds := make([]int, 0)
- for id, _ := range seriesDeleteMap {
- seriesIds = append(seriesIds, id)
- }
- if len(seriesIds) > 0 {
- sql := `DELETE FROM chart_series WHERE chart_series_id IN (` + utils.GetOrmInReplace(len(seriesIds)) + `) and chart_info_id=?`
- _, err = to.Raw(sql, seriesIds, chartInfoId).Exec()
- if err != nil {
- err = fmt.Errorf("删除系列失败 Err:" + err.Error())
- return
- }
- sql = `DELETE FROM chart_series_edb_mapping WHERE chart_series_id IN (` + utils.GetOrmInReplace(len(seriesIds)) + `) and chart_info_id=?`
- _, err = to.Raw(sql, seriesIds, chartInfoId).Exec()
- if err != nil {
- err = fmt.Errorf("删除系列指标 Err:" + err.Error())
- return
- }
- }
- return
- }
- func AddChartSeriesAndEdbMapping(extraConfigStr string, chartInfoId int) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- var sectionExtraConfig ChartSectionAllExtraConf
- err = json.Unmarshal([]byte(extraConfigStr), §ionExtraConfig)
- if err != nil {
- err = fmt.Errorf("截面组合图配置异常")
- return
- }
- seriesList := sectionExtraConfig.SeriesList
- 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,
- IsOrder: false, //todo 是否排序
- 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)
- for _, edbItem := range v.EdbInfoList {
- dateConfStrByte, e := json.Marshal(edbItem.DateConf)
- if e != nil {
- err = e
- return
- }
- dateConfStr := string(dateConfStrByte)
- edbTmp := &ChartSeriesEdbMapping{
- ChartSeriesId: seriesId, //todo 系列ID
- ChartInfoId: chartInfoId, //todo 表图ID
- EdbInfoId: edbItem.EdbInfoId,
- //EdbAliasName: "",
- //EdbAliasNameEn: "",
- DateConfName: edbItem.DateConfName,
- DateConfType: edbItem.DateConfType,
- DateConf: dateConfStr,
- 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.NewOrmUsingDB("data")
- 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
- }
- type ChartSectionSeriesValSortAsc []ChartSectionSeriesValSort
- type ChartSectionSeriesValSortDesc []ChartSectionSeriesValSort
- type ChartSectionSeriesValSort struct {
- Index int
- Value float64
- }
- func (s ChartSectionSeriesValSortAsc) Len() int {
- return len(s)
- }
- func (s ChartSectionSeriesValSortAsc) Less(i, j int) bool {
- return s[i].Value < s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
- }
- func (s ChartSectionSeriesValSortAsc) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
- }
- func (s ChartSectionSeriesValSortDesc) Len() int {
- return len(s)
- }
- func (s ChartSectionSeriesValSortDesc) Less(i, j int) bool {
- return s[i].Value > s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
- }
- func (s ChartSectionSeriesValSortDesc) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
- }
|