chart_info_show.go 72 KB

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