chart.go 12 KB

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