chart_info.go 43 KB

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