chart_info.go 21 KB

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