chart.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/models"
  5. "eta/eta_chart_lib/models/data_manage"
  6. "eta/eta_chart_lib/services/data"
  7. "eta/eta_chart_lib/utils"
  8. "fmt"
  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. // 单位
  195. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  196. chartInfo.Unit = yDataList[0].Unit
  197. chartInfo.UnitEn = yDataList[0].UnitEn
  198. }
  199. resp.ChartInfo = chartInfo
  200. resp.EdbInfoList = edbList
  201. resp.XEdbIdValue = xEdbIdValue
  202. resp.YDataList = yDataList
  203. resp.DataResp = dataResp
  204. if utils.Re == nil {
  205. data, _ := json.Marshal(resp)
  206. utils.Rc.Put(key, data, 10*time.Minute)
  207. }
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. br.Data = resp
  212. }
  213. // ChartInfoRefresh
  214. // @Title 图表刷新接口
  215. // @Description 图表刷新接口
  216. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  217. // @Success Ret=200 刷新成功
  218. // @router /refresh [get]
  219. func (this *ChartController) ChartInfoRefresh() {
  220. br := new(models.BaseResponse).Init()
  221. chartId := 0
  222. defer func() {
  223. // 添加日志
  224. if chartId > 0 {
  225. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  226. Ip: this.Ctx.Input.IP(),
  227. ChartId: chartId,
  228. CreateTime: time.Now(),
  229. }
  230. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  231. }
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. uniqueCode := this.GetString("UniqueCode")
  236. if uniqueCode == "" {
  237. br.Msg = "参数错误"
  238. br.ErrMsg = "参数错误,uniqueCode is empty"
  239. return
  240. }
  241. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  242. if err != nil {
  243. if err.Error() == utils.ErrNoRow() {
  244. br.Msg = "该图已被删除,请刷新页面"
  245. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  246. return
  247. }
  248. br.Msg = "获取失败"
  249. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  250. return
  251. }
  252. chartId = chartInfo.ChartInfoId
  253. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  254. //if err != nil {
  255. // br.Msg = "刷新失败"
  256. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  257. // return
  258. //}
  259. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  260. if err != nil {
  261. br.Msg = "刷新失败"
  262. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  263. return
  264. }
  265. //清除数据缓存
  266. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  267. if utils.Re == nil {
  268. utils.Rc.Delete(key)
  269. }
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = "刷新成功"
  273. }
  274. // 获取频度的英文版
  275. func GetFrequencyEn(frequency string) (frequencyEn string) {
  276. switch frequency {
  277. case "日度":
  278. frequencyEn = "day"
  279. return
  280. case "周度":
  281. frequencyEn = "week"
  282. return
  283. case "旬度":
  284. frequencyEn = "ten days"
  285. return
  286. case "月度":
  287. frequencyEn = "month"
  288. return
  289. case "季度":
  290. frequencyEn = "quarter"
  291. return
  292. case "年度":
  293. frequencyEn = "year"
  294. return
  295. }
  296. return
  297. }
  298. func GetLeadUnitEn(unit string) (unitEn string) {
  299. switch unit {
  300. case "天":
  301. unitEn = "day"
  302. return
  303. case "周":
  304. unitEn = "week"
  305. return
  306. case "月":
  307. unitEn = "month"
  308. return
  309. case "季":
  310. unitEn = "quarter"
  311. return
  312. case "年":
  313. unitEn = "year"
  314. return
  315. }
  316. return
  317. }
  318. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  319. func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  320. resp = new(models.ChartInfoDetailResp)
  321. chartInfoId := chartInfo.ChartInfoId
  322. dateType := chartInfo.DateType
  323. if dateType <= 0 {
  324. dateType = 3
  325. }
  326. startDate := chartInfo.StartDate
  327. endDate := chartInfo.EndDate
  328. seasonStartDate := chartInfo.SeasonStartDate
  329. seasonEndDate := chartInfo.SeasonEndDate
  330. calendar := chartInfo.Calendar
  331. chartType := chartInfo.ChartType
  332. if calendar == "" {
  333. calendar = "公历"
  334. }
  335. switch dateType {
  336. case 1:
  337. startDate = "2000-01-01"
  338. //endDate = time.Now().Format(utils.FormatDate)
  339. endDate = ""
  340. case 2:
  341. startDate = "2010-01-01"
  342. //endDate = time.Now().Format(utils.FormatDate)
  343. endDate = ""
  344. case 3:
  345. startDate = "2015-01-01"
  346. //endDate = time.Now().Format(utils.FormatDate)
  347. endDate = ""
  348. case 4:
  349. startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  350. startDate = "2021-01-01"
  351. //endDate = time.Now().Format(utils.FormatDate)
  352. endDate = ""
  353. case 5:
  354. startDate = startDate + "-01"
  355. endDate = endDate + "-01"
  356. case 6:
  357. //startDate = startDate + "-01"
  358. //endDate = time.Now().Format(utils.FormatDate)
  359. endDate = ""
  360. case 7:
  361. startDate = "2018-01-01"
  362. //endDate = time.Now().Format(utils.FormatDate)
  363. endDate = ""
  364. case 8:
  365. startDate = "2019-01-01"
  366. //endDate = time.Now().Format(utils.FormatDate)
  367. endDate = ""
  368. case 9:
  369. startDate = "2020-01-01"
  370. //endDate = time.Now().Format(utils.FormatDate)
  371. endDate = ""
  372. case 11:
  373. startDate = "2022-01-01"
  374. endDate = ""
  375. }
  376. // 兼容日期错误
  377. {
  378. if strings.Count(startDate, "-") == 1 {
  379. startDate = startDate + "-01"
  380. }
  381. if strings.Count(endDate, "-") == 1 {
  382. endDate = endDate + "-01"
  383. }
  384. }
  385. if chartType == 2 {
  386. if seasonStartDate != "" {
  387. startDate = seasonStartDate + "-01"
  388. } else {
  389. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  390. startDate = strconv.Itoa(fivePre) + "-01-01"
  391. }
  392. if seasonEndDate != "" {
  393. seasonEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, seasonEndDate+"-01", time.Local)
  394. if tmpErr != nil {
  395. msg = "获取失败"
  396. errMsg = "获取图表,指标信息失败,Err:" + tmpErr.Error()
  397. return
  398. }
  399. endDate = seasonEndDateTime.AddDate(0, 1, -1).Format(utils.FormatDate)
  400. } else {
  401. //endDate = time.Now().Format(utils.FormatDate)
  402. endDate = ""
  403. }
  404. }
  405. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  406. if err != nil {
  407. msg = "获取失败"
  408. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  409. return
  410. }
  411. extraConfigStr := chartInfo.ExtraConfig
  412. // 柱方图的一些配置
  413. var barConfig data_manage.BarChartInfoReq
  414. if chartInfo != nil && chartInfo.ChartType == 7 {
  415. if chartInfo.BarConfig == `` {
  416. msg = "柱方图未配置"
  417. errMsg = "柱方图未配置"
  418. return
  419. }
  420. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  421. if err != nil {
  422. msg = "柱方图配置异常"
  423. errMsg = "柱方图配置异常"
  424. return
  425. }
  426. extraConfigStr = chartInfo.BarConfig
  427. }
  428. edbList, xEdbIdValue, yDataList, dataResp, err, tmpErrMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  429. if err != nil {
  430. msg = "获取失败"
  431. if tmpErrMsg != `` {
  432. msg = tmpErrMsg
  433. }
  434. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  435. return
  436. }
  437. for _, v := range edbList {
  438. // 指标别名
  439. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  440. for _, reqEdb := range barConfig.EdbInfoIdList {
  441. if v.EdbInfoId == reqEdb.EdbInfoId {
  442. v.EdbAliasName = reqEdb.Name
  443. }
  444. }
  445. }
  446. }
  447. // 图表的指标来源
  448. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  449. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  450. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  451. // 单位
  452. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  453. chartInfo.Unit = yDataList[0].Unit
  454. chartInfo.UnitEn = yDataList[0].UnitEn
  455. }
  456. resp.ChartInfo = chartInfo
  457. resp.EdbInfoList = edbList
  458. resp.XEdbIdValue = xEdbIdValue
  459. resp.YDataList = yDataList
  460. resp.DataResp = dataResp
  461. if utils.Re == nil {
  462. jsonData, _ := json.Marshal(resp)
  463. utils.Rc.Put(key, jsonData, 10*time.Minute)
  464. }
  465. isOk = true
  466. return
  467. }