chart_info.go 47 KB

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