chart_theme.go 21 KB

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