chart_info.go 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  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 := getEdbDataMapList(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. for _, date := range dataTimeList {
  1890. dateTime, e := time.Parse(utils.FormatDate, date)
  1891. if e != nil {
  1892. err = errors.New("时间格式化异常, Err:" + e.Error())
  1893. return
  1894. }
  1895. if dateTime.Year() < startYear {
  1896. continue
  1897. }
  1898. // 不包含2月29号
  1899. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  1900. continue
  1901. }
  1902. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  1903. if seasonConfig.JumpYear == 1 {
  1904. if startDate.After(endDate) {
  1905. // 如果跨年且不到一年
  1906. // 全年截取一部分
  1907. if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
  1908. continue
  1909. }
  1910. if newDate.After(startDate.AddDate(0, 0, -1)) {
  1911. // 减一年
  1912. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
  1913. }
  1914. // 处理上下限列表
  1915. if value, ok := maxValueMap[newDate]; ok {
  1916. if value < handleDataMap[date] {
  1917. maxValueMap[newDate] = handleDataMap[date]
  1918. }
  1919. } else {
  1920. maxValueMap[newDate] = handleDataMap[date]
  1921. }
  1922. if value, ok := minValueMap[newDate]; ok {
  1923. if value > handleDataMap[date] {
  1924. minValueMap[newDate] = handleDataMap[date]
  1925. }
  1926. } else {
  1927. minValueMap[newDate] = handleDataMap[date]
  1928. }
  1929. dataTimeMap[newDate] = newDate
  1930. } else {
  1931. // 如果跨年且大于等于一年
  1932. // double后截取
  1933. if newDate.After(startDate) {
  1934. // 处理上下限列表
  1935. if value, ok := maxValueMap[newDate]; ok {
  1936. if value < handleDataMap[date] {
  1937. maxValueMap[newDate] = handleDataMap[date]
  1938. }
  1939. } else {
  1940. maxValueMap[newDate] = handleDataMap[date]
  1941. }
  1942. if value, ok := minValueMap[newDate]; ok {
  1943. if value > handleDataMap[date] {
  1944. minValueMap[newDate] = handleDataMap[date]
  1945. }
  1946. } else {
  1947. minValueMap[newDate] = handleDataMap[date]
  1948. }
  1949. dataTimeMap[newDate] = newDate
  1950. }
  1951. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
  1952. newEndDate := endDate.AddDate(1, 0, 0)
  1953. if newDate.Before(newEndDate) {
  1954. // 处理上下限列表
  1955. if value, ok := maxValueMap[newDate]; ok {
  1956. if value < handleDataMap[date] {
  1957. maxValueMap[newDate] = handleDataMap[date]
  1958. }
  1959. } else {
  1960. maxValueMap[newDate] = handleDataMap[date]
  1961. }
  1962. if value, ok := minValueMap[newDate]; ok {
  1963. if value > handleDataMap[date] {
  1964. minValueMap[newDate] = handleDataMap[date]
  1965. }
  1966. } else {
  1967. minValueMap[newDate] = handleDataMap[date]
  1968. }
  1969. dataTimeMap[newDate] = newDate
  1970. }
  1971. }
  1972. } else {
  1973. // 如果不跨年 正常获取
  1974. // 获取当前日期所在区间
  1975. // 处理上下限列表
  1976. if value, ok := maxValueMap[newDate]; ok {
  1977. if value < handleDataMap[date] {
  1978. maxValueMap[newDate] = handleDataMap[date]
  1979. }
  1980. } else {
  1981. maxValueMap[newDate] = handleDataMap[date]
  1982. }
  1983. if value, ok := minValueMap[newDate]; ok {
  1984. if value > handleDataMap[date] {
  1985. minValueMap[newDate] = handleDataMap[date]
  1986. }
  1987. } else {
  1988. minValueMap[newDate] = handleDataMap[date]
  1989. }
  1990. dataTimeMap[newDate] = newDate
  1991. }
  1992. }
  1993. } else {
  1994. // 旬度、月度、季度、半年度 不插值,需要先把日期列表和数据map取出来
  1995. for _, vv := range v.DataList {
  1996. dateTime, e := time.Parse(utils.FormatDate, vv.DataTime)
  1997. if e != nil {
  1998. err = errors.New("时间格式化异常, Err:" + e.Error())
  1999. return
  2000. }
  2001. // 不包含当年
  2002. if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
  2003. continue
  2004. }
  2005. if dateTime.Year() < startYear {
  2006. continue
  2007. }
  2008. // 月度的2月29号日期改为2月28日
  2009. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  2010. dateTime = dateTime.AddDate(0, 0, -1)
  2011. }
  2012. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  2013. if seasonConfig.JumpYear == 1 {
  2014. if startDate.After(endDate) {
  2015. // 如果跨年且不到一年
  2016. // 全年截取一部分
  2017. if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
  2018. continue
  2019. }
  2020. if newDate.After(startDate.AddDate(0, 0, -1)) {
  2021. // 减一年
  2022. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
  2023. }
  2024. // 处理上下限列表
  2025. if value, ok := maxValueMap[newDate]; ok {
  2026. if value < vv.Value {
  2027. maxValueMap[newDate] = vv.Value
  2028. }
  2029. } else {
  2030. maxValueMap[newDate] = vv.Value
  2031. }
  2032. if value, ok := minValueMap[newDate]; ok {
  2033. if value > vv.Value {
  2034. minValueMap[newDate] = vv.Value
  2035. }
  2036. } else {
  2037. minValueMap[newDate] = vv.Value
  2038. }
  2039. dataTimeMap[newDate] = newDate
  2040. } else {
  2041. // 如果跨年且大于等于一年
  2042. // double后截取
  2043. if newDate.After(startDate) {
  2044. // 处理上下限列表
  2045. if value, ok := maxValueMap[newDate]; ok {
  2046. if value < vv.Value {
  2047. maxValueMap[newDate] = vv.Value
  2048. }
  2049. } else {
  2050. maxValueMap[newDate] = vv.Value
  2051. }
  2052. if value, ok := minValueMap[newDate]; ok {
  2053. if value > vv.Value {
  2054. minValueMap[newDate] = vv.Value
  2055. }
  2056. } else {
  2057. minValueMap[newDate] = vv.Value
  2058. }
  2059. dataTimeMap[newDate] = newDate
  2060. }
  2061. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
  2062. newEndDate := endDate.AddDate(1, 0, 0)
  2063. if newDate.Before(newEndDate) {
  2064. // 处理上下限列表
  2065. if value, ok := maxValueMap[newDate]; ok {
  2066. if value < vv.Value {
  2067. maxValueMap[newDate] = vv.Value
  2068. }
  2069. } else {
  2070. maxValueMap[newDate] = vv.Value
  2071. }
  2072. if value, ok := minValueMap[newDate]; ok {
  2073. if value > vv.Value {
  2074. minValueMap[newDate] = vv.Value
  2075. }
  2076. } else {
  2077. minValueMap[newDate] = vv.Value
  2078. }
  2079. dataTimeMap[newDate] = newDate
  2080. }
  2081. }
  2082. } else {
  2083. // 如果不跨年 正常获取
  2084. // 获取当前日期所在区间
  2085. // 处理上下限列表
  2086. if value, ok := maxValueMap[newDate]; ok {
  2087. if value < vv.Value {
  2088. maxValueMap[newDate] = vv.Value
  2089. }
  2090. } else {
  2091. maxValueMap[newDate] = vv.Value
  2092. }
  2093. if value, ok := minValueMap[newDate]; ok {
  2094. if value > vv.Value {
  2095. minValueMap[newDate] = vv.Value
  2096. }
  2097. } else {
  2098. minValueMap[newDate] = vv.Value
  2099. }
  2100. dataTimeMap[newDate] = newDate
  2101. }
  2102. }
  2103. }
  2104. }
  2105. for _, v := range dataTimeMap {
  2106. maxMinItem := &models.MaxMinLimitsData{}
  2107. if maxValue, ok := maxValueMap[v]; ok {
  2108. maxMinItem.MaxValue = maxValue
  2109. } else {
  2110. maxMinItem.MaxValue = minValueMap[v]
  2111. }
  2112. if minValue, ok := minValueMap[v]; ok {
  2113. maxMinItem.MinValue = minValue
  2114. } else {
  2115. maxMinItem.MinValue = maxValueMap[v]
  2116. }
  2117. maxMinItem.DataTime = v.Format(utils.FormatDate)
  2118. maxMinItem.DataTimestamp = v.UnixNano() / 1e6
  2119. maxMinDataList = append(maxMinDataList, maxMinItem)
  2120. }
  2121. // 排序
  2122. sort.Slice(maxMinDataList, func(i, j int) bool {
  2123. return maxMinDataList[i].DataTime < maxMinDataList[j].DataTime
  2124. })
  2125. dataResp.MaxMinLimits.List = maxMinDataList
  2126. dataResp.MaxMinLimits.Color = seasonConfig.MaxMinLimits.Color
  2127. dataResp.MaxMinLimits.Legend = seasonConfig.MaxMinLimits.Legend
  2128. dataResp.MaxMinLimits.IsShow = seasonConfig.MaxMinLimits.IsShow
  2129. dataResp.MaxMinLimits.IsAdd = seasonConfig.MaxMinLimits.IsAdd
  2130. dataResp.MaxMinLimits.Year = seasonConfig.MaxMinLimits.Year
  2131. }
  2132. // 自定义同期均线
  2133. if seasonConfig.SamePeriodAverage.Year > 0 {
  2134. handleDataMap := make(map[string]float64)
  2135. dataTimeMap := make(map[time.Time]time.Time)
  2136. dataTimeList := make([]string, 0)
  2137. valueMap := make(map[time.Time]float64)
  2138. averageDataList := make([]*models.SamePeriodAverageData, 0)
  2139. for i := len(quarterDataList) - 1; i >= 0; i-- {
  2140. // 插值成日度
  2141. dataTimeList, _, err = HandleDataByLinearRegressionToList(quarterDataList[i].DataList, handleDataMap)
  2142. if err != nil {
  2143. err = errors.New("插值处理数据异常, Err:" + err.Error())
  2144. return
  2145. }
  2146. for _, date := range dataTimeList {
  2147. dateTime, e := time.Parse(utils.FormatDate, date)
  2148. if e != nil {
  2149. err = errors.New("时间格式化异常, Err:" + e.Error())
  2150. return
  2151. }
  2152. startYear := time.Now().AddDate(-seasonConfig.SamePeriodAverage.Year, 0, 0).Year()
  2153. if dateTime.Year() < startYear {
  2154. continue
  2155. }
  2156. // 不包含2月29号
  2157. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  2158. continue
  2159. }
  2160. // 不包含当年
  2161. if quarterDataList[i].ChartLegend == strconv.Itoa(time.Now().Year()) {
  2162. continue
  2163. }
  2164. newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  2165. // 处理均值
  2166. if value, ok := valueMap[newDate]; ok {
  2167. valueMap[newDate] = (handleDataMap[date] + value) / 2
  2168. } else {
  2169. valueMap[newDate] = handleDataMap[date]
  2170. }
  2171. dataTimeMap[newDate] = newDate
  2172. }
  2173. }
  2174. for _, v := range dataTimeMap {
  2175. averageItem := &models.SamePeriodAverageData{}
  2176. if value, ok := valueMap[v]; ok {
  2177. averageItem.Value = value
  2178. }
  2179. averageItem.DataTime = v.Format(utils.FormatDate)
  2180. averageItem.DataTimestamp = v.UnixNano() / 1e6
  2181. averageDataList = append(averageDataList, averageItem)
  2182. }
  2183. // 排序
  2184. sort.Slice(averageDataList, func(i, j int) bool {
  2185. return averageDataList[i].DataTime < averageDataList[j].DataTime
  2186. })
  2187. dataResp.SamePeriodAverage.List = averageDataList
  2188. dataResp.SamePeriodAverage.Year = seasonConfig.SamePeriodAverage.Year
  2189. dataResp.SamePeriodAverage.Color = seasonConfig.SamePeriodAverage.Color
  2190. dataResp.SamePeriodAverage.Legend = seasonConfig.SamePeriodAverage.Legend
  2191. dataResp.SamePeriodAverage.IsShow = seasonConfig.SamePeriodAverage.IsShow
  2192. dataResp.SamePeriodAverage.LineType = seasonConfig.SamePeriodAverage.LineType
  2193. dataResp.SamePeriodAverage.LineWidth = seasonConfig.SamePeriodAverage.LineWidth
  2194. dataResp.SamePeriodAverage.IsAdd = seasonConfig.SamePeriodAverage.IsAdd
  2195. }
  2196. // 自定义同期标准差
  2197. if seasonConfig.SamePeriodStandardDeviation.Year > 1 && seasonConfig.SamePeriodStandardDeviation.Multiple > 0 {
  2198. startYear := time.Now().AddDate(-seasonConfig.SamePeriodStandardDeviation.Year, 0, 0).Year()
  2199. // 先算均值,再算标准差
  2200. handleDataMap := make(map[string]float64)
  2201. dataTimeMap := make(map[time.Time]time.Time)
  2202. dataTimeValueMap := make(map[time.Time][]float64)
  2203. dataTimeList := make([]string, 0)
  2204. valueMap := make(map[time.Time]float64)
  2205. samePeriodStandardDeviationList := make([]*models.MaxMinLimitsData, 0)
  2206. for i := len(quarterDataList) - 1; i > len(quarterDataList)-seasonConfig.SamePeriodAverage.Year-1 && i > 0; i-- {
  2207. // 插值成日度
  2208. dataTimeList, _, err = HandleDataByLinearRegressionToListV2(quarterDataList[i].DataList, handleDataMap)
  2209. if err != nil {
  2210. err = errors.New("插值处理数据异常, Err:" + err.Error())
  2211. return
  2212. }
  2213. for _, date := range dataTimeList {
  2214. dateTime, e := time.Parse(utils.FormatDate, date)
  2215. if e != nil {
  2216. err = errors.New("时间格式化异常, Err:" + e.Error())
  2217. return
  2218. }
  2219. if dateTime.Year() < startYear {
  2220. continue
  2221. }
  2222. // 不包含2月29号
  2223. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  2224. continue
  2225. }
  2226. newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  2227. // 处理均值
  2228. if value, ok := valueMap[newDate]; ok {
  2229. valueMap[newDate] = (handleDataMap[date] + value) / 2
  2230. } else {
  2231. valueMap[newDate] = handleDataMap[date]
  2232. }
  2233. dataTimeMap[newDate] = newDate
  2234. valueList := dataTimeValueMap[newDate]
  2235. valueList = append(valueList, handleDataMap[date])
  2236. dataTimeValueMap[newDate] = valueList
  2237. }
  2238. }
  2239. for _, v := range dataTimeMap {
  2240. valueList := dataTimeValueMap[v]
  2241. stdev := utils.CalculateStandardDeviation(valueList)
  2242. stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
  2243. maxMinItem := &models.MaxMinLimitsData{}
  2244. if value, ok := valueMap[v]; ok {
  2245. maxMinItem.MaxValue = value + stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
  2246. maxMinItem.MinValue = value - stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
  2247. }
  2248. maxMinItem.DataTime = v.Format(utils.FormatDate)
  2249. maxMinItem.DataTimestamp = v.UnixNano() / 1e6
  2250. samePeriodStandardDeviationList = append(samePeriodStandardDeviationList, maxMinItem)
  2251. }
  2252. // 排序
  2253. sort.Slice(samePeriodStandardDeviationList, func(i, j int) bool {
  2254. return samePeriodStandardDeviationList[i].DataTime < samePeriodStandardDeviationList[j].DataTime
  2255. })
  2256. dataResp.SamePeriodStandardDeviation.List = samePeriodStandardDeviationList
  2257. dataResp.SamePeriodStandardDeviation.Color = seasonConfig.SamePeriodStandardDeviation.Color
  2258. dataResp.SamePeriodStandardDeviation.Legend = seasonConfig.SamePeriodStandardDeviation.Legend
  2259. dataResp.SamePeriodStandardDeviation.IsShow = seasonConfig.SamePeriodStandardDeviation.IsShow
  2260. dataResp.SamePeriodStandardDeviation.Multiple = seasonConfig.SamePeriodStandardDeviation.Multiple
  2261. dataResp.SamePeriodStandardDeviation.Year = seasonConfig.SamePeriodStandardDeviation.Year
  2262. dataResp.SamePeriodStandardDeviation.IsAdd = seasonConfig.SamePeriodStandardDeviation.IsAdd
  2263. }
  2264. // 自定义右轴
  2265. if seasonConfig.RightAxis.IndicatorType != 0 {
  2266. if seasonConfig.RightAxis.IndicatorType == 1 {
  2267. startTime, _ := time.Parse(utils.FormatDate, mappingItem.StartDate)
  2268. for i := len(quarterDataList) - 1; i > len(quarterDataList)-2 && i > 0; i-- {
  2269. var rightMappingItem models.ChartEdbInfoMapping
  2270. rightMappingItem = *mappingItem
  2271. // 计算同比值
  2272. tbzDataList, minValue, maxValue, e := GetEdbDataTbzForSeason(mappingItem.Frequency, quarterDataList[i].DataList, startTime)
  2273. if e != nil {
  2274. err = errors.New("计算同比值失败, Err:" + e.Error())
  2275. return
  2276. }
  2277. rightMappingItem.DataList = tbzDataList
  2278. rightMappingItem.MaxData = maxValue
  2279. rightMappingItem.MinData = minValue
  2280. dataResp.RightAxis.EdbInfoList = append(dataResp.RightAxis.EdbInfoList, &rightMappingItem)
  2281. }
  2282. }
  2283. dataResp.RightAxis.SeasonRightAxis = seasonConfig.RightAxis
  2284. }
  2285. }
  2286. return
  2287. }