chart_info.go 88 KB

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