chart_info.go 39 KB

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