chart_theme.go 22 KB

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