chart_info.go 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  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. xEdbIdValue = make([]int, 0)
  64. yDataList = make([]models.YData, 0)
  65. var extraConfig interface{}
  66. switch chartType {
  67. case 6:
  68. var tmpConfig models.ChartTimeCombineExtraConf
  69. if extraConfigStr != `` {
  70. err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
  71. if err != nil {
  72. errMsg = "雷达图配置异常"
  73. err = errors.New(errMsg)
  74. return
  75. }
  76. }
  77. extraConfig = tmpConfig
  78. case 7: // 柱形图
  79. var barConfig data_manage.BarChartInfoReq
  80. if extraConfigStr == `` {
  81. errMsg = "柱方图未配置"
  82. err = errors.New(errMsg)
  83. return
  84. }
  85. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  86. if err != nil {
  87. errMsg = "柱方图配置异常"
  88. err = errors.New(errMsg)
  89. return
  90. }
  91. extraConfig = barConfig
  92. case 10: // 截面散点图
  93. var tmpExtraConfig data_manage.SectionScatterReq
  94. if extraConfigStr == `` {
  95. errMsg = "截面散点图未配置"
  96. err = errors.New(errMsg)
  97. return
  98. }
  99. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  100. if err != nil {
  101. errMsg = "截面散点配置异常"
  102. err = errors.New(errMsg)
  103. return
  104. }
  105. extraConfig = tmpExtraConfig
  106. case utils.CHART_TYPE_RADAR:
  107. var barConfig data_manage.RadarChartInfoReq
  108. if extraConfigStr == `` {
  109. errMsg = "雷达图未配置"
  110. err = errors.New(errMsg)
  111. return
  112. }
  113. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  114. if err != nil {
  115. errMsg = "雷达图配置异常"
  116. err = errors.New(errMsg)
  117. return
  118. }
  119. extraConfig = barConfig
  120. case utils.CHART_TYPE_SECTION_COMBINE:
  121. // 预览和详情都走这个接口
  122. var sectionExtraConfig models.ChartSectionAllExtraConf
  123. if extraConfigStr == `` {
  124. errMsg = "截面组合图未配置"
  125. err = errors.New(errMsg)
  126. return
  127. }
  128. if chartInfoId > 0 {
  129. var sectionExtraConfigTmp models.ChartSectionExtraConf
  130. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfigTmp)
  131. if err != nil {
  132. errMsg = "截面组合图配置异常"
  133. err = errors.New(errMsg)
  134. return
  135. }
  136. sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
  137. sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
  138. sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
  139. sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
  140. sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
  141. sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
  142. // 查询所有的seriesEdb
  143. seriesEdbList, e := models.GetChartSeriesEdbByChartInfoId(chartInfoId)
  144. if e != nil {
  145. errMsg = "查询seriesEdb失败"
  146. err = errors.New(errMsg + e.Error())
  147. return
  148. }
  149. // todo 是否有必要返回单位信息
  150. // 组装成map
  151. seriesEdbMap := make(map[int][]*models.ChartSectionSeriesEdbConf)
  152. for _, v := range seriesEdbList {
  153. var dateConf *models.ChartSectionSeriesDateConfItem
  154. if v.DateConf != "" {
  155. err = json.Unmarshal([]byte(v.DateConf), &dateConf)
  156. if err != nil {
  157. errMsg = "截面组合图配置异常"
  158. err = errors.New(errMsg + err.Error())
  159. return
  160. }
  161. }
  162. tmp := &models.ChartSectionSeriesEdbConf{
  163. ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
  164. ChartSeriesId: v.ChartSeriesId,
  165. //ChartInfoId: v.ChartInfoId,
  166. EdbInfoId: v.EdbInfoId,
  167. DateConf: dateConf,
  168. EdbName: "",
  169. EdbNameEn: "",
  170. Unit: "",
  171. UnitEn: "",
  172. DateConfName: v.DateConfName,
  173. DateConfType: v.DateConfType,
  174. }
  175. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
  176. }
  177. //查询series
  178. seriesListTmp, e := models.GetChartSeriesByChartInfoId(chartInfoId)
  179. if e != nil {
  180. errMsg = "查询series失败"
  181. err = errors.New(errMsg + e.Error())
  182. return
  183. }
  184. seriesList := make([]*models.ChartSectionSeriesItem, 0)
  185. for _, v := range seriesListTmp {
  186. tmpSeries := &models.ChartSectionSeriesItem{
  187. ChartSeriesId: v.ChartSeriesId,
  188. SeriesName: v.SeriesName,
  189. SeriesNameEn: v.SeriesNameEn,
  190. ChartStyle: v.ChartStyle,
  191. ChartColor: v.ChartColor,
  192. ChartWidth: v.ChartWidth,
  193. IsPoint: v.IsPoint,
  194. IsNumber: v.IsNumber,
  195. IsAxis: v.IsAxis,
  196. MaxData: v.MaxData,
  197. MinData: v.MinData,
  198. //IsOrder: false,
  199. EdbInfoList: nil,
  200. DataList: nil,
  201. }
  202. edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
  203. if ok {
  204. tmpSeries.EdbInfoList = edbInfoList
  205. }
  206. seriesList = append(seriesList, tmpSeries)
  207. }
  208. sectionExtraConfig.SeriesList = seriesList
  209. } else {
  210. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  211. if err != nil {
  212. errMsg = "截面组合图配置异常"
  213. err = errors.New(errMsg)
  214. return
  215. }
  216. }
  217. //校验引用日期名称是否重复
  218. dateNameMap := make(map[string]int)
  219. dateNameEnMap := make(map[string]int)
  220. for _, v := range sectionExtraConfig.DateConfList {
  221. if _, ok := dateNameMap[v.DateConfName]; ok {
  222. errMsg = "截面组合图引用日期名称设置重复"
  223. err = errors.New(errMsg + v.DateConfName)
  224. return
  225. }
  226. if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
  227. errMsg = "截面组合图引用日期名称设置重复"
  228. err = errors.New(errMsg + v.DateConfNameEn)
  229. return
  230. }
  231. if v.DateConfName != "" {
  232. dateNameMap[v.DateConfName] = 1
  233. }
  234. if v.DateConfNameEn != "" {
  235. dateNameEnMap[v.DateConfNameEn] = 1
  236. }
  237. }
  238. //检查系列名称是否重复
  239. seriesNameMap := make(map[string]int)
  240. seriesNameEnMap := make(map[string]int)
  241. for _, v := range sectionExtraConfig.SeriesList {
  242. if _, ok := seriesNameMap[v.SeriesName]; ok {
  243. errMsg = "截面组合图系列名称设置重复"
  244. err = errors.New(errMsg + v.SeriesName)
  245. return
  246. }
  247. if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
  248. errMsg = "截面组合图系列名称设置重复"
  249. err = errors.New(errMsg + v.SeriesNameEn)
  250. return
  251. }
  252. if v.SeriesName != "" {
  253. seriesNameMap[v.SeriesName] = 1
  254. }
  255. if v.SeriesNameEn != "" {
  256. seriesNameEnMap[v.SeriesNameEn] = 1
  257. }
  258. }
  259. // 检查基准系列是否设置
  260. if sectionExtraConfig.BaseChartSeriesName == "" {
  261. errMsg = "截面组合图基准系列名称未设置"
  262. err = errors.New(errMsg)
  263. return
  264. }
  265. //todo 如果是详情接口,应该要从其他表里查询配置
  266. extraConfig = sectionExtraConfig
  267. default:
  268. xEdbIdValue = make([]int, 0)
  269. yDataList = make([]models.YData, 0)
  270. }
  271. // 指标对应的所有数据
  272. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  273. if err != nil {
  274. return
  275. }
  276. // 特殊图形数据处理
  277. switch chartType {
  278. case 2: // 季节性图
  279. if seasonExtraConfig != "" {
  280. // 季节性图计算不管图上数据时间,拿所有数据
  281. _, tempEdbList, e := getEdbDataMapListForSeason(chartInfoId, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
  282. if e != nil {
  283. err = e
  284. return
  285. }
  286. dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
  287. } else {
  288. // 兼容无配置的老图
  289. dataResp = new(models.SeasonChartResp)
  290. }
  291. case 6: //时序组合图
  292. //判断是否堆积
  293. timeConf := extraConfig.(models.ChartTimeCombineExtraConf)
  294. if extraConfigStr == "" { //历史数据,默认开启堆积
  295. timeConf = models.ChartTimeCombineExtraConf{
  296. IsHeap: 1,
  297. }
  298. }
  299. dataResp = models.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
  300. case 7: // 柱形图
  301. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  302. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  303. for _, v := range edbList {
  304. // 指标别名
  305. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  306. for _, reqEdb := range barChartConf.EdbInfoIdList {
  307. if v.EdbInfoId == reqEdb.EdbInfoId {
  308. v.EdbAliasName = reqEdb.Name
  309. }
  310. }
  311. }
  312. }
  313. case 10: // 截面散点图
  314. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  315. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  316. case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
  317. sectionConf := extraConfig.(models.ChartSectionAllExtraConf)
  318. var chartInfo *models.ChartInfo
  319. if chartInfoId > 0 {
  320. chartInfo, err = models.GetChartInfoById(chartInfoId)
  321. if err != nil {
  322. errMsg = "获取图表信息失败"
  323. return
  324. }
  325. }
  326. xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)
  327. // 这个数据没有必要返回给前端
  328. for _, v := range edbList {
  329. v.DataList = nil
  330. }
  331. case utils.CHART_TYPE_RADAR: //雷达图
  332. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  333. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  334. }
  335. return
  336. }
  337. // GetEdbDataMapList 获取指标最后的基础数据
  338. func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
  339. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  340. return
  341. }
  342. // getEdbDataMapList 获取指标最后的基础数据
  343. func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
  344. // 指标对应的所有数据
  345. edbDataListMap = make(map[int][]*models.EdbDataList)
  346. for _, v := range mappingList {
  347. //fmt.Println("v:", v.EdbInfoId)
  348. item := new(models.ChartEdbInfoMapping)
  349. item.EdbInfoId = v.EdbInfoId
  350. item.SourceName = v.SourceName
  351. item.Source = v.Source
  352. item.EdbCode = v.EdbCode
  353. item.EdbName = v.EdbName
  354. item.EdbType = v.EdbType
  355. item.EdbNameEn = v.EdbNameEn
  356. item.Frequency = v.Frequency
  357. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  358. if v.Unit != `无` {
  359. item.Unit = v.Unit
  360. }
  361. item.UnitEn = v.UnitEn
  362. item.StartDate = v.StartDate
  363. item.EndDate = v.EndDate
  364. item.ModifyTime = v.ModifyTime
  365. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  366. item.PredictChartColor = v.PredictChartColor
  367. item.EdbAliasName = v.EdbAliasName
  368. if chartInfoId <= 0 {
  369. item.IsAxis = 1
  370. item.LeadValue = 0
  371. item.LeadUnit = ""
  372. //item.ChartEdbMappingId = 0
  373. item.ChartInfoId = 0
  374. item.IsOrder = false
  375. item.EdbInfoType = 1
  376. item.ChartStyle = ""
  377. item.ChartColor = ""
  378. item.ChartWidth = 0
  379. item.MaxData = v.MaxValue
  380. item.MinData = v.MinValue
  381. } else {
  382. item.IsAxis = v.IsAxis
  383. item.EdbInfoType = v.EdbInfoType
  384. item.LeadValue = v.LeadValue
  385. item.LeadUnit = v.LeadUnit
  386. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  387. //item.ChartEdbMappingId = v.ChartEdbMappingId
  388. item.ChartInfoId = v.ChartInfoId
  389. item.ChartStyle = v.ChartStyle
  390. item.ChartColor = v.ChartColor
  391. item.ChartWidth = v.ChartWidth
  392. item.IsOrder = v.IsOrder
  393. item.MaxData = v.MaxData
  394. item.MinData = v.MinData
  395. }
  396. item.LatestValue = v.LatestValue
  397. item.LatestDate = v.LatestDate
  398. item.UniqueCode = v.UniqueCode
  399. item.MoveLatestDate = v.LatestDate
  400. item.IsConvert = v.IsConvert
  401. item.ConvertType = v.ConvertType
  402. item.ConvertValue = v.ConvertValue
  403. item.ConvertUnit = v.ConvertUnit
  404. item.ConvertEnUnit = v.ConvertEnUnit
  405. var startDateReal string
  406. var diffSeconds int64
  407. if chartType == 2 { //季节性图
  408. startDateReal = startDate
  409. if len(mappingList) > 1 {
  410. item.IsAxis = v.IsAxis
  411. }
  412. } else {
  413. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  414. var startTimeRealTemp time.Time
  415. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  416. switch v.LeadUnit {
  417. case "天":
  418. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  419. case "月":
  420. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  421. case "季":
  422. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  423. case "周":
  424. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  425. case "年":
  426. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  427. }
  428. if startTimeRealTemp.Before(startDateParse) {
  429. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  430. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  431. } else {
  432. startDateReal = startDate
  433. diffSeconds = 0
  434. }
  435. // 预测指标的开始日期也要偏移
  436. {
  437. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  438. if tmpErr != nil {
  439. err = tmpErr
  440. return
  441. }
  442. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  443. if tmpErr != nil {
  444. err = tmpErr
  445. return
  446. }
  447. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  448. }
  449. } else {
  450. startDateReal = startDate
  451. }
  452. }
  453. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  454. calendarPreYear := 0
  455. if calendar == "农历" {
  456. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  457. if err != nil {
  458. fmt.Println("time.Parse:" + err.Error())
  459. }
  460. calendarPreYear = newStartDateReal.Year() - 1
  461. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  462. startDateReal = newStartDateReal.Format(utils.FormatDate)
  463. }
  464. dataList := make([]*models.EdbDataList, 0)
  465. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  466. switch v.EdbInfoCategoryType {
  467. case 0:
  468. dataList, err = models.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  469. case 1:
  470. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
  471. default:
  472. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  473. }
  474. if err != nil {
  475. return
  476. }
  477. if v.IsConvert == 1 {
  478. switch v.ConvertType {
  479. case 1:
  480. for i, data := range dataList {
  481. dataList[i].Value = data.Value * v.ConvertValue
  482. }
  483. //item.MaxData = item.MaxData * v.ConvertValue
  484. //item.MinData = item.MinData * v.ConvertValue
  485. case 2:
  486. for i, data := range dataList {
  487. dataList[i].Value = data.Value / v.ConvertValue
  488. }
  489. //item.MaxData = item.MaxData / v.ConvertValue
  490. //item.MinData = item.MinData / v.ConvertValue
  491. case 3:
  492. for i, data := range dataList {
  493. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  494. }
  495. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  496. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  497. }
  498. }
  499. edbDataListMap[v.EdbInfoId] = dataList
  500. if diffSeconds != 0 && v.EdbInfoType == 0 {
  501. dataListLen := len(dataList)
  502. for i := 0; i < dataListLen; i++ {
  503. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  504. }
  505. }
  506. if chartType == 2 && item.IsAxis == 1 {
  507. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  508. if tmpErr != nil {
  509. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  510. return
  511. }
  512. if calendar == "农历" {
  513. if len(dataList) <= 0 {
  514. result := new(models.EdbDataResult)
  515. item.DataList = result
  516. } else {
  517. result, tmpErr := models.AddCalculateQuarterV6(dataList)
  518. if tmpErr != nil {
  519. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  520. return
  521. }
  522. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  523. if tErr != nil {
  524. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  525. return
  526. }
  527. item.DataList = quarterDataList
  528. }
  529. } else {
  530. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  531. if tErr != nil {
  532. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  533. return
  534. }
  535. item.DataList = quarterDataList
  536. }
  537. } else if chartType == 2 && item.IsAxis == 0 {
  538. // 右轴数据处理,只要最新一年
  539. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  540. if tmpErr != nil {
  541. //item.DataList = dataList
  542. item.IsNullData = true
  543. edbList = append(edbList, item)
  544. continue
  545. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  546. return
  547. }
  548. newDataList := make([]*models.EdbDataList, 0)
  549. for _, v := range dataList {
  550. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  551. if e != nil {
  552. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  553. return
  554. }
  555. if dataTime.Year() == latestDate.Year() {
  556. newDataList = append(newDataList, v)
  557. }
  558. }
  559. item.DataList = newDataList
  560. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  561. //item.DataList = dataList
  562. } else {
  563. item.DataList = dataList
  564. }
  565. edbList = append(edbList, item)
  566. }
  567. return
  568. }
  569. // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
  570. func GetSeasonEdbInfoDataListByXDate(dataList []*models.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort models.QuarterDataList, err error) {
  571. xStartDate := "01-01"
  572. xEndDate := "12-31"
  573. jumpYear := 0
  574. legends := make([]models.SeasonChartLegend, 0)
  575. var seasonExtra models.SeasonExtraItem
  576. if seasonExtraConfig != "" {
  577. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  578. if err != nil {
  579. return
  580. }
  581. }
  582. if seasonExtra.XStartDate != "" {
  583. xStartDate = seasonExtra.XStartDate
  584. xEndDate = seasonExtra.XEndDate
  585. jumpYear = seasonExtra.JumpYear
  586. legends = seasonExtra.ChartLegend
  587. }
  588. length := len(dataList)
  589. if length == 0 {
  590. return
  591. }
  592. legendMap := make(map[string]string, 0)
  593. if len(legends) > 0 {
  594. for _, v := range legends {
  595. legendMap[v.Name] = v.Value
  596. }
  597. }
  598. latestDateStr := latestDate.Format(utils.FormatDate)
  599. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  600. //根据数据确定最早的年份,和最近年份
  601. //根据横轴的日期,汇总所有的年份
  602. startDate := dataList[0].DataTime
  603. startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
  604. if tmpErr != nil {
  605. err = tmpErr
  606. return
  607. }
  608. startYear := startDateT.Year()
  609. //获取数据的最新日期
  610. lastDate := dataList[length-1].DataTime
  611. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  612. if tmpErr != nil {
  613. err = tmpErr
  614. return
  615. }
  616. endYear := lastDateT.Year()
  617. nowYear := time.Now().Year()
  618. chartLegendMaxYear := 0
  619. dataMap := make(map[string]models.QuarterXDateItem, 0)
  620. quarterDataList := make([]*models.QuarterData, 0)
  621. quarterMap := make(map[string][]*models.EdbDataList, 0)
  622. //整理出日期
  623. idx := 1
  624. chartLegendMap := make(map[string]int, 0)
  625. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  626. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  627. currentEndYear := currentStartYear
  628. if jumpYear == 1 {
  629. currentEndYear = currentStartYear + 1
  630. }
  631. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  632. name := fmt.Sprintf("%s_%s", startStr, endStr)
  633. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  634. startT, tEr := time.Parse(utils.FormatDate, startStr)
  635. if tEr != nil {
  636. err = tEr
  637. return
  638. }
  639. endT, tEr := time.Parse(utils.FormatDate, endStr)
  640. if tEr != nil {
  641. err = tEr
  642. return
  643. }
  644. if lastDateT.Before(startT) {
  645. //如果最新的日期在起始日之前,则跳出循环
  646. break
  647. }
  648. if endT.Year() > nowYear {
  649. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  650. nowYear = endT.Year()
  651. }
  652. item := models.QuarterXDateItem{
  653. StartDate: startT,
  654. EndDate: endT,
  655. ShowName: showName,
  656. }
  657. chartLegendMaxYear = endT.Year()
  658. dataMap[name] = item
  659. chartLegendMap[name] = idx
  660. idx++
  661. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  662. if lastDateT.Before(endT) {
  663. //如果最新的日期在起始日之前,则跳出循环
  664. break
  665. }
  666. }
  667. lenYear := len(dataMap)
  668. if chartLegendMaxYear > endYear {
  669. chartLegendMaxYear = endYear
  670. }
  671. for k, v := range dataMap {
  672. if i, ok := chartLegendMap[k]; ok {
  673. //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  674. v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
  675. }
  676. dataMap[k] = v
  677. }
  678. for _, v := range dataList {
  679. dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
  680. year := dataTimeT.Year()
  681. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  682. for k, dateItem := range dataMap {
  683. tmpVal := models.EdbDataList{
  684. EdbDataId: v.EdbDataId,
  685. EdbInfoId: v.EdbInfoId,
  686. DataTime: v.DataTime,
  687. DataTimestamp: v.DataTimestamp,
  688. Value: v.Value,
  689. }
  690. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  691. if jumpYear == 1 {
  692. //计算前一年最大的日期, 只补齐数据到去年
  693. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  694. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  695. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  696. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  697. } else {
  698. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  699. }
  700. } else {
  701. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  702. }
  703. timestamp := newItemDate.UnixNano() / 1e6
  704. tmpVal.DataTimestamp = timestamp
  705. tmpV := &tmpVal
  706. if findVal, ok := quarterMap[k]; !ok {
  707. findVal = append(findVal, tmpV)
  708. quarterMap[k] = findVal
  709. } else {
  710. findVal = append(findVal, tmpV)
  711. quarterMap[k] = findVal
  712. }
  713. if v.DataTime == latestDateStr {
  714. dateItem.CuttingDataTimestamp = timestamp
  715. dataMap[k] = dateItem
  716. }
  717. //break
  718. }
  719. }
  720. }
  721. for k, v := range dataMap {
  722. itemList := quarterMap[k]
  723. quarterItem := new(models.QuarterData)
  724. quarterItem.Years = v.ShowName
  725. quarterItem.Year = v.ChartLegend
  726. quarterItem.ChartLegend = v.ChartLegend
  727. if le, ok := legendMap[v.ShowName]; ok {
  728. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  729. quarterItem.ChartLegend = le
  730. }
  731. }
  732. quarterItem.DataList = itemList
  733. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  734. //如果等于最后的实际日期,那么将切割时间戳记录
  735. if quarterItem.CuttingDataTimestamp == 0 {
  736. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  737. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  738. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  739. }
  740. }
  741. quarterDataList = append(quarterDataList, quarterItem)
  742. }
  743. if len(quarterDataList) > 0 {
  744. quarterDataListSort = quarterDataList
  745. sort.Sort(quarterDataListSort)
  746. }
  747. return
  748. }
  749. // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
  750. func GetSeasonEdbInfoDataListByXDateNong(result *models.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort models.QuarterDataList, err error) {
  751. xStartDate := "01-01"
  752. xEndDate := "12-31"
  753. jumpYear := 0
  754. legends := make([]models.SeasonChartLegend, 0)
  755. var seasonExtra models.SeasonExtraItem
  756. if seasonExtraConfig != "" {
  757. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  758. if err != nil {
  759. return
  760. }
  761. }
  762. if seasonExtra.XStartDate != "" {
  763. xStartDate = seasonExtra.XStartDate
  764. xEndDate = seasonExtra.XEndDate
  765. jumpYear = seasonExtra.JumpYear
  766. legends = seasonExtra.ChartLegend
  767. }
  768. length := len(result.List)
  769. if length == 0 {
  770. return
  771. }
  772. legendMap := make(map[string]string, 0)
  773. if len(legends) > 0 {
  774. for _, v := range legends {
  775. legendMap[v.Name] = v.Value
  776. }
  777. }
  778. latestDateYear := latestDate.Year()
  779. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  780. //根据数据确定最早的年份,和最近年份
  781. //根据横轴的日期,汇总所有的年份
  782. startYear := result.List[0].Year
  783. /*if jumpYear == 1 {
  784. if startYear != calendarPreYear {
  785. startYear = startYear - 1
  786. }
  787. }*/
  788. itemLength := len(result.List[length-1].Items)
  789. //获取数据的最新日期
  790. lastDate := result.List[length-1].Items[itemLength-1].DataTime
  791. maxY := result.List[length-1].Year
  792. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  793. if tmpErr != nil {
  794. err = tmpErr
  795. return
  796. }
  797. endYear := lastDateT.Year()
  798. nowYear := time.Now().Year()
  799. chartLegendMaxYear := 0
  800. dataMap := make(map[string]models.QuarterXDateItem, 0)
  801. quarterDataList := make([]*models.QuarterData, 0)
  802. resultData := make([]*models.QuarterData, 0)
  803. quarterMap := make(map[string][]*models.EdbDataList, 0)
  804. //整理出日期
  805. var startTmpT, endTmpT time.Time
  806. idx := 1
  807. chartLegendMap := make(map[string]int, 0)
  808. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  809. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  810. currentEndYear := currentStartYear
  811. if jumpYear == 1 {
  812. currentEndYear = currentStartYear + 1
  813. }
  814. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  815. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  816. startT, tEr := time.Parse(utils.FormatDate, startStr)
  817. if tEr != nil {
  818. err = tEr
  819. return
  820. }
  821. endT, tEr := time.Parse(utils.FormatDate, endStr)
  822. if tEr != nil {
  823. err = tEr
  824. return
  825. }
  826. if lastDateT.Before(startT) {
  827. //如果最新的日期在起始日之前,则跳出循环
  828. break
  829. }
  830. if endT.Year() > nowYear {
  831. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  832. nowYear = endT.Year()
  833. }
  834. item := models.QuarterXDateItem{
  835. StartDate: startT,
  836. EndDate: endT,
  837. ShowName: showName,
  838. }
  839. chartLegendMaxYear = endT.Year()
  840. dataMap[showName] = item
  841. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  842. startTmpT = startT
  843. endTmpT = endT
  844. chartLegendMap[showName] = idx
  845. idx++
  846. if lastDateT.Before(endT) {
  847. //如果最新的日期在起始日之前,则跳出循环
  848. break
  849. }
  850. }
  851. lenYear := len(dataMap)
  852. if chartLegendMaxYear > endYear {
  853. chartLegendMaxYear = endYear
  854. }
  855. for k, v := range dataMap {
  856. if i, ok := chartLegendMap[k]; ok {
  857. //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  858. v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
  859. }
  860. dataMap[k] = v
  861. }
  862. yearDataListMap := make(map[int]*models.EdbDataItems, 0)
  863. for _, lv := range result.List {
  864. yearDataListMap[lv.Year] = lv
  865. }
  866. //判断哪些点应该落在同一条时间线上
  867. /*maxY := lastDateT.Year()
  868. changeFlag := false
  869. if lastDateT.Month() >= 11 {
  870. maxY = maxY + 1
  871. }
  872. if maxY < nowYear {
  873. changeFlag = true
  874. maxY = nowYear
  875. }*/
  876. /*endTmp := fmt.Sprintf("%d-%s", maxY, xEndDate)
  877. endTmpT, _ := time.Parse(utils.FormatDate, endTmp)
  878. minY := maxY
  879. if jumpYear == 1 {
  880. minY = maxY - 1
  881. }
  882. startTmp := fmt.Sprintf("%d-%s", minY, xStartDate)
  883. startTmpT, _ := time.Parse(utils.FormatDate, startTmp)*/
  884. fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
  885. fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
  886. fmt.Printf("maxY 为%d \n", maxY)
  887. for name, dateItem := range dataMap {
  888. tY := dateItem.EndDate.Year()
  889. if lastDateT.Month() >= 11 {
  890. if maxY > endTmpT.Year() {
  891. tY = tY + 1
  892. }
  893. }
  894. lv, ok1 := yearDataListMap[tY]
  895. fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
  896. if !ok1 {
  897. continue
  898. }
  899. for _, item := range lv.Items {
  900. tmpVal := models.EdbDataList{
  901. EdbDataId: item.EdbDataId,
  902. EdbInfoId: item.EdbInfoId,
  903. DataTime: item.DataTime,
  904. DataTimestamp: item.DataTimestamp,
  905. Value: item.Value,
  906. }
  907. dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
  908. if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
  909. tmpV := &tmpVal
  910. if findVal, ok := quarterMap[name]; !ok {
  911. findVal = append(findVal, tmpV)
  912. quarterMap[name] = findVal
  913. } else {
  914. findVal = append(findVal, tmpV)
  915. quarterMap[name] = findVal
  916. }
  917. if lv.Year >= latestDateYear {
  918. // 切割的日期时间字符串
  919. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  920. if item.DataTime == cuttingDataTimeStr {
  921. dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
  922. dataMap[name] = dateItem
  923. }
  924. }
  925. }
  926. }
  927. }
  928. for k, v := range dataMap {
  929. itemList := quarterMap[k]
  930. quarterItem := new(models.QuarterData)
  931. quarterItem.Years = v.ShowName
  932. quarterItem.Year = v.ChartLegend
  933. quarterItem.ChartLegend = v.ChartLegend
  934. if le, ok := legendMap[v.ShowName]; ok {
  935. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  936. quarterItem.ChartLegend = le
  937. }
  938. }
  939. quarterItem.DataList = itemList
  940. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  941. //如果等于最后的实际日期,那么将切割时间戳记录
  942. if quarterItem.CuttingDataTimestamp == 0 {
  943. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  944. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  945. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  946. }
  947. }
  948. quarterDataList = append(quarterDataList, quarterItem)
  949. }
  950. if result.List[0].Year != calendarPreYear {
  951. itemList := make([]*models.EdbDataList, 0)
  952. items := new(models.QuarterData)
  953. //items.Year = calendarPreYear
  954. items.DataList = itemList
  955. newResult := make([]*models.QuarterData, 0)
  956. newResult = append(newResult, items)
  957. newResult = append(newResult, quarterDataList...)
  958. resultData = newResult
  959. } else {
  960. resultData = quarterDataList
  961. }
  962. if len(quarterDataList) > 0 {
  963. quarterDataListSort = resultData
  964. sort.Sort(quarterDataListSort)
  965. }
  966. return
  967. }
  968. // BarChartData 柱方图的数据处理
  969. 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) {
  970. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  971. edbDataMap := make(map[int]map[string]float64)
  972. for edbInfoId, edbDataList := range edbDataListMap {
  973. edbDateData := make(map[string]float64)
  974. for _, edbData := range edbDataList {
  975. edbDateData[edbData.DataTime] = edbData.Value
  976. }
  977. edbDataMap[edbInfoId] = edbDateData
  978. }
  979. // edbIdList 指标展示顺序;x轴的指标顺序
  980. edbIdList = make([]int, 0)
  981. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  982. dateData := make(map[int]float64)
  983. if barChartInfoSort.Sort == 0 {
  984. for _, v := range mappingList {
  985. edbIdList = append(edbIdList, v.EdbInfoId)
  986. }
  987. } else {
  988. lenBarChartInfoDateList := len(barChartInfoDateList)
  989. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  990. err = errors.New("排序日期异常")
  991. return
  992. }
  993. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  994. // 日期配置
  995. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  996. for edbInfoId, dataList := range edbDataListMap {
  997. if len(dataList) <= 0 {
  998. // 没有数据的指标id
  999. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1000. continue
  1001. }
  1002. findDate := barChartInfoDate.Date
  1003. switch barChartInfoDate.Type {
  1004. case 1: //最新值
  1005. findDate = dataList[len(dataList)-1].DataTime
  1006. case 2: //近期几天
  1007. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1008. if tmpErr != nil {
  1009. err = tmpErr
  1010. return
  1011. }
  1012. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1013. lenData := len(dataList) - 1
  1014. for i := lenData; i >= 0; i-- {
  1015. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1016. if tmpErr != nil {
  1017. err = tmpErr
  1018. return
  1019. }
  1020. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1021. findDate = dataList[i].DataTime
  1022. break
  1023. }
  1024. }
  1025. case 3: // 固定日期
  1026. //最早的日期
  1027. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1028. if tmpErr != nil {
  1029. err = tmpErr
  1030. return
  1031. }
  1032. //寻找固定日期的数据
  1033. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1034. if tmpErr != nil {
  1035. err = tmpErr
  1036. return
  1037. }
  1038. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1039. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1040. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1041. findDate = tmpDate
  1042. break
  1043. }
  1044. }
  1045. default:
  1046. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1047. return
  1048. }
  1049. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1050. dateData[edbInfoId] = tmpValue
  1051. } else {
  1052. // 没有数据的指标id
  1053. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1054. }
  1055. }
  1056. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1057. // 排序
  1058. dateDataSort := utils.NewMapSorter(dateData)
  1059. sort.Sort(dateDataSort)
  1060. if barChartInfoSort.Sort == 1 {
  1061. // 先将没有数据的指标id放在最前面
  1062. if len(notDataEdbIdList) > 0 {
  1063. edbIdList = append(edbIdList, notDataEdbIdList...)
  1064. }
  1065. for _, v := range dateDataSort {
  1066. edbIdList = append(edbIdList, v.Key)
  1067. }
  1068. } else {
  1069. for i := len(dateDataSort) - 1; i >= 0; i-- {
  1070. edbIdList = append(edbIdList, dateDataSort[i].Key)
  1071. }
  1072. // 再将没有数据的指标id放在最后面
  1073. if len(notDataEdbIdList) > 0 {
  1074. edbIdList = append(edbIdList, notDataEdbIdList...)
  1075. }
  1076. }
  1077. }
  1078. yDataList = make([]models.YData, 0) //y轴的数据列表
  1079. for _, barChartInfoDate := range barChartInfoDateList {
  1080. var maxDate time.Time
  1081. findDataList := make([]float64, 0) // 当前日期的数据值
  1082. for _, edbInfoId := range edbIdList {
  1083. findDate := barChartInfoDate.Date //需要的日期值
  1084. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1085. if len(dataList) <= 0 {
  1086. // 没有数据的指标id
  1087. findDataList = append(findDataList, 0)
  1088. continue
  1089. }
  1090. switch barChartInfoDate.Type {
  1091. case 1: //最新值
  1092. dataList := edbDataListMap[edbInfoId]
  1093. findDate = dataList[len(dataList)-1].DataTime
  1094. case 2: //近期几天
  1095. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1096. if tmpErr != nil {
  1097. err = tmpErr
  1098. return
  1099. }
  1100. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1101. lenData := len(dataList) - 1
  1102. for i := lenData; i >= 0; i-- {
  1103. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1104. if tmpErr != nil {
  1105. err = tmpErr
  1106. return
  1107. }
  1108. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1109. findDate = dataList[i].DataTime
  1110. break
  1111. }
  1112. }
  1113. case 3: // 固定日期
  1114. //最早的日期
  1115. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1116. if tmpErr != nil {
  1117. err = tmpErr
  1118. return
  1119. }
  1120. //寻找固定日期的数据
  1121. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1122. if tmpErr != nil {
  1123. err = tmpErr
  1124. return
  1125. }
  1126. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1127. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1128. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1129. findDate = tmpDate
  1130. break
  1131. }
  1132. }
  1133. default:
  1134. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1135. return
  1136. }
  1137. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1138. if maxDate.IsZero() {
  1139. maxDate = findDateTime
  1140. } else {
  1141. if findDateTime.After(maxDate) {
  1142. maxDate = findDateTime
  1143. }
  1144. }
  1145. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1146. findDataList = append(findDataList, tmpValue)
  1147. } else {
  1148. findDataList = append(findDataList, 0)
  1149. }
  1150. }
  1151. yDate := "0000-00-00"
  1152. if !maxDate.IsZero() {
  1153. yDate = maxDate.Format(utils.FormatDate)
  1154. }
  1155. yDataList = append(yDataList, models.YData{
  1156. Date: yDate,
  1157. Value: findDataList,
  1158. Color: barChartInfoDate.Color,
  1159. Name: barChartInfoDate.Name,
  1160. })
  1161. }
  1162. return
  1163. }
  1164. // ChartInfoRefreshV2 图表刷新
  1165. // @author Roc
  1166. // @datetime 2022-09-16 10:15:38
  1167. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1168. func ChartInfoRefreshV2(chartInfoId int) (err error) {
  1169. var errmsg string
  1170. defer func() {
  1171. if err != nil {
  1172. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1173. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1174. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1175. }
  1176. }()
  1177. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1178. if err != nil {
  1179. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1180. return
  1181. }
  1182. edbIdList := make([]int, 0)
  1183. for _, v := range edbMappingList {
  1184. edbIdList = append(edbIdList, v.EdbInfoId)
  1185. }
  1186. // 批量刷新
  1187. err, errmsg = EdbInfoRefreshAllFromBase(edbIdList, false)
  1188. if err != nil {
  1189. return
  1190. }
  1191. return
  1192. }
  1193. // GetSectionScatterChartData 柱方图的数据处理
  1194. func GetSectionScatterChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  1195. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1196. edbDataMap := make(map[int]map[string]float64)
  1197. for edbInfoId, edbDataList := range edbDataListMap {
  1198. edbDateData := make(map[string]float64)
  1199. for _, edbData := range edbDataList {
  1200. edbDateData[edbData.DataTime] = edbData.Value
  1201. }
  1202. edbDataMap[edbInfoId] = edbDateData
  1203. }
  1204. // edbIdList 指标展示顺序;x轴的指标顺序
  1205. edbIdList = make([]int, 0)
  1206. edbMappingMap := make(map[int]*models.ChartEdbInfoMapping)
  1207. for _, v := range mappingList {
  1208. edbIdList = append(edbIdList, v.EdbInfoId)
  1209. edbMappingMap[v.EdbInfoId] = v
  1210. }
  1211. //SectionScatterSeriesInfoResp
  1212. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1213. for _, seriesItem := range extraConfig.SeriesList {
  1214. var maxDate time.Time
  1215. // 系列中的指标数据
  1216. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  1217. var minXVal, maxXVal, minYVal, maxYVal float64
  1218. for _, edbConf := range seriesItem.EdbInfoList {
  1219. tmpItem := data_manage.SectionScatterEdbItemResp{
  1220. IsShow: edbConf.IsShow,
  1221. Name: edbConf.Name,
  1222. NameEn: edbConf.NameEn,
  1223. } //单个坐标点的数据
  1224. //X轴的数据
  1225. {
  1226. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1227. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1228. if !ok {
  1229. continue
  1230. }
  1231. findDate := edbConf.XDate //需要的日期值
  1232. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1233. if len(dataList) <= 0 {
  1234. // 没有数据的指标id
  1235. //findDataList = append(findDataList, 0)
  1236. continue
  1237. }
  1238. tmpItem.XEdbInfoId = edbInfoId
  1239. tmpItem.XName = edbMappingInfo.EdbName
  1240. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1241. switch edbConf.XDateType {
  1242. case 1: //最新值
  1243. dataList := edbDataListMap[edbInfoId]
  1244. findDate = dataList[len(dataList)-1].DataTime
  1245. case 2: //近期几天
  1246. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1247. if tmpErr != nil {
  1248. err = tmpErr
  1249. return
  1250. }
  1251. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1252. lenData := len(dataList) - 1
  1253. for i := lenData; i >= 0; i-- {
  1254. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1255. if tmpErr != nil {
  1256. err = tmpErr
  1257. return
  1258. }
  1259. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1260. findDate = dataList[i].DataTime
  1261. break
  1262. }
  1263. }
  1264. case 3: // 固定日期
  1265. //最早的日期
  1266. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1267. if tmpErr != nil {
  1268. err = tmpErr
  1269. return
  1270. }
  1271. //寻找固定日期的数据
  1272. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1273. if tmpErr != nil {
  1274. err = tmpErr
  1275. return
  1276. }
  1277. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1278. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1279. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1280. findDate = tmpDate
  1281. break
  1282. }
  1283. }
  1284. default:
  1285. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1286. return
  1287. }
  1288. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1289. if maxDate.IsZero() {
  1290. maxDate = findDateTime
  1291. } else {
  1292. if findDateTime.After(maxDate) {
  1293. maxDate = findDateTime
  1294. }
  1295. }
  1296. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1297. tmpItem.XDate = findDate
  1298. tmpItem.XValue = tmpValue
  1299. } else {
  1300. continue
  1301. }
  1302. }
  1303. //Y轴的数据
  1304. {
  1305. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1306. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1307. if !ok {
  1308. continue
  1309. }
  1310. findDate := edbConf.YDate //需要的日期值
  1311. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1312. if len(dataList) <= 0 {
  1313. // 没有数据的指标id
  1314. //findDataList = append(findDataList, 0)
  1315. continue
  1316. }
  1317. tmpItem.YEdbInfoId = edbInfoId
  1318. tmpItem.YName = edbMappingInfo.EdbName
  1319. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1320. switch edbConf.YDateType {
  1321. case 1: //最新值
  1322. dataList := edbDataListMap[edbInfoId]
  1323. findDate = dataList[len(dataList)-1].DataTime
  1324. case 2: //近期几天
  1325. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1326. if tmpErr != nil {
  1327. err = tmpErr
  1328. return
  1329. }
  1330. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1331. lenData := len(dataList) - 1
  1332. for i := lenData; i >= 0; i-- {
  1333. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1334. if tmpErr != nil {
  1335. err = tmpErr
  1336. return
  1337. }
  1338. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1339. findDate = dataList[i].DataTime
  1340. break
  1341. }
  1342. }
  1343. case 3: // 固定日期
  1344. //最早的日期
  1345. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1346. if tmpErr != nil {
  1347. err = tmpErr
  1348. return
  1349. }
  1350. //寻找固定日期的数据
  1351. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1352. if tmpErr != nil {
  1353. err = tmpErr
  1354. return
  1355. }
  1356. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1357. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1358. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1359. findDate = tmpDate
  1360. break
  1361. }
  1362. }
  1363. default:
  1364. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1365. return
  1366. }
  1367. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1368. if maxDate.IsZero() {
  1369. maxDate = findDateTime
  1370. } else {
  1371. if findDateTime.After(maxDate) {
  1372. maxDate = findDateTime
  1373. }
  1374. }
  1375. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1376. tmpItem.YDate = findDate
  1377. tmpItem.YValue = tmpValue
  1378. } else {
  1379. continue
  1380. }
  1381. }
  1382. // 获取当前系列的X轴的最大最小值
  1383. {
  1384. if tmpItem.XValue < minXVal {
  1385. minXVal = tmpItem.XValue
  1386. }
  1387. if tmpItem.XValue > maxXVal {
  1388. maxXVal = tmpItem.XValue
  1389. }
  1390. if tmpItem.YValue < minYVal {
  1391. minYVal = tmpItem.YValue
  1392. }
  1393. if tmpItem.YValue > maxYVal {
  1394. maxYVal = tmpItem.YValue
  1395. }
  1396. }
  1397. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1398. }
  1399. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1400. var trendLine, rSquare string
  1401. // 生成线性方程式
  1402. var a, b float64
  1403. {
  1404. coordinateData := make([]utils.Coordinate, 0)
  1405. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1406. tmpCoordinate1 := utils.Coordinate{
  1407. X: tmpSeriesEdbInfo.XValue,
  1408. Y: tmpSeriesEdbInfo.YValue,
  1409. }
  1410. coordinateData = append(coordinateData, tmpCoordinate1)
  1411. }
  1412. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1413. if len(coordinateData) >= 2 {
  1414. a, b = utils.GetLinearResult(coordinateData)
  1415. if !math.IsNaN(a) || !math.IsNaN(b) {
  1416. if b > 0 {
  1417. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1418. } else {
  1419. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1420. }
  1421. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1422. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1423. }
  1424. // 计算R平方
  1425. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1426. }
  1427. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  1428. X: minXVal,
  1429. Y: minYVal,
  1430. }, data_manage.CoordinatePoint{
  1431. X: maxXVal,
  1432. Y: maxYVal,
  1433. })
  1434. }
  1435. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  1436. Name: seriesItem.Name,
  1437. NameEn: seriesItem.NameEn,
  1438. Color: seriesItem.Color,
  1439. EdbInfoList: tmpSeriesEdbInfoList,
  1440. ShowTrendLine: seriesItem.ShowTrendLine,
  1441. ShowFitEquation: seriesItem.ShowFitEquation,
  1442. ShowRSquare: seriesItem.ShowRSquare,
  1443. TrendLine: trendLine,
  1444. RSquare: rSquare,
  1445. TrendLimitData: trendLimitData,
  1446. })
  1447. }
  1448. chartDataResp = data_manage.SectionScatterInfoResp{
  1449. XName: extraConfig.XName,
  1450. XNameEn: extraConfig.XNameEn,
  1451. XUnitName: extraConfig.XUnitName,
  1452. XUnitNameEn: extraConfig.XUnitNameEn,
  1453. YName: extraConfig.YName,
  1454. YNameEn: extraConfig.YNameEn,
  1455. YUnitName: extraConfig.YUnitName,
  1456. YUnitNameEn: extraConfig.YUnitNameEn,
  1457. XMinValue: extraConfig.XMinValue,
  1458. XMaxValue: extraConfig.XMaxValue,
  1459. YMinValue: extraConfig.YMinValue,
  1460. YMaxValue: extraConfig.YMaxValue,
  1461. DataList: dataListResp,
  1462. }
  1463. return
  1464. }
  1465. // RadarChartData 雷达图的数据处理
  1466. func RadarChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, extraConfig data_manage.RadarChartInfoReq) (edbIdList []int, chartDataResp data_manage.RadarChartInfoResp, err error) {
  1467. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1468. edbDataMap := make(map[int]map[string]float64)
  1469. for edbInfoId, edbDataList := range edbDataListMap {
  1470. edbDateData := make(map[string]float64)
  1471. for _, edbData := range edbDataList {
  1472. edbDateData[edbData.DataTime] = edbData.Value
  1473. }
  1474. edbDataMap[edbInfoId] = edbDateData
  1475. }
  1476. // edbIdList 指标展示顺序;x轴的指标顺序
  1477. edbIdList = make([]int, 0)
  1478. for _, v := range mappingList {
  1479. edbIdList = append(edbIdList, v.EdbInfoId)
  1480. }
  1481. chartDateList := extraConfig.DateList
  1482. yDataList := make([]data_manage.RadarYData, 0) //y轴的数据列表
  1483. for _, chartDate := range chartDateList {
  1484. var maxDate time.Time
  1485. findDataList := make([]float64, 0) // 当前日期的数据值
  1486. for _, edbInfoId := range edbIdList {
  1487. findDate := chartDate.Date //需要的日期值
  1488. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1489. if len(dataList) <= 0 {
  1490. // 没有数据的指标id
  1491. findDataList = append(findDataList, 0)
  1492. continue
  1493. }
  1494. switch chartDate.Type {
  1495. case 1: //最新值
  1496. findDate = dataList[len(dataList)-1].DataTime
  1497. case 2: //近期几天
  1498. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1499. if tmpErr != nil {
  1500. err = tmpErr
  1501. return
  1502. }
  1503. findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)
  1504. lenData := len(dataList) - 1
  1505. for i := lenData; i >= 0; i-- {
  1506. currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1507. if e != nil {
  1508. err = e
  1509. return
  1510. }
  1511. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1512. findDate = dataList[i].DataTime
  1513. break
  1514. }
  1515. }
  1516. case 3: // 固定日期
  1517. //最早的日期
  1518. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1519. if tmpErr != nil {
  1520. err = tmpErr
  1521. return
  1522. }
  1523. //寻找固定日期的数据
  1524. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
  1525. if tmpErr != nil {
  1526. err = tmpErr
  1527. return
  1528. }
  1529. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1530. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1531. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1532. findDate = tmpDate
  1533. break
  1534. }
  1535. }
  1536. default:
  1537. err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
  1538. return
  1539. }
  1540. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1541. if maxDate.IsZero() {
  1542. maxDate = findDateTime
  1543. } else {
  1544. if findDateTime.After(maxDate) {
  1545. maxDate = findDateTime
  1546. }
  1547. }
  1548. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1549. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  1550. findDataList = append(findDataList, tmpValue)
  1551. } else {
  1552. findDataList = append(findDataList, 0)
  1553. }
  1554. }
  1555. yDate := "0000-00-00"
  1556. if !maxDate.IsZero() {
  1557. yDate = maxDate.Format(utils.FormatDate)
  1558. }
  1559. yDataList = append(yDataList, data_manage.RadarYData{
  1560. Date: yDate,
  1561. Value: findDataList,
  1562. Color: chartDate.Color,
  1563. Name: chartDate.Name,
  1564. })
  1565. }
  1566. chartDataResp = data_manage.RadarChartInfoResp{
  1567. YDataList: yDataList,
  1568. XEdbIdValue: edbIdList,
  1569. }
  1570. return
  1571. }
  1572. // GetChartEdbDataV2 获取图表的指标数据
  1573. 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) {
  1574. edbList = make([]*models.ChartEdbInfoMapping, 0)
  1575. xEdbIdValue = make([]int, 0)
  1576. yDataList = make([]models.YData, 0)
  1577. var extraConfig interface{}
  1578. switch chartType {
  1579. case 6:
  1580. var tmpConfig models.ChartTimeCombineExtraConf
  1581. if extraConfigStr != `` {
  1582. err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
  1583. if err != nil {
  1584. errMsg = "雷达图配置异常"
  1585. err = errors.New(errMsg)
  1586. return
  1587. }
  1588. }
  1589. extraConfig = tmpConfig
  1590. case 7: // 柱形图
  1591. var barConfig data_manage.BarChartInfoReq
  1592. if extraConfigStr == `` {
  1593. errMsg = "柱方图未配置"
  1594. err = errors.New(errMsg)
  1595. return
  1596. }
  1597. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  1598. if err != nil {
  1599. errMsg = "柱方图配置异常"
  1600. err = errors.New(errMsg)
  1601. return
  1602. }
  1603. extraConfig = barConfig
  1604. case 10: // 截面散点图
  1605. var tmpExtraConfig data_manage.SectionScatterReq
  1606. if extraConfigStr == `` {
  1607. errMsg = "截面散点图未配置"
  1608. err = errors.New(errMsg)
  1609. return
  1610. }
  1611. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  1612. if err != nil {
  1613. errMsg = "截面散点配置异常"
  1614. err = errors.New(errMsg)
  1615. return
  1616. }
  1617. extraConfig = tmpExtraConfig
  1618. case utils.CHART_TYPE_RADAR:
  1619. var barConfig data_manage.RadarChartInfoReq
  1620. if extraConfigStr == `` {
  1621. errMsg = "雷达图未配置"
  1622. err = errors.New(errMsg)
  1623. return
  1624. }
  1625. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  1626. if err != nil {
  1627. errMsg = "雷达图配置异常"
  1628. err = errors.New(errMsg)
  1629. return
  1630. }
  1631. extraConfig = barConfig
  1632. case utils.CHART_TYPE_SECTION_COMBINE:
  1633. // 预览和详情都走这个接口
  1634. var sectionExtraConfig models.ChartSectionAllExtraConf
  1635. if extraConfigStr == `` {
  1636. errMsg = "截面组合图未配置"
  1637. err = errors.New(errMsg)
  1638. return
  1639. }
  1640. if chartInfoId > 0 {
  1641. var sectionExtraConfigTmp models.ChartSectionExtraConf
  1642. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfigTmp)
  1643. if err != nil {
  1644. errMsg = "截面组合图配置异常"
  1645. err = errors.New(errMsg)
  1646. return
  1647. }
  1648. sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
  1649. sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
  1650. sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
  1651. sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
  1652. sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
  1653. sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
  1654. // 查询所有的seriesEdb
  1655. seriesEdbList, e := models.GetChartSeriesEdbByChartInfoId(chartInfoId)
  1656. if e != nil {
  1657. errMsg = "查询seriesEdb失败"
  1658. err = errors.New(errMsg + e.Error())
  1659. return
  1660. }
  1661. // todo 是否有必要返回单位信息
  1662. // 组装成map
  1663. seriesEdbMap := make(map[int][]*models.ChartSectionSeriesEdbConf)
  1664. for _, v := range seriesEdbList {
  1665. var dateConf *models.ChartSectionSeriesDateConfItem
  1666. if v.DateConf != "" {
  1667. err = json.Unmarshal([]byte(v.DateConf), &dateConf)
  1668. if err != nil {
  1669. errMsg = "截面组合图配置异常"
  1670. err = errors.New(errMsg + err.Error())
  1671. return
  1672. }
  1673. }
  1674. tmp := &models.ChartSectionSeriesEdbConf{
  1675. ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
  1676. ChartSeriesId: v.ChartSeriesId,
  1677. //ChartInfoId: v.ChartInfoId,
  1678. EdbInfoId: v.EdbInfoId,
  1679. DateConf: dateConf,
  1680. EdbName: "",
  1681. EdbNameEn: "",
  1682. Unit: "",
  1683. UnitEn: "",
  1684. DateConfName: v.DateConfName,
  1685. DateConfType: v.DateConfType,
  1686. }
  1687. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
  1688. }
  1689. //查询series
  1690. seriesListTmp, e := models.GetChartSeriesByChartInfoId(chartInfoId)
  1691. if e != nil {
  1692. errMsg = "查询series失败"
  1693. err = errors.New(errMsg + e.Error())
  1694. return
  1695. }
  1696. seriesList := make([]*models.ChartSectionSeriesItem, 0)
  1697. for _, v := range seriesListTmp {
  1698. tmpSeries := &models.ChartSectionSeriesItem{
  1699. ChartSeriesId: v.ChartSeriesId,
  1700. SeriesName: v.SeriesName,
  1701. SeriesNameEn: v.SeriesNameEn,
  1702. ChartStyle: v.ChartStyle,
  1703. ChartColor: v.ChartColor,
  1704. ChartWidth: v.ChartWidth,
  1705. IsPoint: v.IsPoint,
  1706. IsNumber: v.IsNumber,
  1707. IsAxis: v.IsAxis,
  1708. MaxData: v.MaxData,
  1709. MinData: v.MinData,
  1710. //IsOrder: false,
  1711. EdbInfoList: nil,
  1712. DataList: nil,
  1713. }
  1714. edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
  1715. if ok {
  1716. tmpSeries.EdbInfoList = edbInfoList
  1717. }
  1718. seriesList = append(seriesList, tmpSeries)
  1719. }
  1720. sectionExtraConfig.SeriesList = seriesList
  1721. } else {
  1722. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  1723. if err != nil {
  1724. errMsg = "截面组合图配置异常"
  1725. err = errors.New(errMsg)
  1726. return
  1727. }
  1728. }
  1729. //校验引用日期名称是否重复
  1730. dateNameMap := make(map[string]int)
  1731. dateNameEnMap := make(map[string]int)
  1732. for _, v := range sectionExtraConfig.DateConfList {
  1733. if _, ok := dateNameMap[v.DateConfName]; ok {
  1734. errMsg = "截面组合图引用日期名称设置重复"
  1735. err = errors.New(errMsg + v.DateConfName)
  1736. return
  1737. }
  1738. if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
  1739. errMsg = "截面组合图引用日期名称设置重复"
  1740. err = errors.New(errMsg + v.DateConfNameEn)
  1741. return
  1742. }
  1743. if v.DateConfName != "" {
  1744. dateNameMap[v.DateConfName] = 1
  1745. }
  1746. if v.DateConfNameEn != "" {
  1747. dateNameEnMap[v.DateConfNameEn] = 1
  1748. }
  1749. }
  1750. //检查系列名称是否重复
  1751. seriesNameMap := make(map[string]int)
  1752. seriesNameEnMap := make(map[string]int)
  1753. for _, v := range sectionExtraConfig.SeriesList {
  1754. if _, ok := seriesNameMap[v.SeriesName]; ok {
  1755. errMsg = "截面组合图系列名称设置重复"
  1756. err = errors.New(errMsg + v.SeriesName)
  1757. return
  1758. }
  1759. if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
  1760. errMsg = "截面组合图系列名称设置重复"
  1761. err = errors.New(errMsg + v.SeriesNameEn)
  1762. return
  1763. }
  1764. if v.SeriesName != "" {
  1765. seriesNameMap[v.SeriesName] = 1
  1766. }
  1767. if v.SeriesNameEn != "" {
  1768. seriesNameEnMap[v.SeriesNameEn] = 1
  1769. }
  1770. }
  1771. // 检查基准系列是否设置
  1772. if sectionExtraConfig.BaseChartSeriesName == "" {
  1773. errMsg = "截面组合图基准系列名称未设置"
  1774. err = errors.New(errMsg)
  1775. return
  1776. }
  1777. //todo 如果是详情接口,应该要从其他表里查询配置
  1778. extraConfig = sectionExtraConfig
  1779. default:
  1780. xEdbIdValue = make([]int, 0)
  1781. yDataList = make([]models.YData, 0)
  1782. }
  1783. // 指标对应的所有数据
  1784. edbDataListMap, edbList, err := chartInfoData.GetEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  1785. if err != nil {
  1786. return
  1787. }
  1788. // 特殊图形数据处理
  1789. switch chartType {
  1790. case 6: //时序组合图
  1791. //判断是否堆积
  1792. timeConf := extraConfig.(models.ChartTimeCombineExtraConf)
  1793. if extraConfigStr == "" { //历史数据,默认开启堆积
  1794. timeConf = models.ChartTimeCombineExtraConf{
  1795. IsHeap: 1,
  1796. }
  1797. }
  1798. dataResp = models.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
  1799. case 7: // 柱形图
  1800. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  1801. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  1802. for _, v := range edbList {
  1803. // 指标别名
  1804. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  1805. for _, reqEdb := range barChartConf.EdbInfoIdList {
  1806. if v.EdbInfoId == reqEdb.EdbInfoId {
  1807. v.EdbAliasName = reqEdb.Name
  1808. }
  1809. }
  1810. }
  1811. }
  1812. case 10: // 截面散点图
  1813. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  1814. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  1815. case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
  1816. sectionConf := extraConfig.(models.ChartSectionAllExtraConf)
  1817. var chartInfo *models.ChartInfo
  1818. if chartInfoId > 0 {
  1819. chartInfo, err = models.GetChartInfoById(chartInfoId)
  1820. if err != nil {
  1821. errMsg = "获取图表信息失败"
  1822. return
  1823. }
  1824. }
  1825. xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)
  1826. // 这个数据没有必要返回给前端
  1827. for _, v := range edbList {
  1828. v.DataList = nil
  1829. }
  1830. case utils.CHART_TYPE_RADAR: //雷达图
  1831. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  1832. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  1833. }
  1834. return
  1835. }
  1836. // SeasonChartData 季节性图的数据处理
  1837. func SeasonChartData(dataList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (dataResp models.SeasonChartResp, err error) {
  1838. var seasonConfig models.SeasonExtraItem
  1839. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonConfig)
  1840. if err != nil {
  1841. err = errors.New("季节性图配置异常, Err:" + err.Error())
  1842. return
  1843. }
  1844. for _, mappingItem := range dataList {
  1845. if mappingItem.IsAxis == 0 {
  1846. continue
  1847. }
  1848. quarterDataList, ok := mappingItem.DataList.(models.QuarterDataList)
  1849. if !ok {
  1850. continue
  1851. }
  1852. // 上下限区间
  1853. if seasonConfig.MaxMinLimits.Year > 0 {
  1854. startYear := time.Now().AddDate(-seasonConfig.MaxMinLimits.Year, 0, 0).Year()
  1855. dataResp.MaxMinLimits.List = make([]*models.MaxMinLimitsData, 0)
  1856. dataTimeMap := make(map[time.Time]time.Time)
  1857. dataTimeList := make([]string, 0)
  1858. maxValueMap := make(map[time.Time]float64)
  1859. minValueMap := make(map[time.Time]float64)
  1860. maxMinDataList := make([]*models.MaxMinLimitsData, 0)
  1861. var startDateStr string
  1862. var endDateStr string
  1863. var newDate time.Time
  1864. startDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XStartDate)
  1865. endDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XEndDate)
  1866. startDate, e := time.Parse(utils.FormatDate, startDateStr)
  1867. if e != nil {
  1868. err = e
  1869. return
  1870. }
  1871. endDate, e := time.Parse(utils.FormatDate, endDateStr)
  1872. if e != nil {
  1873. err = e
  1874. return
  1875. }
  1876. // 日度 周度插值
  1877. for _, v := range quarterDataList {
  1878. if mappingItem.Frequency == "日度" || mappingItem.Frequency == "周度" {
  1879. handleDataMap := make(map[string]float64)
  1880. dataTimeList, _, err = HandleDataByLinearRegressionToList(v.DataList, handleDataMap)
  1881. if err != nil {
  1882. err = errors.New("插值处理数据异常, Err:" + err.Error())
  1883. return
  1884. }
  1885. // 不包含当年
  1886. if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
  1887. continue
  1888. }
  1889. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  1890. chartLegend,_ := strconv.Atoi(v.ChartLegend)
  1891. if chartLegend < startYear {
  1892. continue
  1893. }
  1894. for _, date := range dataTimeList {
  1895. dateTime, e := time.Parse(utils.FormatDate, date)
  1896. if e != nil {
  1897. err = errors.New("时间格式化异常, Err:" + e.Error())
  1898. return
  1899. }
  1900. if dateTime.Year() < startYear {
  1901. continue
  1902. }
  1903. // 不包含2月29号
  1904. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  1905. continue
  1906. }
  1907. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  1908. if seasonConfig.JumpYear == 1 {
  1909. if startDate.After(endDate) {
  1910. // 如果跨年且不到一年
  1911. // 全年截取一部分
  1912. if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
  1913. continue
  1914. }
  1915. if newDate.After(startDate.AddDate(0, 0, -1)) {
  1916. // 减一年
  1917. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
  1918. }
  1919. // 处理上下限列表
  1920. if value, ok := maxValueMap[newDate]; ok {
  1921. if value < handleDataMap[date] {
  1922. maxValueMap[newDate] = handleDataMap[date]
  1923. }
  1924. } else {
  1925. maxValueMap[newDate] = handleDataMap[date]
  1926. }
  1927. if value, ok := minValueMap[newDate]; ok {
  1928. if value > handleDataMap[date] {
  1929. minValueMap[newDate] = handleDataMap[date]
  1930. }
  1931. } else {
  1932. minValueMap[newDate] = handleDataMap[date]
  1933. }
  1934. dataTimeMap[newDate] = newDate
  1935. } else {
  1936. // 如果跨年且大于等于一年
  1937. // double后截取
  1938. if newDate.After(startDate) {
  1939. // 处理上下限列表
  1940. if value, ok := maxValueMap[newDate]; ok {
  1941. if value < handleDataMap[date] {
  1942. maxValueMap[newDate] = handleDataMap[date]
  1943. }
  1944. } else {
  1945. maxValueMap[newDate] = handleDataMap[date]
  1946. }
  1947. if value, ok := minValueMap[newDate]; ok {
  1948. if value > handleDataMap[date] {
  1949. minValueMap[newDate] = handleDataMap[date]
  1950. }
  1951. } else {
  1952. minValueMap[newDate] = handleDataMap[date]
  1953. }
  1954. dataTimeMap[newDate] = newDate
  1955. }
  1956. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
  1957. newEndDate := endDate.AddDate(1, 0, 0)
  1958. if newDate.Before(newEndDate) {
  1959. // 处理上下限列表
  1960. if value, ok := maxValueMap[newDate]; ok {
  1961. if value < handleDataMap[date] {
  1962. maxValueMap[newDate] = handleDataMap[date]
  1963. }
  1964. } else {
  1965. maxValueMap[newDate] = handleDataMap[date]
  1966. }
  1967. if value, ok := minValueMap[newDate]; ok {
  1968. if value > handleDataMap[date] {
  1969. minValueMap[newDate] = handleDataMap[date]
  1970. }
  1971. } else {
  1972. minValueMap[newDate] = handleDataMap[date]
  1973. }
  1974. dataTimeMap[newDate] = newDate
  1975. }
  1976. }
  1977. } else {
  1978. // 如果不跨年 正常获取
  1979. // 获取当前日期所在区间
  1980. // 处理上下限列表
  1981. if value, ok := maxValueMap[newDate]; ok {
  1982. if value < handleDataMap[date] {
  1983. maxValueMap[newDate] = handleDataMap[date]
  1984. }
  1985. } else {
  1986. maxValueMap[newDate] = handleDataMap[date]
  1987. }
  1988. if value, ok := minValueMap[newDate]; ok {
  1989. if value > handleDataMap[date] {
  1990. minValueMap[newDate] = handleDataMap[date]
  1991. }
  1992. } else {
  1993. minValueMap[newDate] = handleDataMap[date]
  1994. }
  1995. dataTimeMap[newDate] = newDate
  1996. }
  1997. }
  1998. } else {
  1999. // 旬度、月度、季度、半年度 不插值,需要先把日期列表和数据map取出来
  2000. for _, vv := range v.DataList {
  2001. dateTime, e := time.Parse(utils.FormatDate, vv.DataTime)
  2002. if e != nil {
  2003. err = errors.New("时间格式化异常, Err:" + e.Error())
  2004. return
  2005. }
  2006. // 不包含当年
  2007. if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
  2008. continue
  2009. }
  2010. if dateTime.Year() < startYear {
  2011. continue
  2012. }
  2013. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  2014. chartLegend,_ := strconv.Atoi(v.ChartLegend)
  2015. if chartLegend < startYear {
  2016. continue
  2017. }
  2018. // 月度的2月29号日期改为2月28日
  2019. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  2020. dateTime = dateTime.AddDate(0, 0, -1)
  2021. }
  2022. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  2023. if seasonConfig.JumpYear == 1 {
  2024. if startDate.After(endDate) {
  2025. // 如果跨年且不到一年
  2026. // 全年截取一部分
  2027. if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
  2028. continue
  2029. }
  2030. if newDate.After(startDate.AddDate(0, 0, -1)) {
  2031. // 减一年
  2032. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
  2033. }
  2034. // 处理上下限列表
  2035. if value, ok := maxValueMap[newDate]; ok {
  2036. if value < vv.Value {
  2037. maxValueMap[newDate] = vv.Value
  2038. }
  2039. } else {
  2040. maxValueMap[newDate] = vv.Value
  2041. }
  2042. if value, ok := minValueMap[newDate]; ok {
  2043. if value > vv.Value {
  2044. minValueMap[newDate] = vv.Value
  2045. }
  2046. } else {
  2047. minValueMap[newDate] = vv.Value
  2048. }
  2049. dataTimeMap[newDate] = newDate
  2050. } else {
  2051. // 如果跨年且大于等于一年
  2052. // double后截取
  2053. if newDate.After(startDate) {
  2054. // 处理上下限列表
  2055. if value, ok := maxValueMap[newDate]; ok {
  2056. if value < vv.Value {
  2057. maxValueMap[newDate] = vv.Value
  2058. }
  2059. } else {
  2060. maxValueMap[newDate] = vv.Value
  2061. }
  2062. if value, ok := minValueMap[newDate]; ok {
  2063. if value > vv.Value {
  2064. minValueMap[newDate] = vv.Value
  2065. }
  2066. } else {
  2067. minValueMap[newDate] = vv.Value
  2068. }
  2069. dataTimeMap[newDate] = newDate
  2070. }
  2071. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
  2072. newEndDate := endDate.AddDate(1, 0, 0)
  2073. if newDate.Before(newEndDate) {
  2074. // 处理上下限列表
  2075. if value, ok := maxValueMap[newDate]; ok {
  2076. if value < vv.Value {
  2077. maxValueMap[newDate] = vv.Value
  2078. }
  2079. } else {
  2080. maxValueMap[newDate] = vv.Value
  2081. }
  2082. if value, ok := minValueMap[newDate]; ok {
  2083. if value > vv.Value {
  2084. minValueMap[newDate] = vv.Value
  2085. }
  2086. } else {
  2087. minValueMap[newDate] = vv.Value
  2088. }
  2089. dataTimeMap[newDate] = newDate
  2090. }
  2091. }
  2092. } else {
  2093. // 如果不跨年 正常获取
  2094. // 获取当前日期所在区间
  2095. // 处理上下限列表
  2096. if value, ok := maxValueMap[newDate]; ok {
  2097. if value < vv.Value {
  2098. maxValueMap[newDate] = vv.Value
  2099. }
  2100. } else {
  2101. maxValueMap[newDate] = vv.Value
  2102. }
  2103. if value, ok := minValueMap[newDate]; ok {
  2104. if value > vv.Value {
  2105. minValueMap[newDate] = vv.Value
  2106. }
  2107. } else {
  2108. minValueMap[newDate] = vv.Value
  2109. }
  2110. dataTimeMap[newDate] = newDate
  2111. }
  2112. }
  2113. }
  2114. }
  2115. for _, v := range dataTimeMap {
  2116. maxMinItem := &models.MaxMinLimitsData{}
  2117. if maxValue, ok := maxValueMap[v]; ok {
  2118. maxMinItem.MaxValue = maxValue
  2119. } else {
  2120. maxMinItem.MaxValue = minValueMap[v]
  2121. }
  2122. if minValue, ok := minValueMap[v]; ok {
  2123. maxMinItem.MinValue = minValue
  2124. } else {
  2125. maxMinItem.MinValue = maxValueMap[v]
  2126. }
  2127. maxMinItem.DataTime = v.Format(utils.FormatDate)
  2128. maxMinItem.DataTimestamp = v.UnixNano() / 1e6
  2129. maxMinDataList = append(maxMinDataList, maxMinItem)
  2130. }
  2131. // 排序
  2132. sort.Slice(maxMinDataList, func(i, j int) bool {
  2133. return maxMinDataList[i].DataTime < maxMinDataList[j].DataTime
  2134. })
  2135. dataResp.MaxMinLimits.List = maxMinDataList
  2136. dataResp.MaxMinLimits.Color = seasonConfig.MaxMinLimits.Color
  2137. dataResp.MaxMinLimits.Legend = seasonConfig.MaxMinLimits.Legend
  2138. dataResp.MaxMinLimits.IsShow = seasonConfig.MaxMinLimits.IsShow
  2139. dataResp.MaxMinLimits.IsAdd = seasonConfig.MaxMinLimits.IsAdd
  2140. dataResp.MaxMinLimits.Year = seasonConfig.MaxMinLimits.Year
  2141. }
  2142. // 自定义同期均线
  2143. if seasonConfig.SamePeriodAverage.Year > 0 {
  2144. handleDataMap := make(map[string]float64)
  2145. dataTimeMap := make(map[time.Time]time.Time)
  2146. dataTimeList := make([]string, 0)
  2147. valueMap := make(map[time.Time]float64)
  2148. averageDataList := make([]*models.SamePeriodAverageData, 0)
  2149. dateNumMap := make(map[time.Time]float64)
  2150. for i := len(quarterDataList) - 1; i >= 0; i-- {
  2151. // 插值成日度
  2152. dataTimeList, _, err = HandleDataByLinearRegressionToList(quarterDataList[i].DataList, handleDataMap)
  2153. if err != nil {
  2154. err = errors.New("插值处理数据异常, Err:" + err.Error())
  2155. return
  2156. }
  2157. for _, date := range dataTimeList {
  2158. dateTime, e := time.Parse(utils.FormatDate, date)
  2159. if e != nil {
  2160. err = errors.New("时间格式化异常, Err:" + e.Error())
  2161. return
  2162. }
  2163. startYear := time.Now().AddDate(-seasonConfig.SamePeriodAverage.Year, 0, 0).Year()
  2164. if dateTime.Year() < startYear {
  2165. continue
  2166. }
  2167. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  2168. chartLegend,_ := strconv.Atoi(quarterDataList[i].ChartLegend)
  2169. if chartLegend < startYear {
  2170. continue
  2171. }
  2172. // 不包含2月29号
  2173. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  2174. continue
  2175. }
  2176. // 不包含当年
  2177. if quarterDataList[i].ChartLegend == strconv.Itoa(time.Now().Year()) {
  2178. continue
  2179. }
  2180. newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  2181. // 处理均值
  2182. if value, ok := valueMap[newDate]; ok {
  2183. valueMap[newDate] = handleDataMap[date] + value
  2184. } else {
  2185. valueMap[newDate] = handleDataMap[date]
  2186. }
  2187. dateNumMap[newDate] += 1
  2188. dataTimeMap[newDate] = newDate
  2189. }
  2190. }
  2191. //year := float64(seasonConfig.SamePeriodAverage.Year)
  2192. for _, v := range dataTimeMap {
  2193. averageItem := &models.SamePeriodAverageData{}
  2194. year := dateNumMap[v]
  2195. if value, ok := valueMap[v]; ok {
  2196. if year > 0 {
  2197. averageItem.Value = value / year
  2198. } else {
  2199. averageItem.Value = value
  2200. }
  2201. }
  2202. averageItem.DataTime = v.Format(utils.FormatDate)
  2203. averageItem.DataTimestamp = v.UnixNano() / 1e6
  2204. averageDataList = append(averageDataList, averageItem)
  2205. }
  2206. // 排序
  2207. sort.Slice(averageDataList, func(i, j int) bool {
  2208. return averageDataList[i].DataTime < averageDataList[j].DataTime
  2209. })
  2210. dataResp.SamePeriodAverage.List = averageDataList
  2211. dataResp.SamePeriodAverage.Year = seasonConfig.SamePeriodAverage.Year
  2212. dataResp.SamePeriodAverage.Color = seasonConfig.SamePeriodAverage.Color
  2213. dataResp.SamePeriodAverage.Legend = seasonConfig.SamePeriodAverage.Legend
  2214. dataResp.SamePeriodAverage.IsShow = seasonConfig.SamePeriodAverage.IsShow
  2215. dataResp.SamePeriodAverage.LineType = seasonConfig.SamePeriodAverage.LineType
  2216. dataResp.SamePeriodAverage.LineWidth = seasonConfig.SamePeriodAverage.LineWidth
  2217. dataResp.SamePeriodAverage.IsAdd = seasonConfig.SamePeriodAverage.IsAdd
  2218. }
  2219. // 自定义同期标准差
  2220. if seasonConfig.SamePeriodStandardDeviation.Year > 1 && seasonConfig.SamePeriodStandardDeviation.Multiple > 0 {
  2221. startYear := time.Now().AddDate(-seasonConfig.SamePeriodStandardDeviation.Year, 0, 0).Year()
  2222. // 先算均值,再算标准差
  2223. handleDataMap := make(map[string]float64)
  2224. dataTimeMap := make(map[time.Time]time.Time)
  2225. dataTimeValueMap := make(map[time.Time][]float64)
  2226. dataTimeList := make([]string, 0)
  2227. valueMap := make(map[time.Time]float64)
  2228. samePeriodStandardDeviationList := make([]*models.MaxMinLimitsData, 0)
  2229. for i := len(quarterDataList) - 1; i > len(quarterDataList)-seasonConfig.SamePeriodAverage.Year-1 && i > 0; i-- {
  2230. // 插值成日度
  2231. dataTimeList, _, err = HandleDataByLinearRegressionToListV2(quarterDataList[i].DataList, handleDataMap)
  2232. if err != nil {
  2233. err = errors.New("插值处理数据异常, Err:" + err.Error())
  2234. return
  2235. }
  2236. for _, date := range dataTimeList {
  2237. dateTime, e := time.Parse(utils.FormatDate, date)
  2238. if e != nil {
  2239. err = errors.New("时间格式化异常, Err:" + e.Error())
  2240. return
  2241. }
  2242. if dateTime.Year() < startYear {
  2243. continue
  2244. }
  2245. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  2246. chartLegend,_ := strconv.Atoi(quarterDataList[i].ChartLegend)
  2247. if chartLegend < startYear {
  2248. continue
  2249. }
  2250. // 不包含2月29号
  2251. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  2252. continue
  2253. }
  2254. newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  2255. // 处理均值
  2256. if value, ok := valueMap[newDate]; ok {
  2257. valueMap[newDate] = (handleDataMap[date] + value) / 2
  2258. } else {
  2259. valueMap[newDate] = handleDataMap[date]
  2260. }
  2261. dataTimeMap[newDate] = newDate
  2262. valueList := dataTimeValueMap[newDate]
  2263. valueList = append(valueList, handleDataMap[date])
  2264. dataTimeValueMap[newDate] = valueList
  2265. }
  2266. }
  2267. for _, v := range dataTimeMap {
  2268. valueList := dataTimeValueMap[v]
  2269. stdev := utils.CalculateStandardDeviation(valueList)
  2270. stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
  2271. maxMinItem := &models.MaxMinLimitsData{}
  2272. if value, ok := valueMap[v]; ok {
  2273. maxMinItem.MaxValue = value + stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
  2274. maxMinItem.MinValue = value - stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
  2275. }
  2276. maxMinItem.DataTime = v.Format(utils.FormatDate)
  2277. maxMinItem.DataTimestamp = v.UnixNano() / 1e6
  2278. samePeriodStandardDeviationList = append(samePeriodStandardDeviationList, maxMinItem)
  2279. }
  2280. // 排序
  2281. sort.Slice(samePeriodStandardDeviationList, func(i, j int) bool {
  2282. return samePeriodStandardDeviationList[i].DataTime < samePeriodStandardDeviationList[j].DataTime
  2283. })
  2284. dataResp.SamePeriodStandardDeviation.List = samePeriodStandardDeviationList
  2285. dataResp.SamePeriodStandardDeviation.Color = seasonConfig.SamePeriodStandardDeviation.Color
  2286. dataResp.SamePeriodStandardDeviation.Legend = seasonConfig.SamePeriodStandardDeviation.Legend
  2287. dataResp.SamePeriodStandardDeviation.IsShow = seasonConfig.SamePeriodStandardDeviation.IsShow
  2288. dataResp.SamePeriodStandardDeviation.Multiple = seasonConfig.SamePeriodStandardDeviation.Multiple
  2289. dataResp.SamePeriodStandardDeviation.Year = seasonConfig.SamePeriodStandardDeviation.Year
  2290. dataResp.SamePeriodStandardDeviation.IsAdd = seasonConfig.SamePeriodStandardDeviation.IsAdd
  2291. }
  2292. // 自定义右轴
  2293. if seasonConfig.RightAxis.IndicatorType != 0 {
  2294. if seasonConfig.RightAxis.IndicatorType == 1 {
  2295. startTime, _ := time.Parse(utils.FormatDate, mappingItem.StartDate)
  2296. for i := len(quarterDataList) - 1; i > len(quarterDataList)-2 && i > 0; i-- {
  2297. var rightMappingItem models.ChartEdbInfoMapping
  2298. rightMappingItem = *mappingItem
  2299. // 计算同比值
  2300. tbzDataList, minValue, maxValue, e := GetEdbDataTbzForSeason(mappingItem.Frequency, quarterDataList[i].DataList, startTime)
  2301. if e != nil {
  2302. err = errors.New("计算同比值失败, Err:" + e.Error())
  2303. return
  2304. }
  2305. rightMappingItem.DataList = tbzDataList
  2306. rightMappingItem.MaxData = maxValue
  2307. rightMappingItem.MinData = minValue
  2308. dataResp.RightAxis.EdbInfoList = append(dataResp.RightAxis.EdbInfoList, &rightMappingItem)
  2309. }
  2310. }
  2311. dataResp.RightAxis.SeasonRightAxis = seasonConfig.RightAxis
  2312. }
  2313. }
  2314. return
  2315. }
  2316. // getEdbDataMapListForSeason 获取指标最后的基础数据-插值补充好数据-季节性图用
  2317. func getEdbDataMapListForSeason(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
  2318. // 指标对应的所有数据
  2319. edbDataListMap = make(map[int][]*models.EdbDataList)
  2320. for _, v := range mappingList {
  2321. //fmt.Println("v:", v.EdbInfoId)
  2322. item := new(models.ChartEdbInfoMapping)
  2323. item.EdbInfoId = v.EdbInfoId
  2324. item.SourceName = v.SourceName
  2325. item.Source = v.Source
  2326. item.EdbCode = v.EdbCode
  2327. item.EdbName = v.EdbName
  2328. item.EdbNameEn = v.EdbNameEn
  2329. item.Frequency = v.Frequency
  2330. item.EdbType = v.EdbType
  2331. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  2332. if v.Unit != `无` {
  2333. item.Unit = v.Unit
  2334. }
  2335. item.UnitEn = v.UnitEn
  2336. item.StartDate = v.StartDate
  2337. item.EndDate = v.EndDate
  2338. item.ModifyTime = v.ModifyTime
  2339. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  2340. item.PredictChartColor = v.PredictChartColor
  2341. item.ClassifyId = v.ClassifyId
  2342. if chartInfoId <= 0 {
  2343. item.IsAxis = 1
  2344. item.LeadValue = 0
  2345. item.LeadUnit = ""
  2346. item.ChartEdbMappingId = 0
  2347. item.ChartInfoId = 0
  2348. item.IsOrder = false
  2349. item.EdbInfoType = 1
  2350. item.ChartStyle = ""
  2351. item.ChartColor = ""
  2352. item.ChartWidth = 0
  2353. item.MaxData = v.MaxValue
  2354. item.MinData = v.MinValue
  2355. } else {
  2356. item.IsAxis = v.IsAxis
  2357. item.EdbInfoType = v.EdbInfoType
  2358. item.LeadValue = v.LeadValue
  2359. item.LeadUnit = v.LeadUnit
  2360. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  2361. item.ChartEdbMappingId = v.ChartEdbMappingId
  2362. item.ChartInfoId = v.ChartInfoId
  2363. item.ChartStyle = v.ChartStyle
  2364. item.ChartColor = v.ChartColor
  2365. item.ChartWidth = v.ChartWidth
  2366. item.IsOrder = v.IsOrder
  2367. item.MaxData = v.MaxData
  2368. item.MinData = v.MinData
  2369. }
  2370. item.LatestValue = v.LatestValue
  2371. item.LatestDate = v.LatestDate
  2372. item.UniqueCode = v.UniqueCode
  2373. item.MoveLatestDate = v.LatestDate
  2374. item.EdbAliasName = v.EdbAliasName
  2375. item.IsConvert = v.IsConvert
  2376. item.ConvertType = v.ConvertType
  2377. item.ConvertValue = v.ConvertValue
  2378. item.ConvertUnit = v.ConvertUnit
  2379. item.ConvertEnUnit = v.ConvertEnUnit
  2380. item.IsJoinPermission = v.IsJoinPermission
  2381. var startDateReal string
  2382. var diffSeconds int64
  2383. if chartType == 2 { //季节性图
  2384. startDateReal = startDate
  2385. if len(mappingList) > 1 {
  2386. item.IsAxis = v.IsAxis
  2387. }
  2388. } else {
  2389. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  2390. var startTimeRealTemp time.Time
  2391. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  2392. switch v.LeadUnit {
  2393. case "天":
  2394. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  2395. case "月":
  2396. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  2397. case "季":
  2398. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  2399. case "周":
  2400. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  2401. case "年":
  2402. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  2403. }
  2404. if startTimeRealTemp.Before(startDateParse) {
  2405. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  2406. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  2407. } else {
  2408. startDateReal = startDate
  2409. diffSeconds = 0
  2410. }
  2411. // 预测指标的开始日期也要偏移
  2412. {
  2413. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  2414. if tmpErr != nil {
  2415. err = tmpErr
  2416. return
  2417. }
  2418. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  2419. if tmpErr != nil {
  2420. err = tmpErr
  2421. return
  2422. }
  2423. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  2424. }
  2425. } else {
  2426. startDateReal = startDate
  2427. }
  2428. }
  2429. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  2430. calendarPreYear := 0
  2431. if calendar == "农历" {
  2432. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  2433. if err != nil {
  2434. fmt.Println("time.Parse:" + err.Error())
  2435. }
  2436. calendarPreYear = newStartDateReal.Year() - 1
  2437. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  2438. startDateReal = newStartDateReal.Format(utils.FormatDate)
  2439. }
  2440. dataList := make([]*models.EdbDataList, 0)
  2441. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  2442. //fmt.Println("calendarPreYear:", calendarPreYear)
  2443. //var newEdbInfo *data_manage.EdbInfo
  2444. switch v.EdbInfoCategoryType {
  2445. case 0:
  2446. dataList, err = models.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  2447. case 1:
  2448. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  2449. default:
  2450. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  2451. }
  2452. if err != nil {
  2453. return
  2454. }
  2455. handleDataMap := make(map[string]float64)
  2456. newDataList, _, _, e := HandleDataByLinearRegressionToListV3(dataList, handleDataMap)
  2457. if e != nil {
  2458. err = e
  2459. return
  2460. }
  2461. if v.IsConvert == 1 {
  2462. switch v.ConvertType {
  2463. case 1:
  2464. for i, data := range newDataList {
  2465. newDataList[i].Value = data.Value * v.ConvertValue
  2466. }
  2467. //item.MaxData = item.MaxData * v.ConvertValue
  2468. //item.MinData = item.MinData * v.ConvertValue
  2469. case 2:
  2470. for i, data := range newDataList {
  2471. newDataList[i].Value = data.Value / v.ConvertValue
  2472. }
  2473. //item.MaxData = item.MaxData / v.ConvertValue
  2474. //item.MinData = item.MinData / v.ConvertValue
  2475. case 3:
  2476. for i, data := range newDataList {
  2477. if data.Value <= 0 {
  2478. err = errors.New("数据中含有负数或0,无法对数运算")
  2479. return
  2480. }
  2481. newDataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  2482. }
  2483. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  2484. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  2485. }
  2486. }
  2487. edbDataListMap[v.EdbInfoId] = newDataList
  2488. if diffSeconds != 0 && v.EdbInfoType == 0 {
  2489. newDataListLen := len(newDataList)
  2490. for i := 0; i < newDataListLen; i++ {
  2491. newDataList[i].DataTimestamp = newDataList[i].DataTimestamp - diffSeconds
  2492. }
  2493. }
  2494. if chartType == 2 && item.IsAxis == 1 {
  2495. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  2496. if tmpErr != nil {
  2497. //item.DataList = newDataList
  2498. item.IsNullData = true
  2499. edbList = append(edbList, item)
  2500. continue
  2501. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  2502. return
  2503. }
  2504. if calendar == "农历" {
  2505. if len(newDataList) <= 0 {
  2506. result := new(models.EdbDataResult)
  2507. item.DataList = result
  2508. } else {
  2509. result, tmpErr := models.AddCalculateQuarterV6(newDataList)
  2510. if tmpErr != nil {
  2511. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  2512. return
  2513. }
  2514. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  2515. if tErr != nil {
  2516. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  2517. return
  2518. }
  2519. item.DataList = quarterDataList
  2520. }
  2521. } else {
  2522. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(newDataList, latestDate, seasonExtraConfig)
  2523. if tErr != nil {
  2524. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  2525. return
  2526. }
  2527. item.DataList = quarterDataList
  2528. }
  2529. } else if chartType == 2 && item.IsAxis == 0 {
  2530. // 右轴数据处理
  2531. xStartDate := "01-01"
  2532. jumpYear := 0
  2533. var seasonExtra models.SeasonExtraItem
  2534. if seasonExtraConfig != "" {
  2535. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  2536. if err != nil {
  2537. return
  2538. }
  2539. }
  2540. if seasonExtra.XStartDate != "" {
  2541. xStartDate = seasonExtra.XStartDate
  2542. jumpYear = seasonExtra.JumpYear
  2543. }
  2544. length := len(newDataList)
  2545. if length == 0 {
  2546. return
  2547. }
  2548. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  2549. if tmpErr != nil {
  2550. //item.DataList = newDataList
  2551. item.IsNullData = true
  2552. edbList = append(edbList, item)
  2553. continue
  2554. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  2555. return
  2556. }
  2557. var rightAxisDate time.Time
  2558. if jumpYear == 1 {
  2559. latestDate = latestDate.AddDate(-1, 0, 0)
  2560. }
  2561. latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(), xStartDate)
  2562. rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
  2563. if err != nil {
  2564. return
  2565. }
  2566. newList := make([]*models.EdbDataList, 0)
  2567. for _, v := range newDataList {
  2568. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  2569. if e != nil {
  2570. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  2571. return
  2572. }
  2573. if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
  2574. newList = append(newList, v)
  2575. }
  2576. }
  2577. item.DataList = newList
  2578. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  2579. //item.DataList = newDataList
  2580. } else {
  2581. item.DataList = newDataList
  2582. }
  2583. edbList = append(edbList, item)
  2584. }
  2585. return
  2586. }