chart.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_chart_lib/models"
  6. "hongze/hongze_chart_lib/utils"
  7. "strconv"
  8. "time"
  9. )
  10. //图表
  11. type ChartController struct {
  12. BaseAuthController
  13. }
  14. // @Title 获取图表详情
  15. // @Description 获取图表详情接口
  16. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  17. // @Success 200 {object} data_manage.ChartInfoDetailResp
  18. // @router /detail [get]
  19. func (this *ChartController) ChartInfoDetail() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. uniqueCode := this.GetString("UniqueCode")
  26. if uniqueCode == "" {
  27. br.Msg = "参数错误"
  28. br.ErrMsg = "参数错误,uniqueCode is empty"
  29. return
  30. }
  31. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  32. resp := new(models.ChartInfoDetailResp)
  33. //判断是否有缓存
  34. if utils.Re == nil {
  35. if utils.Re == nil && utils.Rc.IsExist(key) {
  36. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  37. err := json.Unmarshal(data, &resp)
  38. if err == nil && resp != nil {
  39. br.Ret = 200
  40. br.Success = true
  41. br.Msg = "获取成功"
  42. br.Data = resp
  43. }
  44. }
  45. }
  46. }
  47. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  48. if err != nil {
  49. if err.Error() == utils.ErrNoRow() {
  50. br.Msg = "改图已被删除,请刷新页面"
  51. br.ErrMsg = "改图已被删除,请刷新页面,Err:" + err.Error()
  52. return
  53. }
  54. br.Msg = "获取失败"
  55. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  56. return
  57. }
  58. chartInfoId := chartInfo.ChartInfoId
  59. dateType := chartInfo.DateType
  60. if dateType <= 0 {
  61. dateType = 3
  62. }
  63. startDate := chartInfo.StartDate
  64. endDate := chartInfo.EndDate
  65. seasonStartDate := chartInfo.SeasonStartDate
  66. seasonEndDate := chartInfo.SeasonEndDate
  67. calendar := chartInfo.Calendar
  68. chartType := chartInfo.ChartType
  69. if calendar == "" {
  70. calendar = "公历"
  71. }
  72. switch dateType {
  73. case 1:
  74. startDate = "2000-01-01"
  75. endDate = time.Now().Format(utils.FormatDate)
  76. case 2:
  77. startDate = "2010-01-01"
  78. endDate = time.Now().Format(utils.FormatDate)
  79. case 3:
  80. startDate = "2015-01-01"
  81. endDate = time.Now().Format(utils.FormatDate)
  82. case 4:
  83. startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  84. endDate = time.Now().Format(utils.FormatDate)
  85. case 5:
  86. startDate = startDate + "-01"
  87. endDate = endDate + "-01"
  88. case 6:
  89. startDate = startDate + "-01"
  90. endDate = time.Now().Format(utils.FormatDate)
  91. case 7:
  92. startDate = "2018-01-01"
  93. endDate = time.Now().Format(utils.FormatDate)
  94. case 8:
  95. startDate = "2019-01-01"
  96. endDate = time.Now().Format(utils.FormatDate)
  97. case 9:
  98. startDate = "2020-01-01"
  99. endDate = time.Now().Format(utils.FormatDate)
  100. }
  101. if startDate == "" || endDate == "" {
  102. br.Msg = "参数错误"
  103. br.Msg = "参数错误,无效的查询日期"
  104. return
  105. }
  106. if chartType == 2 {
  107. if seasonStartDate != "" {
  108. startDate = seasonStartDate + "-01-01"
  109. } else {
  110. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  111. startDate = strconv.Itoa(fivePre) + "-01-01"
  112. }
  113. if seasonEndDate != "" {
  114. endDate = seasonEndDate + "-12-31"
  115. } else {
  116. endDate = time.Now().Format(utils.FormatDate)
  117. }
  118. }
  119. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  120. if err != nil {
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  123. return
  124. }
  125. edbList := make([]*models.ChartEdbInfoMapping, 0)
  126. for _, v := range mappingList {
  127. //minData, maxData, err := data_manage.GetEdbDataListMinAndMax(v.Source, v.EdbInfoId, startDate, endDate)
  128. item := new(models.ChartEdbInfoMapping)
  129. item.EdbInfoId = v.EdbInfoId
  130. item.SourceName = v.SourceName
  131. item.Source = v.Source
  132. item.EdbCode = v.EdbCode
  133. item.EdbName = v.EdbName
  134. item.Frequency = v.Frequency
  135. item.Unit = v.Unit
  136. item.StartDate = v.StartDate
  137. item.EndDate = v.EndDate
  138. item.ModifyTime = v.ModifyTime
  139. item.ChartEdbMappingId = v.ChartEdbMappingId
  140. item.ChartInfoId = v.ChartInfoId
  141. item.MaxData = v.MaxData
  142. item.MinData = v.MinData
  143. item.IsOrder = v.IsOrder
  144. item.IsAxis = v.IsAxis
  145. item.EdbInfoType = v.EdbInfoType
  146. item.LeadValue = v.LeadValue
  147. item.LeadUnit = v.LeadUnit
  148. item.ChartStyle = v.ChartStyle
  149. item.ChartColor = v.ChartColor
  150. item.ChartWidth = v.ChartWidth
  151. var startDateReal string
  152. var diffSeconds int64
  153. if chartType == 1 {
  154. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  155. var startTimeRealTemp time.Time
  156. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  157. switch v.LeadUnit {
  158. case "天":
  159. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  160. case "月":
  161. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  162. case "季":
  163. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  164. case "周":
  165. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  166. case "年":
  167. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  168. }
  169. if startTimeRealTemp.Before(startDateParse) {
  170. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  171. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  172. } else {
  173. startDateReal = startDate
  174. diffSeconds = 0
  175. }
  176. } else {
  177. startDateReal = startDate
  178. }
  179. } else {
  180. startDateReal = startDate
  181. }
  182. calendarPreYear := 0
  183. if calendar == "农历" {
  184. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  185. if err != nil {
  186. fmt.Println("time.Parse:" + err.Error())
  187. utils.FileLog.Info("startDateReal parse err:" + err.Error())
  188. }
  189. calendarPreYear = newStartDateReal.Year() - 1
  190. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  191. startDateReal = newStartDateReal.Format(utils.FormatDate)
  192. }
  193. dataList := make([]*models.EdbDataList, 0)
  194. fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  195. dataList, err = models.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
  196. if err != nil {
  197. br.Msg = "获取失败"
  198. br.Msg = "获取失败,Err:" + err.Error()
  199. return
  200. }
  201. if diffSeconds != 0 && v.EdbInfoType == 0 {
  202. dataListLen := len(dataList)
  203. for i := 0; i < dataListLen; i++ {
  204. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  205. }
  206. }
  207. if chartType == 2 {
  208. if calendar == "农历" {
  209. result, err := models.AddCalculateQuarterV4(dataList)
  210. if err != nil {
  211. br.Msg = "获取失败"
  212. br.Msg = "获取农历数据失败,Err:" + err.Error()
  213. return
  214. }
  215. if result.List[0].Year != calendarPreYear {
  216. itemList := make([]*models.EdbDataList, 0)
  217. items := new(models.EdbDataItems)
  218. //items.Year = calendarPreYear
  219. items.Items = itemList
  220. newResult := new(models.EdbDataResult)
  221. newResult.List = append(newResult.List, items)
  222. newResult.List = append(newResult.List, result.List...)
  223. item.DataList = newResult
  224. } else {
  225. item.DataList = result
  226. }
  227. } else {
  228. currentYear := time.Now().Year()
  229. quarterDataList := make([]*models.QuarterData, 0)
  230. quarterMap := make(map[int][]*models.EdbDataList)
  231. var quarterArr []int
  232. for _, v := range dataList {
  233. //v.DataTime
  234. itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
  235. if err != nil {
  236. br.Msg = "获取失败"
  237. br.Msg = "季度指标日期转换,Err:" + err.Error() + ";DataTime:" + v.DataTime
  238. return
  239. }
  240. year := itemDate.Year()
  241. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  242. timestamp := newItemDate.UnixNano() / 1e6
  243. v.DataTimestamp = timestamp
  244. if findVal, ok := quarterMap[year]; !ok {
  245. quarterArr = append(quarterArr, year)
  246. findVal = append(findVal, v)
  247. quarterMap[year] = findVal
  248. } else {
  249. findVal = append(findVal, v)
  250. quarterMap[year] = findVal
  251. }
  252. }
  253. for _, v := range quarterArr {
  254. itemList := quarterMap[v]
  255. quarterItem := new(models.QuarterData)
  256. quarterItem.Year = v
  257. quarterItem.DataList = itemList
  258. quarterDataList = append(quarterDataList, quarterItem)
  259. }
  260. item.DataList = quarterDataList
  261. }
  262. } else {
  263. item.DataList = dataList
  264. }
  265. edbList = append(edbList, item)
  266. }
  267. resp.ChartInfo = chartInfo
  268. resp.EdbInfoList = edbList
  269. if utils.Re == nil {
  270. data, _ := json.Marshal(resp)
  271. utils.Rc.Put(key, data, utils.GetTodayLastSecond())
  272. }
  273. br.Ret = 200
  274. br.Success = true
  275. br.Msg = "获取成功"
  276. br.Data = resp
  277. }