chart_info.go 88 KB

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