chart_theme.go 22 KB

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