chart_info.go 23 KB

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