123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package services
- import (
- "encoding/json"
- "eta/eta_forum_admin/models"
- "eta/eta_forum_admin/models/chart_theme/request"
- "eta/eta_forum_admin/utils"
- )
- // GetChartThemeConfig
- // @Description: 根据主题id获取主题信息,如果获取不到的话,那么就获取默认的主题
- // @author: Roc
- // @datetime 2023-12-19 14:31:17
- // @param chartThemeId int
- // @param chartType int
- // @param source int
- // @return chartTheme *models.ChartTheme
- // @return err error
- func GetChartThemeConfig(chartThemeId, source, chartType int) (chartTheme *models.ChartTheme, err error) {
- chartTheme, err = models.GetChartThemeId(chartThemeId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- err = nil
- // 如果找到了,那么就返回
- if chartTheme != nil {
- // 兼容历史数据,加入新字段LineOptionList
- newConfig, e := ConvertOldChartOptions(chartTheme.Config)
- if e == nil {
- chartTheme.Config = newConfig
- }
- return
- }
- // 没有找到的话,那么就找默认的主题
- // 查找主题类型id
- chartThemeType, err := models.GetChartThemeTypeByChartTypeAndSource(chartType, source)
- if err != nil {
- return
- }
- // 寻找默认的主题id
- chartTheme, err = models.GetChartThemeId(chartThemeType.DefaultChartThemeId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- err = nil
- // 如果找到了,那么就返回
- if chartTheme != nil {
- // 兼容历史数据,加入新字段LineOptionList
- newConfig, e := ConvertOldChartOptions(chartTheme.Config)
- if e == nil {
- chartTheme.Config = newConfig
- }
- return
- }
- // 如果还是没找到,那就系统的主题id
- chartTheme, err = models.GetSystemChartTheme(chartThemeType.ChartThemeTypeId)
- // 兼容历史数据,加入新字段LineOptionList
- newConfig, e := ConvertOldChartOptions(chartTheme.Config)
- if e == nil {
- chartTheme.Config = newConfig
- }
- return
- }
- // 兼容历史数据,加入新字段LineOptionList
- func ConvertOldChartOptions(config string) (newConfig string, err error) {
- var oldTheme request.OldChartOptions
- var newTheme request.NewChartOptions
- err = json.Unmarshal([]byte(config), &oldTheme)
- if err != nil {
- return
- }
- if oldTheme.LineOptionList != nil && len(oldTheme.LineOptionList) > 0 {
- newConfig = config
- return
- }
- newTheme.OldChartOptions = oldTheme
- for i := 0; i < 10; i++ {
- newLineOption := request.NewLineOptions{
- LineOptions: oldTheme.LineOptions,
- Color: oldTheme.ColorsOptions[i],
- DataMark: "none",
- MarkType: "square",
- MarkSize: 5,
- MarkColor: oldTheme.ColorsOptions[i],
- }
- newTheme.LineOptionList = append(newTheme.LineOptionList, newLineOption)
- }
- newThemeStr, _ := json.Marshal(newTheme)
- newConfig = string(newThemeStr)
- return
- }
|