chart.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. var barConfig data_manage.BarChartInfoReq
  149. barChartInfoDateList := make([]data_manage.BarChartInfoDateReq, 0)
  150. barChartInfoSort := data_manage.BarChartInfoSortReq{}
  151. // 柱方图的一些配置
  152. if chartInfo != nil && chartInfo.ChartType == 7 {
  153. if chartInfo.BarConfig == `` {
  154. br.Msg = "柱方图未配置"
  155. br.ErrMsg = "柱方图未配置"
  156. return
  157. }
  158. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  159. if err != nil {
  160. br.Msg = "柱方图配置异常"
  161. br.ErrMsg = "柱方图配置异常"
  162. return
  163. }
  164. barChartInfoDateList = barConfig.DateList
  165. barChartInfoSort = barConfig.Sort
  166. // 指标别名
  167. for _, v := range mappingList {
  168. for _, confEdb := range barConfig.EdbInfoIdList {
  169. if v.EdbInfoId == confEdb.EdbInfoId {
  170. v.EdbAliasName = confEdb.Name
  171. }
  172. }
  173. }
  174. }
  175. edbList, xEdbIdValue, yDataList, err := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, barChartInfoDateList, barChartInfoSort)
  176. if err != nil {
  177. br.Msg = "获取失败"
  178. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  179. return
  180. }
  181. for _, v := range edbList {
  182. // 指标别名
  183. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  184. for _, reqEdb := range barConfig.EdbInfoIdList {
  185. if v.EdbInfoId == reqEdb.EdbInfoId {
  186. v.EdbAliasName = reqEdb.Name
  187. }
  188. }
  189. }
  190. }
  191. resp.ChartInfo = chartInfo
  192. resp.EdbInfoList = edbList
  193. resp.XEdbIdValue = xEdbIdValue
  194. resp.YDataList = yDataList
  195. if utils.Re == nil {
  196. data, _ := json.Marshal(resp)
  197. utils.Rc.Put(key, data, 10*time.Minute)
  198. }
  199. br.Ret = 200
  200. br.Success = true
  201. br.Msg = "获取成功"
  202. br.Data = resp
  203. }
  204. // ChartInfoRefresh
  205. // @Title 图表刷新接口
  206. // @Description 图表刷新接口
  207. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  208. // @Success Ret=200 刷新成功
  209. // @router /refresh [get]
  210. func (this *ChartController) ChartInfoRefresh() {
  211. br := new(models.BaseResponse).Init()
  212. chartId := 0
  213. defer func() {
  214. // 添加日志
  215. if chartId > 0 {
  216. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  217. Ip: this.Ctx.Input.IP(),
  218. ChartId: chartId,
  219. CreateTime: time.Now(),
  220. }
  221. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  222. }
  223. this.Data["json"] = br
  224. this.ServeJSON()
  225. }()
  226. uniqueCode := this.GetString("UniqueCode")
  227. if uniqueCode == "" {
  228. br.Msg = "参数错误"
  229. br.ErrMsg = "参数错误,uniqueCode is empty"
  230. return
  231. }
  232. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  233. if err != nil {
  234. if err.Error() == utils.ErrNoRow() {
  235. br.Msg = "该图已被删除,请刷新页面"
  236. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  237. return
  238. }
  239. br.Msg = "获取失败"
  240. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  241. return
  242. }
  243. chartId = chartInfo.ChartInfoId
  244. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  245. //if err != nil {
  246. // br.Msg = "刷新失败"
  247. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  248. // return
  249. //}
  250. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  251. if err != nil {
  252. br.Msg = "刷新失败"
  253. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  254. return
  255. }
  256. //清除数据缓存
  257. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  258. if utils.Re == nil {
  259. utils.Rc.Delete(key)
  260. }
  261. br.Ret = 200
  262. br.Success = true
  263. br.Msg = "刷新成功"
  264. }
  265. // 获取频度的英文版
  266. func GetFrequencyEn(frequency string) (frequencyEn string) {
  267. switch frequency {
  268. case "日度":
  269. frequencyEn = "day"
  270. return
  271. case "周度":
  272. frequencyEn = "week"
  273. return
  274. case "旬度":
  275. frequencyEn = "ten days"
  276. return
  277. case "月度":
  278. frequencyEn = "month"
  279. return
  280. case "季度":
  281. frequencyEn = "quarter"
  282. return
  283. case "年度":
  284. frequencyEn = "year"
  285. return
  286. }
  287. return
  288. }
  289. func GetLeadUnitEn(unit string) (unitEn string) {
  290. switch unit {
  291. case "天":
  292. unitEn = "day"
  293. return
  294. case "周":
  295. unitEn = "week"
  296. return
  297. case "月":
  298. unitEn = "month"
  299. return
  300. case "季":
  301. unitEn = "quarter"
  302. return
  303. case "年":
  304. unitEn = "year"
  305. return
  306. }
  307. return
  308. }