chart_theme.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/data_manage/chart_theme"
  8. "eta/eta_api/models/data_manage/chart_theme/request"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/services/eta_forum"
  11. "eta/eta_api/utils"
  12. "time"
  13. )
  14. // ChartThemeController
  15. // @Description: 图表主题配置模块
  16. type ChartThemeController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 获取主题列表
  21. // @Description 获取主题列表接口
  22. // @Param ChartThemeTypeId query int true "图表主题类型id"
  23. // @Success 200 {object} []*chart_theme.ChartTheme
  24. // @router /chart/theme/list [get]
  25. func (c *ChartThemeController) List() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. c.Data["json"] = br
  29. c.ServeJSON()
  30. }()
  31. sysUser := c.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. chartThemeTypeId, _ := c.GetInt("ChartThemeTypeId")
  39. if chartThemeTypeId <= 0 {
  40. br.Msg = "请选择图表样式类型"
  41. br.ErrMsg = "请选择图表样式类型"
  42. br.IsSendEmail = false
  43. return
  44. }
  45. list, err := chart_theme.GetChartThemeItemList(chartThemeTypeId)
  46. if err != nil {
  47. br.Msg = "获取失败"
  48. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  49. return
  50. }
  51. // 兼容历史数据,加入新字段LineOptionList
  52. for i, v := range list {
  53. newConfig, e := data.ConvertOldChartOptions(v.Config)
  54. if e != nil {
  55. continue
  56. }
  57. list[i].Config = newConfig
  58. }
  59. br.Ret = 200
  60. br.Success = true
  61. br.Msg = "获取成功"
  62. br.Data = list
  63. }
  64. // TypeList
  65. // @Title 获取主题类型列表
  66. // @Description 获取主题类型列表接口
  67. // @Success 200 {object} []*chart_theme.ChartThemeType
  68. // @router /chart/theme/type/list [get]
  69. func (c *ChartThemeController) TypeList() {
  70. br := new(models.BaseResponse).Init()
  71. defer func() {
  72. c.Data["json"] = br
  73. c.ServeJSON()
  74. }()
  75. sysUser := c.SysUser
  76. if sysUser == nil {
  77. br.Msg = "请登录"
  78. br.ErrMsg = "请登录,SysUser Is Empty"
  79. br.Ret = 408
  80. return
  81. }
  82. listTmp, err := chart_theme.GetAllChartThemeTypeList()
  83. if err != nil {
  84. br.Msg = "获取失败"
  85. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  86. return
  87. }
  88. //遍历list,将id和name组成map
  89. chartTypeMap := make(map[int][]*chart_theme.ChartThemeTypeList)
  90. for _, v := range listTmp {
  91. if v.ParentId > 0 {
  92. chartTypeMap[v.ParentId] = append(chartTypeMap[v.ParentId], v)
  93. }
  94. }
  95. list := make([]*chart_theme.ChartThemeTypeList, 0)
  96. for _, v := range listTmp {
  97. if v.ParentId == 0 {
  98. tmp := v
  99. child, ok := chartTypeMap[v.ChartType]
  100. if ok {
  101. tmp.Child = child
  102. }
  103. list = append(list, tmp)
  104. }
  105. }
  106. br.Ret = 200
  107. br.Success = true
  108. br.Msg = "获取成功"
  109. br.Data = list
  110. }
  111. // GetThemePreviewData
  112. // @Title 获取默认的预览图表数据
  113. // @Description 获取默认的预览图表数据接口
  114. // @Param ChartThemeTypeId query int true "图表主题类型id"
  115. // @Success 200 {object} data_manage.ChartInfoDetailResp
  116. // @router /chart/theme/preview_data [get]
  117. func (c *ChartThemeController) GetThemePreviewData() {
  118. br := new(models.BaseResponse).Init()
  119. defer func() {
  120. c.Data["json"] = br
  121. c.ServeJSON()
  122. }()
  123. sysUser := c.SysUser
  124. if sysUser == nil {
  125. br.Msg = "请登录"
  126. br.ErrMsg = "请登录,SysUser Is Empty"
  127. br.Ret = 408
  128. return
  129. }
  130. chartThemeTypeId, _ := c.GetInt("ChartThemeTypeId")
  131. if chartThemeTypeId <= 0 {
  132. br.Msg = "请选择图表样式类型"
  133. br.ErrMsg = "请选择图表样式类型"
  134. br.IsSendEmail = false
  135. return
  136. }
  137. chartThemeType, err := chart_theme.GetChartThemeTypeById(chartThemeTypeId)
  138. if err != nil {
  139. br.Msg = "获取失败"
  140. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  141. return
  142. }
  143. dateType := 3 // 曲线图
  144. startYear := 5
  145. chartType := chartThemeType.ChartType
  146. chartInfo := new(data_manage.ChartInfoView)
  147. // 图表额外数据参数
  148. extraConfigStr := ``
  149. var barConfig data_manage.BarChartInfoReq
  150. // 开始时间,结束时间
  151. var tmpStartDate, tmpEndDate string
  152. edbInfoIdList := make([]int, 0) // 指标列表
  153. switch chartType {
  154. case 1: // 曲线图
  155. edbInfoIdList = []int{1, 2, 3, 4, 5}
  156. chartInfo.ChartName = "曲线图"
  157. case 4: // 堆积柱状图
  158. edbInfoIdList = []int{1, 2, 3, 4, 5}
  159. chartInfo.ChartName = "堆积柱状图"
  160. dateType = 5
  161. tmpStartDate = `2021-03-05`
  162. case 6: // 组合图
  163. edbInfoIdList = []int{1, 2, 3, 4, 5}
  164. chartInfo.ChartName = "组合图"
  165. dateType = 5
  166. tmpStartDate = `2021-03-05`
  167. case 2: // 季节性
  168. dateType = 20 // 季节性图
  169. edbInfoIdList = []int{6}
  170. chartInfo.ChartName = "季节性图"
  171. case 5: // 散点图
  172. edbInfoIdList = []int{7, 8}
  173. chartInfo.ChartName = "散点图"
  174. case 7: // 柱形图
  175. edbInfoIdList = []int{1, 2, 3, 4, 5}
  176. chartInfo.LeftMin = "260"
  177. chartInfo.LeftMax = "430"
  178. extraConfigStr = `{"EdbInfoIdList":[{"EdbInfoId":1,"Name":"指标1","NameEn":"","Source":0},{"EdbInfoId":2,"Name":"指标2","NameEn":"","Source":0},{"EdbInfoId":3,"Name":"指标3","NameEn":"","Source":0},{"EdbInfoId":4,"Name":"指标4","NameEn":"","Source":0},{"EdbInfoId":5,"Name":"指标5","NameEn":"","Source":0}],"DateList":[{"Type":2,"Date":"","Value":100,"Color":"","Name":""},{"Type":1,"Date":"","Value":0,"Color":"","Name":""}],"Sort":{"Sort":0,"DateIndex":0},"XEdbList":null,"YEdbList":null,"Unit":"千桶","UnitEn":""}`
  179. chartInfo.ChartName = "柱形图"
  180. case 10: // 截面散点图
  181. edbInfoIdList = []int{9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
  182. extraConfigStr = `{"XName":"123","XNameEn":"123","XUnitName":"无","XUnitNameEn":"none","YName":"321","YNameEn":"321","YUnitName":"无","YUnitNameEn":"none","XMinValue":"230","XMaxValue":"395","YMinValue":"380","YMaxValue":"850","SeriesList":[{"Name":"2021-11-21","NameEn":"2021-11-21","IsNameDefault":true,"Color":"","EdbInfoList":[{"XEdbInfoId":9,"YEdbInfoId":14,"Name":"指标1","NameEn":"","XDateType":1,"XDate":"","XDateValue":0,"YDateType":1,"YDate":"","YDateValue":0,"IsShow":false},{"XEdbInfoId":10,"YEdbInfoId":15,"Name":"指标2","NameEn":"指标2","XDateType":1,"XDate":"","XDateValue":0,"YDateType":1,"YDate":"","YDateValue":0,"IsShow":false},{"XEdbInfoId":11,"YEdbInfoId":16,"Name":"指标3","NameEn":"","XDateType":1,"XDate":"","XDateValue":0,"YDateType":1,"YDate":"","YDateValue":0,"IsShow":false},{"XEdbInfoId":12,"YEdbInfoId":17,"Name":"指标4","NameEn":"指标4","XDateType":1,"XDate":"","XDateValue":0,"YDateType":1,"YDate":"","YDateValue":0,"IsShow":false},{"XEdbInfoId":13,"YEdbInfoId":18,"Name":"指标5","NameEn":"","XDateType":1,"XDate":"","XDateValue":0,"YDateType":1,"YDate":"","YDateValue":0,"IsShow":false}],"ShowTrendLine":false,"ShowFitEquation":false,"ShowRSquare":false}]}`
  183. chartInfo.ChartName = "截面散点图"
  184. case utils.CHART_TYPE_RADAR:
  185. edbInfoIdList = []int{19, 20, 21, 22, 23, 24}
  186. chartInfo.LeftMin = "10000"
  187. chartInfo.LeftMax = "60000"
  188. extraConfigStr = `{"DateList":[{"Type":3,"Date":"2023-11-01","Value":0,"Color":"","Name":""},{"Type":1,"Date":"","Value":0,"Color":"","Name":""}]}`
  189. chartInfo.ChartName = "雷达图"
  190. case utils.CHART_TYPE_SECTION_COMBINE:
  191. edbInfoIdList = []int{19, 20, 21, 22, 23, 24, 25, 28}
  192. chartInfo.LeftMin = "0"
  193. chartInfo.LeftMax = "4000"
  194. extraConfigStr = `{"DateConfList":[],"IsHeap":0,"XDataList":[{"Name":"内销"},{"Name":"出口"},{"Name":"销量"},{"Name":"产量"}],"UnitList":{"LeftName":"万台","RightName":"%","RightTwoName":""},"BaseChartSeriesName":"增量","SortType":0,"SeriesList":[{"ChartSeriesId":0,"SeriesName":"增量","ChartStyle":"column","ChartColor":"rgba(0, 0, 255, 1)","ChartWidth":1,"IsPoint":0,"IsNumber":0,"IsAxis":1,"EdbInfoList":[{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":19,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}},{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":20,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}},{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":21,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}},{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":22,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}}]},{"ChartSeriesId":0,"SeriesName":"增速","ChartStyle":"spline","ChartColor":"rgba(255, 0, 200, 1)","ChartWidth":1,"IsPoint":0,"IsNumber":0,"IsAxis":0,"EdbInfoList":[{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":23,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}},{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":24,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}},{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":25,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}},{"ChartSeriesEdbMappingId":0,"ChartSeriesId":0,"EdbInfoId":26,"DateConfName":"","DateConfType":0,"DateConf":{"MoveForward":0,"DateChange":[]}}]}]}`
  195. chartInfo.ChartName = "图表标题"
  196. default:
  197. br.Msg = "暂不支持该类型"
  198. br.IsSendEmail = false
  199. return
  200. }
  201. calendar := "公历"
  202. mappingList, err := chart_theme.GetChartEdbMappingListByEdbInfoId(edbInfoIdList)
  203. if err != nil {
  204. br.Msg = "获取失败"
  205. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  206. return
  207. }
  208. switch chartType {
  209. case 6: // 组合图
  210. mappingList[0].ChartStyle = "spline"
  211. mappingList[1].ChartStyle = "spline"
  212. mappingList[2].ChartStyle = "spline"
  213. mappingList[3].ChartStyle = "column"
  214. mappingList[4].ChartStyle = "column"
  215. }
  216. chartInfo.Source = chartThemeType.ChartSource
  217. chartInfo.ChartType = chartThemeType.ChartType
  218. var dateMax time.Time
  219. if dateType == utils.DateTypeNYears {
  220. for _, v := range mappingList {
  221. if v.LatestDate != "" {
  222. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  223. if tErr != nil {
  224. br.Msg = "获取失败"
  225. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  226. return
  227. }
  228. if lastDateT.After(dateMax) {
  229. dateMax = lastDateT
  230. }
  231. }
  232. }
  233. }
  234. if chartType == utils.CHART_TYPE_SEASON && dateType == utils.DateTypeNYears {
  235. dateMax = time.Date(dateMax.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  236. }
  237. // 开始/结束日期
  238. startDate, endDate := utils.GetDateByDateTypeV2(dateType, tmpStartDate, tmpEndDate, startYear, dateMax)
  239. // 获取图表中的指标数据
  240. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetThemePreviewChartEdbData(chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  241. if err != nil {
  242. br.Msg = "获取失败"
  243. if errMsg != `` {
  244. br.Msg = errMsg
  245. }
  246. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  247. return
  248. }
  249. switch chartType {
  250. case 6: // 组合图
  251. edbList[0].MaxData = 1000
  252. case 4: // 堆积柱状图
  253. edbList[0].MaxData = 2500
  254. }
  255. // 单位
  256. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  257. chartInfo.Unit = yDataList[0].Unit
  258. chartInfo.UnitEn = yDataList[0].UnitEn
  259. }
  260. resp := new(data_manage.ChartInfoDetailResp)
  261. resp.ChartInfo = chartInfo
  262. resp.EdbInfoList = edbList
  263. resp.XEdbIdValue = xEdbIdValue
  264. resp.YDataList = yDataList
  265. resp.DataResp = dataResp
  266. resp.BarChartInfo = barConfig
  267. br.Ret = 200
  268. br.Success = true
  269. br.Msg = "获取成功"
  270. br.Data = resp
  271. }
  272. // Add
  273. // @Title 新增主题
  274. // @Description
  275. // @Param request body request.DeleteThemeConfReq true "type json string"
  276. // @Success 200 Ret=200 添加成功
  277. // @router /chart/theme/add [post]
  278. func (c *ChartThemeController) Add() {
  279. br := new(models.BaseResponse).Init()
  280. defer func() {
  281. c.Data["json"] = br
  282. c.ServeJSON()
  283. }()
  284. sysUser := c.SysUser
  285. if sysUser == nil {
  286. br.Msg = "请登录"
  287. br.ErrMsg = "请登录,SysUser Is Empty"
  288. br.Ret = 408
  289. return
  290. }
  291. var req request.AddThemeReq
  292. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  293. if err != nil {
  294. br.Msg = "参数解析异常!"
  295. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  296. return
  297. }
  298. if req.ChartThemeTypeId <= 0 {
  299. br.Msg = "请选择图表类型"
  300. br.IsSendEmail = false
  301. return
  302. }
  303. req.ChartThemeName = utils.TrimLRStr(req.ChartThemeName)
  304. if req.ChartThemeName == `` {
  305. br.Msg = "请输入主题名称"
  306. br.IsSendEmail = false
  307. return
  308. }
  309. // 获取系统默认配置
  310. systemChartTheme, err := chart_theme.GetSystemChartTheme(req.ChartThemeTypeId)
  311. if err != nil {
  312. br.Msg = "添加失败"
  313. br.ErrMsg = "添加失败,查询系统默认配置异常,Err:" + err.Error()
  314. return
  315. }
  316. chartTheme := &chart_theme.ChartTheme{
  317. ChartThemeId: 0,
  318. ChartThemeName: req.ChartThemeName,
  319. ChartThemeTypeId: req.ChartThemeTypeId,
  320. Config: systemChartTheme.Config,
  321. ChartImage: systemChartTheme.ChartImage,
  322. IsDelete: 0,
  323. SysUserId: sysUser.AdminId,
  324. SysUserRealName: sysUser.RealName,
  325. ModifyTime: time.Now(),
  326. CreateTime: time.Now(),
  327. }
  328. // 添加入库
  329. err = chartTheme.Add()
  330. if err != nil {
  331. br.Msg = "添加失败"
  332. br.ErrMsg = "添加失败,Err:" + err.Error()
  333. return
  334. }
  335. go eta_forum.ChartThemeSave(chartTheme)
  336. br.Ret = 200
  337. br.Success = true
  338. br.Msg = "添加成功"
  339. }
  340. // Edit
  341. // @Title 编辑主题
  342. // @Description
  343. // @Param request body request.DeleteThemeConfReq true "type json string"
  344. // @Success 200 Ret=200 修改成功
  345. // @router /chart/theme/edit [post]
  346. func (c *ChartThemeController) Edit() {
  347. br := new(models.BaseResponse).Init()
  348. defer func() {
  349. c.Data["json"] = br
  350. c.ServeJSON()
  351. }()
  352. sysUser := c.SysUser
  353. if sysUser == nil {
  354. br.Msg = "请登录"
  355. br.ErrMsg = "请登录,SysUser Is Empty"
  356. br.Ret = 408
  357. return
  358. }
  359. var req request.EditThemeReq
  360. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  361. if err != nil {
  362. br.Msg = "参数解析异常!"
  363. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  364. return
  365. }
  366. if req.ChartThemeId < 0 {
  367. br.Msg = "请选择配置"
  368. br.IsSendEmail = false
  369. return
  370. }
  371. // 获取配置
  372. chartTheme, err := chart_theme.GetChartThemeId(req.ChartThemeId)
  373. if err != nil {
  374. if err.Error() == utils.ErrNoRow() {
  375. br.Msg = "主题不存在或已删除"
  376. br.ErrMsg = "主题不存在或已删除"
  377. br.IsSendEmail = false
  378. } else {
  379. br.Msg = "获取失败"
  380. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  381. }
  382. return
  383. }
  384. // 更新
  385. if req.ChartThemeName != `` {
  386. chartTheme.ChartThemeName = req.ChartThemeName
  387. }
  388. if req.Config != `` {
  389. chartTheme.Config = req.Config
  390. }
  391. if req.ChartImage != `` {
  392. chartTheme.ChartImage = req.ChartImage
  393. }
  394. chartTheme.ModifyTime = time.Now()
  395. err = chartTheme.Update([]string{"ChartThemeName", "Config", "ChartImage", "ModifyTime"})
  396. if err != nil {
  397. br.Msg = "修改失败"
  398. br.ErrMsg = "修改失败,Err:" + err.Error()
  399. return
  400. }
  401. go eta_forum.ChartThemeSave(chartTheme)
  402. br.Ret = 200
  403. br.Success = true
  404. br.Msg = "修改成功"
  405. }
  406. // Delete
  407. // @Title 删除主题
  408. // @Description
  409. // @Param request body request.DeleteThemeConfReq true "type json string"
  410. // @Success 200 Ret=200 删除成功
  411. // @router /chart/theme/delete [post]
  412. func (c *ChartThemeController) Delete() {
  413. br := new(models.BaseResponse).Init()
  414. defer func() {
  415. c.Data["json"] = br
  416. c.ServeJSON()
  417. }()
  418. sysUser := c.SysUser
  419. if sysUser == nil {
  420. br.Msg = "请登录"
  421. br.ErrMsg = "请登录,SysUser Is Empty"
  422. br.Ret = 408
  423. return
  424. }
  425. var req request.DeleteThemeReq
  426. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  427. if err != nil {
  428. br.Msg = "参数解析异常!"
  429. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  430. return
  431. }
  432. if req.ChartThemeId < 0 {
  433. br.Msg = "请选择配置"
  434. br.IsSendEmail = false
  435. return
  436. }
  437. // 获取配置
  438. chartTheme, err := chart_theme.GetChartThemeId(req.ChartThemeId)
  439. if err != nil {
  440. if err.Error() == utils.ErrNoRow() {
  441. br.Msg = "主题不存在或已删除"
  442. br.ErrMsg = "主题不存在或已删除"
  443. br.IsSendEmail = false
  444. } else {
  445. br.Msg = "获取失败"
  446. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  447. }
  448. return
  449. }
  450. if chartTheme.IsSystemTheme == 1 {
  451. br.Msg = "系统默认配置,无法删除"
  452. br.ErrMsg = "系统默认配置,无法删除"
  453. br.IsSendEmail = false
  454. return
  455. }
  456. // 根据给定的图表主题类型ID获取图表主题类型
  457. chartThemeType, err := chart_theme.GetChartThemeTypeById(chartTheme.ChartThemeTypeId)
  458. if err != nil {
  459. br.Msg = "获取失败"
  460. br.ErrMsg = "获取图表主题类型信息失败,Err:" + err.Error()
  461. return
  462. }
  463. // 判断是否是配置为默认设置,如果已经设置了,那么不能删除
  464. if chartThemeType.DefaultChartThemeId == chartTheme.ChartThemeId {
  465. br.Msg = "该主题已经配置为默认设置,不允许删除"
  466. br.ErrMsg = "该主题已经配置为默认设置,不允许删除"
  467. br.IsSendEmail = false
  468. return
  469. }
  470. // 更新
  471. chartTheme.IsDelete = 1
  472. err = chartTheme.Update([]string{"IsDelete"})
  473. if err != nil {
  474. br.Msg = "删除失败"
  475. br.ErrMsg = "删除失败,Err:" + err.Error()
  476. return
  477. }
  478. go eta_forum.ChartThemeSave(chartTheme)
  479. br.Ret = 200
  480. br.Success = true
  481. br.Msg = "删除成功"
  482. }
  483. // SetDefaultTheme
  484. // @Title 配置默认主题
  485. // @Description 配置默认主题
  486. // @Param request body request.DeleteThemeConfReq true "type json string"
  487. // @Success 200 Ret=200 配置成功
  488. // @router /chart/theme/set_default [post]
  489. func (c *ChartThemeController) SetDefaultTheme() {
  490. br := new(models.BaseResponse).Init()
  491. defer func() {
  492. c.Data["json"] = br
  493. c.ServeJSON()
  494. }()
  495. sysUser := c.SysUser
  496. if sysUser == nil {
  497. br.Msg = "请登录"
  498. br.ErrMsg = "请登录,SysUser Is Empty"
  499. br.Ret = 408
  500. return
  501. }
  502. var req request.SetDefaultThemeReq
  503. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  504. if err != nil {
  505. br.Msg = "参数解析异常!"
  506. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  507. return
  508. }
  509. if req.ChartThemeId < 0 {
  510. br.Msg = "请选择配置"
  511. br.IsSendEmail = false
  512. return
  513. }
  514. if req.ChartThemeTypeId < 0 {
  515. br.Msg = "请选择图表类型"
  516. br.IsSendEmail = false
  517. return
  518. }
  519. // 获取图表配置
  520. chartTheme, err := chart_theme.GetChartThemeId(req.ChartThemeId)
  521. if err != nil {
  522. if err.Error() == utils.ErrNoRow() {
  523. br.Msg = "主题不存在或已删除"
  524. br.ErrMsg = "主题不存在或已删除"
  525. br.IsSendEmail = false
  526. } else {
  527. br.Msg = "获取失败"
  528. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  529. }
  530. return
  531. }
  532. // 获取图表类型
  533. chartThemeType, err := chart_theme.GetChartThemeTypeById(req.ChartThemeTypeId)
  534. if err != nil {
  535. if err.Error() == utils.ErrNoRow() {
  536. br.Msg = "获取图表类型不存在或已删除"
  537. br.ErrMsg = "获取图表类型不存在或已删除"
  538. br.IsSendEmail = false
  539. } else {
  540. br.Msg = "获取失败"
  541. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  542. }
  543. return
  544. }
  545. // 判断主题与图表类型是否相符
  546. if chartTheme.ChartThemeTypeId != chartThemeType.ChartThemeTypeId {
  547. br.Msg = "主题与图表类型不符"
  548. br.ErrMsg = "主题与图表类型不符"
  549. br.IsSendEmail = false
  550. return
  551. }
  552. // 更新
  553. chartThemeType.DefaultChartThemeId = chartTheme.ChartThemeId
  554. chartThemeType.ModifyTime = time.Now()
  555. err = chartThemeType.Update([]string{"DefaultChartThemeId", "ModifyTime"})
  556. if err != nil {
  557. br.Msg = "配置失败"
  558. br.ErrMsg = "配置失败,Err:" + err.Error()
  559. return
  560. }
  561. go eta_forum.ChartThemeTypeSave(chartThemeType)
  562. br.Ret = 200
  563. br.Success = true
  564. br.Msg = "配置成功"
  565. }
  566. // TypeListBySource
  567. // @Title 根据来源获取主题类型列表
  568. // @Description 获取主题类型列表接口
  569. // @Param Source query int true "图表主题类型id"
  570. // @Success 200 {object} []*chart_theme.ChartThemeType
  571. // @router /chart/theme/type/list_by_source [get]
  572. func (c *ChartThemeController) TypeListBySource() {
  573. br := new(models.BaseResponse).Init()
  574. defer func() {
  575. c.Data["json"] = br
  576. c.ServeJSON()
  577. }()
  578. sysUser := c.SysUser
  579. if sysUser == nil {
  580. br.Msg = "请登录"
  581. br.ErrMsg = "请登录,SysUser Is Empty"
  582. br.Ret = 408
  583. return
  584. }
  585. source, _ := c.GetInt("Source")
  586. if source <= 0 {
  587. br.Msg = "请选择来源"
  588. br.ErrMsg = "请选择来源"
  589. br.IsSendEmail = false
  590. return
  591. }
  592. list, err := chart_theme.GetChartThemeTypeListBySource(source)
  593. if err != nil {
  594. br.Msg = "获取失败"
  595. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  596. return
  597. }
  598. br.Ret = 200
  599. br.Success = true
  600. br.Msg = "获取成功"
  601. br.Data = list
  602. }
  603. // ListBySource
  604. // @Title 根据来源获取主题列表
  605. // @Description 获取主题列表接口
  606. // @Param ChartType query int true "图表类型"
  607. // @Param Source query int true "来源"
  608. // @Success 200 {object} []*chart_theme.ChartTheme
  609. // @router /chart/theme/list_by_source [get]
  610. func (c *ChartThemeController) ListBySource() {
  611. br := new(models.BaseResponse).Init()
  612. defer func() {
  613. c.Data["json"] = br
  614. c.ServeJSON()
  615. }()
  616. sysUser := c.SysUser
  617. if sysUser == nil {
  618. br.Msg = "请登录"
  619. br.ErrMsg = "请登录,SysUser Is Empty"
  620. br.Ret = 408
  621. return
  622. }
  623. chartType, _ := c.GetInt("ChartType")
  624. if chartType < 0 {
  625. br.Msg = "请选择图表类型"
  626. br.ErrMsg = "请选择图表类型"
  627. br.IsSendEmail = false
  628. return
  629. }
  630. source, _ := c.GetInt("Source")
  631. if chartType < 0 {
  632. br.Msg = "请选择图表来源"
  633. br.ErrMsg = "请选择图表来源"
  634. br.IsSendEmail = false
  635. return
  636. }
  637. chartThemeType, err := chart_theme.GetChartThemeTypeByChartTypeAndSource(chartType, source)
  638. if err != nil {
  639. br.Msg = "获取失败"
  640. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  641. return
  642. }
  643. list, err := chart_theme.GetChartThemeItemList(chartThemeType.ChartThemeTypeId)
  644. if err != nil {
  645. br.Msg = "获取失败"
  646. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  647. return
  648. }
  649. // 兼容历史数据,加入新字段LineOptionList
  650. for i, v := range list {
  651. newConfig, e := data.ConvertOldChartOptions(v.Config)
  652. if e != nil {
  653. continue
  654. }
  655. list[i].Config = newConfig
  656. }
  657. br.Ret = 200
  658. br.Success = true
  659. br.Msg = "获取成功"
  660. br.Data = list
  661. }