chart_info.go 21 KB

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