chart_info.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package chart
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "hongze/hongze_yb/controller/response"
  8. "hongze/hongze_yb/global"
  9. "hongze/hongze_yb/models/request"
  10. responseModel "hongze/hongze_yb/models/response"
  11. "hongze/hongze_yb/models/response/chart_info"
  12. chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
  13. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  14. "hongze/hongze_yb/models/tables/chart_info_log"
  15. edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
  16. "hongze/hongze_yb/models/tables/yb_my_chart"
  17. "hongze/hongze_yb/services/alarm_msg"
  18. "hongze/hongze_yb/services/chart"
  19. "hongze/hongze_yb/services/user"
  20. "hongze/hongze_yb/utils"
  21. "io/ioutil"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. // GetChartInfoDetail 获取图表详情
  28. // @Tags 图库模块
  29. // @Summary 获取图表详情
  30. // @Description 获取图表详情
  31. // @Security ApiKeyAuth
  32. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  33. // @Accept json
  34. // @Product json
  35. // @Param DateType query string false "时间段:1-00年至今; 2-10年至今; 3-15年至今; 4-21年至今; 5-指定区间; 6-指定年月至今; 7-18年至今; 8-19年至今; 9-20年至今"
  36. // @Param ClassifyId query string false "图表分类ID"
  37. // @Success 200 {object} chart_info.ChartInfoDetailResp
  38. // @failure 400 {string} string "图表详情获取失败"
  39. // @Router /my_chart/getChartInfoDetail [get]
  40. func GetChartInfoDetail(c *gin.Context) {
  41. // 图表ID
  42. reqChartInfoId := c.DefaultQuery("ChartInfoId", "")
  43. if reqChartInfoId == "" {
  44. response.Fail("参数有误:图表ID", c)
  45. return
  46. }
  47. chartInfoId, _ := strconv.Atoi(reqChartInfoId)
  48. startDate := c.DefaultQuery("StartDate", "")
  49. endDate := c.DefaultQuery("EndDate", "")
  50. // 图表样式类型
  51. reqChartType := c.DefaultQuery("ChartType", "")
  52. chartType, _ := strconv.Atoi(reqChartType)
  53. // 季节性图表时间
  54. reqSeasonStartDate := c.DefaultQuery("SeasonStartDate", "")
  55. reqSeasonEndDate := c.DefaultQuery("SeasonEndDate", "")
  56. // 指标ID
  57. edbInfoId := c.DefaultQuery("EdbInfoId", "")
  58. // 公历/农历
  59. reqCalendar := c.DefaultQuery("Calendar", "")
  60. // 获取图表信息
  61. var err error
  62. chartInfo := new(chartInfoModel.ChartInfoView)
  63. chartInfo, err = chartInfoModel.GetChartInfoViewById(chartInfoId)
  64. if err != nil {
  65. if err == utils.ErrNoRow {
  66. response.Custom(4003, "图表不存在,请刷新页面", c)
  67. return
  68. }
  69. response.FailMsg("获取失败", "获取图表信息失败, Err:"+err.Error(), c)
  70. return
  71. }
  72. chartType = chartInfo.ChartType
  73. calendar := chartInfo.Calendar
  74. if reqCalendar != "" {
  75. calendar = reqCalendar
  76. }
  77. // 时段筛选
  78. reqDateType := c.DefaultQuery("DateType", "")
  79. dateType := chartInfo.DateType
  80. if reqDateType != "" {
  81. dateType, _ = strconv.Atoi(reqDateType)
  82. }
  83. if dateType <= 0 {
  84. dateType = 3 // 默认同后台15年至今
  85. }
  86. switch dateType {
  87. case 1:
  88. startDate = "2000-01-01"
  89. case 2:
  90. startDate = "2010-01-01"
  91. case 3:
  92. startDate = "2015-01-01"
  93. case 4:
  94. startDate = "2021-01-01"
  95. case 5:
  96. if startDate == "" && chartInfo.StartDate != "" {
  97. startDate = chartInfo.StartDate
  98. endDate = chartInfo.EndDate
  99. }
  100. case 6:
  101. if startDate == "" && chartInfo.StartDate != "" {
  102. startDate = chartInfo.StartDate
  103. }
  104. case 7:
  105. startDate = "2018-01-01"
  106. case 8:
  107. startDate = "2019-01-01"
  108. case 9:
  109. startDate = "2020-01-01"
  110. case 11:
  111. startDate = "2022-01-01"
  112. }
  113. // 兼容日期错误
  114. {
  115. if strings.Count(startDate, "-") == 1 {
  116. startDate = startDate + "-01"
  117. }
  118. if strings.Count(endDate, "-") == 1 {
  119. endDate = endDate + "-01"
  120. }
  121. }
  122. if chartType == 2 {
  123. // 季节性图表
  124. var seasonStartDate, seasonEndDate string
  125. if reqSeasonStartDate == "" {
  126. seasonStartDate = chartInfo.SeasonStartDate
  127. } else {
  128. seasonStartDate = reqSeasonStartDate
  129. }
  130. if reqSeasonEndDate == "" {
  131. seasonEndDate = chartInfo.SeasonEndDate
  132. } else {
  133. seasonEndDate = reqSeasonEndDate
  134. }
  135. if seasonStartDate != "" {
  136. startDate = seasonStartDate + "-01-01"
  137. } else {
  138. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  139. startDate = strconv.Itoa(fivePre) + "-01-01"
  140. }
  141. if seasonEndDate != "" {
  142. endDate = seasonEndDate + "-12-31"
  143. } else {
  144. endDate = time.Now().Format(utils.FormatDate)
  145. }
  146. }
  147. // 获取图表指标映射
  148. mappingList := make([]*chartEdbMappingModel.ChartEdbInfoMapping, 0)
  149. if chartInfoId > 0 {
  150. mappingList, err = chartEdbMappingModel.GetMappingListByChartInfoId(chartInfoId)
  151. if err != nil {
  152. response.FailMsg("获取失败", "获取图表指标信息失败4001, Err:"+err.Error(), c)
  153. return
  154. }
  155. } else {
  156. if edbInfoId != "" {
  157. mappingList, err = chartEdbMappingModel.GetMappingListByEdbInfoId(edbInfoId)
  158. if err != nil {
  159. response.FailMsg("获取失败", "获取图表指标信息失败4002, Err:"+err.Error(), c)
  160. return
  161. }
  162. }
  163. }
  164. //fmt.Println("start_date:", startDate)
  165. //fmt.Println("end_date:", endDate)
  166. // 柱方图的一些配置
  167. var barConfig request.BarChartInfoReq
  168. barChartInfoDateList := make([]request.BarChartInfoDateReq, 0)
  169. barChartInfoSort := request.BarChartInfoSortReq{}
  170. if chartInfo != nil && chartInfo.ChartType == 7 {
  171. if chartInfo.BarConfig == `` {
  172. response.FailMsg("柱方图未配置", "柱方图未配置", c)
  173. return
  174. }
  175. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  176. if err != nil {
  177. response.FailMsg("柱方图配置异常", "柱方图配置异常", c)
  178. return
  179. }
  180. barChartInfoDateList = barConfig.DateList
  181. barChartInfoSort = barConfig.Sort
  182. // 指标别名
  183. for _, v := range mappingList {
  184. for _, confEdb := range barConfig.EdbInfoIdList {
  185. if v.EdbInfoId == confEdb.EdbInfoId {
  186. v.EdbAliasName = confEdb.Name
  187. }
  188. }
  189. }
  190. }
  191. // 获取图表中的指标数据
  192. edbList, xEdbIdValue, yDataList, sourceArr, err := chart.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, barChartInfoDateList, barChartInfoSort)
  193. if err != nil {
  194. response.FailMsg("获取失败", "获取图表,指标信息失败, Err:"+err.Error(), c)
  195. return
  196. }
  197. for _, v := range edbList {
  198. // 指标别名
  199. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  200. for _, reqEdb := range barConfig.EdbInfoIdList {
  201. if v.EdbInfoId == reqEdb.EdbInfoId {
  202. v.EdbAliasName = reqEdb.Name
  203. }
  204. }
  205. }
  206. }
  207. sourceArr = append(sourceArr, "弘则研究")
  208. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  209. // 访问记录-仅普通用户记录
  210. userInfo := user.GetInfoByClaims(c)
  211. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  212. if !ok {
  213. reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
  214. myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
  215. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  216. }
  217. // 用户是否有收藏该图表
  218. ob := new(yb_my_chart.YbMyChart)
  219. cond := `user_id = ? AND chart_info_id = ?`
  220. pars := make([]interface{}, 0)
  221. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  222. exists, e := ob.FetchByCondition(cond, pars)
  223. if e != nil && e != utils.ErrNoRow {
  224. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  225. return
  226. }
  227. myChartInfo := new(responseModel.MyChartItem)
  228. if exists != nil && exists.MyChartID > 0 {
  229. myChartInfo.MyChartID = exists.MyChartID
  230. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  231. myChartInfo.ChartInfoID = exists.ChartInfoID
  232. myChartInfo.ChartName = exists.ChartName
  233. myChartInfo.UniqueCode = exists.UniqueCode
  234. myChartInfo.ChartImage = exists.ChartImage
  235. myChartInfo.UserID = exists.UserID
  236. myChartInfo.ReportID = exists.ReportID
  237. myChartInfo.ReportChapterID = exists.ReportChapterID
  238. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  239. }
  240. resp := new(chart_info.ChartInfoDetailResp)
  241. resp.ChartInfo = chartInfo
  242. resp.EdbInfoList = edbList
  243. resp.XEdbIdValue = xEdbIdValue
  244. resp.YDataList = yDataList
  245. resp.MyChartInfo = myChartInfo
  246. response.OkData("获取成功", resp, c)
  247. }
  248. func getChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  249. // 获取图表信息
  250. var err error
  251. chartType := chartInfo.ChartType
  252. calendar := chartInfo.Calendar
  253. // 时段筛选
  254. dateType := chartInfo.DateType
  255. if dateType <= 0 {
  256. dateType = 3 // 默认同后台15年至今
  257. }
  258. var startDate, endDate string
  259. switch dateType {
  260. case 1:
  261. startDate = "2000-01-01"
  262. case 2:
  263. startDate = "2010-01-01"
  264. case 3:
  265. startDate = "2015-01-01"
  266. case 4:
  267. startDate = "2021-01-01"
  268. case 5:
  269. if startDate == "" && chartInfo.StartDate != "" {
  270. startDate = chartInfo.StartDate
  271. endDate = chartInfo.EndDate
  272. }
  273. case 6:
  274. if startDate == "" && chartInfo.StartDate != "" {
  275. startDate = chartInfo.StartDate
  276. }
  277. case 7:
  278. startDate = "2018-01-01"
  279. case 8:
  280. startDate = "2019-01-01"
  281. case 9:
  282. startDate = "2020-01-01"
  283. case 11:
  284. startDate = "2022-01-01"
  285. }
  286. // 兼容日期错误
  287. {
  288. if strings.Count(startDate, "-") == 1 {
  289. startDate = startDate + "-01"
  290. }
  291. if strings.Count(endDate, "-") == 1 {
  292. endDate = endDate + "-01"
  293. }
  294. }
  295. if chartType == 2 {
  296. // 季节性图表
  297. var seasonStartDate, seasonEndDate string
  298. seasonStartDate = chartInfo.SeasonStartDate
  299. seasonEndDate = chartInfo.SeasonEndDate
  300. if seasonStartDate != "" {
  301. startDate = seasonStartDate + "-01-01"
  302. } else {
  303. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  304. startDate = strconv.Itoa(fivePre) + "-01-01"
  305. }
  306. if seasonEndDate != "" {
  307. endDate = seasonEndDate + "-12-31"
  308. } else {
  309. endDate = time.Now().Format(utils.FormatDate)
  310. }
  311. }
  312. // 获取图表指标映射
  313. mappingList := make([]*chartEdbMappingModel.ChartEdbInfoMapping, 0)
  314. mappingList, err = chartEdbMappingModel.GetMappingListByChartInfoId(chartInfo.ChartInfoId)
  315. if err != nil {
  316. msg = `获取失败`
  317. errMsg = "获取图表指标信息失败4001, Err:" + err.Error()
  318. return
  319. }
  320. //fmt.Println("start_date:", startDate)
  321. //fmt.Println("end_date:", endDate)
  322. // 柱方图的一些配置
  323. var barConfig request.BarChartInfoReq
  324. barChartInfoDateList := make([]request.BarChartInfoDateReq, 0)
  325. barChartInfoSort := request.BarChartInfoSortReq{}
  326. if chartInfo != nil && chartInfo.ChartType == 7 {
  327. if chartInfo.BarConfig == `` {
  328. msg = `柱方图未配置`
  329. errMsg = `柱方图未配置`
  330. return
  331. }
  332. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  333. if err != nil {
  334. msg = `柱方图配置异常`
  335. errMsg = `柱方图配置异常`
  336. return
  337. }
  338. barChartInfoDateList = barConfig.DateList
  339. barChartInfoSort = barConfig.Sort
  340. // 指标别名
  341. for _, v := range mappingList {
  342. for _, confEdb := range barConfig.EdbInfoIdList {
  343. if v.EdbInfoId == confEdb.EdbInfoId {
  344. v.EdbAliasName = confEdb.Name
  345. }
  346. }
  347. }
  348. }
  349. // 获取图表中的指标数据
  350. edbList, xEdbIdValue, yDataList, sourceArr, err := chart.GetChartEdbData(chartInfo.ChartInfoId, chartType, calendar, startDate, endDate, mappingList, barChartInfoDateList, barChartInfoSort)
  351. if err != nil {
  352. msg = `获取失败`
  353. errMsg = "获取图表,指标信息失败, Err:" + err.Error()
  354. return
  355. }
  356. for _, v := range edbList {
  357. // 指标别名
  358. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  359. for _, reqEdb := range barConfig.EdbInfoIdList {
  360. if v.EdbInfoId == reqEdb.EdbInfoId {
  361. v.EdbAliasName = reqEdb.Name
  362. }
  363. }
  364. }
  365. }
  366. sourceArr = append(sourceArr, "弘则研究")
  367. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  368. // 访问记录-仅普通用户记录
  369. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  370. if !ok {
  371. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  372. }
  373. // 用户是否有收藏该图表
  374. ob := new(yb_my_chart.YbMyChart)
  375. cond := `user_id = ? AND chart_info_id = ?`
  376. pars := make([]interface{}, 0)
  377. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  378. exists, e := ob.FetchByCondition(cond, pars)
  379. if e != nil && e != utils.ErrNoRow {
  380. msg = `操作失败`
  381. errMsg = "获取用户图表失败, Err: " + e.Error()
  382. return
  383. }
  384. myChartInfo := new(responseModel.MyChartItem)
  385. if exists != nil && exists.MyChartID > 0 {
  386. myChartInfo.MyChartID = exists.MyChartID
  387. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  388. myChartInfo.ChartInfoID = exists.ChartInfoID
  389. myChartInfo.ChartName = exists.ChartName
  390. myChartInfo.UniqueCode = exists.UniqueCode
  391. myChartInfo.ChartImage = exists.ChartImage
  392. myChartInfo.UserID = exists.UserID
  393. myChartInfo.ReportID = exists.ReportID
  394. myChartInfo.ReportChapterID = exists.ReportChapterID
  395. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  396. }
  397. resp = new(chart_info.ChartInfoDetailResp)
  398. resp.ChartInfo = chartInfo
  399. resp.EdbInfoList = edbList
  400. resp.XEdbIdValue = xEdbIdValue
  401. resp.YDataList = yDataList
  402. resp.MyChartInfo = myChartInfo
  403. isOk = true
  404. return
  405. }
  406. // RefreshChartInfo 刷新图表信息
  407. // @Tags 图库模块
  408. // @Summary 刷新图表信息
  409. // @Description 刷新图表信息
  410. // @Security ApiKeyAuth
  411. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  412. // @Accept json
  413. // @Product json
  414. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  415. // @Success 200 {string} string "操作成功"
  416. // @failure 400 {string} string "操作失败"
  417. // @Router /my_chart/refreshChartInfo [post]
  418. func RefreshChartInfo(c *gin.Context) {
  419. // 参数校验
  420. var req chartInfoModel.RefreshChartInfoReq
  421. if c.ShouldBind(&req) != nil {
  422. response.Fail("参数异常", c)
  423. return
  424. }
  425. chartInfoId := req.ChartInfoId
  426. if chartInfoId == 0 {
  427. response.Fail("参数有误", c)
  428. return
  429. }
  430. userInfo := user.GetInfoByClaims(c)
  431. ok, _, err := user.GetAdminByUserInfo(userInfo)
  432. if err != nil {
  433. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  434. return
  435. }
  436. if !ok {
  437. // 普通用户刷新频率限制-每个用户/图/天/2次
  438. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  439. fmt.Println("refreshCacheKey:", cacheKey)
  440. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  441. if countUserRefresh >= 2 {
  442. response.Ok("目前已是最新数据", c)
  443. return
  444. }
  445. countUserRefresh += 1
  446. now := time.Now()
  447. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  448. sub := today.Sub(now)
  449. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  450. }
  451. // 图表信息校验
  452. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  453. if err != nil {
  454. if err == utils.ErrNoRow {
  455. response.Fail("图表已被删除,无需刷新", c)
  456. return
  457. }
  458. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  459. return
  460. }
  461. // 刷新图表
  462. if err = chart.ChartInfoRefreshV2(chartInfo.ChartInfoId); err != nil {
  463. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  464. if global.CONFIG.Serve.RunMode == "release" {
  465. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  466. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  467. } else {
  468. global.LOG.Info(errContent)
  469. }
  470. }
  471. //清除图表缓存
  472. {
  473. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  474. _ = global.Redis.Del(context.TODO(), key)
  475. }
  476. response.OkData("刷新成功", "", c)
  477. }
  478. // EditChartInfo 编辑图表信息
  479. // @Tags 图库模块
  480. // @Summary 编辑图表信息
  481. // @Description 编辑图表信息
  482. // @Security ApiKeyAuth
  483. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  484. // @Accept json
  485. // @Product json
  486. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  487. // @Success 200 {string} string "操作成功"
  488. // @failure 400 {string} string "操作失败"
  489. // @Router /my_chart/editChartInfo [post]
  490. func EditChartInfo(c *gin.Context) {
  491. // 参数校验
  492. var req chartInfoModel.SaveChartInfoReq
  493. if c.ShouldBind(&req) != nil {
  494. response.Fail("参数异常", c)
  495. return
  496. }
  497. if req.ChartInfoId > 0 && len(req.ChartEdbInfoList) <= 0 {
  498. response.Fail("参数异常", c)
  499. return
  500. }
  501. // 操作权限校验
  502. userInfo := user.GetInfoByClaims(c)
  503. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  504. if err != nil {
  505. response.Fail("操作人信息有误", c)
  506. return
  507. }
  508. if !ok {
  509. response.Fail("非内部人员无权进行操作", c)
  510. return
  511. }
  512. // 图表信息校验
  513. chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
  514. if err != nil {
  515. if err == utils.ErrNoRow {
  516. response.Fail("图表已被删除,请刷新页面!", c)
  517. return
  518. }
  519. response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
  520. return
  521. }
  522. if chartItem.ChartType == 2 && len(req.ChartEdbInfoList) > 1 {
  523. response.Fail("您选择的图表样式为季节性图表,只支持单指标画图!", c)
  524. return
  525. }
  526. if req.Calendar == "" {
  527. req.Calendar = "公历"
  528. }
  529. // 指标信息
  530. var edbInfoIdArr []int
  531. edbList := make([]*edbInfoModel.EdbInfo, 0)
  532. for _, v := range req.ChartEdbInfoList {
  533. edbInfo, err := edbInfoModel.GetEdbInfoById(v.EdbInfoId)
  534. if err != nil {
  535. if err == utils.ErrNoRow {
  536. response.FailMsg("操作失败", "图表不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  537. return
  538. }
  539. response.FailMsg("操作失败", "获取图表的指标信息失败,Err:"+err.Error(), c)
  540. return
  541. }
  542. if edbInfo == nil {
  543. response.FailMsg("操作失败", "指标不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  544. return
  545. }
  546. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  547. edbInfo.EdbNameSource = edbInfo.EdbName
  548. edbList = append(edbList, edbInfo)
  549. }
  550. sort.Ints(edbInfoIdArr)
  551. var edbInfoIdArrStr []string
  552. for _, v := range edbInfoIdArr {
  553. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  554. }
  555. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  556. err = chart.ModifyChartInfoAndMapping(edbInfoIdStr, &req, chartItem.ChartType)
  557. if err != nil {
  558. response.Fail("图表保存失败, Err:"+err.Error(), c)
  559. return
  560. }
  561. // 清除图表缓存
  562. cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  563. _ = global.Redis.Del(context.TODO(), cacheKey)
  564. // 新增操作日志
  565. {
  566. chartLog := new(chart_info_log.ChartInfoLog)
  567. chartLog.ChartName = chartItem.ChartName
  568. chartLog.ChartInfoId = req.ChartInfoId
  569. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  570. chartLog.SysUserId = int(adminInfo.AdminID)
  571. chartLog.SysUserRealName = adminInfo.RealName
  572. chartLog.UniqueCode = chartItem.UniqueCode
  573. chartLog.CreateTime = time.Now()
  574. bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
  575. chartLog.Content = string(bodyBytes)
  576. chartLog.Status = "修改配置项"
  577. chartLog.Method = c.Request.URL.String()
  578. go chartLog.Create()
  579. }
  580. response.OkData("操作成功", "", c)
  581. }