chart.go 11 KB

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