chart_info.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. "hongze/hongze_yb/models/response/chart_info"
  11. chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
  12. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  13. "hongze/hongze_yb/models/tables/chart_info_log"
  14. edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
  15. "hongze/hongze_yb/services/alarm_msg"
  16. "hongze/hongze_yb/services/chart"
  17. "hongze/hongze_yb/services/user"
  18. "hongze/hongze_yb/utils"
  19. "io/ioutil"
  20. "sort"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. // GetChartInfoDetail 获取图表详情
  26. // @Tags 图库模块
  27. // @Summary 获取图表详情
  28. // @Description 获取图表详情
  29. // @Security ApiKeyAuth
  30. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  31. // @Accept json
  32. // @Product json
  33. // @Param DateType query string false "时间段:1-00年至今; 2-10年至今; 3-15年至今; 4-21年至今; 5-指定区间; 6-指定年月至今; 7-18年至今; 8-19年至今; 9-20年至今"
  34. // @Param ClassifyId query string false "图表分类ID"
  35. // @Success 200 {object} chart_info.ChartInfoDetailResp
  36. // @failure 400 {string} string "图表详情获取失败"
  37. // @Router /my_chart/getChartInfoDetail [get]
  38. func GetChartInfoDetail(c *gin.Context) {
  39. // 图表ID
  40. reqChartInfoId := c.DefaultQuery("ChartInfoId", "")
  41. if reqChartInfoId == "" {
  42. response.Fail("参数有误:图表ID", c)
  43. return
  44. }
  45. chartInfoId, _ := strconv.Atoi(reqChartInfoId)
  46. startDate := c.DefaultQuery("StartDate", "")
  47. endDate := c.DefaultQuery("EndDate", "")
  48. // 图表样式类型
  49. reqChartType := c.DefaultQuery("ChartType", "")
  50. chartType, _ := strconv.Atoi(reqChartType)
  51. // 季节性图表时间
  52. reqSeasonStartDate := c.DefaultQuery("SeasonStartDate", "")
  53. reqSeasonEndDate := c.DefaultQuery("SeasonEndDate", "")
  54. // 指标ID
  55. edbInfoId := c.DefaultQuery("EdbInfoId", "")
  56. // 公历/农历
  57. reqCalendar := c.DefaultQuery("Calendar", "")
  58. // 获取图表信息
  59. var err error
  60. chartInfo := new(chartInfoModel.ChartInfoView)
  61. chartInfo, err = chartInfoModel.GetChartInfoViewById(chartInfoId)
  62. if err != nil {
  63. if err == utils.ErrNoRow {
  64. response.Fail("图表不存在,请刷新页面", c)
  65. return
  66. }
  67. response.FailMsg("获取失败", "获取图表信息失败, Err:"+err.Error(), c)
  68. return
  69. }
  70. chartType = chartInfo.ChartType
  71. calendar := chartInfo.Calendar
  72. if reqCalendar != "" {
  73. calendar = reqCalendar
  74. }
  75. // 时段筛选
  76. reqDateType := c.DefaultQuery("DateType", "")
  77. dateType := chartInfo.DateType
  78. if reqDateType != "" {
  79. dateType, _ = strconv.Atoi(reqDateType)
  80. }
  81. if dateType <= 0 {
  82. dateType = 3 // 默认同后台15年至今
  83. }
  84. switch dateType {
  85. case 1:
  86. startDate = "2000-01-01"
  87. case 2:
  88. startDate = "2010-01-01"
  89. case 3:
  90. startDate = "2015-01-01"
  91. case 4:
  92. startDate = "2021-01-01"
  93. case 5:
  94. if startDate == "" && chartInfo.StartDate != "" {
  95. startDate = chartInfo.StartDate
  96. endDate = chartInfo.EndDate
  97. }
  98. case 6:
  99. if startDate == "" && chartInfo.StartDate != "" {
  100. startDate = chartInfo.StartDate
  101. } else {
  102. startDate = startDate + "-01"
  103. }
  104. //兼容日期数据为2022-09的错误
  105. if strings.Count(startDate, "-") == 1 {
  106. startDate = startDate + "-01"
  107. }
  108. case 7:
  109. startDate = "2018-01-01"
  110. case 8:
  111. startDate = "2019-01-01"
  112. case 9:
  113. startDate = "2020-01-01"
  114. case 11:
  115. startDate = "2022-01-01"
  116. }
  117. // 兼容日期错误
  118. {
  119. if strings.Count(startDate, "-") == 1 {
  120. startDate = startDate + "-01"
  121. }
  122. if strings.Count(endDate, "-") == 1 {
  123. endDate = endDate + "-01"
  124. }
  125. }
  126. if chartType == 2 {
  127. // 季节性图表
  128. var seasonStartDate, seasonEndDate string
  129. if reqSeasonStartDate == "" {
  130. seasonStartDate = chartInfo.SeasonStartDate
  131. } else {
  132. seasonStartDate = reqSeasonStartDate
  133. }
  134. if reqSeasonEndDate == "" {
  135. seasonEndDate = chartInfo.SeasonEndDate
  136. } else {
  137. seasonEndDate = reqSeasonEndDate
  138. }
  139. if seasonStartDate != "" {
  140. startDate = seasonStartDate + "-01-01"
  141. } else {
  142. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  143. startDate = strconv.Itoa(fivePre) + "-01-01"
  144. }
  145. if seasonEndDate != "" {
  146. endDate = seasonEndDate + "-12-31"
  147. } else {
  148. endDate = time.Now().Format(utils.FormatDate)
  149. }
  150. }
  151. // 获取图表指标映射
  152. mappingList := make([]*chartEdbMappingModel.ChartEdbInfoMapping, 0)
  153. if chartInfoId > 0 {
  154. mappingList, err = chartEdbMappingModel.GetMappingListByChartInfoId(chartInfoId)
  155. if err != nil {
  156. response.FailMsg("获取失败", "获取图表指标信息失败4001, Err:"+err.Error(), c)
  157. return
  158. }
  159. } else {
  160. if edbInfoId != "" {
  161. mappingList, err = chartEdbMappingModel.GetMappingListByEdbInfoId(edbInfoId)
  162. if err != nil {
  163. response.FailMsg("获取失败", "获取图表指标信息失败4002, Err:"+err.Error(), c)
  164. return
  165. }
  166. }
  167. }
  168. //fmt.Println("start_date:", startDate)
  169. //fmt.Println("end_date:", endDate)
  170. barChartInfoDateList := make([]request.BarChartInfoDateReq, 0)
  171. barChartInfoSort := request.BarChartInfoSortReq{}
  172. // 柱方图的一些配置
  173. if chartInfo != nil && chartInfo.ChartType == 7 {
  174. if chartInfo.BarConfig == `` {
  175. response.FailMsg("柱方图未配置", "柱方图未配置", c)
  176. return
  177. }
  178. var barConfig request.BarChartInfoReq
  179. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  180. if err != nil {
  181. response.FailMsg("柱方图配置异常", "柱方图配置异常", c)
  182. return
  183. }
  184. barChartInfoDateList = barConfig.DateList
  185. barChartInfoSort = barConfig.Sort
  186. // 指标别名
  187. for _, v := range mappingList {
  188. for _, confEdb := range barConfig.EdbInfoIdList {
  189. if v.EdbInfoId == confEdb.EdbInfoId {
  190. v.EdbAliasName = confEdb.Name
  191. }
  192. }
  193. }
  194. }
  195. // 获取图表中的指标数据
  196. edbList, xEdbIdValue, yDataList, sourceArr, err := chart.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, barChartInfoDateList, barChartInfoSort)
  197. if err != nil {
  198. response.FailMsg("获取失败", "获取图表,指标信息失败, Err:"+err.Error(), c)
  199. return
  200. }
  201. sourceArr = append(sourceArr, "弘则研究")
  202. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  203. // 访问记录-仅普通用户记录
  204. userInfo := user.GetInfoByClaims(c)
  205. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  206. if !ok {
  207. reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
  208. myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
  209. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  210. }
  211. resp := new(chart_info.ChartInfoDetailResp)
  212. resp.ChartInfo = chartInfo
  213. resp.EdbInfoList = edbList
  214. resp.XEdbIdValue = xEdbIdValue
  215. resp.YDataList = yDataList
  216. response.OkData("获取成功", resp, c)
  217. }
  218. // RefreshChartInfo 刷新图表信息
  219. // @Tags 图库模块
  220. // @Summary 刷新图表信息
  221. // @Description 刷新图表信息
  222. // @Security ApiKeyAuth
  223. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  224. // @Accept json
  225. // @Product json
  226. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  227. // @Success 200 {string} string "操作成功"
  228. // @failure 400 {string} string "操作失败"
  229. // @Router /my_chart/refreshChartInfo [post]
  230. func RefreshChartInfo(c *gin.Context) {
  231. // 参数校验
  232. var req chartInfoModel.RefreshChartInfoReq
  233. if c.ShouldBind(&req) != nil {
  234. response.Fail("参数异常", c)
  235. return
  236. }
  237. chartInfoId := req.ChartInfoId
  238. if chartInfoId == 0 {
  239. response.Fail("参数有误", c)
  240. return
  241. }
  242. userInfo := user.GetInfoByClaims(c)
  243. ok, _, err := user.GetAdminByUserInfo(userInfo)
  244. if err != nil {
  245. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  246. return
  247. }
  248. if !ok {
  249. // 普通用户刷新频率限制-每个用户/图/天/2次
  250. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  251. fmt.Println("refreshCacheKey:", cacheKey)
  252. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  253. if countUserRefresh >= 2 {
  254. response.Ok("目前已是最新数据", c)
  255. return
  256. }
  257. countUserRefresh += 1
  258. now := time.Now()
  259. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  260. sub := today.Sub(now)
  261. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  262. }
  263. // 图表信息校验
  264. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  265. if err != nil {
  266. if err == utils.ErrNoRow {
  267. response.Fail("图表已被删除,无需刷新", c)
  268. return
  269. }
  270. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  271. return
  272. }
  273. // 刷新图表
  274. if err = chart.ChartInfoRefreshV2(chartInfo.ChartInfoId); err != nil {
  275. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  276. if global.CONFIG.Serve.RunMode == "release" {
  277. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  278. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  279. } else {
  280. global.LOG.Info(errContent)
  281. }
  282. }
  283. //清除图表缓存
  284. {
  285. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  286. _ = global.Redis.Del(context.TODO(), key)
  287. }
  288. response.OkData("刷新成功", "", c)
  289. }
  290. // EditChartInfo 编辑图表信息
  291. // @Tags 图库模块
  292. // @Summary 编辑图表信息
  293. // @Description 编辑图表信息
  294. // @Security ApiKeyAuth
  295. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  296. // @Accept json
  297. // @Product json
  298. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  299. // @Success 200 {string} string "操作成功"
  300. // @failure 400 {string} string "操作失败"
  301. // @Router /my_chart/editChartInfo [post]
  302. func EditChartInfo(c *gin.Context) {
  303. // 参数校验
  304. var req chartInfoModel.SaveChartInfoReq
  305. if c.ShouldBind(&req) != nil {
  306. response.Fail("参数异常", c)
  307. return
  308. }
  309. if req.ChartInfoId > 0 && len(req.ChartEdbInfoList) <= 0 {
  310. response.Fail("参数异常", c)
  311. return
  312. }
  313. // 操作权限校验
  314. userInfo := user.GetInfoByClaims(c)
  315. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  316. if err != nil {
  317. response.Fail("操作人信息有误", c)
  318. return
  319. }
  320. if !ok {
  321. response.Fail("非内部人员无权进行操作", c)
  322. return
  323. }
  324. // 图表信息校验
  325. chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
  326. if err != nil {
  327. if err == utils.ErrNoRow {
  328. response.Fail("图表已被删除,请刷新页面!", c)
  329. return
  330. }
  331. response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
  332. return
  333. }
  334. if chartItem.ChartType == 2 && len(req.ChartEdbInfoList) > 1 {
  335. response.Fail("您选择的图表样式为季节性图表,只支持单指标画图!", c)
  336. return
  337. }
  338. if req.Calendar == "" {
  339. req.Calendar = "公历"
  340. }
  341. // 指标信息
  342. var edbInfoIdArr []int
  343. edbList := make([]*edbInfoModel.EdbInfo, 0)
  344. for _, v := range req.ChartEdbInfoList {
  345. edbInfo, err := edbInfoModel.GetEdbInfoById(v.EdbInfoId)
  346. if err != nil {
  347. if err == utils.ErrNoRow {
  348. response.FailMsg("操作失败", "图表不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  349. return
  350. }
  351. response.FailMsg("操作失败", "获取图表的指标信息失败,Err:"+err.Error(), c)
  352. return
  353. }
  354. if edbInfo == nil {
  355. response.FailMsg("操作失败", "指标不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  356. return
  357. }
  358. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  359. edbInfo.EdbNameSource = edbInfo.EdbName
  360. edbList = append(edbList, edbInfo)
  361. }
  362. sort.Ints(edbInfoIdArr)
  363. var edbInfoIdArrStr []string
  364. for _, v := range edbInfoIdArr {
  365. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  366. }
  367. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  368. err = chart.ModifyChartInfoAndMapping(edbInfoIdStr, &req, chartItem.ChartType)
  369. if err != nil {
  370. response.Fail("图表保存失败, Err:"+err.Error(), c)
  371. return
  372. }
  373. // 清除图表缓存
  374. cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  375. _ = global.Redis.Del(context.TODO(), cacheKey)
  376. // 新增操作日志
  377. {
  378. chartLog := new(chart_info_log.ChartInfoLog)
  379. chartLog.ChartName = chartItem.ChartName
  380. chartLog.ChartInfoId = req.ChartInfoId
  381. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  382. chartLog.SysUserId = int(adminInfo.AdminID)
  383. chartLog.SysUserRealName = adminInfo.RealName
  384. chartLog.UniqueCode = chartItem.UniqueCode
  385. chartLog.CreateTime = time.Now()
  386. bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
  387. chartLog.Content = string(bodyBytes)
  388. chartLog.Status = "修改配置项"
  389. chartLog.Method = c.Request.URL.String()
  390. go chartLog.Create()
  391. }
  392. response.OkData("操作成功", "", c)
  393. }