chart_info.go 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage"
  6. "eta/eta_mobile/models/system"
  7. "eta/eta_mobile/services/alarm_msg"
  8. "eta/eta_mobile/services/data"
  9. "eta/eta_mobile/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "math"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // GetAutoCalculateDateDataList 获取当前时间相关的区间作为计算依据
  19. func GetAutoCalculateDateDataList(currentDate string, dataList []*data_manage.EdbDataList, req *data_manage.ChartRangeAnalysisExtraConf) (newDataList []*data_manage.EdbDataList, err error) {
  20. currentDateTime, _ := time.ParseInLocation(utils.FormatDate, currentDate, time.Local)
  21. switch req.DateRangeType {
  22. case 0:
  23. // 智能划分得到一个开始日期,和结束日期
  24. var startDateTime time.Time
  25. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  26. startDate := req.AutoDateConf.StartDate
  27. if startDate == "" {
  28. startDate = "2020-01-01"
  29. }
  30. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  31. } else {
  32. startConf := req.AutoDateConf.StartDateConf
  33. startDate := ""
  34. if startConf.BaseDateType == 0 { //
  35. startDate = currentDate
  36. } else if startConf.BaseDateType == 1 {
  37. startDate = time.Now().Format(utils.FormatDate)
  38. }
  39. if startConf.MoveForward > 0 {
  40. startDate = GetEdbDateByMoveForward(startDate, startConf.MoveForward, dataList)
  41. }
  42. if len(startConf.DateChange) > 0 {
  43. startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
  44. if err != nil {
  45. err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
  46. return
  47. }
  48. }
  49. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  50. }
  51. var calStartTime, calEndTime time.Time
  52. if currentDateTime.Before(startDateTime) {
  53. calStartTime = currentDateTime
  54. calEndTime = startDateTime
  55. } else {
  56. calStartTime = startDateTime
  57. calEndTime = currentDateTime
  58. }
  59. // 根据日期,获取数据
  60. for _, vv := range dataList {
  61. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  62. if (dataTimeT.After(calStartTime) && dataTimeT.Before(calEndTime)) ||
  63. dataTimeT.Equal(calStartTime) ||
  64. dataTimeT.Equal(calEndTime) {
  65. newDataList = append(newDataList, vv)
  66. }
  67. }
  68. }
  69. return
  70. }
  71. // HandleDataByCalculateType 根据计算公式处理数据
  72. func HandleDataByCalculateType(originList []*data_manage.ChartRangeAnalysisDateDataItem, originDataList []*data_manage.EdbDataList, req *data_manage.ChartRangeAnalysisExtraConf) (newList []*data_manage.EdbDataList, err error) {
  73. if len(originList) == 0 {
  74. return
  75. }
  76. calculateType := req.CalculateType
  77. switch calculateType {
  78. case 0: //均值
  79. var sum float64
  80. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  81. for _, item := range originList {
  82. for _, v := range item.DataList {
  83. sum = 0
  84. //计算的数据返回需要重新确定
  85. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  86. if e != nil {
  87. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  88. return
  89. }
  90. for _, vv := range calDataList {
  91. sum += vv.Value
  92. }
  93. val := sum / float64(len(calDataList))
  94. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  95. newList = append(newList, &data_manage.EdbDataList{
  96. DataTime: v.DataTime,
  97. Value: val,
  98. DataTimestamp: v.DataTimestamp,
  99. })
  100. }
  101. }
  102. } else {
  103. for _, item := range originList {
  104. sum = 0
  105. for k, v := range item.DataList {
  106. sum += v.Value
  107. val := sum / float64(k+1)
  108. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  109. newList = append(newList, &data_manage.EdbDataList{
  110. DataTime: v.DataTime,
  111. Value: val,
  112. DataTimestamp: v.DataTimestamp,
  113. })
  114. }
  115. }
  116. }
  117. case 1: //累计值
  118. var sum float64
  119. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  120. for _, item := range originList {
  121. sum = 0
  122. for _, v := range item.DataList {
  123. sum = 0
  124. //计算的数据返回需要重新确定
  125. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  126. if e != nil {
  127. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  128. return
  129. }
  130. for _, vv := range calDataList {
  131. sum += vv.Value
  132. }
  133. val := sum
  134. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  135. newList = append(newList, &data_manage.EdbDataList{
  136. DataTime: v.DataTime,
  137. Value: val,
  138. DataTimestamp: v.DataTimestamp,
  139. })
  140. }
  141. }
  142. } else {
  143. for _, item := range originList {
  144. sum = 0
  145. for _, v := range item.DataList {
  146. sum += v.Value
  147. val := sum
  148. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  149. newList = append(newList, &data_manage.EdbDataList{
  150. DataTime: v.DataTime,
  151. Value: val,
  152. DataTimestamp: v.DataTimestamp,
  153. })
  154. }
  155. }
  156. }
  157. case 2: //涨幅
  158. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  159. for _, item := range originList {
  160. for _, v := range item.DataList {
  161. var baseVal float64
  162. //计算的数据返回需要重新确定
  163. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  164. if e != nil {
  165. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  166. return
  167. }
  168. if len(calDataList) == 0 {
  169. continue
  170. }
  171. baseVal = calDataList[0].Value
  172. baseDate := calDataList[0].DataTime
  173. if baseVal == 0 {
  174. continue
  175. }
  176. if v.DataTime == baseDate {
  177. continue
  178. }
  179. val := (v.Value - baseVal) / baseVal
  180. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  181. newList = append(newList, &data_manage.EdbDataList{
  182. DataTime: v.DataTime,
  183. Value: val,
  184. DataTimestamp: v.DataTimestamp,
  185. })
  186. }
  187. }
  188. } else {
  189. for _, item := range originList {
  190. if len(item.DataList) == 0 {
  191. break
  192. }
  193. baseVal := item.DataList[0].Value
  194. baseDate := item.DataList[0].DataTime
  195. if baseVal == 0 {
  196. break
  197. }
  198. for _, v := range item.DataList {
  199. if v.DataTime == baseDate {
  200. continue
  201. }
  202. val := (v.Value - baseVal) / baseVal
  203. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  204. newList = append(newList, &data_manage.EdbDataList{
  205. DataTime: v.DataTime,
  206. Value: val,
  207. DataTimestamp: v.DataTimestamp,
  208. })
  209. }
  210. }
  211. }
  212. case 3: //复合增长率
  213. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  214. for _, item := range originList {
  215. for _, v := range item.DataList {
  216. var baseVal float64
  217. var baseDate string
  218. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  219. if e != nil {
  220. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  221. return
  222. }
  223. if len(calDataList) == 0 {
  224. continue
  225. }
  226. baseVal = calDataList[0].Value
  227. baseDate = calDataList[0].DataTime
  228. if v.DataTime == baseDate {
  229. continue
  230. }
  231. if baseVal == 0 {
  232. continue
  233. }
  234. baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
  235. if e != nil {
  236. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  237. return
  238. }
  239. tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  240. if e != nil {
  241. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  242. return
  243. }
  244. // 计算两个日期相差的天数
  245. diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
  246. val := v.Value / baseVal
  247. val = math.Pow(val, 1/diff) - 1
  248. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  249. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  250. }
  251. }
  252. } else {
  253. for _, item := range originList {
  254. if len(item.DataList) == 0 {
  255. break
  256. }
  257. baseVal := item.DataList[0].Value
  258. baseDate := item.DataList[0].DataTime
  259. if baseVal == 0 {
  260. break
  261. }
  262. for _, v := range item.DataList {
  263. if v.DataTime == baseDate {
  264. continue
  265. }
  266. baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
  267. if e != nil {
  268. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  269. return
  270. }
  271. tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  272. if e != nil {
  273. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  274. return
  275. }
  276. // 计算两个日期相差的天数
  277. diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
  278. val := v.Value / baseVal
  279. val = math.Pow(val, 1/diff) - 1
  280. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  281. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  282. }
  283. }
  284. }
  285. case 4: //最大值
  286. var maxVal float64
  287. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  288. for _, item := range originList {
  289. for _, v := range item.DataList {
  290. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  291. if e != nil {
  292. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  293. return
  294. }
  295. for kk, vv := range calDataList {
  296. if kk == 0 {
  297. maxVal = vv.Value
  298. }
  299. if vv.Value > maxVal {
  300. maxVal = vv.Value
  301. }
  302. }
  303. val := maxVal
  304. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  305. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  306. }
  307. }
  308. } else {
  309. for _, item := range originList {
  310. for k, v := range item.DataList {
  311. if k == 0 {
  312. maxVal = v.Value
  313. }
  314. if v.Value > maxVal {
  315. maxVal = v.Value
  316. }
  317. val := maxVal
  318. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  319. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  320. }
  321. }
  322. }
  323. case 5: //最小值
  324. var minVal float64
  325. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  326. for _, item := range originList {
  327. for _, v := range item.DataList {
  328. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  329. if e != nil {
  330. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  331. return
  332. }
  333. for kk, vv := range calDataList {
  334. if kk == 0 {
  335. minVal = vv.Value
  336. }
  337. if vv.Value < minVal {
  338. minVal = vv.Value
  339. }
  340. }
  341. val := minVal
  342. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  343. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  344. }
  345. }
  346. } else {
  347. for _, item := range originList {
  348. for k, v := range item.DataList {
  349. if k == 0 {
  350. minVal = v.Value
  351. }
  352. if v.Value < minVal {
  353. minVal = v.Value
  354. }
  355. val := minVal
  356. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  357. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  358. }
  359. }
  360. }
  361. }
  362. return
  363. }
  364. // GetChartEdbInfoFormat 区间计算图表-获取指标信息
  365. func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingList []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
  366. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  367. for _, edbInfoMapping := range edbInfoMappingList {
  368. if edbInfoMapping == nil {
  369. err = fmt.Errorf("指标信息有误")
  370. return
  371. }
  372. edbInfoMapping.FrequencyEn = data.GetFrequencyEn(edbInfoMapping.Frequency)
  373. if edbInfoMapping.Unit == `无` {
  374. edbInfoMapping.Unit = ``
  375. }
  376. if chartInfoId <= 0 {
  377. edbInfoMapping.IsAxis = 1
  378. edbInfoMapping.LeadValue = 0
  379. edbInfoMapping.LeadUnit = ""
  380. edbInfoMapping.ChartEdbMappingId = 0
  381. edbInfoMapping.ChartInfoId = 0
  382. edbInfoMapping.IsOrder = false
  383. edbInfoMapping.EdbInfoType = 1
  384. edbInfoMapping.ChartStyle = ""
  385. edbInfoMapping.ChartColor = ""
  386. edbInfoMapping.ChartWidth = 0
  387. } else {
  388. edbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(edbInfoMapping.LeadUnit)
  389. }
  390. edbList = append(edbList, edbInfoMapping)
  391. }
  392. return
  393. }
  394. // GetChartDataByEdbInfoList 区间计算图表-根据指标信息获取x轴和y轴
  395. func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
  396. if chartInfoId > 0 && req.EdbInfoMode == 1 {
  397. edbList, xEdbIdValue, dataResp, err = GetChartDataByEdbInfoListBySeries(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, req)
  398. return
  399. }
  400. for _, edbInfoMapping := range edbInfoMappingList {
  401. edbInfoMapping, err = getChartDataByEdbInfo(edbInfoMapping, req)
  402. if err != nil {
  403. return
  404. }
  405. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  406. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  407. if req.CalculateType == 2 || req.CalculateType == 3 {
  408. edbInfoMapping.ConvertUnit = "无"
  409. edbInfoMapping.ConvertEnUnit = "无"
  410. }
  411. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  412. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  413. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  414. }
  415. if edbInfoMapping.ConvertUnit == "无" {
  416. edbInfoMapping.ConvertUnit = ""
  417. }
  418. if edbInfoMapping.ConvertEnUnit == "无" {
  419. edbInfoMapping.ConvertEnUnit = ""
  420. }
  421. dataList := edbInfoMapping.DataList.([]*data_manage.EdbDataList)
  422. // 处理上下限
  423. var maxData, minData float64
  424. if len(dataList) > 0 {
  425. maxData = dataList[0].Value
  426. minData = dataList[0].Value
  427. for _, v := range dataList {
  428. if v.Value > maxData {
  429. maxData = v.Value
  430. }
  431. if v.Value < minData {
  432. minData = v.Value
  433. }
  434. }
  435. }
  436. edbInfoMapping.MaxData = maxData
  437. edbInfoMapping.MinData = minData
  438. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  439. }
  440. //根据时间类型来筛选最终的数据
  441. yearMax := 0
  442. if dateType == utils.DateTypeNYears {
  443. for _, v := range edbInfoMappingList {
  444. dataList := v.DataList.([]*data_manage.EdbDataList)
  445. if len(dataList) > 0 {
  446. latestDate := dataList[len(dataList)-1].DataTime
  447. if latestDate != "" {
  448. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  449. if tErr != nil {
  450. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  451. return
  452. }
  453. if lastDateT.Year() > yearMax {
  454. yearMax = lastDateT.Year()
  455. }
  456. }
  457. }
  458. }
  459. }
  460. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  461. if startDate != "" {
  462. for k, v := range edbInfoMappingList {
  463. var maxData, minData float64
  464. dataList := v.DataList.([]*data_manage.EdbDataList)
  465. newDataList := make([]*data_manage.EdbDataList, 0)
  466. if len(dataList) == 0 {
  467. newDataList = dataList
  468. } else {
  469. firstFlag := true
  470. for _, d := range dataList {
  471. if endDate != "" && d.DataTime > endDate {
  472. continue
  473. }
  474. if d.DataTime < startDate {
  475. continue
  476. }
  477. newDataList = append(newDataList, d)
  478. if firstFlag {
  479. maxData = d.Value
  480. minData = d.Value
  481. firstFlag = false
  482. } else {
  483. if d.Value > maxData {
  484. maxData = d.Value
  485. }
  486. if d.Value < minData {
  487. minData = d.Value
  488. }
  489. }
  490. }
  491. }
  492. edbInfoMappingList[k].DataList = newDataList
  493. edbInfoMappingList[k].MinData = minData
  494. edbInfoMappingList[k].MaxData = maxData
  495. }
  496. }
  497. dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
  498. if req.MultipleGraphConfigId > 0 {
  499. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  500. if e != nil && e.Error() != utils.ErrNoRow() {
  501. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  502. return
  503. }
  504. // 查询是否已经生成指标
  505. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  506. }
  507. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  508. if err != nil {
  509. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  510. return
  511. }
  512. return
  513. }
  514. func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
  515. //查询seriesId
  516. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  517. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfoId)
  518. if err != nil {
  519. if err.Error() == utils.ErrNoRow() {
  520. err = fmt.Errorf("图表关联关系不存在")
  521. return
  522. } else {
  523. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  524. return
  525. }
  526. }
  527. //根据seriesId查询数据
  528. //并把数据放到dataList中
  529. for _, edbInfoMapping := range edbInfoMappingList {
  530. dataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
  531. dataList, e := dataOb.GetEdbDataList(seriesMappingItem.FactorEdbSeriesId, edbInfoMapping.EdbInfoId, startDate, endDate)
  532. if e != nil {
  533. err = e
  534. return
  535. }
  536. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  537. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  538. if req.CalculateType == 2 || req.CalculateType == 3 {
  539. edbInfoMapping.ConvertUnit = "无"
  540. edbInfoMapping.ConvertEnUnit = "无"
  541. }
  542. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  543. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  544. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  545. }
  546. if edbInfoMapping.ConvertUnit == "无" {
  547. edbInfoMapping.ConvertUnit = ""
  548. }
  549. if edbInfoMapping.ConvertEnUnit == "无" {
  550. edbInfoMapping.ConvertEnUnit = ""
  551. }
  552. edbInfoMapping.DataList = dataList
  553. // 处理上下限
  554. var maxData, minData float64
  555. if len(dataList) > 0 {
  556. maxData = dataList[0].Value
  557. minData = dataList[0].Value
  558. for _, v := range dataList {
  559. if v.Value > maxData {
  560. maxData = v.Value
  561. }
  562. if v.Value < minData {
  563. minData = v.Value
  564. }
  565. }
  566. }
  567. edbInfoMapping.MaxData = maxData
  568. edbInfoMapping.MinData = minData
  569. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  570. }
  571. yearMax := 0
  572. if dateType == utils.DateTypeNYears {
  573. for _, v := range edbInfoMappingList {
  574. dataList := v.DataList.([]*data_manage.EdbDataList)
  575. latestDate := dataList[len(dataList)-1].DataTime
  576. if latestDate != "" {
  577. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  578. if tErr != nil {
  579. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  580. return
  581. }
  582. if lastDateT.Year() > yearMax {
  583. yearMax = lastDateT.Year()
  584. }
  585. }
  586. }
  587. }
  588. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  589. if startDate != "" {
  590. for k, v := range edbInfoMappingList {
  591. var maxData, minData float64
  592. dataList := v.DataList.([]*data_manage.EdbDataList)
  593. newDataList := make([]*data_manage.EdbDataList, 0)
  594. if len(dataList) == 0 {
  595. newDataList = dataList
  596. } else {
  597. firstFlag := true
  598. for _, d := range dataList {
  599. if endDate != "" && d.DataTime > endDate {
  600. continue
  601. }
  602. if d.DataTime < startDate {
  603. continue
  604. }
  605. newDataList = append(newDataList, d)
  606. if firstFlag {
  607. maxData = d.Value
  608. minData = d.Value
  609. firstFlag = false
  610. } else {
  611. if d.Value > maxData {
  612. maxData = d.Value
  613. }
  614. if d.Value < minData {
  615. minData = d.Value
  616. }
  617. }
  618. }
  619. }
  620. edbInfoMappingList[k].DataList = newDataList
  621. edbInfoMappingList[k].MinData = minData
  622. edbInfoMappingList[k].MaxData = maxData
  623. }
  624. }
  625. dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
  626. // 查询配置关联关系
  627. if req.MultipleGraphConfigId > 0 {
  628. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  629. if e != nil && e.Error() != utils.ErrNoRow() {
  630. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  631. return
  632. }
  633. // 查询是否已经生成指标
  634. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  635. }
  636. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  637. if err != nil {
  638. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  639. return
  640. }
  641. return
  642. }
  643. // getChartDataByEdbInfo 区间计算图表-根据指标信息获取x轴和y轴
  644. func getChartDataByEdbInfo(edbInfoMapping *data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (newEdbInfoMapping *data_manage.ChartEdbInfoMapping, err error) {
  645. newEdbInfoMapping = edbInfoMapping
  646. // 指标的开始日期和结束日期
  647. edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
  648. //edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  649. edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
  650. edbEndDate := edbEndDateTime.Format(utils.FormatDate)
  651. // 获取时间基准指标在时间区间内的值
  652. dataList := make([]*data_manage.EdbDataList, 0)
  653. switch edbInfoMapping.EdbInfoCategoryType {
  654. case 0:
  655. dataList, err = data_manage.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
  656. case 1:
  657. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
  658. default:
  659. err = errors.New("指标base类型异常")
  660. return
  661. }
  662. /*var dataList data_manage.SortEdbDataList
  663. dataList = dataListTmp
  664. ascDataList := dataListTmp
  665. sort.Sort(dataList)*/
  666. dateList := make([]*data_manage.ChartRangeAnalysisDateDataItem, 0)
  667. switch req.DateRangeType {
  668. case 0:
  669. // 智能划分得到一个开始日期,和结束日期
  670. var startDateTime, endDateTime time.Time
  671. startDateTime = edbStartDateTime
  672. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  673. startDate := req.AutoDateConf.StartDate
  674. if startDate == "" {
  675. startDate = "2020-01-01"
  676. }
  677. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  678. } /*else {
  679. startConf := req.AutoDateConf.StartDateConf
  680. startDate := ""
  681. if startConf.BaseDateType == 0 { //
  682. startDate = edbEndDate
  683. } else if startConf.BaseDateType == 1 {
  684. startDate = time.Now().Format(utils.FormatDate)
  685. }
  686. if startConf.MoveForward > 0 {
  687. startDate = GetEdbDateByMoveForward(startConf.MoveForward, dataList)
  688. }
  689. if len(startConf.DateChange) > 0 {
  690. startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
  691. if err != nil {
  692. err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
  693. return
  694. }
  695. }
  696. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  697. }*/
  698. if req.AutoDateConf.IsAutoEndDate == 0 { //固定设置
  699. endDate := req.AutoDateConf.EndDate
  700. if endDate == "" {
  701. err = fmt.Errorf("智能划分截止日期处理失败:请输入截止日期")
  702. return
  703. }
  704. // todo 如果截止日期比指标日期还要大,则用指标的最新日期
  705. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  706. } else {
  707. endConf := req.AutoDateConf.EndDateConf
  708. endDate := edbEndDate
  709. if endConf.MoveForward > 0 {
  710. endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
  711. }
  712. if len(endConf.DateChange) > 0 {
  713. endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
  714. if err != nil {
  715. err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
  716. return
  717. }
  718. }
  719. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  720. }
  721. dateList = append(dateList, &data_manage.ChartRangeAnalysisDateDataItem{
  722. StartDate: startDateTime,
  723. EndDate: endDateTime})
  724. case 1:
  725. // 手工划分得到多个开始日期和结束日期(已排序)
  726. for _, v := range req.ManualDateConf {
  727. startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
  728. endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  729. tmp := &data_manage.ChartRangeAnalysisDateDataItem{
  730. StartDate: startDateT,
  731. EndDate: endDateT,
  732. }
  733. dateList = append(dateList, tmp)
  734. }
  735. case 2:
  736. // 跨年划分得到多个开始日期和结束日期
  737. startYear := edbStartDateTime.Year()
  738. endYear := edbEndDateTime.Year()
  739. startDay := req.YearDateConf.StartDay
  740. endDay := req.YearDateConf.EndDay
  741. for year := startYear; year <= endYear; year++ {
  742. startDate := fmt.Sprintf("%d-%s", year, startDay)
  743. endDate := fmt.Sprintf("%d-%s", year+1, endDay)
  744. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  745. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  746. if startDateTime.Before(edbStartDateTime) {
  747. break
  748. }
  749. tmp := &data_manage.ChartRangeAnalysisDateDataItem{
  750. StartDate: startDateTime,
  751. EndDate: endDateTime,
  752. }
  753. dateList = append(dateList, tmp)
  754. }
  755. }
  756. // 根据日期,获取数据
  757. for _, v := range dateList {
  758. for _, vv := range dataList {
  759. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  760. if dataTimeT.After(v.StartDate) && dataTimeT.Before(v.EndDate) ||
  761. dataTimeT.Equal(v.StartDate) ||
  762. dataTimeT.Equal(v.EndDate) {
  763. v.DataList = append(v.DataList, vv)
  764. }
  765. }
  766. }
  767. // 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
  768. newDataList, err := HandleDataByCalculateType(dateList, dataList, req)
  769. if err != nil {
  770. return
  771. }
  772. if req.UnNormalDataDealType > 0 {
  773. switch req.UnNormalDataDealType { //0:不处理,1:剔除,2替换
  774. case 1:
  775. dealDataList := make([]*data_manage.EdbDataList, 0)
  776. for _, v := range newDataList {
  777. if !utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  778. dealDataList = append(dealDataList, v)
  779. }
  780. }
  781. newDataList = dealDataList
  782. case 2:
  783. for i, v := range newDataList {
  784. if utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  785. newDataList[i].Value = req.UnNormalDataConf.ReplaceValue
  786. }
  787. }
  788. }
  789. }
  790. if req.DataConvertType > 0 {
  791. // 数据转换类型 0不转, 1乘 2除 3对数
  792. switch req.DataConvertType {
  793. case 1:
  794. for i, v := range newDataList {
  795. val := v.Value * req.DataConvertConf.Value
  796. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  797. newDataList[i].Value = val
  798. }
  799. //item.MaxData = item.MaxData * v.ConvertValue
  800. //item.MinData = item.MinData * v.ConvertValue
  801. case 2:
  802. for i, v := range newDataList {
  803. val := v.Value / req.DataConvertConf.Value
  804. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  805. newDataList[i].Value = val
  806. }
  807. //item.MaxData = item.MaxData / v.ConvertValue
  808. //item.MinData = item.MinData / v.ConvertValue
  809. case 3:
  810. for i, v := range newDataList {
  811. if v.Value <= 0 {
  812. err = errors.New("数据中含有负数或0,无法对数运算")
  813. return
  814. }
  815. val := math.Log(v.Value) / math.Log(req.DataConvertConf.Value)
  816. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  817. newDataList[i].Value = val
  818. }
  819. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  820. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  821. }
  822. }
  823. newEdbInfoMapping.DataList = newDataList
  824. //时间处理
  825. return
  826. }
  827. // RollingCorrelationChartDataResp 滚动区间计算图表数据
  828. type RollingCorrelationChartDataResp struct {
  829. MaxData float64
  830. MinData float64
  831. LatestDate string `description:"真实数据的最后日期"`
  832. EdbInfoCategoryType int
  833. ChartColor string
  834. ChartStyle string
  835. PredictChartColor string
  836. ChartType int
  837. ChartWidth int
  838. EdbName string
  839. EdbNameEn string
  840. Unit string
  841. UnitEn string
  842. IsAxis int
  843. DataList []data_manage.EdbDataList
  844. }
  845. // ChartInfoRefresh 图表刷新
  846. func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
  847. var errMsg string
  848. defer func() {
  849. if err != nil {
  850. tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
  851. utils.FileLog.Info(tips)
  852. go alarm_msg.SendAlarmMsg(tips, 3)
  853. }
  854. }()
  855. var chartInfo *data_manage.ChartInfo
  856. if chartInfoId > 0 {
  857. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  858. if err != nil {
  859. if err.Error() == utils.ErrNoRow() {
  860. errMsg = "图表已被删除,请刷新页面"
  861. err = errors.New(errMsg)
  862. return
  863. }
  864. errMsg = "获取图表信息失败"
  865. err = errors.New("获取图表信息失败,Err:" + err.Error())
  866. return
  867. }
  868. } else {
  869. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  870. if err != nil {
  871. if err.Error() == utils.ErrNoRow() {
  872. errMsg = "图表已被删除,请刷新页面"
  873. err = errors.New(errMsg)
  874. return
  875. }
  876. errMsg = "获取图表信息失败"
  877. err = errors.New("获取图表信息失败,Err:" + err.Error())
  878. return
  879. }
  880. }
  881. // 1.刷新图表关联的指标
  882. mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
  883. if e != nil {
  884. utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
  885. return
  886. }
  887. if len(mappings) == 0 {
  888. utils.FileLog.Info("图表无关联指标")
  889. return
  890. }
  891. var edbIds []int
  892. for _, v := range mappings {
  893. edbIds = append(edbIds, v.EdbInfoId)
  894. }
  895. if e, _ = data.EdbInfoRefreshAllFromBaseV3(edbIds, false, true, false); e != nil {
  896. utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
  897. return
  898. }
  899. // todo 重新计算
  900. // 区间计算图表配置校验
  901. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  902. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  903. if err != nil {
  904. errMsg = "配置信息错误"
  905. err = errors.New(errMsg + ", Err: " + err.Error())
  906. return
  907. }
  908. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  909. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfo.ChartInfoId)
  910. if err != nil {
  911. if err.Error() == utils.ErrNoRow() {
  912. err = nil
  913. } else {
  914. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  915. return
  916. }
  917. } else {
  918. _, e = FactorEdbStepCalculateRange(seriesMappingItem.FactorEdbSeriesId, edbIds, extraConfig, true)
  919. if e != nil {
  920. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  921. return
  922. }
  923. }
  924. // 4.清除图表缓存
  925. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  926. _ = utils.Rc.Delete(key)
  927. return
  928. }
  929. // CopyChartInfo 复制图表
  930. func CopyChartInfo(classifyId int, chartName string, oldChartInfo *data_manage.ChartInfo, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  931. isSendEmail = true
  932. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  933. chartInfo = &data_manage.ChartInfo{
  934. ChartInfoId: 0,
  935. ChartName: chartName,
  936. ChartClassifyId: classifyId,
  937. SysUserId: sysUser.AdminId,
  938. SysUserRealName: sysUser.RealName,
  939. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  940. CreateTime: time.Now(),
  941. ModifyTime: time.Now(),
  942. DateType: oldChartInfo.DateType,
  943. StartDate: oldChartInfo.StartDate,
  944. EndDate: oldChartInfo.EndDate,
  945. IsSetName: oldChartInfo.IsSetName,
  946. EdbInfoIds: oldChartInfo.EdbInfoIds,
  947. ChartType: oldChartInfo.ChartType,
  948. Calendar: oldChartInfo.Calendar,
  949. SeasonStartDate: oldChartInfo.SeasonStartDate,
  950. SeasonEndDate: oldChartInfo.SeasonEndDate,
  951. ChartImage: oldChartInfo.ChartImage,
  952. BarConfig: oldChartInfo.BarConfig,
  953. //Sort: sort,
  954. LeftMin: oldChartInfo.LeftMin,
  955. LeftMax: oldChartInfo.LeftMax,
  956. RightMin: oldChartInfo.RightMin,
  957. RightMax: oldChartInfo.RightMax,
  958. Right2Min: oldChartInfo.Right2Min,
  959. Right2Max: oldChartInfo.Right2Max,
  960. Disabled: oldChartInfo.Disabled,
  961. Source: oldChartInfo.Source,
  962. ExtraConfig: oldChartInfo.ExtraConfig,
  963. SeasonExtraConfig: oldChartInfo.SeasonExtraConfig,
  964. StartYear: oldChartInfo.StartYear,
  965. Unit: oldChartInfo.Unit,
  966. UnitEn: oldChartInfo.UnitEn,
  967. ChartThemeId: oldChartInfo.ChartThemeId,
  968. SourcesFrom: oldChartInfo.SourcesFrom,
  969. Instructions: oldChartInfo.Instructions,
  970. MarkersLines: oldChartInfo.MarkersLines,
  971. MarkersAreas: oldChartInfo.MarkersAreas,
  972. }
  973. newId, err := data_manage.AddChartInfo(chartInfo)
  974. if err != nil {
  975. err = fmt.Errorf("保存失败,Err:%s", err.Error())
  976. return
  977. }
  978. chartInfo.ChartInfoId = int(newId)
  979. edbInfoMappingList, err := data_manage.GetChartEdbMappingList(oldChartInfo.ChartInfoId)
  980. if err != nil {
  981. err = fmt.Errorf("获取图表,指标信息失败,Err:" + err.Error())
  982. return
  983. }
  984. // 添加图表与指标的关联关系
  985. edbInfoIdArr := make([]int, 0)
  986. {
  987. mapList := make([]*data_manage.ChartEdbMapping, 0)
  988. for _, v := range edbInfoMappingList {
  989. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  990. timestamp = strconv.FormatInt(time.Now().UnixNano(), 10)
  991. mapItem := &data_manage.ChartEdbMapping{
  992. //ChartEdbMappingId: 0,
  993. ChartInfoId: chartInfo.ChartInfoId,
  994. EdbInfoId: v.EdbInfoId,
  995. CreateTime: time.Now(),
  996. ModifyTime: time.Now(),
  997. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  998. MaxData: v.MaxData,
  999. MinData: v.MinData,
  1000. IsOrder: v.IsOrder,
  1001. IsAxis: v.IsAxis,
  1002. EdbInfoType: v.EdbInfoType,
  1003. LeadValue: v.LeadValue,
  1004. LeadUnit: v.LeadUnit,
  1005. ChartStyle: v.ChartStyle,
  1006. ChartColor: v.ChartColor,
  1007. ChartWidth: v.ChartWidth,
  1008. Source: v.Source,
  1009. EdbAliasName: v.EdbAliasName,
  1010. IsConvert: v.IsConvert,
  1011. ConvertType: v.ConvertType,
  1012. ConvertValue: v.ConvertValue,
  1013. ConvertUnit: v.ConvertEnUnit,
  1014. ConvertEnUnit: v.ConvertEnUnit,
  1015. }
  1016. mapList = append(mapList, mapItem)
  1017. }
  1018. err = data_manage.AddChartEdbMapping(mapList)
  1019. if err != nil {
  1020. err = fmt.Errorf("保存失败,Err:%s", err.Error())
  1021. return
  1022. }
  1023. }
  1024. // 添加系列和图表映射
  1025. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  1026. _, err = chartSeriesOb.GetItemByChartInfoId(oldChartInfo.ChartInfoId)
  1027. if err != nil {
  1028. if err.Error() == utils.ErrNoRow() {
  1029. err = nil
  1030. } else {
  1031. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  1032. return
  1033. }
  1034. } else {
  1035. // 新增指标系列
  1036. // 区间计算图表配置校验
  1037. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  1038. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  1039. if err != nil {
  1040. errMsg = "配置信息错误"
  1041. err = errors.New(errMsg + ", Err: " + err.Error())
  1042. return
  1043. }
  1044. err = AddSeries(edbInfoIdArr, chartInfo.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, chartInfo.ExtraConfig)
  1045. if err != nil {
  1046. errMsg = "操作失败"
  1047. err = errors.New("新增区间计算图表失败, Err: " + err.Error())
  1048. return
  1049. }
  1050. }
  1051. //添加es数据
  1052. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1053. return
  1054. }
  1055. /*
  1056. // CalculateCorrelation 计算区间计算-获取x轴和y轴
  1057. func CalculateCorrelation(leadValue int, leadUnit, frequencyA, frequencyB string, dataListA, dataListB []*data_manage.EdbDataList) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
  1058. xData := make([]int, 0)
  1059. yData := make([]float64, 0)
  1060. if leadValue == 0 {
  1061. xData = append(xData, 0)
  1062. }
  1063. if leadValue > 0 {
  1064. leadMin := 0 - leadValue
  1065. xLen := 2*leadValue + 1
  1066. for i := 0; i < xLen; i++ {
  1067. n := leadMin + i
  1068. xData = append(xData, n)
  1069. }
  1070. }
  1071. // 计算窗口,不包含第一天
  1072. //startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  1073. //startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  1074. //// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
  1075. //baseEdbInfo := edbInfoMappingB
  1076. //changeEdbInfo := edbInfoMappingA
  1077. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  1078. //baseEdbInfo := edbInfoMappingA
  1079. //changeEdbInfo := edbInfoMappingB
  1080. // 获取时间基准指标在时间区间内的值
  1081. //aDataList := make([]*data_manage.EdbDataList, 0)
  1082. //switch baseEdbInfo.EdbInfoCategoryType {
  1083. //case 0:
  1084. // aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
  1085. //case 1:
  1086. // _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, false)
  1087. //default:
  1088. // err = errors.New("指标base类型异常")
  1089. // return
  1090. //}
  1091. //
  1092. //// 获取变频指标所有日期的值, 插值法完善数据
  1093. //bDataList := make([]*data_manage.EdbDataList, 0)
  1094. //switch changeEdbInfo.EdbInfoCategoryType {
  1095. //case 0:
  1096. // bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
  1097. //case 1:
  1098. // _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
  1099. //default:
  1100. // err = errors.New("指标change类型异常")
  1101. // return
  1102. //}
  1103. //changeDataMap := make(map[string]float64)
  1104. //newChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  1105. //if e != nil {
  1106. // err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  1107. // return
  1108. //}
  1109. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  1110. baseDataList := make([]*data_manage.EdbDataList, 0)
  1111. baseDataMap := make(map[string]float64)
  1112. changeDataList := make([]*data_manage.EdbDataList, 0)
  1113. changeDataMap := make(map[string]float64)
  1114. // 先把低频指标升频为高频
  1115. {
  1116. frequencyIntMap := map[string]int{
  1117. "日度": 1,
  1118. "周度": 2,
  1119. "旬度": 3,
  1120. "月度": 4,
  1121. "季度": 5,
  1122. "年度": 6,
  1123. }
  1124. // 如果A指标是高频,那么就需要对B指标进行升频
  1125. if frequencyIntMap[frequencyA] < frequencyIntMap[frequencyB] {
  1126. tmpNewChangeDataList, e := HandleDataByLinearRegression(dataListB, changeDataMap)
  1127. if e != nil {
  1128. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  1129. return
  1130. }
  1131. changeDataList = tmpNewChangeDataList
  1132. baseDataList = dataListA
  1133. for _, v := range baseDataList {
  1134. baseDataMap[v.DataTime] = v.Value
  1135. }
  1136. } else if frequencyIntMap[frequencyA] > frequencyIntMap[frequencyB] {
  1137. // 如果B指标是高频,那么就需要对A指标进行升频
  1138. tmpNewChangeDataList, e := HandleDataByLinearRegression(dataListA, baseDataMap)
  1139. if e != nil {
  1140. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  1141. return
  1142. }
  1143. baseDataList = tmpNewChangeDataList
  1144. changeDataList = dataListB
  1145. for _, v := range changeDataList {
  1146. changeDataMap[v.DataTime] = v.Value
  1147. }
  1148. } else {
  1149. baseDataList = dataListA
  1150. for _, v := range baseDataList {
  1151. baseDataMap[v.DataTime] = v.Value
  1152. }
  1153. changeDataList = dataListB
  1154. for _, v := range changeDataList {
  1155. changeDataMap[v.DataTime] = v.Value
  1156. }
  1157. }
  1158. }
  1159. // 计算不领先也不滞后时的相关系数
  1160. baseCalculateData := make([]float64, 0)
  1161. baseDataTimeArr := make([]string, 0)
  1162. for i := range baseDataList {
  1163. baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
  1164. baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
  1165. }
  1166. //zeroBaseData := make([]float64, 0)
  1167. //zeroCalculateData := make([]float64, 0)
  1168. //for i := range baseDataTimeArr {
  1169. // tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
  1170. // tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
  1171. // if ok1 && ok2 {
  1172. // zeroBaseData = append(zeroBaseData, tmpBaseVal)
  1173. // zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
  1174. // }
  1175. //}
  1176. //if len(zeroBaseData) != len(zeroCalculateData) {
  1177. // err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
  1178. // return
  1179. //}
  1180. //zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
  1181. //if leadValue == 0 {
  1182. // yData = append(yData, zeroRatio)
  1183. //}
  1184. // 计算领先/滞后N期
  1185. if leadValue > 0 {
  1186. // 平移变频指标领先/滞后的日期(单位天)
  1187. moveUnitDays := utils.FrequencyDaysMap[leadUnit]
  1188. for i := range xData {
  1189. //if xData[i] == 0 {
  1190. // yData = append(yData, zeroRatio)
  1191. // continue
  1192. //}
  1193. xCalculateData := make([]float64, 0)
  1194. yCalculateData := make([]float64, 0)
  1195. // 平移指定天数
  1196. mDays := int(moveUnitDays) * xData[i]
  1197. _, dMap := MoveDataDaysToNewDataList(changeDataList, mDays)
  1198. // 取出对应的基准日期的值
  1199. for i2 := range baseDataTimeArr {
  1200. tmpDate := baseDataTimeArr[i2]
  1201. if yVal, ok := dMap[tmpDate]; ok {
  1202. xCalculateData = append(xCalculateData, baseCalculateData[i2])
  1203. yCalculateData = append(yCalculateData, yVal)
  1204. }
  1205. }
  1206. if len(yCalculateData) <= 0 {
  1207. //err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
  1208. //return
  1209. // 领先滞后后,没有可以计算的数据了
  1210. continue
  1211. }
  1212. // 公式计算出领先/滞后频度对应点的区间计算系数
  1213. ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
  1214. yData = append(yData, ratio)
  1215. }
  1216. }
  1217. xEdbIdValue = xData
  1218. yDataList = make([]data_manage.YData, 0)
  1219. yDate := "0000-00-00"
  1220. yDataList = append(yDataList, data_manage.YData{
  1221. Date: yDate,
  1222. Value: yData,
  1223. })
  1224. return
  1225. }
  1226. // GetFactorChartDataByChartId 获取多因子区间计算图表数据
  1227. func GetFactorChartDataByChartId(chartInfoId int, extraConfig string) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
  1228. if chartInfoId <= 0 {
  1229. return
  1230. }
  1231. // 指标对应的图例
  1232. extra := new(data_manage.CorrelationChartInfoExtraConfig)
  1233. if extraConfig != "" {
  1234. if e := json.Unmarshal([]byte(extraConfig), extra); e != nil {
  1235. err = fmt.Errorf("解析图表额外配置失败, err: %v", e)
  1236. return
  1237. }
  1238. }
  1239. legends := make(map[string]*data_manage.CorrelationChartLegend)
  1240. if extra != nil {
  1241. for _, v := range extra.LegendConfig {
  1242. s := fmt.Sprintf("%d-%d", v.SeriesId, v.EdbInfoId)
  1243. legends[s] = v
  1244. }
  1245. }
  1246. // 获取图表引用到的系列指标
  1247. chartMappingOb := new(data_manage.FactorEdbSeriesChartMapping)
  1248. cond := fmt.Sprintf(" AND %s = ? AND %s = 1", chartMappingOb.Cols().ChartInfoId, chartMappingOb.Cols().EdbUsed)
  1249. pars := make([]interface{}, 0)
  1250. pars = append(pars, chartInfoId)
  1251. chartMappings, e := chartMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
  1252. if e != nil {
  1253. err = fmt.Errorf("获取图表引用系列指标失败")
  1254. return
  1255. }
  1256. // 取出计算结果
  1257. yDataList = make([]data_manage.YData, 0)
  1258. yDate := "0000-00-00"
  1259. for k, m := range chartMappings {
  1260. var values []data_manage.FactorEdbSeriesCorrelationMatrixValues
  1261. if m.CalculateData != "" {
  1262. e = json.Unmarshal([]byte(m.CalculateData), &values)
  1263. if e != nil {
  1264. err = fmt.Errorf("系列指标计算数据有误, err: %v", e)
  1265. return
  1266. }
  1267. }
  1268. var y []float64
  1269. for _, v := range values {
  1270. if k == 0 {
  1271. xEdbIdValue = append(xEdbIdValue, v.XData)
  1272. }
  1273. y = append(y, v.YData)
  1274. }
  1275. var yData data_manage.YData
  1276. yData.Date = yDate
  1277. yData.Value = y
  1278. yData.SeriesEdb.SeriesId = m.FactorEdbSeriesId
  1279. yData.SeriesEdb.EdbInfoId = m.EdbInfoId
  1280. // 图例
  1281. s := fmt.Sprintf("%d-%d", m.FactorEdbSeriesId, m.EdbInfoId)
  1282. legend := legends[s]
  1283. if legend != nil {
  1284. yData.Name = legend.LegendName
  1285. yData.Color = legend.Color
  1286. }
  1287. yDataList = append(yDataList, yData)
  1288. }
  1289. return
  1290. }
  1291. // FormatChartEdbInfoMappings 补充指标信息
  1292. func FormatChartEdbInfoMappings(chartInfoId int, mappings []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
  1293. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  1294. if len(mappings) == 0 {
  1295. return
  1296. }
  1297. for _, v := range mappings {
  1298. if chartInfoId <= 0 {
  1299. v.IsAxis = 1
  1300. v.LeadValue = 0
  1301. v.LeadUnit = ""
  1302. v.ChartEdbMappingId = 0
  1303. v.ChartInfoId = 0
  1304. v.IsOrder = false
  1305. v.EdbInfoType = 1
  1306. v.ChartStyle = ""
  1307. v.ChartColor = ""
  1308. v.ChartWidth = 0
  1309. } else {
  1310. v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
  1311. v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
  1312. }
  1313. v.FrequencyEn = data.GetFrequencyEn(v.Frequency)
  1314. if v.Unit == `无` {
  1315. v.Unit = ``
  1316. }
  1317. edbList = append(edbList, v)
  1318. }
  1319. return
  1320. }*/
  1321. func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
  1322. // 根据日期进行排序
  1323. index := 0
  1324. length := len(edbDataList)
  1325. for i := length - 1; i >= 0; i-- {
  1326. item := edbDataList[i]
  1327. if item.DataTime == startDate {
  1328. index += 1
  1329. continue
  1330. }
  1331. if index >= moveForward {
  1332. date = item.DataTime
  1333. break
  1334. }
  1335. if index > 0 {
  1336. index += 1
  1337. date = item.DataTime
  1338. }
  1339. }
  1340. return
  1341. }
  1342. // HandleEdbDateChange 处理日期变换
  1343. func HandleEdbDateChange(date string, dateChange []*data_manage.EdbDataDateChangeConf) (newDate string, err error) {
  1344. newDate = date
  1345. if newDate != "" {
  1346. if len(dateChange) > 0 {
  1347. var dateTime time.Time
  1348. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  1349. if err != nil {
  1350. err = fmt.Errorf("日期解析失败: %s", err.Error())
  1351. return
  1352. }
  1353. for _, v := range dateChange {
  1354. if v.ChangeType == 1 {
  1355. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  1356. newDate = dateTime.Format(utils.FormatDate)
  1357. } else if v.ChangeType == 2 {
  1358. newDate, err, _ = utils.HandleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  1359. if err != nil {
  1360. return
  1361. }
  1362. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  1363. if err != nil {
  1364. err = fmt.Errorf("日期解析失败: %s", err.Error())
  1365. return
  1366. }
  1367. }
  1368. }
  1369. }
  1370. }
  1371. return
  1372. }
  1373. // 添加指标系列和数据
  1374. func AddSeries(edbInfoIds []int, chartInfoId, chartInfoSource int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string) (err error) {
  1375. edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  1376. if e != nil {
  1377. err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
  1378. return
  1379. }
  1380. if len(edbArr) == 0 {
  1381. err = fmt.Errorf("获取指标列表失败, 指标不存在")
  1382. return
  1383. }
  1384. edbInfoType := edbArr[0].EdbInfoType
  1385. // 新增指标系列
  1386. seriesItem := new(data_manage.FactorEdbSeries)
  1387. seriesItem.SeriesName = extraConf.SeriesName
  1388. seriesItem.EdbInfoType = edbInfoType
  1389. seriesItem.CreateTime = time.Now().Local()
  1390. seriesItem.ModifyTime = time.Now().Local()
  1391. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  1392. seriesItem.CalculateStep = calculatesJson
  1393. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  1394. for _, v := range edbArr {
  1395. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  1396. EdbInfoId: v.EdbInfoId,
  1397. EdbCode: v.EdbCode,
  1398. CreateTime: time.Now().Local(),
  1399. ModifyTime: time.Now().Local(),
  1400. })
  1401. }
  1402. seriesId, e := seriesItem.CreateSeriesAndMapping(seriesItem, mappings)
  1403. if e != nil {
  1404. err = fmt.Errorf("新增因子指标系列失败, Err: " + e.Error())
  1405. return
  1406. }
  1407. // 图表关联-此处添加的chart_info_id=0
  1408. seriesChartMapping := new(data_manage.FactorEdbSeriesChartMapping)
  1409. seriesChartMapping.CalculateType = data_manage.FactorEdbSeriesChartCalculateTypeRange
  1410. //新增图表和指标的映射关系
  1411. seriesChartMapping.CalculateData = ""
  1412. seriesChartMapping.FactorEdbSeriesId = seriesId
  1413. seriesChartMapping.ChartInfoId = chartInfoId
  1414. seriesChartMapping.Source = chartInfoSource
  1415. seriesChartMapping.CreateTime = time.Now().Local()
  1416. seriesChartMapping.ModifyTime = time.Now().Local()
  1417. if e = seriesChartMapping.Create(); e != nil {
  1418. err = fmt.Errorf("新增图表关联失败, Err: " + e.Error())
  1419. return
  1420. }
  1421. // todo 计算指标数据并存储
  1422. _, e = FactorEdbStepCalculateRange(seriesId, edbInfoIds, extraConf, false)
  1423. if e != nil {
  1424. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  1425. return
  1426. }
  1427. // 更新系列计算状态
  1428. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  1429. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  1430. seriesItem.ModifyTime = time.Now().Local()
  1431. if e = seriesItem.Update(cols); e != nil {
  1432. err = fmt.Errorf("更新因子指标系列计算状态失败, Err: " + e.Error())
  1433. return
  1434. }
  1435. return
  1436. }
  1437. func EditSeries(seriesMapping *data_manage.FactorEdbSeriesChartMapping, edbInfoIds []int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string, recalculate bool) (err error) {
  1438. seriesOb := new(data_manage.FactorEdbSeries)
  1439. seriesItem, e := seriesOb.GetItemById(seriesMapping.FactorEdbSeriesId)
  1440. if e != nil {
  1441. if e.Error() == utils.ErrNoRow() {
  1442. err = fmt.Errorf("因子指标系列不存在, Err: " + e.Error())
  1443. return
  1444. }
  1445. err = fmt.Errorf("获取因子指标系列失败, Err: " + e.Error())
  1446. return
  1447. }
  1448. edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  1449. if e != nil {
  1450. err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
  1451. return
  1452. }
  1453. if len(edbArr) == 0 {
  1454. err = fmt.Errorf("指标列表为空")
  1455. return
  1456. }
  1457. var calculateResp data_manage.FactorEdbSeriesStepCalculateResp
  1458. calculateResp.SeriesId = seriesItem.FactorEdbSeriesId
  1459. // 如果不需要进行重新计算(比如只改了系列名称)那么只更新指标系列
  1460. seriesItem.SeriesName = extraConf.SeriesName
  1461. seriesItem.EdbInfoType = edbArr[0].EdbInfoType
  1462. seriesItem.ModifyTime = time.Now().Local()
  1463. updateCols := []string{seriesOb.Cols().SeriesName, seriesOb.Cols().EdbInfoType, seriesOb.Cols().ModifyTime}
  1464. if !recalculate {
  1465. if e = seriesItem.Update(updateCols); e != nil {
  1466. err = fmt.Errorf("更新因子指标系列失败, Err: " + e.Error())
  1467. return
  1468. }
  1469. return
  1470. }
  1471. // 更新系列信息和指标关联
  1472. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  1473. seriesItem.CalculateStep = calculatesJson
  1474. updateCols = append(updateCols, seriesOb.Cols().CalculateState, seriesOb.Cols().CalculateStep)
  1475. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  1476. for _, v := range edbArr {
  1477. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  1478. EdbInfoId: v.EdbInfoId,
  1479. EdbCode: v.EdbCode,
  1480. CreateTime: time.Now().Local(),
  1481. ModifyTime: time.Now().Local(),
  1482. })
  1483. }
  1484. if e = seriesItem.EditSeriesAndMapping(seriesItem, mappings, updateCols); e != nil {
  1485. err = fmt.Errorf("更新因子指标系列信息失败, Err: %s", e.Error())
  1486. return
  1487. }
  1488. // todo 重新计算
  1489. _, e = FactorEdbStepCalculateRange(seriesItem.FactorEdbSeriesId, edbInfoIds, extraConf, false)
  1490. if e != nil {
  1491. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  1492. return
  1493. }
  1494. // 更新系列计算状态
  1495. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  1496. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  1497. seriesItem.ModifyTime = time.Now().Local()
  1498. if e = seriesItem.Update(cols); e != nil {
  1499. err = fmt.Errorf("更新因子指标系列计算状态失败, Err: %s", e.Error())
  1500. return
  1501. }
  1502. return
  1503. }
  1504. // FactorEdbStepCalculateRange 因子指标-区间计算
  1505. func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf data_manage.ChartRangeAnalysisExtraConf, recalculate bool) (calculateResp data_manage.FactorEdbSeriesStepCalculateResp, err error) {
  1506. // todo 如果指标已保存,则用指标数据还是图表指标数据?
  1507. // 获取图表x轴y轴
  1508. defer func() {
  1509. if err != nil {
  1510. tips := fmt.Sprintf("StepCalculate计算失败, ErrMsg: %v", err)
  1511. fmt.Println(tips)
  1512. utils.FileLog.Info(tips)
  1513. go alarm_msg.SendAlarmMsg(tips, 3)
  1514. }
  1515. /*if len(calculateResp.Fail) > 0 {
  1516. tips := "StepCalculate计算失败, ErrMsg: "
  1517. for _, f := range calculateResp.Fail {
  1518. tips += fmt.Sprintf("code: %s, err: %s\n", f.EdbCode, f.ErrMsg)
  1519. }
  1520. fmt.Println(tips)
  1521. utils.FileLog.Info(tips)
  1522. go alarm_msg.SendAlarmMsg(tips, 2)
  1523. }*/
  1524. }()
  1525. edbInfoMappingList, e := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbArr)
  1526. if e != nil {
  1527. err = fmt.Errorf("获取区间计算图表, A指标mapping信息失败, Err:%v", e)
  1528. return
  1529. }
  1530. _, _, _, err = GetChartDataByEdbInfoList(0, 0, 0, "", "", edbInfoMappingList, &extraConf)
  1531. if err != nil {
  1532. err = fmt.Errorf("获取图表数据失败, ErrMsg: %v", err)
  1533. return
  1534. }
  1535. // 重新计算-先清除原数据
  1536. calculateDataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
  1537. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  1538. pars := make([]interface{}, 0)
  1539. pars = append(pars, seriesId)
  1540. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  1541. err = fmt.Errorf("清除原数据失败, err: %v", e)
  1542. return
  1543. }
  1544. // 计算成功的保存结果
  1545. dataArr := make([]*data_manage.FactorEdbSeriesCalculateDataQjjs, 0)
  1546. for _, v := range edbInfoMappingList {
  1547. dataList := v.DataList.([]*data_manage.EdbDataList)
  1548. for _, dataItem := range dataList {
  1549. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  1550. dataArr = append(dataArr, &data_manage.FactorEdbSeriesCalculateDataQjjs{
  1551. FactorEdbSeriesId: seriesId,
  1552. EdbInfoId: v.EdbInfoId,
  1553. EdbCode: v.EdbCode,
  1554. DataTime: dataTime,
  1555. Value: dataItem.Value,
  1556. CreateTime: time.Now().Local(),
  1557. ModifyTime: time.Now().Local(),
  1558. DataTimestamp: dataItem.DataTimestamp,
  1559. })
  1560. }
  1561. }
  1562. if len(dataArr) == 0 {
  1563. err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
  1564. return
  1565. }
  1566. if e = calculateDataOb.CreateMulti(dataArr); e != nil {
  1567. err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
  1568. return
  1569. }
  1570. return
  1571. }
  1572. func CheckChartRangeExtraConfig(extraConfig data_manage.ChartRangeAnalysisExtraConf) (err error, errMsg string, isSendEmail bool) {
  1573. extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
  1574. if extraConfig.SeriesName == "" && extraConfig.EdbInfoMode == 1 {
  1575. errMsg = "请输入指标系列名称"
  1576. err = errors.New(errMsg)
  1577. isSendEmail = false
  1578. return
  1579. }
  1580. if extraConfig.CalculateType > 5 || extraConfig.CalculateType < 0 {
  1581. errMsg = "计算方式参数错误"
  1582. err = errors.New(errMsg)
  1583. isSendEmail = false
  1584. return
  1585. }
  1586. switch extraConfig.DateRangeType {
  1587. case 0:
  1588. case 1:
  1589. if len(extraConfig.ManualDateConf) == 0 {
  1590. errMsg = "请选择时间区间"
  1591. err = errors.New(errMsg)
  1592. return
  1593. }
  1594. // 先按开始时间排序
  1595. sort.Sort(data_manage.ChartRangeAnalysisManualDateConfList(extraConfig.ManualDateConf))
  1596. // 校验日期
  1597. // 1.如果截止时间小于指标的截止日期,需要重置为指标的截止日期
  1598. // 2.时间区间不能重叠
  1599. for i := 1; i < len(extraConfig.ManualDateConf); i++ {
  1600. start1, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i-1].EndDate)
  1601. if e != nil {
  1602. err = e
  1603. errMsg = "截止日期格式有误"
  1604. return
  1605. }
  1606. start2, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].EndDate)
  1607. if e != nil {
  1608. err = e
  1609. errMsg = "截止日期格式有误"
  1610. return
  1611. }
  1612. start3, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].StartDate)
  1613. if e != nil {
  1614. err = e
  1615. errMsg = "截止日期格式有误"
  1616. return
  1617. }
  1618. // 如果当前区间的开始时间小于等于前一个区间的结束时间,则存在重叠
  1619. if !start2.After(start1) || start3.Before(start1) {
  1620. errMsg = "日期区间存在重叠"
  1621. return
  1622. }
  1623. }
  1624. //如果截止时间大于指标的截止日期,需要重置为指标的截止日期
  1625. case 2:
  1626. if extraConfig.YearDateConf.StartDay == "" || extraConfig.YearDateConf.EndDay == "" {
  1627. errMsg = "请选择时间区间"
  1628. return
  1629. }
  1630. if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.StartDay); e != nil {
  1631. errMsg = "开始日期格式有误"
  1632. return
  1633. }
  1634. if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.EndDay); e != nil {
  1635. errMsg = "结束日期格式有误"
  1636. return
  1637. }
  1638. }
  1639. return
  1640. }