chart.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_chart_lib/models"
  6. "hongze/hongze_chart_lib/services/data"
  7. "hongze/hongze_chart_lib/utils"
  8. "strconv"
  9. "time"
  10. )
  11. //图表
  12. type ChartController struct {
  13. BaseAuthController
  14. }
  15. // ChartInfoDetail
  16. // @Title 获取图表详情
  17. // @Description 获取图表详情接口
  18. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  19. // @Success 200 {object} data_manage.ChartInfoDetailResp
  20. // @router /detail [get]
  21. func (this *ChartController) ChartInfoDetail() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. uniqueCode := this.GetString("UniqueCode")
  28. if uniqueCode == "" {
  29. br.Msg = "参数错误"
  30. br.ErrMsg = "参数错误,uniqueCode is empty"
  31. return
  32. }
  33. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  34. resp := new(models.ChartInfoDetailResp)
  35. //判断是否有缓存
  36. if utils.Re == nil {
  37. if utils.Re == nil && utils.Rc.IsExist(key) {
  38. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  39. err := json.Unmarshal(data, &resp)
  40. if err == nil && resp != nil {
  41. br.Ret = 200
  42. br.Success = true
  43. br.Msg = "获取成功"
  44. br.Data = resp
  45. fmt.Println("source redis")
  46. return
  47. }
  48. }
  49. }
  50. }
  51. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  52. if err != nil {
  53. if err.Error() == utils.ErrNoRow() {
  54. br.Msg = "该图已被删除,请刷新页面"
  55. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  56. return
  57. }
  58. br.Msg = "获取失败"
  59. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  60. return
  61. }
  62. chartInfoId := chartInfo.ChartInfoId
  63. dateType := chartInfo.DateType
  64. if dateType <= 0 {
  65. dateType = 3
  66. }
  67. startDate := chartInfo.StartDate
  68. endDate := chartInfo.EndDate
  69. seasonStartDate := chartInfo.SeasonStartDate
  70. seasonEndDate := chartInfo.SeasonEndDate
  71. calendar := chartInfo.Calendar
  72. chartType := chartInfo.ChartType
  73. if calendar == "" {
  74. calendar = "公历"
  75. }
  76. switch dateType {
  77. case 1:
  78. startDate = "2000-01-01"
  79. //endDate = time.Now().Format(utils.FormatDate)
  80. endDate = ""
  81. case 2:
  82. startDate = "2010-01-01"
  83. //endDate = time.Now().Format(utils.FormatDate)
  84. endDate = ""
  85. case 3:
  86. startDate = "2015-01-01"
  87. //endDate = time.Now().Format(utils.FormatDate)
  88. endDate = ""
  89. case 4:
  90. startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  91. startDate = "2021-01-01"
  92. //endDate = time.Now().Format(utils.FormatDate)
  93. endDate = ""
  94. case 5:
  95. startDate = startDate + "-01"
  96. endDate = endDate + "-01"
  97. case 6:
  98. startDate = startDate + "-01"
  99. //endDate = time.Now().Format(utils.FormatDate)
  100. endDate = ""
  101. case 7:
  102. startDate = "2018-01-01"
  103. //endDate = time.Now().Format(utils.FormatDate)
  104. endDate = ""
  105. case 8:
  106. startDate = "2019-01-01"
  107. //endDate = time.Now().Format(utils.FormatDate)
  108. endDate = ""
  109. case 9:
  110. startDate = "2020-01-01"
  111. //endDate = time.Now().Format(utils.FormatDate)
  112. endDate = ""
  113. case 11:
  114. startDate = "2022-01-01"
  115. endDate = ""
  116. }
  117. if chartType == 2 {
  118. if seasonStartDate != "" {
  119. startDate = seasonStartDate + "-01-01"
  120. } else {
  121. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  122. startDate = strconv.Itoa(fivePre) + "-01-01"
  123. }
  124. if seasonEndDate != "" {
  125. endDate = seasonEndDate + "-12-31"
  126. } else {
  127. //endDate = time.Now().Format(utils.FormatDate)
  128. endDate = ""
  129. }
  130. }
  131. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  132. if err != nil {
  133. br.Msg = "获取失败"
  134. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  135. return
  136. }
  137. edbList := make([]*models.ChartEdbInfoMapping, 0)
  138. for _, v := range mappingList {
  139. //minData, maxData, err := data_manage.GetEdbDataListMinAndMax(v.Source, v.EdbInfoId, startDate, endDate)
  140. item := new(models.ChartEdbInfoMapping)
  141. item.EdbInfoId = v.EdbInfoId
  142. item.SourceName = v.SourceName
  143. item.Source = v.Source
  144. item.EdbCode = v.EdbCode
  145. item.EdbName = v.EdbName
  146. item.EdbNameEn = v.EdbNameEn
  147. item.Frequency = v.Frequency
  148. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  149. //无就是不展示
  150. if v.Unit != `无` {
  151. item.Unit = v.Unit
  152. }
  153. item.UnitEn = v.UnitEn
  154. item.StartDate = v.StartDate
  155. item.EndDate = v.EndDate
  156. //item.ModifyTime = v.ModifyTime
  157. //item.ChartEdbMappingId = v.ChartEdbMappingId
  158. item.ChartInfoId = v.ChartInfoId
  159. item.MaxData = v.MaxData
  160. item.MinData = v.MinData
  161. item.IsOrder = v.IsOrder
  162. item.IsAxis = v.IsAxis
  163. item.EdbInfoType = v.EdbInfoType
  164. item.EdbType = v.EdbType
  165. item.LeadValue = v.LeadValue
  166. item.LeadUnit = v.LeadUnit
  167. item.ChartStyle = v.ChartStyle
  168. item.ChartColor = v.ChartColor
  169. item.ChartWidth = v.ChartWidth
  170. var startDateReal string
  171. var diffSeconds int64
  172. if chartType == 2 { //季节性图表
  173. startDateReal = startDate
  174. } else {
  175. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  176. var startTimeRealTemp time.Time
  177. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  178. switch v.LeadUnit {
  179. case "天":
  180. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  181. case "月":
  182. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  183. case "季":
  184. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  185. case "周":
  186. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  187. case "年":
  188. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  189. }
  190. if startTimeRealTemp.Before(startDateParse) {
  191. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  192. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  193. } else {
  194. startDateReal = startDate
  195. diffSeconds = 0
  196. }
  197. } else {
  198. startDateReal = startDate
  199. }
  200. }
  201. calendarPreYear := 0
  202. if calendar == "农历" {
  203. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  204. if err != nil {
  205. fmt.Println("time.Parse:" + err.Error())
  206. utils.FileLog.Info("startDateReal parse err:" + err.Error())
  207. }
  208. calendarPreYear = newStartDateReal.Year() - 1
  209. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  210. startDateReal = newStartDateReal.Format(utils.FormatDate)
  211. }
  212. dataList := make([]*models.EdbDataList, 0)
  213. fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  214. dataList, err = models.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
  215. if err != nil {
  216. br.Msg = "获取失败"
  217. br.Msg = "获取失败,Err:" + err.Error()
  218. return
  219. }
  220. if diffSeconds != 0 && v.EdbInfoType == 0 {
  221. dataListLen := len(dataList)
  222. for i := 0; i < dataListLen; i++ {
  223. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  224. }
  225. }
  226. if chartType == 2 {
  227. if calendar == "农历" {
  228. result, err := models.AddCalculateQuarterV5(dataList)
  229. if err != nil {
  230. br.Msg = "获取失败"
  231. br.Msg = "获取农历数据失败,Err:" + err.Error()
  232. return
  233. }
  234. if result.List[0].Year != calendarPreYear {
  235. itemList := make([]*models.EdbDataList, 0)
  236. items := new(models.EdbDataItems)
  237. //items.Year = calendarPreYear
  238. items.Items = itemList
  239. newResult := new(models.EdbDataResult)
  240. newResult.List = append(newResult.List, items)
  241. newResult.List = append(newResult.List, result.List...)
  242. item.DataList = newResult
  243. } else {
  244. item.DataList = result
  245. }
  246. } else {
  247. currentYear := time.Now().Year()
  248. quarterDataList := make([]*models.QuarterData, 0)
  249. quarterMap := make(map[int][]*models.EdbDataList)
  250. var quarterArr []int
  251. for _, v := range dataList {
  252. //v.DataTime
  253. itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
  254. if err != nil {
  255. br.Msg = "获取失败"
  256. br.Msg = "季度指标日期转换,Err:" + err.Error() + ";DataTime:" + v.DataTime
  257. return
  258. }
  259. year := itemDate.Year()
  260. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  261. timestamp := newItemDate.UnixNano() / 1e6
  262. v.DataTimestamp = timestamp
  263. if findVal, ok := quarterMap[year]; !ok {
  264. quarterArr = append(quarterArr, year)
  265. findVal = append(findVal, v)
  266. quarterMap[year] = findVal
  267. } else {
  268. findVal = append(findVal, v)
  269. quarterMap[year] = findVal
  270. }
  271. }
  272. for _, v := range quarterArr {
  273. itemList := quarterMap[v]
  274. quarterItem := new(models.QuarterData)
  275. quarterItem.Year = v
  276. quarterItem.DataList = itemList
  277. quarterDataList = append(quarterDataList, quarterItem)
  278. }
  279. item.DataList = quarterDataList
  280. }
  281. } else {
  282. item.DataList = dataList
  283. }
  284. edbList = append(edbList, item)
  285. }
  286. resp.ChartInfo = chartInfo
  287. resp.EdbInfoList = edbList
  288. if utils.Re == nil {
  289. data, _ := json.Marshal(resp)
  290. utils.Rc.Put(key, data, 10*time.Minute)
  291. }
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "获取成功"
  295. br.Data = resp
  296. }
  297. // ChartInfoRefresh
  298. // @Title 图表刷新接口
  299. // @Description 图表刷新接口
  300. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  301. // @Success Ret=200 刷新成功
  302. // @router /refresh [get]
  303. func (this *ChartController) ChartInfoRefresh() {
  304. br := new(models.BaseResponse).Init()
  305. chartId := 0
  306. defer func() {
  307. // 添加日志
  308. if chartId > 0 {
  309. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  310. Ip: this.Ctx.Input.IP(),
  311. ChartId: chartId,
  312. CreateTime: time.Now(),
  313. }
  314. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  315. }
  316. this.Data["json"] = br
  317. this.ServeJSON()
  318. }()
  319. uniqueCode := this.GetString("UniqueCode")
  320. if uniqueCode == "" {
  321. br.Msg = "参数错误"
  322. br.ErrMsg = "参数错误,uniqueCode is empty"
  323. return
  324. }
  325. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  326. if err != nil {
  327. if err.Error() == utils.ErrNoRow() {
  328. br.Msg = "该图已被删除,请刷新页面"
  329. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  330. return
  331. }
  332. br.Msg = "获取失败"
  333. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  334. return
  335. }
  336. chartId = chartInfo.ChartInfoId
  337. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  338. //if err != nil {
  339. // br.Msg = "刷新失败"
  340. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  341. // return
  342. //}
  343. err = data.ChartInfoRefreshV1(chartInfo.ChartInfoId)
  344. if err != nil {
  345. br.Msg = "刷新失败"
  346. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  347. return
  348. }
  349. //清除数据缓存
  350. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  351. if utils.Re == nil {
  352. utils.Rc.Delete(key)
  353. }
  354. br.Ret = 200
  355. br.Success = true
  356. br.Msg = "刷新成功"
  357. }
  358. // 获取频度的英文版
  359. func GetFrequencyEn(frequency string) (frequencyEn string) {
  360. switch frequency {
  361. case "日度":
  362. frequencyEn="day"
  363. return
  364. case "周度":
  365. frequencyEn="week"
  366. return
  367. case "旬度":
  368. frequencyEn="ten days"
  369. return
  370. case "月度":
  371. frequencyEn="month"
  372. return
  373. case "季度":
  374. frequencyEn="quarter"
  375. return
  376. case "年度":
  377. frequencyEn="year"
  378. return
  379. }
  380. return
  381. }