chart_info.go 77 KB

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