chart.go 10 KB

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