chart.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. "strings"
  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. startYear := chartInfo.StartYear
  75. if calendar == "" {
  76. calendar = "公历"
  77. }
  78. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  79. if err != nil {
  80. br.Msg = "获取失败"
  81. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  82. return
  83. }
  84. yearMax := 0
  85. if dateType == utils.DateTypeNYears {
  86. for _, v := range mappingList {
  87. if v.LatestDate != "" {
  88. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  89. if tErr != nil {
  90. br.Msg = "获取失败"
  91. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  92. return
  93. }
  94. if lastDateT.Year() > yearMax {
  95. yearMax = lastDateT.Year()
  96. }
  97. }
  98. }
  99. }
  100. // 开始/结束日期
  101. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  102. extraConfigStr := chartInfo.ExtraConfig
  103. // 柱方图的一些配置
  104. var barConfig data_manage.BarChartInfoReq
  105. if chartInfo != nil && chartInfo.ChartType == 7 {
  106. if chartInfo.BarConfig == `` {
  107. br.Msg = "柱方图未配置"
  108. br.ErrMsg = "柱方图未配置"
  109. return
  110. }
  111. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  112. if err != nil {
  113. br.Msg = "柱方图配置异常"
  114. br.ErrMsg = "柱方图配置异常"
  115. return
  116. }
  117. extraConfigStr = chartInfo.BarConfig
  118. }
  119. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  120. if err != nil {
  121. br.Msg = "获取失败"
  122. if errMsg != `` {
  123. br.Msg = errMsg
  124. }
  125. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  126. return
  127. }
  128. for _, v := range edbList {
  129. // 指标别名
  130. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  131. for _, reqEdb := range barConfig.EdbInfoIdList {
  132. if v.EdbInfoId == reqEdb.EdbInfoId {
  133. v.EdbAliasName = reqEdb.Name
  134. }
  135. }
  136. }
  137. }
  138. // 图表的指标来源
  139. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  140. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  141. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  142. resp.ChartInfo = chartInfo
  143. resp.EdbInfoList = edbList
  144. resp.XEdbIdValue = xEdbIdValue
  145. resp.YDataList = yDataList
  146. resp.DataResp = dataResp
  147. if utils.Re == nil {
  148. data, _ := json.Marshal(resp)
  149. utils.Rc.Put(key, data, 10*time.Minute)
  150. }
  151. br.Ret = 200
  152. br.Success = true
  153. br.Msg = "获取成功"
  154. br.Data = resp
  155. }
  156. // ChartInfoRefresh
  157. // @Title 图表刷新接口
  158. // @Description 图表刷新接口
  159. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  160. // @Success Ret=200 刷新成功
  161. // @router /refresh [get]
  162. func (this *ChartController) ChartInfoRefresh() {
  163. br := new(models.BaseResponse).Init()
  164. chartId := 0
  165. defer func() {
  166. // 添加日志
  167. if chartId > 0 {
  168. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  169. Ip: this.Ctx.Input.IP(),
  170. ChartId: chartId,
  171. CreateTime: time.Now(),
  172. }
  173. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  174. }
  175. this.Data["json"] = br
  176. this.ServeJSON()
  177. }()
  178. uniqueCode := this.GetString("UniqueCode")
  179. if uniqueCode == "" {
  180. br.Msg = "参数错误"
  181. br.ErrMsg = "参数错误,uniqueCode is empty"
  182. return
  183. }
  184. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  185. if err != nil {
  186. if err.Error() == utils.ErrNoRow() {
  187. br.Msg = "该图已被删除,请刷新页面"
  188. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  189. return
  190. }
  191. br.Msg = "获取失败"
  192. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  193. return
  194. }
  195. chartId = chartInfo.ChartInfoId
  196. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  197. //if err != nil {
  198. // br.Msg = "刷新失败"
  199. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  200. // return
  201. //}
  202. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  203. if err != nil {
  204. br.Msg = "刷新失败"
  205. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  206. return
  207. }
  208. //清除数据缓存
  209. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  210. if utils.Re == nil {
  211. utils.Rc.Delete(key)
  212. }
  213. br.Ret = 200
  214. br.Success = true
  215. br.Msg = "刷新成功"
  216. }
  217. // 获取频度的英文版
  218. func GetFrequencyEn(frequency string) (frequencyEn string) {
  219. switch frequency {
  220. case "日度":
  221. frequencyEn = "day"
  222. return
  223. case "周度":
  224. frequencyEn = "week"
  225. return
  226. case "旬度":
  227. frequencyEn = "ten days"
  228. return
  229. case "月度":
  230. frequencyEn = "month"
  231. return
  232. case "季度":
  233. frequencyEn = "quarter"
  234. return
  235. case "年度":
  236. frequencyEn = "year"
  237. return
  238. }
  239. return
  240. }
  241. func GetLeadUnitEn(unit string) (unitEn string) {
  242. switch unit {
  243. case "天":
  244. unitEn = "day"
  245. return
  246. case "周":
  247. unitEn = "week"
  248. return
  249. case "月":
  250. unitEn = "month"
  251. return
  252. case "季":
  253. unitEn = "quarter"
  254. return
  255. case "年":
  256. unitEn = "year"
  257. return
  258. }
  259. return
  260. }
  261. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  262. func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  263. resp = new(models.ChartInfoDetailResp)
  264. chartInfoId := chartInfo.ChartInfoId
  265. dateType := chartInfo.DateType
  266. if dateType <= 0 {
  267. dateType = 3
  268. }
  269. startDate := chartInfo.StartDate
  270. endDate := chartInfo.EndDate
  271. startYear := chartInfo.StartYear
  272. calendar := chartInfo.Calendar
  273. chartType := chartInfo.ChartType
  274. if calendar == "" {
  275. calendar = "公历"
  276. }
  277. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  278. if err != nil {
  279. msg = "获取失败"
  280. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  281. return
  282. }
  283. yearMax := 0
  284. if dateType == utils.DateTypeNYears {
  285. for _, v := range mappingList {
  286. if v.LatestDate != "" {
  287. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  288. if tErr != nil {
  289. msg = "获取失败"
  290. errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  291. return
  292. }
  293. if lastDateT.Year() > yearMax {
  294. yearMax = lastDateT.Year()
  295. }
  296. }
  297. }
  298. }
  299. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  300. extraConfigStr := chartInfo.ExtraConfig
  301. // 柱方图的一些配置
  302. var barConfig data_manage.BarChartInfoReq
  303. if chartInfo != nil && chartInfo.ChartType == 7 {
  304. if chartInfo.BarConfig == `` {
  305. msg = "柱方图未配置"
  306. errMsg = "柱方图未配置"
  307. return
  308. }
  309. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  310. if err != nil {
  311. msg = "柱方图配置异常"
  312. errMsg = "柱方图配置异常"
  313. return
  314. }
  315. extraConfigStr = chartInfo.BarConfig
  316. }
  317. edbList, xEdbIdValue, yDataList, dataResp, err, tmpErrMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  318. if err != nil {
  319. msg = "获取失败"
  320. if tmpErrMsg != `` {
  321. msg = tmpErrMsg
  322. }
  323. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  324. return
  325. }
  326. for _, v := range edbList {
  327. // 指标别名
  328. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  329. for _, reqEdb := range barConfig.EdbInfoIdList {
  330. if v.EdbInfoId == reqEdb.EdbInfoId {
  331. v.EdbAliasName = reqEdb.Name
  332. }
  333. }
  334. }
  335. }
  336. // 图表的指标来源
  337. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  338. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  339. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  340. resp.ChartInfo = chartInfo
  341. resp.EdbInfoList = edbList
  342. resp.XEdbIdValue = xEdbIdValue
  343. resp.YDataList = yDataList
  344. resp.DataResp = dataResp
  345. if utils.Re == nil {
  346. jsonData, _ := json.Marshal(resp)
  347. utils.Rc.Put(key, jsonData, 10*time.Minute)
  348. }
  349. isOk = true
  350. return
  351. }