chart_info.go 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_chart_lib/models"
  6. "eta/eta_chart_lib/models/data_manage"
  7. "eta/eta_chart_lib/services/alarm_msg"
  8. "eta/eta_chart_lib/utils"
  9. "fmt"
  10. "github.com/shopspring/decimal"
  11. "math"
  12. "sort"
  13. "strconv"
  14. "time"
  15. )
  16. // GetFrequencyEn 获取频度的英文版
  17. func GetFrequencyEn(frequency string) (frequencyEn string) {
  18. switch frequency {
  19. case "日度":
  20. frequencyEn = "day"
  21. return
  22. case "周度":
  23. frequencyEn = "week"
  24. return
  25. case "旬度":
  26. frequencyEn = "ten days"
  27. return
  28. case "月度":
  29. frequencyEn = "month"
  30. return
  31. case "季度":
  32. frequencyEn = "quarter"
  33. return
  34. case "年度":
  35. frequencyEn = "year"
  36. return
  37. }
  38. return
  39. }
  40. func GetLeadUnitEn(unit string) (unitEn string) {
  41. switch unit {
  42. case "天":
  43. unitEn = "day"
  44. return
  45. case "周":
  46. unitEn = "week"
  47. return
  48. case "月":
  49. unitEn = "month"
  50. return
  51. case "季":
  52. unitEn = "quarter"
  53. return
  54. case "年":
  55. unitEn = "year"
  56. return
  57. }
  58. return
  59. }
  60. // GetChartEdbData 获取图表的指标数据
  61. func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, extraConfigStr, seasonExtraConfig string) (edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []models.YData, dataResp interface{}, err error, errMsg string) {
  62. edbList = make([]*models.ChartEdbInfoMapping, 0)
  63. // 指标对应的所有数据
  64. xEdbIdValue = make([]int, 0)
  65. yDataList = make([]models.YData, 0)
  66. var extraConfig interface{}
  67. switch chartType {
  68. case 7: // 柱形图
  69. var barConfig data_manage.BarChartInfoReq
  70. if extraConfigStr == `` {
  71. errMsg = "柱方图未配置"
  72. err = errors.New(errMsg)
  73. return
  74. }
  75. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  76. if err != nil {
  77. errMsg = "柱方图配置异常"
  78. err = errors.New(errMsg)
  79. return
  80. }
  81. extraConfig = barConfig
  82. case 10: // 截面散点图
  83. var tmpExtraConfig data_manage.SectionScatterReq
  84. if extraConfigStr == `` {
  85. errMsg = "截面散点图未配置"
  86. err = errors.New(errMsg)
  87. return
  88. }
  89. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  90. if err != nil {
  91. errMsg = "截面散点配置异常"
  92. err = errors.New(errMsg)
  93. return
  94. }
  95. extraConfig = tmpExtraConfig
  96. default:
  97. xEdbIdValue = make([]int, 0)
  98. yDataList = make([]models.YData, 0)
  99. }
  100. // 指标对应的所有数据
  101. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  102. if err != nil {
  103. return
  104. }
  105. // 特殊图形数据处理
  106. switch chartType {
  107. case 7: // 柱形图
  108. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  109. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  110. for _, v := range edbList {
  111. // 指标别名
  112. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  113. for _, reqEdb := range barChartConf.EdbInfoIdList {
  114. if v.EdbInfoId == reqEdb.EdbInfoId {
  115. v.EdbAliasName = reqEdb.Name
  116. }
  117. }
  118. }
  119. }
  120. case 10: // 截面散点图
  121. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  122. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  123. // 这个数据没有必要返回给前端
  124. for _, v := range edbList {
  125. v.DataList = nil
  126. }
  127. }
  128. return
  129. }
  130. // GetEdbDataMapList 获取指标最后的基础数据
  131. func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
  132. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  133. return
  134. }
  135. // getEdbDataMapList 获取指标最后的基础数据
  136. func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
  137. // 指标对应的所有数据
  138. edbDataListMap = make(map[int][]*models.EdbDataList)
  139. for _, v := range mappingList {
  140. //fmt.Println("v:", v.EdbInfoId)
  141. item := new(models.ChartEdbInfoMapping)
  142. item.EdbInfoId = v.EdbInfoId
  143. item.SourceName = v.SourceName
  144. item.Source = v.Source
  145. item.EdbCode = v.EdbCode
  146. item.EdbName = v.EdbName
  147. item.EdbType = v.EdbType
  148. item.EdbNameEn = v.EdbNameEn
  149. item.Frequency = v.Frequency
  150. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  151. if v.Unit != `无` {
  152. item.Unit = v.Unit
  153. }
  154. item.UnitEn = v.UnitEn
  155. item.StartDate = v.StartDate
  156. item.EndDate = v.EndDate
  157. item.ModifyTime = v.ModifyTime
  158. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  159. item.PredictChartColor = v.PredictChartColor
  160. item.EdbAliasName = v.EdbAliasName
  161. if chartInfoId <= 0 {
  162. item.IsAxis = 1
  163. item.LeadValue = 0
  164. item.LeadUnit = ""
  165. //item.ChartEdbMappingId = 0
  166. item.ChartInfoId = 0
  167. item.IsOrder = false
  168. item.EdbInfoType = 1
  169. item.ChartStyle = ""
  170. item.ChartColor = ""
  171. item.ChartWidth = 0
  172. item.MaxData = v.MaxValue
  173. item.MinData = v.MinValue
  174. } else {
  175. item.IsAxis = v.IsAxis
  176. item.EdbInfoType = v.EdbInfoType
  177. item.LeadValue = v.LeadValue
  178. item.LeadUnit = v.LeadUnit
  179. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  180. //item.ChartEdbMappingId = v.ChartEdbMappingId
  181. item.ChartInfoId = v.ChartInfoId
  182. item.ChartStyle = v.ChartStyle
  183. item.ChartColor = v.ChartColor
  184. item.ChartWidth = v.ChartWidth
  185. item.IsOrder = v.IsOrder
  186. item.MaxData = v.MaxData
  187. item.MinData = v.MinData
  188. }
  189. item.LatestValue = v.LatestValue
  190. item.LatestDate = v.LatestDate
  191. item.UniqueCode = v.UniqueCode
  192. item.MoveLatestDate = v.LatestDate
  193. var startDateReal string
  194. var diffSeconds int64
  195. if chartType == 2 { //季节性图
  196. startDateReal = startDate
  197. } else {
  198. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  199. var startTimeRealTemp time.Time
  200. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  201. switch v.LeadUnit {
  202. case "天":
  203. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  204. case "月":
  205. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  206. case "季":
  207. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  208. case "周":
  209. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  210. case "年":
  211. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  212. }
  213. if startTimeRealTemp.Before(startDateParse) {
  214. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  215. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  216. } else {
  217. startDateReal = startDate
  218. diffSeconds = 0
  219. }
  220. // 预测指标的开始日期也要偏移
  221. {
  222. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  223. if tmpErr != nil {
  224. err = tmpErr
  225. return
  226. }
  227. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  228. if tmpErr != nil {
  229. err = tmpErr
  230. return
  231. }
  232. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  233. }
  234. } else {
  235. startDateReal = startDate
  236. }
  237. }
  238. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  239. calendarPreYear := 0
  240. if calendar == "农历" {
  241. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  242. if err != nil {
  243. fmt.Println("time.Parse:" + err.Error())
  244. }
  245. calendarPreYear = newStartDateReal.Year() - 1
  246. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  247. startDateReal = newStartDateReal.Format(utils.FormatDate)
  248. }
  249. dataList := make([]*models.EdbDataList, 0)
  250. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  251. switch v.EdbInfoCategoryType {
  252. case 0:
  253. dataList, err = models.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  254. case 1:
  255. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
  256. default:
  257. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  258. }
  259. if err != nil {
  260. return
  261. }
  262. edbDataListMap[v.EdbInfoId] = dataList
  263. if diffSeconds != 0 && v.EdbInfoType == 0 {
  264. dataListLen := len(dataList)
  265. for i := 0; i < dataListLen; i++ {
  266. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  267. }
  268. }
  269. if chartType == 2 {
  270. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  271. if tmpErr != nil {
  272. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  273. return
  274. }
  275. if calendar == "农历" {
  276. if len(dataList) <= 0 {
  277. result := new(models.EdbDataResult)
  278. item.DataList = result
  279. } else {
  280. result, tmpErr := models.AddCalculateQuarterV6(dataList)
  281. if tmpErr != nil {
  282. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  283. return
  284. }
  285. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  286. if tErr != nil {
  287. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  288. return
  289. }
  290. item.DataList = quarterDataList
  291. }
  292. } else {
  293. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  294. if tErr != nil {
  295. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  296. return
  297. }
  298. item.DataList = quarterDataList
  299. }
  300. } else if chartType == 7 { //柱方图
  301. //item.DataList = dataList
  302. } else {
  303. item.DataList = dataList
  304. }
  305. edbList = append(edbList, item)
  306. }
  307. return
  308. }
  309. // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
  310. func GetSeasonEdbInfoDataListByXDate(dataList []*models.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort models.QuarterDataList, err error) {
  311. xStartDate := "01-01"
  312. xEndDate := "12-31"
  313. jumpYear := 0
  314. legends := make([]models.SeasonChartLegend, 0)
  315. var seasonExtra models.SeasonExtraItem
  316. if seasonExtraConfig != "" {
  317. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  318. if err != nil {
  319. return
  320. }
  321. }
  322. if seasonExtra.XStartDate != "" {
  323. xStartDate = seasonExtra.XStartDate
  324. xEndDate = seasonExtra.XEndDate
  325. jumpYear = seasonExtra.JumpYear
  326. legends = seasonExtra.ChartLegend
  327. }
  328. length := len(dataList)
  329. if length == 0 {
  330. return
  331. }
  332. legendMap := make(map[string]string, 0)
  333. if len(legends) > 0 {
  334. for _, v := range legends {
  335. legendMap[v.Name] = v.Value
  336. }
  337. }
  338. latestDateStr := latestDate.Format(utils.FormatDate)
  339. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  340. //根据数据确定最早的年份,和最近年份
  341. //根据横轴的日期,汇总所有的年份
  342. startDate := dataList[0].DataTime
  343. startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
  344. if tmpErr != nil {
  345. err = tmpErr
  346. return
  347. }
  348. startYear := startDateT.Year()
  349. //获取数据的最新日期
  350. lastDate := dataList[length-1].DataTime
  351. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  352. if tmpErr != nil {
  353. err = tmpErr
  354. return
  355. }
  356. endYear := lastDateT.Year()
  357. nowYear := time.Now().Year()
  358. dataMap := make(map[string]models.QuarterXDateItem, 0)
  359. quarterDataList := make([]*models.QuarterData, 0)
  360. quarterMap := make(map[string][]*models.EdbDataList, 0)
  361. //整理出日期
  362. idx := 1
  363. chartLegendMap := make(map[string]int, 0)
  364. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  365. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  366. currentEndYear := currentStartYear
  367. if jumpYear == 1 {
  368. currentEndYear = currentStartYear + 1
  369. }
  370. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  371. name := fmt.Sprintf("%s_%s", startStr, endStr)
  372. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  373. startT, tEr := time.Parse(utils.FormatDate, startStr)
  374. if tEr != nil {
  375. err = tEr
  376. return
  377. }
  378. endT, tEr := time.Parse(utils.FormatDate, endStr)
  379. if tEr != nil {
  380. err = tEr
  381. return
  382. }
  383. if lastDateT.Before(startT) {
  384. //如果最新的日期在起始日之前,则跳出循环
  385. break
  386. }
  387. if endT.Year() > nowYear {
  388. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  389. nowYear = endT.Year()
  390. }
  391. item := models.QuarterXDateItem{
  392. StartDate: startT,
  393. EndDate: endT,
  394. ShowName: showName,
  395. }
  396. dataMap[name] = item
  397. chartLegendMap[name] = idx
  398. idx++
  399. if lastDateT.Before(endT) {
  400. //如果最新的日期在起始日之前,则跳出循环
  401. break
  402. }
  403. }
  404. lenYear := len(dataMap)
  405. for k, v := range dataMap {
  406. if i, ok := chartLegendMap[k]; ok {
  407. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  408. }
  409. dataMap[k] = v
  410. }
  411. for _, v := range dataList {
  412. dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
  413. year := dataTimeT.Year()
  414. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  415. for k, dateItem := range dataMap {
  416. tmpVal := models.EdbDataList{
  417. EdbDataId: v.EdbDataId,
  418. EdbInfoId: v.EdbInfoId,
  419. DataTime: v.DataTime,
  420. DataTimestamp: v.DataTimestamp,
  421. Value: v.Value,
  422. }
  423. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  424. if jumpYear == 1 {
  425. //计算前一年最大的日期, 只补齐数据到去年
  426. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  427. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  428. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  429. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  430. } else {
  431. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  432. }
  433. } else {
  434. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  435. }
  436. timestamp := newItemDate.UnixNano() / 1e6
  437. tmpVal.DataTimestamp = timestamp
  438. tmpV := &tmpVal
  439. if findVal, ok := quarterMap[k]; !ok {
  440. findVal = append(findVal, tmpV)
  441. quarterMap[k] = findVal
  442. } else {
  443. findVal = append(findVal, tmpV)
  444. quarterMap[k] = findVal
  445. }
  446. if v.DataTime == latestDateStr {
  447. dateItem.CuttingDataTimestamp = timestamp
  448. dataMap[k] = dateItem
  449. }
  450. //break
  451. }
  452. }
  453. }
  454. for k, v := range dataMap {
  455. itemList := quarterMap[k]
  456. quarterItem := new(models.QuarterData)
  457. quarterItem.Years = v.ShowName
  458. quarterItem.Year = v.ChartLegend
  459. quarterItem.ChartLegend = v.ChartLegend
  460. if le, ok := legendMap[v.ShowName]; ok {
  461. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  462. quarterItem.ChartLegend = le
  463. }
  464. }
  465. quarterItem.DataList = itemList
  466. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  467. //如果等于最后的实际日期,那么将切割时间戳记录
  468. if quarterItem.CuttingDataTimestamp == 0 {
  469. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  470. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  471. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  472. }
  473. }
  474. quarterDataList = append(quarterDataList, quarterItem)
  475. }
  476. if len(quarterDataList) > 0 {
  477. quarterDataListSort = quarterDataList
  478. sort.Sort(quarterDataListSort)
  479. }
  480. return
  481. }
  482. // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
  483. func GetSeasonEdbInfoDataListByXDateNong(result *models.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort models.QuarterDataList, err error) {
  484. xStartDate := "01-01"
  485. xEndDate := "12-31"
  486. jumpYear := 0
  487. legends := make([]models.SeasonChartLegend, 0)
  488. var seasonExtra models.SeasonExtraItem
  489. if seasonExtraConfig != "" {
  490. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  491. if err != nil {
  492. return
  493. }
  494. }
  495. if seasonExtra.XStartDate != "" {
  496. xStartDate = seasonExtra.XStartDate
  497. xEndDate = seasonExtra.XEndDate
  498. jumpYear = seasonExtra.JumpYear
  499. legends = seasonExtra.ChartLegend
  500. }
  501. length := len(result.List)
  502. if length == 0 {
  503. return
  504. }
  505. legendMap := make(map[string]string, 0)
  506. if len(legends) > 0 {
  507. for _, v := range legends {
  508. legendMap[v.Name] = v.Value
  509. }
  510. }
  511. latestDateYear := latestDate.Year()
  512. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  513. //根据数据确定最早的年份,和最近年份
  514. //根据横轴的日期,汇总所有的年份
  515. startYear := result.List[0].Year
  516. /*if jumpYear == 1 {
  517. if startYear != calendarPreYear {
  518. startYear = startYear - 1
  519. }
  520. }*/
  521. itemLength := len(result.List[length-1].Items)
  522. //获取数据的最新日期
  523. lastDate := result.List[length-1].Items[itemLength-1].DataTime
  524. maxY := result.List[length-1].Year
  525. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  526. if tmpErr != nil {
  527. err = tmpErr
  528. return
  529. }
  530. endYear := lastDateT.Year()
  531. nowYear := time.Now().Year()
  532. dataMap := make(map[string]models.QuarterXDateItem, 0)
  533. quarterDataList := make([]*models.QuarterData, 0)
  534. resultData := make([]*models.QuarterData, 0)
  535. quarterMap := make(map[string][]*models.EdbDataList, 0)
  536. //整理出日期
  537. var startTmpT, endTmpT time.Time
  538. idx := 1
  539. chartLegendMap := make(map[string]int, 0)
  540. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  541. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  542. currentEndYear := currentStartYear
  543. if jumpYear == 1 {
  544. currentEndYear = currentStartYear + 1
  545. }
  546. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  547. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  548. startT, tEr := time.Parse(utils.FormatDate, startStr)
  549. if tEr != nil {
  550. err = tEr
  551. return
  552. }
  553. endT, tEr := time.Parse(utils.FormatDate, endStr)
  554. if tEr != nil {
  555. err = tEr
  556. return
  557. }
  558. if lastDateT.Before(startT) {
  559. //如果最新的日期在起始日之前,则跳出循环
  560. break
  561. }
  562. if endT.Year() > nowYear {
  563. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  564. nowYear = endT.Year()
  565. }
  566. item := models.QuarterXDateItem{
  567. StartDate: startT,
  568. EndDate: endT,
  569. ShowName: showName,
  570. }
  571. dataMap[showName] = item
  572. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  573. startTmpT = startT
  574. endTmpT = endT
  575. chartLegendMap[showName] = idx
  576. idx++
  577. if lastDateT.Before(endT) {
  578. //如果最新的日期在起始日之前,则跳出循环
  579. break
  580. }
  581. }
  582. lenYear := len(dataMap)
  583. for k, v := range dataMap {
  584. if i, ok := chartLegendMap[k]; ok {
  585. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  586. }
  587. dataMap[k] = v
  588. }
  589. yearDataListMap := make(map[int]*models.EdbDataItems, 0)
  590. for _, lv := range result.List {
  591. yearDataListMap[lv.Year] = lv
  592. }
  593. //判断哪些点应该落在同一条时间线上
  594. fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
  595. fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
  596. fmt.Printf("maxY 为%d \n", maxY)
  597. for name, dateItem := range dataMap {
  598. tY := dateItem.EndDate.Year()
  599. if lastDateT.Month() >= 11 {
  600. if maxY > endTmpT.Year() {
  601. tY = tY + 1
  602. }
  603. }
  604. lv, ok1 := yearDataListMap[tY]
  605. fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
  606. if !ok1 {
  607. continue
  608. }
  609. for _, item := range lv.Items {
  610. tmpVal := models.EdbDataList{
  611. EdbDataId: item.EdbDataId,
  612. EdbInfoId: item.EdbInfoId,
  613. DataTime: item.DataTime,
  614. DataTimestamp: item.DataTimestamp,
  615. Value: item.Value,
  616. }
  617. dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
  618. if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
  619. tmpV := &tmpVal
  620. if findVal, ok := quarterMap[name]; !ok {
  621. findVal = append(findVal, tmpV)
  622. quarterMap[name] = findVal
  623. } else {
  624. findVal = append(findVal, tmpV)
  625. quarterMap[name] = findVal
  626. }
  627. if lv.Year >= latestDateYear {
  628. // 切割的日期时间字符串
  629. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  630. if item.DataTime == cuttingDataTimeStr {
  631. dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
  632. dataMap[name] = dateItem
  633. }
  634. }
  635. }
  636. }
  637. }
  638. for k, v := range dataMap {
  639. itemList := quarterMap[k]
  640. quarterItem := new(models.QuarterData)
  641. quarterItem.Years = v.ShowName
  642. quarterItem.Year = v.ChartLegend
  643. quarterItem.ChartLegend = v.ChartLegend
  644. if le, ok := legendMap[v.ShowName]; ok {
  645. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  646. quarterItem.ChartLegend = le
  647. }
  648. }
  649. quarterItem.DataList = itemList
  650. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  651. //如果等于最后的实际日期,那么将切割时间戳记录
  652. if quarterItem.CuttingDataTimestamp == 0 {
  653. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  654. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  655. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  656. }
  657. }
  658. quarterDataList = append(quarterDataList, quarterItem)
  659. }
  660. if result.List[0].Year != calendarPreYear {
  661. itemList := make([]*models.EdbDataList, 0)
  662. items := new(models.QuarterData)
  663. //items.Year = calendarPreYear
  664. items.DataList = itemList
  665. newResult := make([]*models.QuarterData, 0)
  666. newResult = append(newResult, items)
  667. newResult = append(newResult, quarterDataList...)
  668. resultData = newResult
  669. } else {
  670. resultData = quarterDataList
  671. }
  672. if len(quarterDataList) > 0 {
  673. quarterDataListSort = resultData
  674. sort.Sort(quarterDataListSort)
  675. }
  676. return
  677. }
  678. // BarChartData 柱方图的数据处理
  679. func BarChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, barChartInfoSort data_manage.BarChartInfoSortReq) (edbIdList []int, yDataList []models.YData, err error) {
  680. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  681. edbDataMap := make(map[int]map[string]float64)
  682. for edbInfoId, edbDataList := range edbDataListMap {
  683. edbDateData := make(map[string]float64)
  684. for _, edbData := range edbDataList {
  685. edbDateData[edbData.DataTime] = edbData.Value
  686. }
  687. edbDataMap[edbInfoId] = edbDateData
  688. }
  689. // edbIdList 指标展示顺序;x轴的指标顺序
  690. edbIdList = make([]int, 0)
  691. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  692. dateData := make(map[int]float64)
  693. if barChartInfoSort.Sort == 0 {
  694. for _, v := range mappingList {
  695. edbIdList = append(edbIdList, v.EdbInfoId)
  696. }
  697. } else {
  698. lenBarChartInfoDateList := len(barChartInfoDateList)
  699. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  700. err = errors.New("排序日期异常")
  701. return
  702. }
  703. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  704. // 日期配置
  705. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  706. for edbInfoId, dataList := range edbDataListMap {
  707. if len(dataList) <= 0 {
  708. // 没有数据的指标id
  709. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  710. continue
  711. }
  712. findDate := barChartInfoDate.Date
  713. switch barChartInfoDate.Type {
  714. case 1: //最新值
  715. findDate = dataList[len(dataList)-1].DataTime
  716. case 2: //近期几天
  717. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  718. if tmpErr != nil {
  719. err = tmpErr
  720. return
  721. }
  722. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  723. lenData := len(dataList) - 1
  724. for i := lenData; i >= 0; i-- {
  725. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  726. if tmpErr != nil {
  727. err = tmpErr
  728. return
  729. }
  730. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  731. findDate = dataList[i].DataTime
  732. break
  733. }
  734. }
  735. case 3: // 固定日期
  736. //最早的日期
  737. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  738. if tmpErr != nil {
  739. err = tmpErr
  740. return
  741. }
  742. //寻找固定日期的数据
  743. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  744. if tmpErr != nil {
  745. err = tmpErr
  746. return
  747. }
  748. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  749. tmpDate := tmpDateTime.Format(utils.FormatDate)
  750. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  751. findDate = tmpDate
  752. break
  753. }
  754. }
  755. default:
  756. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  757. return
  758. }
  759. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  760. dateData[edbInfoId] = tmpValue
  761. } else {
  762. // 没有数据的指标id
  763. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  764. }
  765. }
  766. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  767. // 排序
  768. dateDataSort := utils.NewMapSorter(dateData)
  769. sort.Sort(dateDataSort)
  770. if barChartInfoSort.Sort == 1 {
  771. // 先将没有数据的指标id放在最前面
  772. if len(notDataEdbIdList) > 0 {
  773. edbIdList = append(edbIdList, notDataEdbIdList...)
  774. }
  775. for _, v := range dateDataSort {
  776. edbIdList = append(edbIdList, v.Key)
  777. }
  778. } else {
  779. for i := len(dateDataSort) - 1; i >= 0; i-- {
  780. edbIdList = append(edbIdList, dateDataSort[i].Key)
  781. }
  782. // 再将没有数据的指标id放在最后面
  783. if len(notDataEdbIdList) > 0 {
  784. edbIdList = append(edbIdList, notDataEdbIdList...)
  785. }
  786. }
  787. }
  788. yDataList = make([]models.YData, 0) //y轴的数据列表
  789. for _, barChartInfoDate := range barChartInfoDateList {
  790. var maxDate time.Time
  791. findDataList := make([]float64, 0) // 当前日期的数据值
  792. for _, edbInfoId := range edbIdList {
  793. findDate := barChartInfoDate.Date //需要的日期值
  794. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  795. if len(dataList) <= 0 {
  796. // 没有数据的指标id
  797. findDataList = append(findDataList, 0)
  798. continue
  799. }
  800. switch barChartInfoDate.Type {
  801. case 1: //最新值
  802. dataList := edbDataListMap[edbInfoId]
  803. findDate = dataList[len(dataList)-1].DataTime
  804. case 2: //近期几天
  805. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  806. if tmpErr != nil {
  807. err = tmpErr
  808. return
  809. }
  810. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  811. lenData := len(dataList) - 1
  812. for i := lenData; i >= 0; i-- {
  813. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  814. if tmpErr != nil {
  815. err = tmpErr
  816. return
  817. }
  818. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  819. findDate = dataList[i].DataTime
  820. break
  821. }
  822. }
  823. case 3: // 固定日期
  824. //最早的日期
  825. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  826. if tmpErr != nil {
  827. err = tmpErr
  828. return
  829. }
  830. //寻找固定日期的数据
  831. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  832. if tmpErr != nil {
  833. err = tmpErr
  834. return
  835. }
  836. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  837. tmpDate := tmpDateTime.Format(utils.FormatDate)
  838. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  839. findDate = tmpDate
  840. break
  841. }
  842. }
  843. default:
  844. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  845. return
  846. }
  847. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  848. if maxDate.IsZero() {
  849. maxDate = findDateTime
  850. } else {
  851. if findDateTime.After(maxDate) {
  852. maxDate = findDateTime
  853. }
  854. }
  855. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  856. findDataList = append(findDataList, tmpValue)
  857. } else {
  858. findDataList = append(findDataList, 0)
  859. }
  860. }
  861. yDate := "0000-00-00"
  862. if !maxDate.IsZero() {
  863. yDate = maxDate.Format(utils.FormatDate)
  864. }
  865. yDataList = append(yDataList, models.YData{
  866. Date: yDate,
  867. Value: findDataList,
  868. Color: barChartInfoDate.Color,
  869. Name: barChartInfoDate.Name,
  870. })
  871. }
  872. return
  873. }
  874. // ChartInfoRefreshV2 图表刷新
  875. // @author Roc
  876. // @datetime 2022-09-16 10:15:38
  877. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  878. func ChartInfoRefreshV2(chartInfoId int) (err error) {
  879. var errmsg string
  880. defer func() {
  881. if err != nil {
  882. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  883. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  884. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  885. }
  886. }()
  887. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  888. if err != nil {
  889. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  890. return
  891. }
  892. edbIdList := make([]int, 0)
  893. for _, v := range edbMappingList {
  894. edbIdList = append(edbIdList, v.EdbInfoId)
  895. }
  896. // 批量刷新
  897. err, errmsg = EdbInfoRefreshAllFromBase(edbIdList, false)
  898. if err != nil {
  899. return
  900. }
  901. return
  902. }
  903. // GetSectionScatterChartData 柱方图的数据处理
  904. func GetSectionScatterChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  905. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  906. edbDataMap := make(map[int]map[string]float64)
  907. for edbInfoId, edbDataList := range edbDataListMap {
  908. edbDateData := make(map[string]float64)
  909. for _, edbData := range edbDataList {
  910. edbDateData[edbData.DataTime] = edbData.Value
  911. }
  912. edbDataMap[edbInfoId] = edbDateData
  913. }
  914. // edbIdList 指标展示顺序;x轴的指标顺序
  915. edbIdList = make([]int, 0)
  916. edbMappingMap := make(map[int]*models.ChartEdbInfoMapping)
  917. for _, v := range mappingList {
  918. edbIdList = append(edbIdList, v.EdbInfoId)
  919. edbMappingMap[v.EdbInfoId] = v
  920. }
  921. //SectionScatterSeriesInfoResp
  922. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  923. for _, seriesItem := range extraConfig.SeriesList {
  924. var maxDate time.Time
  925. // 系列中的指标数据
  926. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  927. var minXVal, maxXVal, minYVal, maxYVal float64
  928. for _, edbConf := range seriesItem.EdbInfoList {
  929. tmpItem := data_manage.SectionScatterEdbItemResp{
  930. IsShow: edbConf.IsShow,
  931. Name: edbConf.Name,
  932. NameEn: edbConf.NameEn,
  933. } //单个坐标点的数据
  934. //X轴的数据
  935. {
  936. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  937. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  938. if !ok {
  939. continue
  940. }
  941. findDate := edbConf.XDate //需要的日期值
  942. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  943. if len(dataList) <= 0 {
  944. // 没有数据的指标id
  945. //findDataList = append(findDataList, 0)
  946. continue
  947. }
  948. tmpItem.XEdbInfoId = edbInfoId
  949. tmpItem.XName = edbMappingInfo.EdbName
  950. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  951. switch edbConf.XDateType {
  952. case 1: //最新值
  953. dataList := edbDataListMap[edbInfoId]
  954. findDate = dataList[len(dataList)-1].DataTime
  955. case 2: //近期几天
  956. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  957. if tmpErr != nil {
  958. err = tmpErr
  959. return
  960. }
  961. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  962. lenData := len(dataList) - 1
  963. for i := lenData; i >= 0; i-- {
  964. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  965. if tmpErr != nil {
  966. err = tmpErr
  967. return
  968. }
  969. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  970. findDate = dataList[i].DataTime
  971. break
  972. }
  973. }
  974. case 3: // 固定日期
  975. //最早的日期
  976. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  977. if tmpErr != nil {
  978. err = tmpErr
  979. return
  980. }
  981. //寻找固定日期的数据
  982. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  983. if tmpErr != nil {
  984. err = tmpErr
  985. return
  986. }
  987. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  988. tmpDate := tmpDateTime.Format(utils.FormatDate)
  989. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  990. findDate = tmpDate
  991. break
  992. }
  993. }
  994. default:
  995. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  996. return
  997. }
  998. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  999. if maxDate.IsZero() {
  1000. maxDate = findDateTime
  1001. } else {
  1002. if findDateTime.After(maxDate) {
  1003. maxDate = findDateTime
  1004. }
  1005. }
  1006. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1007. tmpItem.XDate = findDate
  1008. tmpItem.XValue = tmpValue
  1009. } else {
  1010. continue
  1011. }
  1012. }
  1013. //Y轴的数据
  1014. {
  1015. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1016. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1017. if !ok {
  1018. continue
  1019. }
  1020. findDate := edbConf.YDate //需要的日期值
  1021. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1022. if len(dataList) <= 0 {
  1023. // 没有数据的指标id
  1024. //findDataList = append(findDataList, 0)
  1025. continue
  1026. }
  1027. tmpItem.YEdbInfoId = edbInfoId
  1028. tmpItem.YName = edbMappingInfo.EdbName
  1029. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1030. switch edbConf.YDateType {
  1031. case 1: //最新值
  1032. dataList := edbDataListMap[edbInfoId]
  1033. findDate = dataList[len(dataList)-1].DataTime
  1034. case 2: //近期几天
  1035. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1036. if tmpErr != nil {
  1037. err = tmpErr
  1038. return
  1039. }
  1040. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1041. lenData := len(dataList) - 1
  1042. for i := lenData; i >= 0; i-- {
  1043. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1044. if tmpErr != nil {
  1045. err = tmpErr
  1046. return
  1047. }
  1048. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1049. findDate = dataList[i].DataTime
  1050. break
  1051. }
  1052. }
  1053. case 3: // 固定日期
  1054. //最早的日期
  1055. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1056. if tmpErr != nil {
  1057. err = tmpErr
  1058. return
  1059. }
  1060. //寻找固定日期的数据
  1061. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1062. if tmpErr != nil {
  1063. err = tmpErr
  1064. return
  1065. }
  1066. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1067. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1068. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1069. findDate = tmpDate
  1070. break
  1071. }
  1072. }
  1073. default:
  1074. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1075. return
  1076. }
  1077. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1078. if maxDate.IsZero() {
  1079. maxDate = findDateTime
  1080. } else {
  1081. if findDateTime.After(maxDate) {
  1082. maxDate = findDateTime
  1083. }
  1084. }
  1085. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1086. tmpItem.YDate = findDate
  1087. tmpItem.YValue = tmpValue
  1088. } else {
  1089. continue
  1090. }
  1091. }
  1092. // 获取当前系列的X轴的最大最小值
  1093. {
  1094. if tmpItem.XValue < minXVal {
  1095. minXVal = tmpItem.XValue
  1096. }
  1097. if tmpItem.XValue > maxXVal {
  1098. maxXVal = tmpItem.XValue
  1099. }
  1100. if tmpItem.YValue < minYVal {
  1101. minYVal = tmpItem.YValue
  1102. }
  1103. if tmpItem.YValue > maxYVal {
  1104. maxYVal = tmpItem.YValue
  1105. }
  1106. }
  1107. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1108. }
  1109. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1110. var trendLine, rSquare string
  1111. // 生成线性方程式
  1112. var a, b float64
  1113. {
  1114. coordinateData := make([]utils.Coordinate, 0)
  1115. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1116. tmpCoordinate1 := utils.Coordinate{
  1117. X: tmpSeriesEdbInfo.XValue,
  1118. Y: tmpSeriesEdbInfo.YValue,
  1119. }
  1120. coordinateData = append(coordinateData, tmpCoordinate1)
  1121. }
  1122. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1123. if len(coordinateData) >= 2 {
  1124. a, b = utils.GetLinearResult(coordinateData)
  1125. if !math.IsNaN(a) || !math.IsNaN(b) {
  1126. if b > 0 {
  1127. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1128. } else {
  1129. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1130. }
  1131. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1132. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1133. }
  1134. // 计算R平方
  1135. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1136. }
  1137. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  1138. X: minXVal,
  1139. Y: minYVal,
  1140. }, data_manage.CoordinatePoint{
  1141. X: maxXVal,
  1142. Y: maxYVal,
  1143. })
  1144. }
  1145. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  1146. Name: seriesItem.Name,
  1147. NameEn: seriesItem.NameEn,
  1148. Color: seriesItem.Color,
  1149. EdbInfoList: tmpSeriesEdbInfoList,
  1150. ShowTrendLine: seriesItem.ShowTrendLine,
  1151. ShowFitEquation: seriesItem.ShowFitEquation,
  1152. ShowRSquare: seriesItem.ShowRSquare,
  1153. TrendLine: trendLine,
  1154. RSquare: rSquare,
  1155. TrendLimitData: trendLimitData,
  1156. })
  1157. }
  1158. chartDataResp = data_manage.SectionScatterInfoResp{
  1159. XName: extraConfig.XName,
  1160. XNameEn: extraConfig.XNameEn,
  1161. XUnitName: extraConfig.XUnitName,
  1162. XUnitNameEn: extraConfig.XUnitNameEn,
  1163. YName: extraConfig.YName,
  1164. YNameEn: extraConfig.YNameEn,
  1165. YUnitName: extraConfig.YUnitName,
  1166. YUnitNameEn: extraConfig.YUnitNameEn,
  1167. XMinValue: extraConfig.XMinValue,
  1168. XMaxValue: extraConfig.XMaxValue,
  1169. YMinValue: extraConfig.YMinValue,
  1170. YMaxValue: extraConfig.YMaxValue,
  1171. DataList: dataListResp,
  1172. }
  1173. return
  1174. }