chart.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/eta_server/eta_chart_lib/models"
  6. "hongze/eta_server/eta_chart_lib/models/data_manage"
  7. "hongze/eta_server/eta_chart_lib/services/data"
  8. "hongze/eta_server/eta_chart_lib/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // 图表
  14. type ChartController struct {
  15. BaseAuthController
  16. }
  17. // ChartInfoDetail
  18. // @Title 获取图表详情
  19. // @Description 获取图表详情接口
  20. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  21. // @Success 200 {object} data_manage.ChartInfoDetailResp
  22. // @router /detail [get]
  23. func (this *ChartController) ChartInfoDetail() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. uniqueCode := this.GetString("UniqueCode")
  30. if uniqueCode == "" {
  31. br.Msg = "参数错误"
  32. br.ErrMsg = "参数错误,uniqueCode is empty"
  33. return
  34. }
  35. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  36. resp := new(models.ChartInfoDetailResp)
  37. //判断是否有缓存
  38. if utils.Re == nil {
  39. if utils.Re == nil && utils.Rc.IsExist(key) {
  40. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  41. err := json.Unmarshal(data, &resp)
  42. if err == nil && resp != nil {
  43. br.Ret = 200
  44. br.Success = true
  45. br.Msg = "获取成功"
  46. br.Data = resp
  47. fmt.Println("source redis")
  48. return
  49. }
  50. }
  51. }
  52. }
  53. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  54. if err != nil {
  55. if err.Error() == utils.ErrNoRow() {
  56. br.Msg = "该图已被删除,请刷新页面"
  57. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  58. return
  59. }
  60. br.Msg = "获取失败"
  61. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  62. return
  63. }
  64. chartInfoId := chartInfo.ChartInfoId
  65. dateType := chartInfo.DateType
  66. if dateType <= 0 {
  67. dateType = 3
  68. }
  69. startDate := chartInfo.StartDate
  70. endDate := chartInfo.EndDate
  71. seasonStartDate := chartInfo.SeasonStartDate
  72. seasonEndDate := chartInfo.SeasonEndDate
  73. calendar := chartInfo.Calendar
  74. chartType := chartInfo.ChartType
  75. if calendar == "" {
  76. calendar = "公历"
  77. }
  78. switch dateType {
  79. case 1:
  80. startDate = "2000-01-01"
  81. //endDate = time.Now().Format(utils.FormatDate)
  82. endDate = ""
  83. case 2:
  84. startDate = "2010-01-01"
  85. //endDate = time.Now().Format(utils.FormatDate)
  86. endDate = ""
  87. case 3:
  88. startDate = "2015-01-01"
  89. //endDate = time.Now().Format(utils.FormatDate)
  90. endDate = ""
  91. case 4:
  92. startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  93. startDate = "2021-01-01"
  94. //endDate = time.Now().Format(utils.FormatDate)
  95. endDate = ""
  96. case 5:
  97. startDate = startDate + "-01"
  98. endDate = endDate + "-01"
  99. case 6:
  100. //startDate = startDate + "-01"
  101. //endDate = time.Now().Format(utils.FormatDate)
  102. endDate = ""
  103. case 7:
  104. startDate = "2018-01-01"
  105. //endDate = time.Now().Format(utils.FormatDate)
  106. endDate = ""
  107. case 8:
  108. startDate = "2019-01-01"
  109. //endDate = time.Now().Format(utils.FormatDate)
  110. endDate = ""
  111. case 9:
  112. startDate = "2020-01-01"
  113. //endDate = time.Now().Format(utils.FormatDate)
  114. endDate = ""
  115. case 11:
  116. startDate = "2022-01-01"
  117. endDate = ""
  118. }
  119. // 兼容日期错误
  120. {
  121. if strings.Count(startDate, "-") == 1 {
  122. startDate = startDate + "-01"
  123. }
  124. if strings.Count(endDate, "-") == 1 {
  125. endDate = endDate + "-01"
  126. }
  127. }
  128. if chartType == 2 {
  129. if seasonStartDate != "" {
  130. startDate = seasonStartDate + "-01"
  131. } else {
  132. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  133. startDate = strconv.Itoa(fivePre) + "-01-01"
  134. }
  135. if seasonEndDate != "" {
  136. seasonEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, seasonEndDate+"-01", time.Local)
  137. if tmpErr != nil {
  138. br.Msg = "获取失败"
  139. br.ErrMsg = "获取图表,指标信息失败,Err:" + tmpErr.Error()
  140. return
  141. }
  142. endDate = seasonEndDateTime.AddDate(0, 1, -1).Format(utils.FormatDate)
  143. } else {
  144. //endDate = time.Now().Format(utils.FormatDate)
  145. endDate = ""
  146. }
  147. }
  148. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  149. if err != nil {
  150. br.Msg = "获取失败"
  151. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  152. return
  153. }
  154. extraConfigStr := chartInfo.ExtraConfig
  155. // 柱方图的一些配置
  156. var barConfig data_manage.BarChartInfoReq
  157. if chartInfo != nil && chartInfo.ChartType == 7 {
  158. if chartInfo.BarConfig == `` {
  159. br.Msg = "柱方图未配置"
  160. br.ErrMsg = "柱方图未配置"
  161. return
  162. }
  163. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  164. if err != nil {
  165. br.Msg = "柱方图配置异常"
  166. br.ErrMsg = "柱方图配置异常"
  167. return
  168. }
  169. extraConfigStr = chartInfo.BarConfig
  170. }
  171. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  172. if err != nil {
  173. br.Msg = "获取失败"
  174. if errMsg != `` {
  175. br.Msg = errMsg
  176. }
  177. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  178. return
  179. }
  180. for _, v := range edbList {
  181. // 指标别名
  182. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  183. for _, reqEdb := range barConfig.EdbInfoIdList {
  184. if v.EdbInfoId == reqEdb.EdbInfoId {
  185. v.EdbAliasName = reqEdb.Name
  186. }
  187. }
  188. }
  189. }
  190. // 图表的指标来源
  191. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  192. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  193. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  194. resp.ChartInfo = chartInfo
  195. resp.EdbInfoList = edbList
  196. resp.XEdbIdValue = xEdbIdValue
  197. resp.YDataList = yDataList
  198. resp.DataResp = dataResp
  199. if utils.Re == nil {
  200. data, _ := json.Marshal(resp)
  201. utils.Rc.Put(key, data, 10*time.Minute)
  202. }
  203. br.Ret = 200
  204. br.Success = true
  205. br.Msg = "获取成功"
  206. br.Data = resp
  207. }
  208. // ChartInfoRefresh
  209. // @Title 图表刷新接口
  210. // @Description 图表刷新接口
  211. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  212. // @Success Ret=200 刷新成功
  213. // @router /refresh [get]
  214. func (this *ChartController) ChartInfoRefresh() {
  215. br := new(models.BaseResponse).Init()
  216. chartId := 0
  217. defer func() {
  218. // 添加日志
  219. if chartId > 0 {
  220. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  221. Ip: this.Ctx.Input.IP(),
  222. ChartId: chartId,
  223. CreateTime: time.Now(),
  224. }
  225. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  226. }
  227. this.Data["json"] = br
  228. this.ServeJSON()
  229. }()
  230. uniqueCode := this.GetString("UniqueCode")
  231. if uniqueCode == "" {
  232. br.Msg = "参数错误"
  233. br.ErrMsg = "参数错误,uniqueCode is empty"
  234. return
  235. }
  236. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  237. if err != nil {
  238. if err.Error() == utils.ErrNoRow() {
  239. br.Msg = "该图已被删除,请刷新页面"
  240. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  241. return
  242. }
  243. br.Msg = "获取失败"
  244. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  245. return
  246. }
  247. chartId = chartInfo.ChartInfoId
  248. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  249. //if err != nil {
  250. // br.Msg = "刷新失败"
  251. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  252. // return
  253. //}
  254. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  255. if err != nil {
  256. br.Msg = "刷新失败"
  257. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  258. return
  259. }
  260. //清除数据缓存
  261. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  262. if utils.Re == nil {
  263. utils.Rc.Delete(key)
  264. }
  265. br.Ret = 200
  266. br.Success = true
  267. br.Msg = "刷新成功"
  268. }
  269. // 获取频度的英文版
  270. func GetFrequencyEn(frequency string) (frequencyEn string) {
  271. switch frequency {
  272. case "日度":
  273. frequencyEn = "day"
  274. return
  275. case "周度":
  276. frequencyEn = "week"
  277. return
  278. case "旬度":
  279. frequencyEn = "ten days"
  280. return
  281. case "月度":
  282. frequencyEn = "month"
  283. return
  284. case "季度":
  285. frequencyEn = "quarter"
  286. return
  287. case "年度":
  288. frequencyEn = "year"
  289. return
  290. }
  291. return
  292. }
  293. func GetLeadUnitEn(unit string) (unitEn string) {
  294. switch unit {
  295. case "天":
  296. unitEn = "day"
  297. return
  298. case "周":
  299. unitEn = "week"
  300. return
  301. case "月":
  302. unitEn = "month"
  303. return
  304. case "季":
  305. unitEn = "quarter"
  306. return
  307. case "年":
  308. unitEn = "year"
  309. return
  310. }
  311. return
  312. }
  313. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  314. func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  315. resp = new(models.ChartInfoDetailResp)
  316. chartInfoId := chartInfo.ChartInfoId
  317. dateType := chartInfo.DateType
  318. if dateType <= 0 {
  319. dateType = 3
  320. }
  321. startDate := chartInfo.StartDate
  322. endDate := chartInfo.EndDate
  323. seasonStartDate := chartInfo.SeasonStartDate
  324. seasonEndDate := chartInfo.SeasonEndDate
  325. calendar := chartInfo.Calendar
  326. chartType := chartInfo.ChartType
  327. if calendar == "" {
  328. calendar = "公历"
  329. }
  330. switch dateType {
  331. case 1:
  332. startDate = "2000-01-01"
  333. //endDate = time.Now().Format(utils.FormatDate)
  334. endDate = ""
  335. case 2:
  336. startDate = "2010-01-01"
  337. //endDate = time.Now().Format(utils.FormatDate)
  338. endDate = ""
  339. case 3:
  340. startDate = "2015-01-01"
  341. //endDate = time.Now().Format(utils.FormatDate)
  342. endDate = ""
  343. case 4:
  344. startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  345. startDate = "2021-01-01"
  346. //endDate = time.Now().Format(utils.FormatDate)
  347. endDate = ""
  348. case 5:
  349. startDate = startDate + "-01"
  350. endDate = endDate + "-01"
  351. case 6:
  352. //startDate = startDate + "-01"
  353. //endDate = time.Now().Format(utils.FormatDate)
  354. endDate = ""
  355. case 7:
  356. startDate = "2018-01-01"
  357. //endDate = time.Now().Format(utils.FormatDate)
  358. endDate = ""
  359. case 8:
  360. startDate = "2019-01-01"
  361. //endDate = time.Now().Format(utils.FormatDate)
  362. endDate = ""
  363. case 9:
  364. startDate = "2020-01-01"
  365. //endDate = time.Now().Format(utils.FormatDate)
  366. endDate = ""
  367. case 11:
  368. startDate = "2022-01-01"
  369. endDate = ""
  370. }
  371. // 兼容日期错误
  372. {
  373. if strings.Count(startDate, "-") == 1 {
  374. startDate = startDate + "-01"
  375. }
  376. if strings.Count(endDate, "-") == 1 {
  377. endDate = endDate + "-01"
  378. }
  379. }
  380. if chartType == 2 {
  381. if seasonStartDate != "" {
  382. startDate = seasonStartDate + "-01"
  383. } else {
  384. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  385. startDate = strconv.Itoa(fivePre) + "-01-01"
  386. }
  387. if seasonEndDate != "" {
  388. seasonEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, seasonEndDate+"-01", time.Local)
  389. if tmpErr != nil {
  390. msg = "获取失败"
  391. errMsg = "获取图表,指标信息失败,Err:" + tmpErr.Error()
  392. return
  393. }
  394. endDate = seasonEndDateTime.AddDate(0, 1, -1).Format(utils.FormatDate)
  395. } else {
  396. //endDate = time.Now().Format(utils.FormatDate)
  397. endDate = ""
  398. }
  399. }
  400. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  401. if err != nil {
  402. msg = "获取失败"
  403. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  404. return
  405. }
  406. extraConfigStr := chartInfo.ExtraConfig
  407. // 柱方图的一些配置
  408. var barConfig data_manage.BarChartInfoReq
  409. if chartInfo != nil && chartInfo.ChartType == 7 {
  410. if chartInfo.BarConfig == `` {
  411. msg = "柱方图未配置"
  412. errMsg = "柱方图未配置"
  413. return
  414. }
  415. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  416. if err != nil {
  417. msg = "柱方图配置异常"
  418. errMsg = "柱方图配置异常"
  419. return
  420. }
  421. extraConfigStr = chartInfo.BarConfig
  422. }
  423. edbList, xEdbIdValue, yDataList, dataResp, err, tmpErrMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  424. if err != nil {
  425. msg = "获取失败"
  426. if tmpErrMsg != `` {
  427. msg = tmpErrMsg
  428. }
  429. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  430. return
  431. }
  432. for _, v := range edbList {
  433. // 指标别名
  434. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  435. for _, reqEdb := range barConfig.EdbInfoIdList {
  436. if v.EdbInfoId == reqEdb.EdbInfoId {
  437. v.EdbAliasName = reqEdb.Name
  438. }
  439. }
  440. }
  441. }
  442. // 图表的指标来源
  443. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  444. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  445. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  446. resp.ChartInfo = chartInfo
  447. resp.EdbInfoList = edbList
  448. resp.XEdbIdValue = xEdbIdValue
  449. resp.YDataList = yDataList
  450. resp.DataResp = dataResp
  451. if utils.Re == nil {
  452. jsonData, _ := json.Marshal(resp)
  453. utils.Rc.Put(key, jsonData, 10*time.Minute)
  454. }
  455. isOk = true
  456. return
  457. }