chart_theme.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_admin/models"
  5. "eta/eta_forum_admin/models/chart_theme/request"
  6. "eta/eta_forum_admin/utils"
  7. )
  8. // GetChartThemeConfig
  9. // @Description: 根据主题id获取主题信息,如果获取不到的话,那么就获取默认的主题
  10. // @author: Roc
  11. // @datetime 2023-12-19 14:31:17
  12. // @param chartThemeId int
  13. // @param chartType int
  14. // @param source int
  15. // @return chartTheme *models.ChartTheme
  16. // @return err error
  17. func GetChartThemeConfig(chartThemeId, source, chartType int) (chartTheme *models.ChartTheme, err error) {
  18. chartTheme, err = models.GetChartThemeId(chartThemeId)
  19. if err != nil && err.Error() != utils.ErrNoRow() {
  20. return
  21. }
  22. err = nil
  23. // 如果找到了,那么就返回
  24. if chartTheme != nil {
  25. // 兼容历史数据,加入新字段LineOptionList
  26. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  27. if e == nil {
  28. chartTheme.Config = newConfig
  29. }
  30. return
  31. }
  32. // 没有找到的话,那么就找默认的主题
  33. // 查找主题类型id
  34. chartThemeType, err := models.GetChartThemeTypeByChartTypeAndSource(chartType, source)
  35. if err != nil {
  36. return
  37. }
  38. // 寻找默认的主题id
  39. chartTheme, err = models.GetChartThemeId(chartThemeType.DefaultChartThemeId)
  40. if err != nil && err.Error() != utils.ErrNoRow() {
  41. return
  42. }
  43. err = nil
  44. // 如果找到了,那么就返回
  45. if chartTheme != nil {
  46. // 兼容历史数据,加入新字段LineOptionList
  47. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  48. if e == nil {
  49. chartTheme.Config = newConfig
  50. }
  51. return
  52. }
  53. // 如果还是没找到,那就系统的主题id
  54. chartTheme, err = models.GetSystemChartTheme(chartThemeType.ChartThemeTypeId)
  55. // 兼容历史数据,加入新字段LineOptionList
  56. newConfig, e := ConvertOldChartOptions(chartTheme.Config)
  57. if e == nil {
  58. chartTheme.Config = newConfig
  59. }
  60. return
  61. }
  62. // 兼容历史数据,加入新字段LineOptionList
  63. func ConvertOldChartOptions(config string) (newConfig string, err error) {
  64. var oldTheme request.OldChartOptions
  65. var newTheme request.NewChartOptions
  66. err = json.Unmarshal([]byte(config), &oldTheme)
  67. if err != nil {
  68. return
  69. }
  70. if oldTheme.LineOptionList != nil && len(oldTheme.LineOptionList) > 0 {
  71. newConfig = config
  72. return
  73. }
  74. newTheme.OldChartOptions = oldTheme
  75. for i := 0; i < 10; i++ {
  76. newLineOption := request.NewLineOptions{
  77. LineOptions: oldTheme.LineOptions,
  78. Color: oldTheme.ColorsOptions[i],
  79. DataMark: "none",
  80. MarkType: "square",
  81. MarkSize: 5,
  82. MarkColor: oldTheme.ColorsOptions[i],
  83. }
  84. newTheme.LineOptionList = append(newTheme.LineOptionList, newLineOption)
  85. }
  86. newThemeStr, _ := json.Marshal(newTheme)
  87. newConfig = string(newThemeStr)
  88. return
  89. }