chart_info.go 23 KB

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