chart_info.go 89 KB

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