chart_info.go 14 KB

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