chart.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. "time"
  11. )
  12. //图表
  13. type ChartController struct {
  14. BaseAuthController
  15. }
  16. // ChartInfoDetail
  17. // @Title 获取图表详情
  18. // @Description 获取图表详情接口
  19. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  20. // @Success 200 {object} data_manage.ChartInfoDetailResp
  21. // @router /detail [get]
  22. func (this *ChartController) ChartInfoDetail() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. uniqueCode := this.GetString("UniqueCode")
  29. if uniqueCode == "" {
  30. br.Msg = "参数错误"
  31. br.ErrMsg = "参数错误,uniqueCode is empty"
  32. return
  33. }
  34. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  35. resp := new(models.ChartInfoDetailResp)
  36. //判断是否有缓存
  37. if utils.Re == nil {
  38. if utils.Re == nil && utils.Rc.IsExist(key) {
  39. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  40. err := json.Unmarshal(data, &resp)
  41. if err == nil && resp != nil {
  42. br.Ret = 200
  43. br.Success = true
  44. br.Msg = "获取成功"
  45. br.Data = resp
  46. fmt.Println("source redis")
  47. return
  48. }
  49. }
  50. }
  51. }
  52. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  53. if err != nil {
  54. if err.Error() == utils.ErrNoRow() {
  55. br.Msg = "该图已被删除,请刷新页面"
  56. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  57. return
  58. }
  59. br.Msg = "获取失败"
  60. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  61. return
  62. }
  63. chartInfoId := chartInfo.ChartInfoId
  64. dateType := chartInfo.DateType
  65. if dateType <= 0 {
  66. dateType = 3
  67. }
  68. startDate := chartInfo.StartDate
  69. endDate := chartInfo.EndDate
  70. seasonStartDate := chartInfo.SeasonStartDate
  71. seasonEndDate := chartInfo.SeasonEndDate
  72. calendar := chartInfo.Calendar
  73. chartType := chartInfo.ChartType
  74. if calendar == "" {
  75. calendar = "公历"
  76. }
  77. switch dateType {
  78. case 1:
  79. startDate = "2000-01-01"
  80. //endDate = time.Now().Format(utils.FormatDate)
  81. endDate = ""
  82. case 2:
  83. startDate = "2010-01-01"
  84. //endDate = time.Now().Format(utils.FormatDate)
  85. endDate = ""
  86. case 3:
  87. startDate = "2015-01-01"
  88. //endDate = time.Now().Format(utils.FormatDate)
  89. endDate = ""
  90. case 4:
  91. startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  92. startDate = "2021-01-01"
  93. //endDate = time.Now().Format(utils.FormatDate)
  94. endDate = ""
  95. case 5:
  96. startDate = startDate + "-01"
  97. endDate = endDate + "-01"
  98. case 6:
  99. startDate = startDate + "-01"
  100. //endDate = time.Now().Format(utils.FormatDate)
  101. endDate = ""
  102. case 7:
  103. startDate = "2018-01-01"
  104. //endDate = time.Now().Format(utils.FormatDate)
  105. endDate = ""
  106. case 8:
  107. startDate = "2019-01-01"
  108. //endDate = time.Now().Format(utils.FormatDate)
  109. endDate = ""
  110. case 9:
  111. startDate = "2020-01-01"
  112. //endDate = time.Now().Format(utils.FormatDate)
  113. endDate = ""
  114. case 11:
  115. startDate = "2022-01-01"
  116. endDate = ""
  117. }
  118. if chartType == 2 {
  119. if seasonStartDate != "" {
  120. startDate = seasonStartDate + "-01-01"
  121. } else {
  122. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  123. startDate = strconv.Itoa(fivePre) + "-01-01"
  124. }
  125. if seasonEndDate != "" {
  126. endDate = seasonEndDate + "-12-31"
  127. } else {
  128. //endDate = time.Now().Format(utils.FormatDate)
  129. endDate = ""
  130. }
  131. }
  132. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  133. if err != nil {
  134. br.Msg = "获取失败"
  135. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  136. return
  137. }
  138. barChartInfoDateList := make([]data_manage.BarChartInfoDateReq, 0)
  139. barChartInfoSort := data_manage.BarChartInfoSortReq{}
  140. // 柱方图的一些配置
  141. if chartInfo != nil && chartInfo.ChartType == 7 {
  142. if chartInfo.BarConfig == `` {
  143. br.Msg = "柱方图未配置"
  144. br.ErrMsg = "柱方图未配置"
  145. return
  146. }
  147. var barConfig data_manage.BarChartInfoReq
  148. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  149. if err != nil {
  150. br.Msg = "柱方图配置异常"
  151. br.ErrMsg = "柱方图配置异常"
  152. return
  153. }
  154. barChartInfoDateList = barConfig.DateList
  155. barChartInfoSort = barConfig.Sort
  156. // 指标别名
  157. for _, v := range mappingList {
  158. for _, confEdb := range barConfig.EdbInfoIdList {
  159. if v.EdbInfoId == confEdb.EdbInfoId {
  160. v.EdbAliasName = confEdb.Name
  161. }
  162. }
  163. }
  164. }
  165. edbList, xEdbIdValue, yDataList, err := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, barChartInfoDateList, barChartInfoSort)
  166. if err != nil {
  167. br.Msg = "获取失败"
  168. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  169. return
  170. }
  171. resp.ChartInfo = chartInfo
  172. resp.EdbInfoList = edbList
  173. resp.XEdbIdValue = xEdbIdValue
  174. resp.YDataList = yDataList
  175. if utils.Re == nil {
  176. data, _ := json.Marshal(resp)
  177. utils.Rc.Put(key, data, 10*time.Minute)
  178. }
  179. br.Ret = 200
  180. br.Success = true
  181. br.Msg = "获取成功"
  182. br.Data = resp
  183. }
  184. // ChartInfoRefresh
  185. // @Title 图表刷新接口
  186. // @Description 图表刷新接口
  187. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  188. // @Success Ret=200 刷新成功
  189. // @router /refresh [get]
  190. func (this *ChartController) ChartInfoRefresh() {
  191. br := new(models.BaseResponse).Init()
  192. chartId := 0
  193. defer func() {
  194. // 添加日志
  195. if chartId > 0 {
  196. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  197. Ip: this.Ctx.Input.IP(),
  198. ChartId: chartId,
  199. CreateTime: time.Now(),
  200. }
  201. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  202. }
  203. this.Data["json"] = br
  204. this.ServeJSON()
  205. }()
  206. uniqueCode := this.GetString("UniqueCode")
  207. if uniqueCode == "" {
  208. br.Msg = "参数错误"
  209. br.ErrMsg = "参数错误,uniqueCode is empty"
  210. return
  211. }
  212. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  213. if err != nil {
  214. if err.Error() == utils.ErrNoRow() {
  215. br.Msg = "该图已被删除,请刷新页面"
  216. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  217. return
  218. }
  219. br.Msg = "获取失败"
  220. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  221. return
  222. }
  223. chartId = chartInfo.ChartInfoId
  224. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  225. //if err != nil {
  226. // br.Msg = "刷新失败"
  227. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  228. // return
  229. //}
  230. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  231. if err != nil {
  232. br.Msg = "刷新失败"
  233. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  234. return
  235. }
  236. //清除数据缓存
  237. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  238. if utils.Re == nil {
  239. utils.Rc.Delete(key)
  240. }
  241. br.Ret = 200
  242. br.Success = true
  243. br.Msg = "刷新成功"
  244. }
  245. // 获取频度的英文版
  246. func GetFrequencyEn(frequency string) (frequencyEn string) {
  247. switch frequency {
  248. case "日度":
  249. frequencyEn = "day"
  250. return
  251. case "周度":
  252. frequencyEn = "week"
  253. return
  254. case "旬度":
  255. frequencyEn = "ten days"
  256. return
  257. case "月度":
  258. frequencyEn = "month"
  259. return
  260. case "季度":
  261. frequencyEn = "quarter"
  262. return
  263. case "年度":
  264. frequencyEn = "year"
  265. return
  266. }
  267. return
  268. }
  269. func GetLeadUnitEn(unit string) (unitEn string) {
  270. switch unit {
  271. case "天":
  272. unitEn = "day"
  273. return
  274. case "周":
  275. unitEn = "week"
  276. return
  277. case "月":
  278. unitEn = "month"
  279. return
  280. case "季":
  281. unitEn = "quarter"
  282. return
  283. case "年":
  284. unitEn = "year"
  285. return
  286. }
  287. return
  288. }