chart_theme.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. // 开始时间,结束时间
  150. var tmpStartDate, tmpEndDate string
  151. edbInfoIdList := make([]int, 0) // 指标列表
  152. switch chartType {
  153. case 1: // 曲线图
  154. edbInfoIdList = []int{1, 2, 3, 4, 5}
  155. chartInfo.ChartName = "曲线图"
  156. case 4: // 堆积柱状图
  157. edbInfoIdList = []int{1, 2, 3, 4, 5}
  158. chartInfo.ChartName = "堆积柱状图"
  159. dateType = 5
  160. tmpStartDate = `2021-03-05`
  161. case 6: // 组合图
  162. edbInfoIdList = []int{1, 2, 3, 4, 5}
  163. chartInfo.ChartName = "组合图"
  164. dateType = 5
  165. tmpStartDate = `2021-03-05`
  166. case 2: // 季节性
  167. dateType = 20 // 季节性图
  168. edbInfoIdList = []int{6}
  169. chartInfo.ChartName = "季节性图"
  170. case 5: // 散点图
  171. edbInfoIdList = []int{7, 8}
  172. chartInfo.ChartName = "散点图"
  173. case 7: // 柱形图
  174. edbInfoIdList = []int{1, 2, 3, 4, 5}
  175. 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":""}`
  176. chartInfo.ChartName = "柱形图"
  177. case 10: // 截面散点图
  178. edbInfoIdList = []int{9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
  179. 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}]}`
  180. chartInfo.ChartName = "截面散点图"
  181. case utils.CHART_TYPE_RADAR:
  182. edbInfoIdList = []int{19, 20, 21, 22, 23, 24}
  183. chartInfo.LeftMin = "10000"
  184. chartInfo.LeftMax = "60000"
  185. extraConfigStr = `{"DateList":[{"Type":3,"Date":"2023-11-01","Value":0,"Color":"","Name":""},{"Type":1,"Date":"","Value":0,"Color":"","Name":""}]}`
  186. chartInfo.ChartName = "雷达图"
  187. case utils.CHART_TYPE_SECTION_COMBINE:
  188. edbInfoIdList = []int{19, 20, 21, 22, 23, 24, 25, 28}
  189. chartInfo.LeftMin = "0"
  190. chartInfo.LeftMax = "4000"
  191. 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":[]}}]}]}`
  192. chartInfo.ChartName = "图表标题"
  193. default:
  194. br.Msg = "暂不支持该类型"
  195. br.IsSendEmail = false
  196. return
  197. }
  198. calendar := "公历"
  199. mappingList, err := chart_theme.GetChartEdbMappingListByEdbInfoId(edbInfoIdList)
  200. if err != nil {
  201. br.Msg = "获取失败"
  202. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  203. return
  204. }
  205. switch chartType {
  206. case 6: // 组合图
  207. mappingList[0].ChartStyle = "spline"
  208. mappingList[1].ChartStyle = "spline"
  209. mappingList[2].ChartStyle = "spline"
  210. mappingList[3].ChartStyle = "column"
  211. mappingList[4].ChartStyle = "column"
  212. }
  213. chartInfo.Source = chartThemeType.ChartSource
  214. chartInfo.ChartType = chartThemeType.ChartType
  215. var dateMax time.Time
  216. if dateType == utils.DateTypeNYears {
  217. for _, v := range mappingList {
  218. if v.LatestDate != "" {
  219. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  220. if tErr != nil {
  221. br.Msg = "获取失败"
  222. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  223. return
  224. }
  225. if lastDateT.After(dateMax) {
  226. dateMax = lastDateT
  227. }
  228. }
  229. }
  230. }
  231. if chartType == utils.CHART_TYPE_SEASON && dateType == utils.DateTypeNYears {
  232. dateMax = time.Date(dateMax.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  233. }
  234. // 开始/结束日期
  235. startDate, endDate := utils.GetDateByDateTypeV2(dateType, tmpStartDate, tmpEndDate, startYear, dateMax)
  236. // 获取图表中的指标数据
  237. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetThemePreviewChartEdbData(chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  238. if err != nil {
  239. br.Msg = "获取失败"
  240. if errMsg != `` {
  241. br.Msg = errMsg
  242. }
  243. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  244. return
  245. }
  246. switch chartType {
  247. case 6: // 组合图
  248. edbList[0].MaxData = 1000
  249. case 4: // 堆积柱状图
  250. edbList[0].MaxData = 2500
  251. }
  252. // 单位
  253. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  254. chartInfo.Unit = yDataList[0].Unit
  255. chartInfo.UnitEn = yDataList[0].UnitEn
  256. }
  257. resp := new(data_manage.ChartInfoDetailResp)
  258. resp.ChartInfo = chartInfo
  259. resp.EdbInfoList = edbList
  260. resp.XEdbIdValue = xEdbIdValue
  261. resp.YDataList = yDataList
  262. resp.DataResp = dataResp
  263. br.Ret = 200
  264. br.Success = true
  265. br.Msg = "获取成功"
  266. br.Data = resp
  267. }
  268. // Add
  269. // @Title 新增主题
  270. // @Description
  271. // @Param request body request.DeleteThemeConfReq true "type json string"
  272. // @Success 200 Ret=200 添加成功
  273. // @router /chart/theme/add [post]
  274. func (c *ChartThemeController) Add() {
  275. br := new(models.BaseResponse).Init()
  276. defer func() {
  277. c.Data["json"] = br
  278. c.ServeJSON()
  279. }()
  280. sysUser := c.SysUser
  281. if sysUser == nil {
  282. br.Msg = "请登录"
  283. br.ErrMsg = "请登录,SysUser Is Empty"
  284. br.Ret = 408
  285. return
  286. }
  287. var req request.AddThemeReq
  288. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  289. if err != nil {
  290. br.Msg = "参数解析异常!"
  291. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  292. return
  293. }
  294. if req.ChartThemeTypeId <= 0 {
  295. br.Msg = "请选择图表类型"
  296. br.IsSendEmail = false
  297. return
  298. }
  299. req.ChartThemeName = utils.TrimLRStr(req.ChartThemeName)
  300. if req.ChartThemeName == `` {
  301. br.Msg = "请输入主题名称"
  302. br.IsSendEmail = false
  303. return
  304. }
  305. // 获取系统默认配置
  306. systemChartTheme, err := chart_theme.GetSystemChartTheme(req.ChartThemeTypeId)
  307. if err != nil {
  308. br.Msg = "添加失败"
  309. br.ErrMsg = "添加失败,查询系统默认配置异常,Err:" + err.Error()
  310. return
  311. }
  312. chartTheme := &chart_theme.ChartTheme{
  313. ChartThemeId: 0,
  314. ChartThemeName: req.ChartThemeName,
  315. ChartThemeTypeId: req.ChartThemeTypeId,
  316. Config: systemChartTheme.Config,
  317. ChartImage: systemChartTheme.ChartImage,
  318. IsDelete: 0,
  319. SysUserId: sysUser.AdminId,
  320. SysUserRealName: sysUser.RealName,
  321. ModifyTime: time.Now(),
  322. CreateTime: time.Now(),
  323. }
  324. // 添加入库
  325. err = chartTheme.Add()
  326. if err != nil {
  327. br.Msg = "添加失败"
  328. br.ErrMsg = "添加失败,Err:" + err.Error()
  329. return
  330. }
  331. go eta_forum.ChartThemeSave(chartTheme)
  332. br.Ret = 200
  333. br.Success = true
  334. br.Msg = "添加成功"
  335. }
  336. // Edit
  337. // @Title 新增主题
  338. // @Description
  339. // @Param request body request.DeleteThemeConfReq true "type json string"
  340. // @Success 200 Ret=200 修改成功
  341. // @router /chart/theme/edit [post]
  342. func (c *ChartThemeController) Edit() {
  343. br := new(models.BaseResponse).Init()
  344. defer func() {
  345. c.Data["json"] = br
  346. c.ServeJSON()
  347. }()
  348. sysUser := c.SysUser
  349. if sysUser == nil {
  350. br.Msg = "请登录"
  351. br.ErrMsg = "请登录,SysUser Is Empty"
  352. br.Ret = 408
  353. return
  354. }
  355. var req request.EditThemeReq
  356. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  357. if err != nil {
  358. br.Msg = "参数解析异常!"
  359. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  360. return
  361. }
  362. if req.ChartThemeId < 0 {
  363. br.Msg = "请选择配置"
  364. br.IsSendEmail = false
  365. return
  366. }
  367. // 获取配置
  368. chartTheme, err := chart_theme.GetChartThemeId(req.ChartThemeId)
  369. if err != nil {
  370. if err.Error() == utils.ErrNoRow() {
  371. br.Msg = "主题不存在或已删除"
  372. br.ErrMsg = "主题不存在或已删除"
  373. br.IsSendEmail = false
  374. } else {
  375. br.Msg = "获取失败"
  376. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  377. }
  378. return
  379. }
  380. // 更新
  381. if req.ChartThemeName != `` {
  382. chartTheme.ChartThemeName = req.ChartThemeName
  383. }
  384. if req.Config != `` {
  385. chartTheme.Config = req.Config
  386. }
  387. if req.ChartImage != `` {
  388. chartTheme.ChartImage = req.ChartImage
  389. }
  390. chartTheme.ModifyTime = time.Now()
  391. err = chartTheme.Update([]string{"ChartThemeName", "Config", "ChartImage", "ModifyTime"})
  392. if err != nil {
  393. br.Msg = "修改失败"
  394. br.ErrMsg = "修改失败,Err:" + err.Error()
  395. return
  396. }
  397. go eta_forum.ChartThemeSave(chartTheme)
  398. br.Ret = 200
  399. br.Success = true
  400. br.Msg = "修改成功"
  401. }
  402. // Delete
  403. // @Title 删除主题
  404. // @Description
  405. // @Param request body request.DeleteThemeConfReq true "type json string"
  406. // @Success 200 Ret=200 删除成功
  407. // @router /chart/theme/delete [post]
  408. func (c *ChartThemeController) Delete() {
  409. br := new(models.BaseResponse).Init()
  410. defer func() {
  411. c.Data["json"] = br
  412. c.ServeJSON()
  413. }()
  414. sysUser := c.SysUser
  415. if sysUser == nil {
  416. br.Msg = "请登录"
  417. br.ErrMsg = "请登录,SysUser Is Empty"
  418. br.Ret = 408
  419. return
  420. }
  421. var req request.DeleteThemeReq
  422. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  423. if err != nil {
  424. br.Msg = "参数解析异常!"
  425. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  426. return
  427. }
  428. if req.ChartThemeId < 0 {
  429. br.Msg = "请选择配置"
  430. br.IsSendEmail = false
  431. return
  432. }
  433. // 获取配置
  434. chartTheme, err := chart_theme.GetChartThemeId(req.ChartThemeId)
  435. if err != nil {
  436. if err.Error() == utils.ErrNoRow() {
  437. br.Msg = "主题不存在或已删除"
  438. br.ErrMsg = "主题不存在或已删除"
  439. br.IsSendEmail = false
  440. } else {
  441. br.Msg = "获取失败"
  442. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  443. }
  444. return
  445. }
  446. if chartTheme.IsSystemTheme == 1 {
  447. br.Msg = "系统默认配置,无法删除"
  448. br.ErrMsg = "系统默认配置,无法删除"
  449. br.IsSendEmail = false
  450. return
  451. }
  452. // 根据给定的图表主题类型ID获取图表主题类型
  453. chartThemeType, err := chart_theme.GetChartThemeTypeById(chartTheme.ChartThemeTypeId)
  454. if err != nil {
  455. br.Msg = "获取失败"
  456. br.ErrMsg = "获取图表主题类型信息失败,Err:" + err.Error()
  457. return
  458. }
  459. // 判断是否是配置为默认设置,如果已经设置了,那么不能删除
  460. if chartThemeType.DefaultChartThemeId == chartTheme.ChartThemeId {
  461. br.Msg = "该主题已经配置为默认设置,不允许删除"
  462. br.ErrMsg = "该主题已经配置为默认设置,不允许删除"
  463. br.IsSendEmail = false
  464. return
  465. }
  466. // 更新
  467. chartTheme.IsDelete = 1
  468. err = chartTheme.Update([]string{"IsDelete"})
  469. if err != nil {
  470. br.Msg = "删除失败"
  471. br.ErrMsg = "删除失败,Err:" + err.Error()
  472. return
  473. }
  474. go eta_forum.ChartThemeSave(chartTheme)
  475. br.Ret = 200
  476. br.Success = true
  477. br.Msg = "删除成功"
  478. }
  479. // SetDefaultTheme
  480. // @Title 配置默认主题
  481. // @Description 配置默认主题
  482. // @Param request body request.DeleteThemeConfReq true "type json string"
  483. // @Success 200 Ret=200 配置成功
  484. // @router /chart/theme/set_default [post]
  485. func (c *ChartThemeController) SetDefaultTheme() {
  486. br := new(models.BaseResponse).Init()
  487. defer func() {
  488. c.Data["json"] = br
  489. c.ServeJSON()
  490. }()
  491. sysUser := c.SysUser
  492. if sysUser == nil {
  493. br.Msg = "请登录"
  494. br.ErrMsg = "请登录,SysUser Is Empty"
  495. br.Ret = 408
  496. return
  497. }
  498. var req request.SetDefaultThemeReq
  499. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  500. if err != nil {
  501. br.Msg = "参数解析异常!"
  502. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  503. return
  504. }
  505. if req.ChartThemeId < 0 {
  506. br.Msg = "请选择配置"
  507. br.IsSendEmail = false
  508. return
  509. }
  510. if req.ChartThemeTypeId < 0 {
  511. br.Msg = "请选择图表类型"
  512. br.IsSendEmail = false
  513. return
  514. }
  515. // 获取图表配置
  516. chartTheme, err := chart_theme.GetChartThemeId(req.ChartThemeId)
  517. if err != nil {
  518. if err.Error() == utils.ErrNoRow() {
  519. br.Msg = "主题不存在或已删除"
  520. br.ErrMsg = "主题不存在或已删除"
  521. br.IsSendEmail = false
  522. } else {
  523. br.Msg = "获取失败"
  524. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  525. }
  526. return
  527. }
  528. // 获取图表类型
  529. chartThemeType, err := chart_theme.GetChartThemeTypeById(req.ChartThemeTypeId)
  530. if err != nil {
  531. if err.Error() == utils.ErrNoRow() {
  532. br.Msg = "获取图表类型不存在或已删除"
  533. br.ErrMsg = "获取图表类型不存在或已删除"
  534. br.IsSendEmail = false
  535. } else {
  536. br.Msg = "获取失败"
  537. br.ErrMsg = "获取图表配置信息失败,Err:" + err.Error()
  538. }
  539. return
  540. }
  541. // 判断主题与图表类型是否相符
  542. if chartTheme.ChartThemeTypeId != chartThemeType.ChartThemeTypeId {
  543. br.Msg = "主题与图表类型不符"
  544. br.ErrMsg = "主题与图表类型不符"
  545. br.IsSendEmail = false
  546. return
  547. }
  548. // 更新
  549. chartThemeType.DefaultChartThemeId = chartTheme.ChartThemeId
  550. chartThemeType.ModifyTime = time.Now()
  551. err = chartThemeType.Update([]string{"DefaultChartThemeId", "ModifyTime"})
  552. if err != nil {
  553. br.Msg = "配置失败"
  554. br.ErrMsg = "配置失败,Err:" + err.Error()
  555. return
  556. }
  557. go eta_forum.ChartThemeTypeSave(chartThemeType)
  558. br.Ret = 200
  559. br.Success = true
  560. br.Msg = "配置成功"
  561. }
  562. // TypeListBySource
  563. // @Title 根据来源获取主题类型列表
  564. // @Description 获取主题类型列表接口
  565. // @Param Source query int true "图表主题类型id"
  566. // @Success 200 {object} []*chart_theme.ChartThemeType
  567. // @router /chart/theme/type/list_by_source [get]
  568. func (c *ChartThemeController) TypeListBySource() {
  569. br := new(models.BaseResponse).Init()
  570. defer func() {
  571. c.Data["json"] = br
  572. c.ServeJSON()
  573. }()
  574. sysUser := c.SysUser
  575. if sysUser == nil {
  576. br.Msg = "请登录"
  577. br.ErrMsg = "请登录,SysUser Is Empty"
  578. br.Ret = 408
  579. return
  580. }
  581. source, _ := c.GetInt("Source")
  582. if source <= 0 {
  583. br.Msg = "请选择来源"
  584. br.ErrMsg = "请选择来源"
  585. br.IsSendEmail = false
  586. return
  587. }
  588. list, err := chart_theme.GetChartThemeTypeListBySource(source)
  589. if err != nil {
  590. br.Msg = "获取失败"
  591. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  592. return
  593. }
  594. br.Ret = 200
  595. br.Success = true
  596. br.Msg = "获取成功"
  597. br.Data = list
  598. }
  599. // ListBySource
  600. // @Title 根据来源获取主题列表
  601. // @Description 获取主题列表接口
  602. // @Param ChartType query int true "图表类型"
  603. // @Param Source query int true "来源"
  604. // @Success 200 {object} []*chart_theme.ChartTheme
  605. // @router /chart/theme/list_by_source [get]
  606. func (c *ChartThemeController) ListBySource() {
  607. br := new(models.BaseResponse).Init()
  608. defer func() {
  609. c.Data["json"] = br
  610. c.ServeJSON()
  611. }()
  612. sysUser := c.SysUser
  613. if sysUser == nil {
  614. br.Msg = "请登录"
  615. br.ErrMsg = "请登录,SysUser Is Empty"
  616. br.Ret = 408
  617. return
  618. }
  619. chartType, _ := c.GetInt("ChartType")
  620. if chartType < 0 {
  621. br.Msg = "请选择图表类型"
  622. br.ErrMsg = "请选择图表类型"
  623. br.IsSendEmail = false
  624. return
  625. }
  626. source, _ := c.GetInt("Source")
  627. if chartType < 0 {
  628. br.Msg = "请选择图表来源"
  629. br.ErrMsg = "请选择图表来源"
  630. br.IsSendEmail = false
  631. return
  632. }
  633. chartThemeType, err := chart_theme.GetChartThemeTypeByChartTypeAndSource(chartType, source)
  634. if err != nil {
  635. br.Msg = "获取失败"
  636. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  637. return
  638. }
  639. list, err := chart_theme.GetChartThemeItemList(chartThemeType.ChartThemeTypeId)
  640. if err != nil {
  641. br.Msg = "获取失败"
  642. br.ErrMsg = "获取图表样式类型信息失败,Err:" + err.Error()
  643. return
  644. }
  645. // 兼容历史数据,加入新字段LineOptionList
  646. for i, v := range list {
  647. newConfig, e := data.ConvertOldChartOptions(v.Config)
  648. if e != nil {
  649. continue
  650. }
  651. list[i].Config = newConfig
  652. }
  653. br.Ret = 200
  654. br.Success = true
  655. br.Msg = "获取成功"
  656. br.Data = list
  657. }