chart_info.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. package chart
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "hongze/hongze_yb/controller/response"
  7. "hongze/hongze_yb/global"
  8. "hongze/hongze_yb/models/response/chart_info"
  9. chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
  10. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  11. "hongze/hongze_yb/models/tables/chart_info_log"
  12. edbDataModel "hongze/hongze_yb/models/tables/edb_data"
  13. edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
  14. "hongze/hongze_yb/services/alarm_msg"
  15. "hongze/hongze_yb/services/chart"
  16. edbDataService "hongze/hongze_yb/services/edb_data"
  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. } else {
  98. startDate = startDate + "-01"
  99. endDate = endDate + "-01"
  100. }
  101. case 6:
  102. if startDate == "" && chartInfo.StartDate != "" {
  103. startDate = chartInfo.StartDate
  104. } else {
  105. startDate = startDate + "-01"
  106. }
  107. case 7:
  108. startDate = "2018-01-01"
  109. case 8:
  110. startDate = "2019-01-01"
  111. case 9:
  112. startDate = "2020-01-01"
  113. case 11:
  114. startDate = "2022-01-01"
  115. }
  116. if chartType == 2 {
  117. // 季节性图表
  118. var seasonStartDate, seasonEndDate string
  119. if reqSeasonStartDate == "" {
  120. seasonStartDate = chartInfo.SeasonStartDate
  121. } else {
  122. seasonStartDate = reqSeasonStartDate
  123. }
  124. if reqSeasonEndDate == "" {
  125. seasonEndDate = chartInfo.SeasonEndDate
  126. } else {
  127. seasonEndDate = reqSeasonEndDate
  128. }
  129. if seasonStartDate != "" {
  130. startDate = seasonStartDate + "-01-01"
  131. } else {
  132. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  133. startDate = strconv.Itoa(fivePre) + "-01-01"
  134. }
  135. if seasonEndDate != "" {
  136. endDate = seasonEndDate + "-12-31"
  137. } else {
  138. endDate = time.Now().Format(utils.FormatDate)
  139. }
  140. }
  141. // 获取图表指标映射
  142. mappingList := make([]*chartEdbMappingModel.ChartEdbInfoMapping, 0)
  143. if chartInfoId > 0 {
  144. mappingList, err = chartEdbMappingModel.GetMappingListByChartInfoId(chartInfoId)
  145. if err != nil {
  146. response.FailMsg("获取失败", "获取图表指标信息失败4001, Err:"+err.Error(), c)
  147. return
  148. }
  149. } else {
  150. if edbInfoId != "" {
  151. mappingList, err = chartEdbMappingModel.GetMappingListByEdbInfoId(edbInfoId)
  152. if err != nil {
  153. response.FailMsg("获取失败", "获取图表指标信息失败4002, Err:"+err.Error(), c)
  154. return
  155. }
  156. }
  157. }
  158. // 指标列表
  159. sourceArr := make([]string, 0)
  160. edbList := make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
  161. for _, v := range mappingList {
  162. item := new(chartEdbMappingModel.ChartEdbInfoMappingList)
  163. item.EdbInfoId = v.EdbInfoId
  164. item.SourceName = v.SourceName
  165. item.Source = v.Source
  166. item.EdbCode = v.EdbCode
  167. item.EdbName = v.EdbName
  168. item.Frequency = v.Frequency
  169. item.Unit = v.Unit
  170. item.StartDate = v.StartDate
  171. item.EndDate = v.EndDate
  172. item.ModifyTime = v.ModifyTime
  173. if !utils.InArray(v.SourceName, sourceArr) {
  174. sourceArr = append(sourceArr, v.SourceName)
  175. }
  176. if chartInfoId <= 0 {
  177. item.IsAxis = 1
  178. item.LeadValue = 0
  179. item.LeadUnit = ""
  180. item.ChartEdbMappingId = 0
  181. item.ChartInfoId = 0
  182. item.IsOrder = false
  183. item.EdbInfoType = 1
  184. item.ChartStyle = ""
  185. item.ChartColor = ""
  186. item.ChartWidth = 0
  187. item.MaxData = v.MaxValue
  188. item.MinData = v.MinValue
  189. } else {
  190. item.IsAxis = v.IsAxis
  191. item.EdbInfoType = v.EdbInfoType
  192. item.LeadValue = v.LeadValue
  193. item.LeadUnit = v.LeadUnit
  194. item.ChartEdbMappingId = v.ChartEdbMappingId
  195. item.ChartInfoId = v.ChartInfoId
  196. item.ChartStyle = v.ChartStyle
  197. item.ChartColor = v.ChartColor
  198. item.ChartWidth = v.ChartWidth
  199. item.IsOrder = v.IsOrder
  200. item.MaxData = v.MaxData
  201. item.MinData = v.MinData
  202. }
  203. item.LatestValue = v.LatestValue
  204. item.LatestDate = v.LatestDate
  205. item.UniqueCode = v.UniqueCode
  206. item.ChartStyle = v.ChartStyle
  207. var startDateReal string
  208. var diffSeconds int64
  209. if chartType == 2 { //季节性图表
  210. startDateReal = startDate
  211. } else {
  212. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 {
  213. // 领先指标
  214. var startTimeRealTemp time.Time
  215. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  216. switch v.LeadUnit {
  217. case "天":
  218. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  219. case "月":
  220. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  221. case "季":
  222. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  223. case "周":
  224. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  225. case "年":
  226. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  227. }
  228. if startTimeRealTemp.Before(startDateParse) {
  229. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  230. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  231. } else {
  232. startDateReal = startDate
  233. diffSeconds = 0
  234. }
  235. } else {
  236. startDateReal = startDate
  237. }
  238. }
  239. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  240. calendarPreYear := 0
  241. if calendar == "农历" {
  242. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  243. if err != nil {
  244. response.FailMsg("获取失败", "农历时间转换有误4001, Err:"+err.Error(), c)
  245. //fmt.Println("time.Parse:" + err.Error())
  246. return
  247. }
  248. calendarPreYear = newStartDateReal.Year() - 1
  249. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  250. startDateReal = newStartDateReal.Format(utils.FormatDate)
  251. }
  252. dataList := make([]*edbDataModel.EdbDataList, 0)
  253. fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  254. dataList, err = edbDataModel.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
  255. if err != nil {
  256. response.FailMsg("获取失败", "获取图表指标信息失败4003, Err:"+err.Error(), c)
  257. return
  258. }
  259. if diffSeconds != 0 && v.EdbInfoType == 0 {
  260. dataListLen := len(dataList)
  261. for i := 0; i < dataListLen; i++ {
  262. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  263. }
  264. }
  265. if chartType == 2 {
  266. if calendar == "农历" {
  267. if len(dataList) <= 0 {
  268. result := new(edbDataModel.EdbDataResult)
  269. item.DataList = result
  270. } else {
  271. result, err := edbDataService.AddCalculateQuarter(dataList)
  272. if err != nil {
  273. response.FailMsg("获取失败", "获取农历数据失败4002, Err:"+err.Error(), c)
  274. return
  275. }
  276. if result.List[0].Year != calendarPreYear {
  277. itemList := make([]*edbDataModel.EdbDataList, 0)
  278. items := new(edbDataModel.EdbDataItems)
  279. //items.Year = calendarPreYear
  280. items.Items = itemList
  281. newResult := new(edbDataModel.EdbDataResult)
  282. newResult.List = append(newResult.List, items)
  283. newResult.List = append(newResult.List, result.List...)
  284. item.DataList = newResult
  285. } else {
  286. item.DataList = result
  287. }
  288. }
  289. } else {
  290. currentYear := time.Now().Year()
  291. quarterDataList := make([]*edbDataModel.QuarterData, 0)
  292. quarterMap := make(map[int][]*edbDataModel.EdbDataList)
  293. var quarterArr []int
  294. for _, v := range dataList {
  295. itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
  296. if err != nil {
  297. response.FailMsg("获取失败", "季度指标日期转换失败, Err:"+err.Error()+";DataTime:"+v.DataTime, c)
  298. return
  299. }
  300. year := itemDate.Year()
  301. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  302. timestamp := newItemDate.UnixNano() / 1e6
  303. v.DataTimestamp = timestamp
  304. if findVal, ok := quarterMap[year]; !ok {
  305. quarterArr = append(quarterArr, year)
  306. findVal = append(findVal, v)
  307. quarterMap[year] = findVal
  308. } else {
  309. findVal = append(findVal, v)
  310. quarterMap[year] = findVal
  311. }
  312. }
  313. for _, v := range quarterArr {
  314. itemList := quarterMap[v]
  315. quarterItem := new(edbDataModel.QuarterData)
  316. quarterItem.Year = v
  317. quarterItem.DataList = itemList
  318. quarterDataList = append(quarterDataList, quarterItem)
  319. }
  320. item.DataList = quarterDataList
  321. }
  322. } else {
  323. item.DataList = dataList
  324. }
  325. edbList = append(edbList, item)
  326. }
  327. sourceArr = append(sourceArr, "弘则研究")
  328. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  329. // 访问记录-仅普通用户记录
  330. userInfo := user.GetInfoByClaims(c)
  331. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  332. if !ok {
  333. reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
  334. myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
  335. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  336. }
  337. resp := new(chart_info.ChartInfoDetailResp)
  338. resp.ChartInfo = chartInfo
  339. resp.EdbInfoList = edbList
  340. response.OkData("获取成功", resp, c)
  341. }
  342. // RefreshChartInfo 刷新图表信息
  343. // @Tags 图库模块
  344. // @Summary 刷新图表信息
  345. // @Description 刷新图表信息
  346. // @Security ApiKeyAuth
  347. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  348. // @Accept json
  349. // @Product json
  350. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  351. // @Success 200 {string} string "操作成功"
  352. // @failure 400 {string} string "操作失败"
  353. // @Router /my_chart/refreshChartInfo [post]
  354. func RefreshChartInfo(c *gin.Context) {
  355. // 参数校验
  356. var req chartInfoModel.RefreshChartInfoReq
  357. if c.ShouldBind(&req) != nil {
  358. response.Fail("参数异常", c)
  359. return
  360. }
  361. chartInfoId := req.ChartInfoId
  362. if chartInfoId == 0 {
  363. response.Fail("参数有误", c)
  364. return
  365. }
  366. userInfo := user.GetInfoByClaims(c)
  367. ok, _, err := user.GetAdminByUserInfo(userInfo)
  368. if err != nil {
  369. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  370. return
  371. }
  372. if !ok {
  373. // 普通用户刷新频率限制-每个用户/图/天/2次
  374. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  375. fmt.Println("refreshCacheKey:", cacheKey)
  376. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  377. if countUserRefresh >= 2 {
  378. response.Ok("目前已是最新数据", c)
  379. return
  380. }
  381. countUserRefresh += 1
  382. now := time.Now()
  383. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  384. sub := today.Sub(now)
  385. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  386. }
  387. // 图表信息校验
  388. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  389. if err != nil {
  390. if err == utils.ErrNoRow {
  391. response.Fail("图表已被删除,无需刷新", c)
  392. return
  393. }
  394. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  395. return
  396. }
  397. // 刷新图表
  398. if err = chart.RefreshChart(chartInfo.ChartInfoId); err != nil {
  399. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  400. if global.CONFIG.Serve.RunMode == "release" {
  401. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  402. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  403. } else {
  404. global.LOG.Info(errContent)
  405. }
  406. }
  407. //清除图表缓存
  408. {
  409. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  410. _ = global.Redis.Del(context.TODO(), key)
  411. }
  412. response.OkData("刷新成功", "", c)
  413. }
  414. // EditChartInfo 编辑图表信息
  415. // @Tags 图库模块
  416. // @Summary 编辑图表信息
  417. // @Description 编辑图表信息
  418. // @Security ApiKeyAuth
  419. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  420. // @Accept json
  421. // @Product json
  422. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  423. // @Success 200 {string} string "操作成功"
  424. // @failure 400 {string} string "操作失败"
  425. // @Router /my_chart/editChartInfo [post]
  426. func EditChartInfo(c *gin.Context) {
  427. // 参数校验
  428. var req chartInfoModel.SaveChartInfoReq
  429. if c.ShouldBind(&req) != nil {
  430. response.Fail("参数异常", c)
  431. return
  432. }
  433. if req.ChartInfoId > 0 && len(req.ChartEdbInfoList) <= 0 {
  434. response.Fail("参数异常", c)
  435. return
  436. }
  437. // 操作权限校验
  438. userInfo := user.GetInfoByClaims(c)
  439. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  440. if err != nil {
  441. response.Fail("操作人信息有误", c)
  442. return
  443. }
  444. if !ok {
  445. response.Fail("非内部人员无权进行操作", c)
  446. return
  447. }
  448. // 图表信息校验
  449. chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
  450. if err != nil {
  451. if err == utils.ErrNoRow {
  452. response.Fail("图表已被删除,请刷新页面!", c)
  453. return
  454. }
  455. response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
  456. return
  457. }
  458. if chartItem.ChartType == 2 && len(req.ChartEdbInfoList) > 1 {
  459. response.Fail("您选择的图表样式为季节性图表,只支持单指标画图!", c)
  460. return
  461. }
  462. if req.Calendar == "" {
  463. req.Calendar = "公历"
  464. }
  465. // 指标信息
  466. var edbInfoIdArr []int
  467. edbList := make([]*edbInfoModel.EdbInfo, 0)
  468. for _, v := range req.ChartEdbInfoList {
  469. edbInfo, err := edbInfoModel.GetEdbInfoById(v.EdbInfoId)
  470. if err != nil {
  471. if err == utils.ErrNoRow {
  472. response.FailMsg("操作失败", "图表不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  473. return
  474. }
  475. response.FailMsg("操作失败", "获取图表的指标信息失败,Err:"+err.Error(), c)
  476. return
  477. }
  478. if edbInfo == nil {
  479. response.FailMsg("操作失败", "指标不存在,ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
  480. return
  481. }
  482. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  483. edbInfo.EdbNameSource = edbInfo.EdbName
  484. edbList = append(edbList, edbInfo)
  485. }
  486. sort.Ints(edbInfoIdArr)
  487. var edbInfoIdArrStr []string
  488. for _, v := range edbInfoIdArr {
  489. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  490. }
  491. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  492. err = chart.ModifyChartInfoAndMapping(edbInfoIdStr, &req, chartItem.ChartType)
  493. if err != nil {
  494. response.Fail("图表保存失败, Err:"+err.Error(), c)
  495. return
  496. }
  497. // 清除图表缓存
  498. cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  499. _ = global.Redis.Del(context.TODO(), cacheKey)
  500. // 新增操作日志
  501. {
  502. chartLog := new(chart_info_log.ChartInfoLog)
  503. chartLog.ChartName = chartItem.ChartName
  504. chartLog.ChartInfoId = req.ChartInfoId
  505. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  506. chartLog.SysUserId = int(adminInfo.AdminID)
  507. chartLog.SysUserRealName = adminInfo.RealName
  508. chartLog.UniqueCode = chartItem.UniqueCode
  509. chartLog.CreateTime = time.Now()
  510. bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
  511. chartLog.Content = string(bodyBytes)
  512. chartLog.Status = "修改配置项"
  513. chartLog.Method = c.Request.URL.String()
  514. go chartLog.Create()
  515. }
  516. response.OkData("操作成功", "", c)
  517. }