chart_info.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. extraConfigStr := chartInfo.ExtraConfig
  168. // 柱方图的一些配置
  169. var barConfig request.BarChartInfoReq
  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. extraConfigStr = chartInfo.BarConfig
  181. }
  182. // 获取图表中的指标数据
  183. edbList, xEdbIdValue, yDataList, sourceArr, dataResp, err, errMsg := chart.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  184. if err != nil {
  185. msg := `获取失败`
  186. if errMsg != `` {
  187. msg = errMsg
  188. }
  189. response.FailMsg(msg, "获取图表,指标信息失败, Err:"+err.Error(), c)
  190. return
  191. }
  192. for _, v := range edbList {
  193. // 指标别名
  194. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  195. for _, reqEdb := range barConfig.EdbInfoIdList {
  196. if v.EdbInfoId == reqEdb.EdbInfoId {
  197. v.EdbAliasName = reqEdb.Name
  198. }
  199. }
  200. }
  201. }
  202. sourceArr = append(sourceArr, "弘则研究")
  203. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  204. // 访问记录-仅普通用户记录
  205. userInfo := user.GetInfoByClaims(c)
  206. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  207. if !ok {
  208. reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
  209. myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
  210. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  211. }
  212. // 用户是否有收藏该图表
  213. ob := new(yb_my_chart.YbMyChart)
  214. cond := `user_id = ? AND chart_info_id = ?`
  215. pars := make([]interface{}, 0)
  216. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  217. exists, e := ob.FetchByCondition(cond, pars)
  218. if e != nil && e != utils.ErrNoRow {
  219. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  220. return
  221. }
  222. myChartInfo := new(responseModel.MyChartItem)
  223. if exists != nil && exists.MyChartID > 0 {
  224. myChartInfo.MyChartID = exists.MyChartID
  225. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  226. myChartInfo.ChartInfoID = exists.ChartInfoID
  227. myChartInfo.ChartName = exists.ChartName
  228. myChartInfo.UniqueCode = exists.UniqueCode
  229. myChartInfo.ChartImage = exists.ChartImage
  230. myChartInfo.UserID = exists.UserID
  231. myChartInfo.ReportID = exists.ReportID
  232. myChartInfo.ReportChapterID = exists.ReportChapterID
  233. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  234. }
  235. resp := new(chart_info.ChartInfoDetailResp)
  236. resp.ChartInfo = chartInfo
  237. resp.EdbInfoList = edbList
  238. resp.XEdbIdValue = xEdbIdValue
  239. resp.YDataList = yDataList
  240. resp.MyChartInfo = myChartInfo
  241. resp.DataResp = dataResp
  242. response.OkData("获取成功", resp, c)
  243. }
  244. func getChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  245. // 获取图表信息
  246. var err error
  247. chartType := chartInfo.ChartType
  248. calendar := chartInfo.Calendar
  249. // 时段筛选
  250. dateType := chartInfo.DateType
  251. if dateType <= 0 {
  252. dateType = 3 // 默认同后台15年至今
  253. }
  254. var startDate, endDate string
  255. switch dateType {
  256. case 1:
  257. startDate = "2000-01-01"
  258. case 2:
  259. startDate = "2010-01-01"
  260. case 3:
  261. startDate = "2015-01-01"
  262. case 4:
  263. startDate = "2021-01-01"
  264. case 5:
  265. if startDate == "" && chartInfo.StartDate != "" {
  266. startDate = chartInfo.StartDate
  267. endDate = chartInfo.EndDate
  268. }
  269. case 6:
  270. if startDate == "" && chartInfo.StartDate != "" {
  271. startDate = chartInfo.StartDate
  272. }
  273. case 7:
  274. startDate = "2018-01-01"
  275. case 8:
  276. startDate = "2019-01-01"
  277. case 9:
  278. startDate = "2020-01-01"
  279. case 11:
  280. startDate = "2022-01-01"
  281. }
  282. // 兼容日期错误
  283. {
  284. if strings.Count(startDate, "-") == 1 {
  285. startDate = startDate + "-01"
  286. }
  287. if strings.Count(endDate, "-") == 1 {
  288. endDate = endDate + "-01"
  289. }
  290. }
  291. if chartType == 2 {
  292. // 季节性图表
  293. var seasonStartDate, seasonEndDate string
  294. seasonStartDate = chartInfo.SeasonStartDate
  295. seasonEndDate = chartInfo.SeasonEndDate
  296. if seasonStartDate != "" {
  297. startDate = seasonStartDate + "-01-01"
  298. } else {
  299. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  300. startDate = strconv.Itoa(fivePre) + "-01-01"
  301. }
  302. if seasonEndDate != "" {
  303. endDate = seasonEndDate + "-12-31"
  304. } else {
  305. endDate = time.Now().Format(utils.FormatDate)
  306. }
  307. }
  308. // 获取图表指标映射
  309. mappingList := make([]*chartEdbMappingModel.ChartEdbInfoMapping, 0)
  310. mappingList, err = chartEdbMappingModel.GetMappingListByChartInfoId(chartInfo.ChartInfoId)
  311. if err != nil {
  312. msg = `获取失败`
  313. errMsg = "获取图表指标信息失败4001, Err:" + err.Error()
  314. return
  315. }
  316. //fmt.Println("start_date:", startDate)
  317. //fmt.Println("end_date:", endDate)
  318. // 图表额外数据参数
  319. extraConfigStr := chartInfo.ExtraConfig
  320. // 柱方图的一些配置
  321. var barConfig request.BarChartInfoReq
  322. if chartInfo != nil && chartInfo.ChartType == 7 {
  323. if chartInfo.BarConfig == `` {
  324. msg = "柱方图未配置"
  325. errMsg = "柱方图未配置"
  326. return
  327. }
  328. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  329. if err != nil {
  330. msg = "柱方图配置异常"
  331. errMsg = "柱方图配置异常"
  332. return
  333. }
  334. extraConfigStr = chartInfo.BarConfig
  335. }
  336. // 获取图表中的指标数据
  337. edbList, xEdbIdValue, yDataList, sourceArr, dataResp, err, tmpErrMsg := chart.GetChartEdbData(chartInfo.ChartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  338. if err != nil {
  339. msg = `获取失败`
  340. if tmpErrMsg != `` {
  341. msg = tmpErrMsg
  342. }
  343. errMsg = "获取图表,指标信息失败, Err:" + err.Error()
  344. return
  345. }
  346. for _, v := range edbList {
  347. // 指标别名
  348. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  349. for _, reqEdb := range barConfig.EdbInfoIdList {
  350. if v.EdbInfoId == reqEdb.EdbInfoId {
  351. v.EdbAliasName = reqEdb.Name
  352. }
  353. }
  354. }
  355. }
  356. sourceArr = append(sourceArr, "弘则研究")
  357. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  358. // 访问记录-仅普通用户记录
  359. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  360. if !ok {
  361. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  362. }
  363. // 用户是否有收藏该图表
  364. ob := new(yb_my_chart.YbMyChart)
  365. cond := `user_id = ? AND chart_info_id = ?`
  366. pars := make([]interface{}, 0)
  367. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  368. exists, e := ob.FetchByCondition(cond, pars)
  369. if e != nil && e != utils.ErrNoRow {
  370. msg = `操作失败`
  371. errMsg = "获取用户图表失败, Err: " + e.Error()
  372. return
  373. }
  374. myChartInfo := new(responseModel.MyChartItem)
  375. if exists != nil && exists.MyChartID > 0 {
  376. myChartInfo.MyChartID = exists.MyChartID
  377. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  378. myChartInfo.ChartInfoID = exists.ChartInfoID
  379. myChartInfo.ChartName = exists.ChartName
  380. myChartInfo.UniqueCode = exists.UniqueCode
  381. myChartInfo.ChartImage = exists.ChartImage
  382. myChartInfo.UserID = exists.UserID
  383. myChartInfo.ReportID = exists.ReportID
  384. myChartInfo.ReportChapterID = exists.ReportChapterID
  385. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  386. }
  387. resp = new(chart_info.ChartInfoDetailResp)
  388. resp.ChartInfo = chartInfo
  389. resp.EdbInfoList = edbList
  390. resp.XEdbIdValue = xEdbIdValue
  391. resp.YDataList = yDataList
  392. resp.MyChartInfo = myChartInfo
  393. resp.DataResp = dataResp
  394. isOk = true
  395. return
  396. }
  397. // RefreshChartInfo 刷新图表信息
  398. // @Tags 图库模块
  399. // @Summary 刷新图表信息
  400. // @Description 刷新图表信息
  401. // @Security ApiKeyAuth
  402. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  403. // @Accept json
  404. // @Product json
  405. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  406. // @Success 200 {string} string "操作成功"
  407. // @failure 400 {string} string "操作失败"
  408. // @Router /my_chart/refreshChartInfo [post]
  409. func RefreshChartInfo(c *gin.Context) {
  410. // 参数校验
  411. var req chartInfoModel.RefreshChartInfoReq
  412. if c.ShouldBind(&req) != nil {
  413. response.Fail("参数异常", c)
  414. return
  415. }
  416. chartInfoId := req.ChartInfoId
  417. if chartInfoId == 0 {
  418. response.Fail("参数有误", c)
  419. return
  420. }
  421. userInfo := user.GetInfoByClaims(c)
  422. ok, _, err := user.GetAdminByUserInfo(userInfo)
  423. if err != nil {
  424. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  425. return
  426. }
  427. if !ok {
  428. // 普通用户刷新频率限制-每个用户/图/天/2次
  429. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  430. fmt.Println("refreshCacheKey:", cacheKey)
  431. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  432. if countUserRefresh >= 2 {
  433. response.Ok("目前已是最新数据", c)
  434. return
  435. }
  436. countUserRefresh += 1
  437. now := time.Now()
  438. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  439. sub := today.Sub(now)
  440. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  441. }
  442. // 图表信息校验
  443. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  444. if err != nil {
  445. if err == utils.ErrNoRow {
  446. response.Fail("图表已被删除,无需刷新", c)
  447. return
  448. }
  449. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  450. return
  451. }
  452. // 刷新图表
  453. if err = chart.ChartInfoRefreshV2(chartInfo.ChartInfoId); err != nil {
  454. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  455. if global.CONFIG.Serve.RunMode == "release" {
  456. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  457. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  458. } else {
  459. global.LOG.Info(errContent)
  460. }
  461. }
  462. //清除图表缓存
  463. {
  464. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  465. _ = global.Redis.Del(context.TODO(), key)
  466. }
  467. response.OkData("刷新成功", "", c)
  468. }
  469. // EditChartInfo 编辑图表信息
  470. // @Tags 图库模块
  471. // @Summary 编辑图表信息
  472. // @Description 编辑图表信息
  473. // @Security ApiKeyAuth
  474. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  475. // @Accept json
  476. // @Product json
  477. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  478. // @Success 200 {string} string "操作成功"
  479. // @failure 400 {string} string "操作失败"
  480. // @Router /my_chart/editChartInfo [post]
  481. func EditChartInfo(c *gin.Context) {
  482. // 参数校验
  483. var req chartInfoModel.SaveChartInfoReq
  484. if c.ShouldBind(&req) != nil {
  485. response.Fail("参数异常", c)
  486. return
  487. }
  488. if req.ChartInfoId <= 0 {
  489. response.Fail("参数异常", c)
  490. return
  491. }
  492. // 操作权限校验
  493. userInfo := user.GetInfoByClaims(c)
  494. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  495. if err != nil {
  496. response.Fail("操作人信息有误", c)
  497. return
  498. }
  499. if !ok {
  500. response.Fail("非内部人员无权进行操作", c)
  501. return
  502. }
  503. // 图表信息校验
  504. chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
  505. if err != nil {
  506. if err == utils.ErrNoRow {
  507. response.Fail("图表已被删除,请刷新页面!", c)
  508. return
  509. }
  510. response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
  511. return
  512. }
  513. // 图表关联指标id
  514. chartEdbInfoList := make([]*chartInfoModel.ChartSaveItem, 0)
  515. // 关联指标
  516. var edbInfoIdArr []int
  517. // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
  518. if chartItem.ChartType != 10 {
  519. //edbList := make([]*data_manage.EdbInfo, 0)
  520. for _, v := range req.ChartEdbInfoList {
  521. edbInfoId := v.EdbInfoId
  522. edbInfo, err := edbInfoModel.GetEdbInfoById(edbInfoId)
  523. if err != nil {
  524. if err == utils.ErrNoRow {
  525. response.FailMsg("操作失败", "图表不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  526. return
  527. }
  528. response.FailMsg("操作失败", "获取图表的指标信息失败,Err:"+err.Error(), c)
  529. return
  530. }
  531. if edbInfo == nil {
  532. response.FailMsg("操作失败", "指标不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  533. return
  534. }
  535. if edbInfo.EdbInfoId <= 0 {
  536. response.FailMsg("指标已被删除,请重新选择!", "指标不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  537. return
  538. }
  539. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  540. edbInfo.EdbNameSource = edbInfo.EdbName
  541. //edbList = append(edbList, edbInfo)
  542. }
  543. chartEdbInfoList = req.ChartEdbInfoList
  544. } else {
  545. if req.ExtraConfig == `` {
  546. response.FailMsg("请选择指标!", "请选择指标!, Err:"+err.Error(), c)
  547. return
  548. }
  549. // 校验配置的指标列表
  550. tmpEdbInfoIdArr, err, errMsg := chart.CheckChartExtraConfig(chartItem.ChartType, req.ExtraConfig)
  551. if err != nil {
  552. response.FailMsg(errMsg, "修改失败!, Err:"+err.Error(), c)
  553. return
  554. }
  555. edbInfoIdArr = tmpEdbInfoIdArr
  556. lenEdbInfoIdArr := len(edbInfoIdArr)
  557. if lenEdbInfoIdArr > 0 {
  558. tmpEdbList, err := edbInfoModel.GetEdbInfoByIdList(edbInfoIdArr)
  559. if err != nil {
  560. response.FailMsg("指标异常!", "指标异常!, Err:"+err.Error(), c)
  561. return
  562. }
  563. if len(tmpEdbList) != lenEdbInfoIdArr {
  564. response.FailMsg("指标异常!", fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr), c)
  565. return
  566. }
  567. for _, v := range tmpEdbList {
  568. chartEdbInfoList = append(chartEdbInfoList, &chartInfoModel.ChartSaveItem{
  569. EdbInfoId: v.EdbInfoId,
  570. MaxData: 0,
  571. MinData: 0,
  572. IsOrder: false,
  573. IsAxis: 1,
  574. EdbInfoType: 1,
  575. LeadValue: 0,
  576. LeadUnit: "",
  577. ChartStyle: "",
  578. ChartColor: "",
  579. //PredictChartColor: "",
  580. ChartWidth: 0,
  581. Source: utils.CHART_SOURCE_DEFAULT,
  582. })
  583. }
  584. }
  585. }
  586. if len(edbInfoIdArr) <= 0 {
  587. response.Fail("请选择指标!", c)
  588. return
  589. }
  590. if chartItem.ChartType == 2 && len(req.ChartEdbInfoList) > 1 {
  591. response.Fail("您选择的图表样式为季节性图表,只支持单指标画图!", c)
  592. return
  593. }
  594. if req.Calendar == "" {
  595. req.Calendar = "公历"
  596. }
  597. sort.Ints(edbInfoIdArr)
  598. var edbInfoIdArrStr []string
  599. for _, v := range edbInfoIdArr {
  600. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  601. }
  602. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  603. err = chart.ModifyChartInfoAndMapping(edbInfoIdStr, &req, chartItem.ChartType, chartEdbInfoList)
  604. if err != nil {
  605. response.Fail("图表保存失败, Err:"+err.Error(), c)
  606. return
  607. }
  608. // 清除图表缓存
  609. cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  610. _ = global.Redis.Del(context.TODO(), cacheKey)
  611. // 新增操作日志
  612. {
  613. chartLog := new(chart_info_log.ChartInfoLog)
  614. chartLog.ChartName = chartItem.ChartName
  615. chartLog.ChartInfoId = req.ChartInfoId
  616. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  617. chartLog.SysUserId = int(adminInfo.AdminID)
  618. chartLog.SysUserRealName = adminInfo.RealName
  619. chartLog.UniqueCode = chartItem.UniqueCode
  620. chartLog.CreateTime = time.Now()
  621. bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
  622. chartLog.Content = string(bodyBytes)
  623. chartLog.Status = "修改配置项"
  624. chartLog.Method = c.Request.URL.String()
  625. go chartLog.Create()
  626. }
  627. response.OkData("操作成功", "", c)
  628. }