chart_info.go 22 KB

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