chart_info.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_chart_lib/models"
  6. "eta_gn/eta_chart_lib/models/data_manage"
  7. "eta_gn/eta_chart_lib/services/alarm_msg"
  8. "eta_gn/eta_chart_lib/services/data"
  9. "eta_gn/eta_chart_lib/utils"
  10. "fmt"
  11. "math"
  12. "time"
  13. "github.com/shopspring/decimal"
  14. )
  15. // GetAutoCalculateDateDataList 获取当前时间相关的区间作为计算依据
  16. func GetAutoCalculateDateDataList(currentDate string, dataList []*models.EdbDataList, req *models.ChartRangeAnalysisExtraConf) (newDataList []*models.EdbDataList, err error) {
  17. currentDateTime, _ := time.ParseInLocation(utils.FormatDate, currentDate, time.Local)
  18. switch req.DateRangeType {
  19. case 0:
  20. // 智能划分得到一个开始日期,和结束日期
  21. var startDateTime time.Time
  22. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  23. startDate := req.AutoDateConf.StartDate
  24. if startDate == "" {
  25. startDate = "2020-01-01"
  26. }
  27. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  28. } else {
  29. startConf := req.AutoDateConf.StartDateConf
  30. startDate := ""
  31. if startConf.BaseDateType == 0 { //
  32. startDate = currentDate
  33. } else if startConf.BaseDateType == 1 {
  34. startDate = time.Now().Format(utils.FormatDate)
  35. }
  36. if startConf.MoveForward > 0 {
  37. startDate = GetEdbDateByMoveForward(startDate, startConf.MoveForward, dataList)
  38. }
  39. if len(startConf.DateChange) > 0 {
  40. startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
  41. if err != nil {
  42. err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
  43. return
  44. }
  45. }
  46. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  47. }
  48. var calStartTime, calEndTime time.Time
  49. if currentDateTime.Before(startDateTime) {
  50. calStartTime = currentDateTime
  51. calEndTime = startDateTime
  52. } else {
  53. calStartTime = startDateTime
  54. calEndTime = currentDateTime
  55. }
  56. // 根据日期,获取数据
  57. for _, vv := range dataList {
  58. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  59. if dataTimeT.After(calStartTime) && dataTimeT.Before(calEndTime) ||
  60. dataTimeT.Equal(calStartTime) ||
  61. dataTimeT.Equal(calEndTime) {
  62. newDataList = append(newDataList, vv)
  63. }
  64. }
  65. }
  66. return
  67. }
  68. // HandleDataByCalculateType 根据计算公式处理数据
  69. func HandleDataByCalculateType(originList []*models.ChartRangeAnalysisDateDataItem, originDataList []*models.EdbDataList, req *models.ChartRangeAnalysisExtraConf) (newList []*models.EdbDataList, err error) {
  70. if len(originList) == 0 {
  71. return
  72. }
  73. calculateType := req.CalculateType
  74. switch calculateType {
  75. case 0: //均值
  76. var sum float64
  77. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  78. for _, item := range originList {
  79. for _, v := range item.DataList {
  80. sum = 0
  81. //计算的数据返回需要重新确定
  82. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  83. if e != nil {
  84. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  85. return
  86. }
  87. for _, vv := range calDataList {
  88. sum += vv.Value
  89. }
  90. val := sum / float64(len(calDataList))
  91. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  92. newList = append(newList, &models.EdbDataList{
  93. DataTime: v.DataTime,
  94. Value: val,
  95. DataTimestamp: v.DataTimestamp,
  96. })
  97. }
  98. }
  99. } else {
  100. for _, item := range originList {
  101. sum = 0
  102. for k, v := range item.DataList {
  103. sum += v.Value
  104. val := sum / float64(k+1)
  105. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  106. newList = append(newList, &models.EdbDataList{
  107. DataTime: v.DataTime,
  108. Value: val,
  109. DataTimestamp: v.DataTimestamp,
  110. })
  111. }
  112. }
  113. }
  114. case 1: //累计值
  115. var sum float64
  116. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  117. for _, item := range originList {
  118. sum = 0
  119. for _, v := range item.DataList {
  120. sum = 0
  121. //计算的数据返回需要重新确定
  122. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  123. if e != nil {
  124. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  125. return
  126. }
  127. for _, vv := range calDataList {
  128. sum += vv.Value
  129. }
  130. val := sum
  131. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  132. newList = append(newList, &models.EdbDataList{
  133. DataTime: v.DataTime,
  134. Value: val,
  135. DataTimestamp: v.DataTimestamp,
  136. })
  137. }
  138. }
  139. } else {
  140. for _, item := range originList {
  141. sum = 0
  142. for _, v := range item.DataList {
  143. sum += v.Value
  144. val := sum
  145. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  146. newList = append(newList, &models.EdbDataList{
  147. DataTime: v.DataTime,
  148. Value: val,
  149. DataTimestamp: v.DataTimestamp,
  150. })
  151. }
  152. }
  153. }
  154. case 2: //涨幅
  155. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  156. for _, item := range originList {
  157. for _, v := range item.DataList {
  158. var baseVal float64
  159. //计算的数据返回需要重新确定
  160. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  161. if e != nil {
  162. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  163. return
  164. }
  165. if len(calDataList) == 0 {
  166. continue
  167. }
  168. baseVal = calDataList[0].Value
  169. baseDate := calDataList[0].DataTime
  170. if baseVal == 0 {
  171. continue
  172. }
  173. if v.DataTime == baseDate {
  174. continue
  175. }
  176. val := (v.Value - baseVal) / baseVal
  177. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  178. newList = append(newList, &models.EdbDataList{
  179. DataTime: v.DataTime,
  180. Value: val,
  181. DataTimestamp: v.DataTimestamp,
  182. })
  183. }
  184. }
  185. } else {
  186. for _, item := range originList {
  187. if len(item.DataList) == 0 {
  188. break
  189. }
  190. baseVal := item.DataList[0].Value
  191. baseDate := item.DataList[0].DataTime
  192. if baseVal == 0 {
  193. break
  194. }
  195. for _, v := range item.DataList {
  196. if v.DataTime == baseDate {
  197. continue
  198. }
  199. val := (v.Value - baseVal) / baseVal
  200. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  201. newList = append(newList, &models.EdbDataList{
  202. DataTime: v.DataTime,
  203. Value: val,
  204. DataTimestamp: v.DataTimestamp,
  205. })
  206. }
  207. }
  208. }
  209. case 3: //复合增长率
  210. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  211. for _, item := range originList {
  212. for _, v := range item.DataList {
  213. var baseVal float64
  214. var baseDate string
  215. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  216. if e != nil {
  217. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  218. return
  219. }
  220. if len(calDataList) == 0 {
  221. continue
  222. }
  223. baseVal = calDataList[0].Value
  224. baseDate = calDataList[0].DataTime
  225. if v.DataTime == baseDate {
  226. continue
  227. }
  228. if baseVal == 0 {
  229. continue
  230. }
  231. baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
  232. if e != nil {
  233. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  234. return
  235. }
  236. tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  237. if e != nil {
  238. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  239. return
  240. }
  241. // 计算两个日期相差的天数
  242. diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
  243. val := v.Value / baseVal
  244. val = math.Pow(val, 1/diff) - 1
  245. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  246. newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  247. }
  248. }
  249. } else {
  250. for _, item := range originList {
  251. if len(item.DataList) == 0 {
  252. break
  253. }
  254. baseVal := item.DataList[0].Value
  255. baseDate := item.DataList[0].DataTime
  256. if baseVal == 0 {
  257. break
  258. }
  259. for _, v := range item.DataList {
  260. if v.DataTime == baseDate {
  261. continue
  262. }
  263. baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
  264. if e != nil {
  265. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  266. return
  267. }
  268. tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  269. if e != nil {
  270. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  271. return
  272. }
  273. // 计算两个日期相差的天数
  274. diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
  275. val := v.Value / baseVal
  276. val = math.Pow(val, 1/diff) - 1
  277. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  278. newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  279. }
  280. }
  281. }
  282. case 4: //最大值
  283. var maxVal float64
  284. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  285. for _, item := range originList {
  286. for _, v := range item.DataList {
  287. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  288. if e != nil {
  289. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  290. return
  291. }
  292. for kk, vv := range calDataList {
  293. if kk == 0 {
  294. maxVal = vv.Value
  295. }
  296. if vv.Value > maxVal {
  297. maxVal = vv.Value
  298. }
  299. }
  300. val := maxVal
  301. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  302. newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  303. }
  304. }
  305. } else {
  306. for _, item := range originList {
  307. for k, v := range item.DataList {
  308. if k == 0 {
  309. maxVal = v.Value
  310. }
  311. if v.Value > maxVal {
  312. maxVal = v.Value
  313. }
  314. val := maxVal
  315. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  316. newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  317. }
  318. }
  319. }
  320. case 5: //最小值
  321. var minVal float64
  322. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  323. for _, item := range originList {
  324. for _, v := range item.DataList {
  325. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  326. if e != nil {
  327. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  328. return
  329. }
  330. for kk, vv := range calDataList {
  331. if kk == 0 {
  332. minVal = vv.Value
  333. }
  334. if vv.Value < minVal {
  335. minVal = vv.Value
  336. }
  337. }
  338. val := minVal
  339. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  340. newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  341. }
  342. }
  343. } else {
  344. for _, item := range originList {
  345. for k, v := range item.DataList {
  346. if k == 0 {
  347. minVal = v.Value
  348. }
  349. if v.Value < minVal {
  350. minVal = v.Value
  351. }
  352. val := minVal
  353. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  354. newList = append(newList, &models.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  355. }
  356. }
  357. }
  358. }
  359. return
  360. }
  361. // GetChartEdbInfoFormat 区间计算图表-获取指标信息
  362. func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingList []*models.ChartEdbInfoMapping) (edbList []*models.ChartEdbInfoMapping, err error) {
  363. edbList = make([]*models.ChartEdbInfoMapping, 0)
  364. for _, edbInfoMapping := range edbInfoMappingList {
  365. if edbInfoMapping == nil {
  366. err = fmt.Errorf("指标信息有误")
  367. return
  368. }
  369. edbInfoMapping.FrequencyEn = data.GetFrequencyEn(edbInfoMapping.Frequency)
  370. if edbInfoMapping.Unit == `无` {
  371. edbInfoMapping.Unit = ``
  372. }
  373. if chartInfoId <= 0 {
  374. edbInfoMapping.IsAxis = 1
  375. edbInfoMapping.LeadValue = 0
  376. edbInfoMapping.LeadUnit = ""
  377. edbInfoMapping.ChartEdbMappingId = 0
  378. edbInfoMapping.ChartInfoId = 0
  379. edbInfoMapping.IsOrder = false
  380. edbInfoMapping.EdbInfoType = 1
  381. edbInfoMapping.ChartStyle = ""
  382. edbInfoMapping.ChartColor = ""
  383. edbInfoMapping.ChartWidth = 0
  384. } else {
  385. edbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(edbInfoMapping.LeadUnit)
  386. }
  387. edbList = append(edbList, edbInfoMapping)
  388. }
  389. return
  390. }
  391. // GetChartDataByEdbInfoList 区间计算图表-根据指标信息获取x轴和y轴
  392. func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*models.ChartEdbInfoMapping, req *models.ChartRangeAnalysisExtraConf) (edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, dataResp models.ChartRangeAnalysisDataResp, err error) {
  393. if chartInfoId > 0 && req.EdbInfoMode == 1 {
  394. edbList, xEdbIdValue, dataResp, err = GetChartDataByEdbInfoListBySeries(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, req)
  395. return
  396. }
  397. for _, edbInfoMapping := range edbInfoMappingList {
  398. edbInfoMapping, err = getChartDataByEdbInfo(edbInfoMapping, req)
  399. if err != nil {
  400. return
  401. }
  402. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  403. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  404. if req.CalculateType == 2 || req.CalculateType == 3 {
  405. edbInfoMapping.ConvertUnit = "无"
  406. edbInfoMapping.ConvertEnUnit = "无"
  407. }
  408. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  409. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  410. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  411. }
  412. dataList := edbInfoMapping.DataList.([]*models.EdbDataList)
  413. // 处理上下限
  414. var maxData, minData float64
  415. if len(dataList) > 0 {
  416. maxData = dataList[0].Value
  417. minData = dataList[0].Value
  418. for _, v := range dataList {
  419. if v.Value > maxData {
  420. maxData = v.Value
  421. }
  422. if v.Value < minData {
  423. minData = v.Value
  424. }
  425. }
  426. }
  427. edbInfoMapping.MaxData = maxData
  428. edbInfoMapping.MinData = minData
  429. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  430. }
  431. //根据时间类型来筛选最终的数据
  432. yearMax := 0
  433. if dateType == utils.DateTypeNYears {
  434. for _, v := range edbInfoMappingList {
  435. dataList := v.DataList.([]*models.EdbDataList)
  436. if len(dataList) > 0 {
  437. latestDate := dataList[len(dataList)-1].DataTime
  438. if latestDate != "" {
  439. latestDate = utils.GormDateStrToDateStr(latestDate)
  440. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  441. if tErr != nil {
  442. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  443. return
  444. }
  445. if lastDateT.Year() > yearMax {
  446. yearMax = lastDateT.Year()
  447. }
  448. }
  449. }
  450. }
  451. }
  452. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  453. if startDate != "" {
  454. for k, v := range edbInfoMappingList {
  455. var maxData, minData float64
  456. dataList := v.DataList.([]*models.EdbDataList)
  457. newDataList := make([]*models.EdbDataList, 0)
  458. if len(dataList) == 0 {
  459. newDataList = dataList
  460. } else {
  461. firstFlag := true
  462. for _, d := range dataList {
  463. if endDate != "" && d.DataTime > endDate {
  464. continue
  465. }
  466. if d.DataTime < startDate {
  467. continue
  468. }
  469. newDataList = append(newDataList, d)
  470. if firstFlag {
  471. maxData = d.Value
  472. minData = d.Value
  473. firstFlag = false
  474. } else {
  475. if d.Value > maxData {
  476. maxData = d.Value
  477. }
  478. if d.Value < minData {
  479. minData = d.Value
  480. }
  481. }
  482. }
  483. }
  484. edbInfoMappingList[k].DataList = newDataList
  485. edbInfoMappingList[k].MinData = minData
  486. edbInfoMappingList[k].MaxData = maxData
  487. }
  488. }
  489. dataResp = models.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
  490. if req.MultipleGraphConfigId > 0 {
  491. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  492. if e != nil && e.Error() != utils.ErrNoRow() {
  493. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  494. return
  495. }
  496. // 查询是否已经生成指标
  497. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  498. }
  499. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  500. if err != nil {
  501. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  502. return
  503. }
  504. return
  505. }
  506. func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*models.ChartEdbInfoMapping, req *models.ChartRangeAnalysisExtraConf) (edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, dataResp models.ChartRangeAnalysisDataResp, err error) {
  507. //查询seriesId
  508. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  509. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfoId)
  510. if err != nil {
  511. if utils.IsErrNoRow(err) {
  512. err = fmt.Errorf("图表关联关系不存在")
  513. return
  514. } else {
  515. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  516. return
  517. }
  518. }
  519. //根据seriesId查询数据
  520. //并把数据放到dataList中
  521. for _, edbInfoMapping := range edbInfoMappingList {
  522. dataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
  523. dataList, e := dataOb.GetEdbDataList(seriesMappingItem.FactorEdbSeriesId, edbInfoMapping.EdbInfoId, startDate, endDate)
  524. if e != nil {
  525. err = e
  526. return
  527. }
  528. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  529. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  530. if req.CalculateType == 2 || req.CalculateType == 3 {
  531. edbInfoMapping.ConvertUnit = "无"
  532. edbInfoMapping.ConvertEnUnit = "无"
  533. }
  534. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  535. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  536. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  537. }
  538. edbInfoMapping.DataList = dataList
  539. // 处理上下限
  540. var maxData, minData float64
  541. if len(dataList) > 0 {
  542. maxData = dataList[0].Value
  543. minData = dataList[0].Value
  544. for _, v := range dataList {
  545. if v.Value > maxData {
  546. maxData = v.Value
  547. }
  548. if v.Value < minData {
  549. minData = v.Value
  550. }
  551. }
  552. }
  553. edbInfoMapping.MaxData = maxData
  554. edbInfoMapping.MinData = minData
  555. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  556. }
  557. yearMax := 0
  558. if dateType == utils.DateTypeNYears {
  559. for _, v := range edbInfoMappingList {
  560. dataList := v.DataList.([]*models.EdbDataList)
  561. latestDate := dataList[len(dataList)-1].DataTime
  562. if latestDate != "" {
  563. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  564. if tErr != nil {
  565. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  566. return
  567. }
  568. if lastDateT.Year() > yearMax {
  569. yearMax = lastDateT.Year()
  570. }
  571. }
  572. }
  573. }
  574. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  575. if startDate != "" {
  576. for k, v := range edbInfoMappingList {
  577. var maxData, minData float64
  578. dataList := v.DataList.([]*models.EdbDataList)
  579. newDataList := make([]*models.EdbDataList, 0)
  580. if len(dataList) == 0 {
  581. newDataList = dataList
  582. } else {
  583. firstFlag := true
  584. for _, d := range dataList {
  585. if endDate != "" && d.DataTime > endDate {
  586. continue
  587. }
  588. if d.DataTime < startDate {
  589. continue
  590. }
  591. newDataList = append(newDataList, d)
  592. if firstFlag {
  593. maxData = d.Value
  594. minData = d.Value
  595. firstFlag = false
  596. } else {
  597. if d.Value > maxData {
  598. maxData = d.Value
  599. }
  600. if d.Value < minData {
  601. minData = d.Value
  602. }
  603. }
  604. }
  605. }
  606. edbInfoMappingList[k].DataList = newDataList
  607. edbInfoMappingList[k].MinData = minData
  608. edbInfoMappingList[k].MaxData = maxData
  609. }
  610. }
  611. dataResp = models.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
  612. // 查询配置关联关系
  613. if req.MultipleGraphConfigId > 0 {
  614. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  615. if e != nil && e.Error() != utils.ErrNoRow() {
  616. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  617. return
  618. }
  619. // 查询是否已经生成指标
  620. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  621. }
  622. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  623. if err != nil {
  624. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  625. return
  626. }
  627. return
  628. }
  629. // getChartDataByEdbInfo 区间计算图表-根据指标信息获取x轴和y轴
  630. func getChartDataByEdbInfo(edbInfoMapping *models.ChartEdbInfoMapping, req *models.ChartRangeAnalysisExtraConf) (newEdbInfoMapping *models.ChartEdbInfoMapping, err error) {
  631. newEdbInfoMapping = edbInfoMapping
  632. // 指标的开始日期和结束日期
  633. edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
  634. //edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  635. edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
  636. edbEndDate := edbEndDateTime.Format(utils.FormatDate)
  637. // 获取时间基准指标在时间区间内的值
  638. dataList := make([]*models.EdbDataList, 0)
  639. switch edbInfoMapping.EdbInfoCategoryType {
  640. case 0:
  641. dataList, err = models.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
  642. case 1:
  643. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
  644. default:
  645. err = errors.New("指标base类型异常")
  646. return
  647. }
  648. dateList := make([]*models.ChartRangeAnalysisDateDataItem, 0)
  649. switch req.DateRangeType {
  650. case 0:
  651. // 智能划分得到一个开始日期,和结束日期
  652. var startDateTime, endDateTime time.Time
  653. startDateTime = edbStartDateTime
  654. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  655. startDate := req.AutoDateConf.StartDate
  656. if startDate == "" {
  657. startDate = "2020-01-01"
  658. }
  659. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  660. }
  661. if req.AutoDateConf.IsAutoEndDate == 0 { //固定设置
  662. endDate := req.AutoDateConf.EndDate
  663. if endDate == "" {
  664. err = fmt.Errorf("智能划分截止日期处理失败:请输入截止日期")
  665. return
  666. }
  667. // todo 如果截止日期比指标日期还要大,则用指标的最新日期
  668. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  669. } else {
  670. endConf := req.AutoDateConf.EndDateConf
  671. endDate := edbEndDate
  672. if endConf.MoveForward > 0 {
  673. endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
  674. }
  675. if len(endConf.DateChange) > 0 {
  676. endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
  677. if err != nil {
  678. err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
  679. return
  680. }
  681. }
  682. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  683. }
  684. dateList = append(dateList, &models.ChartRangeAnalysisDateDataItem{
  685. StartDate: startDateTime,
  686. EndDate: endDateTime})
  687. case 1:
  688. // 手工划分得到多个开始日期和结束日期(已排序)
  689. for _, v := range req.ManualDateConf {
  690. startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
  691. endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  692. tmp := &models.ChartRangeAnalysisDateDataItem{
  693. StartDate: startDateT,
  694. EndDate: endDateT,
  695. }
  696. dateList = append(dateList, tmp)
  697. }
  698. case 2:
  699. // 跨年划分得到多个开始日期和结束日期
  700. startYear := edbStartDateTime.Year()
  701. endYear := edbEndDateTime.Year()
  702. startDay := req.YearDateConf.StartDay
  703. endDay := req.YearDateConf.EndDay
  704. for year := startYear; year <= endYear; year++ {
  705. startDate := fmt.Sprintf("%d-%s", year, startDay)
  706. endDate := fmt.Sprintf("%d-%s", year+1, endDay)
  707. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  708. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  709. if startDateTime.Before(edbStartDateTime) {
  710. break
  711. }
  712. tmp := &models.ChartRangeAnalysisDateDataItem{
  713. StartDate: startDateTime,
  714. EndDate: endDateTime,
  715. }
  716. dateList = append(dateList, tmp)
  717. }
  718. }
  719. // 根据日期,获取数据
  720. for _, v := range dateList {
  721. for _, vv := range dataList {
  722. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  723. if dataTimeT.After(v.StartDate) && dataTimeT.Before(v.EndDate) ||
  724. dataTimeT.Equal(v.StartDate) ||
  725. dataTimeT.Equal(v.EndDate) {
  726. v.DataList = append(v.DataList, vv)
  727. }
  728. }
  729. }
  730. // 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
  731. newDataList, err := HandleDataByCalculateType(dateList, dataList, req)
  732. if err != nil {
  733. return
  734. }
  735. if req.UnNormalDataDealType > 0 {
  736. switch req.UnNormalDataDealType { //0:不处理,1:剔除,2替换
  737. case 1:
  738. dealDataList := make([]*models.EdbDataList, 0)
  739. for _, v := range newDataList {
  740. if !utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  741. dealDataList = append(dealDataList, v)
  742. }
  743. }
  744. case 2:
  745. for i, v := range newDataList {
  746. if utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  747. newDataList[i].Value = req.UnNormalDataConf.ReplaceValue
  748. }
  749. }
  750. }
  751. }
  752. if req.DataConvertType > 0 {
  753. // 数据转换类型 0不转, 1乘 2除 3对数
  754. switch req.DataConvertType {
  755. case 1:
  756. for i, v := range newDataList {
  757. val := v.Value * req.DataConvertConf.Value
  758. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  759. newDataList[i].Value = val
  760. }
  761. //item.MaxData = item.MaxData * v.ConvertValue
  762. //item.MinData = item.MinData * v.ConvertValue
  763. case 2:
  764. for i, v := range newDataList {
  765. val := v.Value / req.DataConvertConf.Value
  766. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  767. newDataList[i].Value = val
  768. }
  769. //item.MaxData = item.MaxData / v.ConvertValue
  770. //item.MinData = item.MinData / v.ConvertValue
  771. case 3:
  772. for i, v := range newDataList {
  773. if v.Value <= 0 {
  774. err = errors.New("数据中含有负数或0,无法对数运算")
  775. return
  776. }
  777. val := math.Log(v.Value) / math.Log(req.DataConvertConf.Value)
  778. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  779. newDataList[i].Value = val
  780. }
  781. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  782. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  783. }
  784. }
  785. newEdbInfoMapping.DataList = newDataList
  786. //时间处理
  787. return
  788. }
  789. // RollingCorrelationChartDataResp 滚动区间计算图表数据
  790. type RollingCorrelationChartDataResp struct {
  791. MaxData float64
  792. MinData float64
  793. LatestDate string `description:"真实数据的最后日期"`
  794. EdbInfoCategoryType int
  795. ChartColor string
  796. ChartStyle string
  797. PredictChartColor string
  798. ChartType int
  799. ChartWidth int
  800. EdbName string
  801. EdbNameEn string
  802. Unit string
  803. UnitEn string
  804. IsAxis int
  805. DataList []models.EdbDataList
  806. }
  807. // ChartInfoRefresh 图表刷新
  808. func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
  809. var errMsg string
  810. defer func() {
  811. if err != nil {
  812. tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
  813. utils.FileLog.Info(tips)
  814. go alarm_msg.SendAlarmMsg(tips, 3)
  815. }
  816. }()
  817. var chartInfo *models.ChartInfo
  818. if chartInfoId > 0 {
  819. chartInfo, err = models.GetChartInfoById(chartInfoId)
  820. if err != nil {
  821. if utils.IsErrNoRow(err) {
  822. errMsg = "图表已被删除,请刷新页面"
  823. err = errors.New(errMsg)
  824. return
  825. }
  826. errMsg = "获取图表信息失败"
  827. err = errors.New("获取图表信息失败,Err:" + err.Error())
  828. return
  829. }
  830. } else {
  831. chartInfo, err = models.GetChartInfoByUniqueCode(uniqueCode)
  832. if err != nil {
  833. if utils.IsErrNoRow(err) {
  834. errMsg = "图表已被删除,请刷新页面"
  835. err = errors.New(errMsg)
  836. return
  837. }
  838. errMsg = "获取图表信息失败"
  839. err = errors.New("获取图表信息失败,Err:" + err.Error())
  840. return
  841. }
  842. }
  843. // 1.刷新图表关联的指标
  844. mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
  845. if e != nil {
  846. utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
  847. return
  848. }
  849. if len(mappings) == 0 {
  850. utils.FileLog.Info("图表无关联指标")
  851. return
  852. }
  853. var edbIds []int
  854. for _, v := range mappings {
  855. edbIds = append(edbIds, v.EdbInfoId)
  856. }
  857. if e, _ = data.EdbInfoRefreshAllFromBase(edbIds, false); e != nil {
  858. utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
  859. return
  860. }
  861. // todo 重新计算
  862. // 区间计算图表配置校验
  863. var extraConfig models.ChartRangeAnalysisExtraConf
  864. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  865. if err != nil {
  866. errMsg = "配置信息错误"
  867. err = errors.New(errMsg + ", Err: " + err.Error())
  868. return
  869. }
  870. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  871. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfo.ChartInfoId)
  872. if err != nil {
  873. if utils.IsErrNoRow(err) {
  874. err = nil
  875. } else {
  876. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  877. return
  878. }
  879. } else {
  880. _, e = FactorEdbStepCalculateRange(seriesMappingItem.FactorEdbSeriesId, edbIds, extraConfig, true)
  881. if e != nil {
  882. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  883. return
  884. }
  885. }
  886. // 4.清除图表缓存
  887. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  888. _ = utils.Rc.Delete(key)
  889. return
  890. }
  891. func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*models.EdbDataList) (date string) {
  892. // 根据日期进行排序
  893. index := 0
  894. length := len(edbDataList)
  895. for i := length - 1; i >= 0; i-- {
  896. item := edbDataList[i]
  897. if item.DataTime == startDate {
  898. index += 1
  899. continue
  900. }
  901. if index >= moveForward {
  902. date = item.DataTime
  903. break
  904. }
  905. if index > 0 {
  906. index += 1
  907. date = item.DataTime
  908. }
  909. }
  910. return
  911. }
  912. // HandleEdbDateChange 处理日期变换
  913. func HandleEdbDateChange(date string, dateChange []*models.EdbDataDateChangeConf) (newDate string, err error) {
  914. newDate = date
  915. if newDate != "" {
  916. if len(dateChange) > 0 {
  917. var dateTime time.Time
  918. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  919. if err != nil {
  920. err = fmt.Errorf("日期解析失败: %s", err.Error())
  921. return
  922. }
  923. for _, v := range dateChange {
  924. if v.ChangeType == 1 {
  925. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  926. newDate = dateTime.Format(utils.FormatDate)
  927. } else if v.ChangeType == 2 {
  928. newDate, err, _ = utils.HandleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  929. if err != nil {
  930. return
  931. }
  932. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  933. if err != nil {
  934. err = fmt.Errorf("日期解析失败: %s", err.Error())
  935. return
  936. }
  937. }
  938. }
  939. }
  940. }
  941. return
  942. }
  943. // FactorEdbStepCalculateRange 因子指标-区间计算
  944. func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf models.ChartRangeAnalysisExtraConf, recalculate bool) (calculateResp data_manage.FactorEdbSeriesStepCalculateResp, err error) {
  945. // todo 如果指标已保存,则用指标数据还是图表指标数据?
  946. // 获取图表x轴y轴
  947. defer func() {
  948. if err != nil {
  949. tips := fmt.Sprintf("FactorEdbStepCalculateRange 区间计算, ErrMsg: %v", err)
  950. fmt.Println(tips)
  951. utils.FileLog.Info(tips)
  952. go alarm_msg.SendAlarmMsg(tips, 3)
  953. }
  954. /*if len(calculateResp.Fail) > 0 {
  955. tips := "StepCalculate计算失败, ErrMsg: "
  956. for _, f := range calculateResp.Fail {
  957. tips += fmt.Sprintf("code: %s, err: %s\n", f.EdbCode, f.ErrMsg)
  958. }
  959. fmt.Println(tips)
  960. utils.FileLog.Info(tips)
  961. go alarm_msg.SendAlarmMsg(tips, 2)
  962. }*/
  963. }()
  964. edbInfoMappingList, e := models.GetChartEdbMappingListByEdbInfoIdList(edbArr)
  965. if e != nil {
  966. err = fmt.Errorf("获取区间计算图表, A指标mapping信息失败, Err:%v", e)
  967. return
  968. }
  969. _, _, _, err = GetChartDataByEdbInfoList(0, 0, 0, "", "", edbInfoMappingList, &extraConf)
  970. if err != nil {
  971. err = fmt.Errorf("获取图表数据失败, ErrMsg: %v", err)
  972. return
  973. }
  974. // 重新计算-先清除原数据
  975. calculateDataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
  976. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  977. pars := make([]interface{}, 0)
  978. pars = append(pars, seriesId)
  979. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  980. err = fmt.Errorf("清除原数据失败, err: %v", e)
  981. return
  982. }
  983. // 计算成功的保存结果
  984. dataArr := make([]*models.FactorEdbSeriesCalculateDataQjjs, 0)
  985. for _, v := range edbInfoMappingList {
  986. dataList := v.DataList.([]*models.EdbDataList)
  987. for _, dataItem := range dataList {
  988. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  989. dataArr = append(dataArr, &models.FactorEdbSeriesCalculateDataQjjs{
  990. FactorEdbSeriesId: seriesId,
  991. EdbInfoId: v.EdbInfoId,
  992. EdbCode: v.EdbCode,
  993. DataTime: dataTime,
  994. Value: dataItem.Value,
  995. CreateTime: time.Now().Local(),
  996. ModifyTime: time.Now().Local(),
  997. DataTimestamp: dataItem.DataTimestamp,
  998. })
  999. }
  1000. }
  1001. if len(dataArr) == 0 {
  1002. err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
  1003. return
  1004. }
  1005. if e = calculateDataOb.CreateMulti(dataArr); e != nil {
  1006. err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
  1007. return
  1008. }
  1009. return
  1010. }