chart_info.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_chart_lib/models"
  6. "eta/eta_chart_lib/models/data_manage"
  7. "eta/eta_chart_lib/services/alarm_msg"
  8. "eta/eta_chart_lib/services/data"
  9. "eta/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. if edbInfoMapping.ConvertUnit == "无" {
  413. edbInfoMapping.ConvertUnit = ""
  414. }
  415. if edbInfoMapping.ConvertEnUnit == "无" {
  416. edbInfoMapping.ConvertEnUnit = ""
  417. }
  418. dataList := edbInfoMapping.DataList.([]*models.EdbDataList)
  419. // 处理上下限
  420. var maxData, minData float64
  421. if len(dataList) > 0 {
  422. maxData = dataList[0].Value
  423. minData = dataList[0].Value
  424. for _, v := range dataList {
  425. if v.Value > maxData {
  426. maxData = v.Value
  427. }
  428. if v.Value < minData {
  429. minData = v.Value
  430. }
  431. }
  432. }
  433. edbInfoMapping.MaxData = maxData
  434. edbInfoMapping.MinData = minData
  435. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  436. }
  437. //根据时间类型来筛选最终的数据
  438. var dateMax time.Time
  439. if dateType == utils.DateTypeNYears {
  440. for _, v := range edbInfoMappingList {
  441. dataList := v.DataList.([]*models.EdbDataList)
  442. if len(dataList) > 0 {
  443. latestDate := dataList[len(dataList)-1].DataTime
  444. if latestDate != "" {
  445. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  446. if tErr != nil {
  447. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  448. }
  449. if lastDateT.After(dateMax) {
  450. dateMax = lastDateT
  451. }
  452. }
  453. }
  454. }
  455. }
  456. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, dateMax)
  457. if startDate != "" {
  458. for k, v := range edbInfoMappingList {
  459. var maxData, minData float64
  460. dataList := v.DataList.([]*models.EdbDataList)
  461. newDataList := make([]*models.EdbDataList, 0)
  462. if len(dataList) == 0 {
  463. newDataList = dataList
  464. } else {
  465. firstFlag := true
  466. for _, d := range dataList {
  467. if endDate != "" && d.DataTime > endDate {
  468. continue
  469. }
  470. if d.DataTime < startDate {
  471. continue
  472. }
  473. newDataList = append(newDataList, d)
  474. if firstFlag {
  475. maxData = d.Value
  476. minData = d.Value
  477. firstFlag = false
  478. } else {
  479. if d.Value > maxData {
  480. maxData = d.Value
  481. }
  482. if d.Value < minData {
  483. minData = d.Value
  484. }
  485. }
  486. }
  487. }
  488. edbInfoMappingList[k].DataList = newDataList
  489. edbInfoMappingList[k].MinData = minData
  490. edbInfoMappingList[k].MaxData = maxData
  491. }
  492. }
  493. dataResp = models.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
  494. if req.MultipleGraphConfigId > 0 {
  495. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  496. if e != nil && e.Error() != utils.ErrNoRow() {
  497. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  498. return
  499. }
  500. // 查询是否已经生成指标
  501. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  502. }
  503. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  504. if err != nil {
  505. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  506. return
  507. }
  508. return
  509. }
  510. 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) {
  511. //查询seriesId
  512. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  513. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfoId)
  514. if err != nil {
  515. if err.Error() == utils.ErrNoRow() {
  516. err = fmt.Errorf("图表关联关系不存在")
  517. return
  518. } else {
  519. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  520. return
  521. }
  522. }
  523. //根据seriesId查询数据
  524. //并把数据放到dataList中
  525. for _, edbInfoMapping := range edbInfoMappingList {
  526. dataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
  527. dataList, e := dataOb.GetEdbDataList(seriesMappingItem.FactorEdbSeriesId, edbInfoMapping.EdbInfoId, startDate, endDate)
  528. if e != nil {
  529. err = e
  530. return
  531. }
  532. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  533. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  534. if req.CalculateType == 2 || req.CalculateType == 3 {
  535. edbInfoMapping.ConvertUnit = "无"
  536. edbInfoMapping.ConvertEnUnit = "无"
  537. }
  538. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  539. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  540. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  541. }
  542. if edbInfoMapping.ConvertUnit == "无" {
  543. edbInfoMapping.ConvertUnit = ""
  544. }
  545. if edbInfoMapping.ConvertEnUnit == "无" {
  546. edbInfoMapping.ConvertEnUnit = ""
  547. }
  548. edbInfoMapping.DataList = dataList
  549. // 处理上下限
  550. var maxData, minData float64
  551. if len(dataList) > 0 {
  552. maxData = dataList[0].Value
  553. minData = dataList[0].Value
  554. for _, v := range dataList {
  555. if v.Value > maxData {
  556. maxData = v.Value
  557. }
  558. if v.Value < minData {
  559. minData = v.Value
  560. }
  561. }
  562. }
  563. edbInfoMapping.MaxData = maxData
  564. edbInfoMapping.MinData = minData
  565. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  566. }
  567. var dateMax time.Time
  568. if dateType == utils.DateTypeNYears {
  569. for _, v := range edbInfoMappingList {
  570. dataList := v.DataList.([]*models.EdbDataList)
  571. latestDate := dataList[len(dataList)-1].DataTime
  572. if latestDate != "" {
  573. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  574. if tErr != nil {
  575. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  576. return
  577. }
  578. if lastDateT.After(dateMax) {
  579. dateMax = lastDateT
  580. }
  581. }
  582. }
  583. }
  584. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, dateMax)
  585. if startDate != "" {
  586. for k, v := range edbInfoMappingList {
  587. var maxData, minData float64
  588. dataList := v.DataList.([]*models.EdbDataList)
  589. newDataList := make([]*models.EdbDataList, 0)
  590. if len(dataList) == 0 {
  591. newDataList = dataList
  592. } else {
  593. firstFlag := true
  594. for _, d := range dataList {
  595. if endDate != "" && d.DataTime > endDate {
  596. continue
  597. }
  598. if d.DataTime < startDate {
  599. continue
  600. }
  601. newDataList = append(newDataList, d)
  602. if firstFlag {
  603. maxData = d.Value
  604. minData = d.Value
  605. firstFlag = false
  606. } else {
  607. if d.Value > maxData {
  608. maxData = d.Value
  609. }
  610. if d.Value < minData {
  611. minData = d.Value
  612. }
  613. }
  614. }
  615. }
  616. edbInfoMappingList[k].DataList = newDataList
  617. edbInfoMappingList[k].MinData = minData
  618. edbInfoMappingList[k].MaxData = maxData
  619. }
  620. }
  621. dataResp = models.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
  622. // 查询配置关联关系
  623. if req.MultipleGraphConfigId > 0 {
  624. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  625. if e != nil && e.Error() != utils.ErrNoRow() {
  626. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  627. return
  628. }
  629. // 查询是否已经生成指标
  630. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  631. }
  632. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  633. if err != nil {
  634. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  635. return
  636. }
  637. return
  638. }
  639. // getChartDataByEdbInfo 区间计算图表-根据指标信息获取x轴和y轴
  640. func getChartDataByEdbInfo(edbInfoMapping *models.ChartEdbInfoMapping, req *models.ChartRangeAnalysisExtraConf) (newEdbInfoMapping *models.ChartEdbInfoMapping, err error) {
  641. newEdbInfoMapping = edbInfoMapping
  642. // 指标的开始日期和结束日期
  643. edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
  644. //edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  645. edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
  646. edbEndDate := edbEndDateTime.Format(utils.FormatDate)
  647. // 获取时间基准指标在时间区间内的值
  648. dataList := make([]*models.EdbDataList, 0)
  649. switch edbInfoMapping.EdbInfoCategoryType {
  650. case 0:
  651. dataList, err = models.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
  652. case 1:
  653. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
  654. default:
  655. err = errors.New("指标base类型异常")
  656. return
  657. }
  658. dateList := make([]*models.ChartRangeAnalysisDateDataItem, 0)
  659. switch req.DateRangeType {
  660. case 0:
  661. // 智能划分得到一个开始日期,和结束日期
  662. var startDateTime, endDateTime time.Time
  663. startDateTime = edbStartDateTime
  664. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  665. startDate := req.AutoDateConf.StartDate
  666. if startDate == "" {
  667. startDate = "2020-01-01"
  668. }
  669. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  670. }
  671. if req.AutoDateConf.IsAutoEndDate == 0 { //固定设置
  672. endDate := req.AutoDateConf.EndDate
  673. if endDate == "" {
  674. err = fmt.Errorf("智能划分截止日期处理失败:请输入截止日期")
  675. return
  676. }
  677. // todo 如果截止日期比指标日期还要大,则用指标的最新日期
  678. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  679. } else {
  680. endConf := req.AutoDateConf.EndDateConf
  681. endDate := edbEndDate
  682. if endConf.MoveForward > 0 {
  683. endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
  684. }
  685. if len(endConf.DateChange) > 0 {
  686. endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
  687. if err != nil {
  688. err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
  689. return
  690. }
  691. }
  692. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  693. }
  694. dateList = append(dateList, &models.ChartRangeAnalysisDateDataItem{
  695. StartDate: startDateTime,
  696. EndDate: endDateTime})
  697. case 1:
  698. // 手工划分得到多个开始日期和结束日期(已排序)
  699. for _, v := range req.ManualDateConf {
  700. startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
  701. endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  702. tmp := &models.ChartRangeAnalysisDateDataItem{
  703. StartDate: startDateT,
  704. EndDate: endDateT,
  705. }
  706. dateList = append(dateList, tmp)
  707. }
  708. case 2:
  709. // 跨年划分得到多个开始日期和结束日期
  710. startYear := edbStartDateTime.Year()
  711. endYear := edbEndDateTime.Year()
  712. startDay := req.YearDateConf.StartDay
  713. endDay := req.YearDateConf.EndDay
  714. for year := startYear; year <= endYear; year++ {
  715. startDate := fmt.Sprintf("%d-%s", year, startDay)
  716. endDate := fmt.Sprintf("%d-%s", year+1, endDay)
  717. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  718. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  719. if startDateTime.Before(edbStartDateTime) {
  720. break
  721. }
  722. tmp := &models.ChartRangeAnalysisDateDataItem{
  723. StartDate: startDateTime,
  724. EndDate: endDateTime,
  725. }
  726. dateList = append(dateList, tmp)
  727. }
  728. }
  729. // 根据日期,获取数据
  730. for _, v := range dateList {
  731. for _, vv := range dataList {
  732. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  733. if dataTimeT.After(v.StartDate) && dataTimeT.Before(v.EndDate) ||
  734. dataTimeT.Equal(v.StartDate) ||
  735. dataTimeT.Equal(v.EndDate) {
  736. v.DataList = append(v.DataList, vv)
  737. }
  738. }
  739. }
  740. // 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
  741. newDataList, err := HandleDataByCalculateType(dateList, dataList, req)
  742. if err != nil {
  743. return
  744. }
  745. if req.UnNormalDataDealType > 0 {
  746. switch req.UnNormalDataDealType { //0:不处理,1:剔除,2替换
  747. case 1:
  748. dealDataList := make([]*models.EdbDataList, 0)
  749. for _, v := range newDataList {
  750. if !utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  751. dealDataList = append(dealDataList, v)
  752. }
  753. }
  754. newDataList = dealDataList
  755. case 2:
  756. for i, v := range newDataList {
  757. if utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  758. newDataList[i].Value = req.UnNormalDataConf.ReplaceValue
  759. }
  760. }
  761. }
  762. }
  763. if req.DataConvertType > 0 {
  764. // 数据转换类型 0不转, 1乘 2除 3对数
  765. switch req.DataConvertType {
  766. case 1:
  767. for i, v := range newDataList {
  768. val := v.Value * req.DataConvertConf.Value
  769. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  770. newDataList[i].Value = val
  771. }
  772. //item.MaxData = item.MaxData * v.ConvertValue
  773. //item.MinData = item.MinData * v.ConvertValue
  774. case 2:
  775. for i, v := range newDataList {
  776. val := v.Value / req.DataConvertConf.Value
  777. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  778. newDataList[i].Value = val
  779. }
  780. //item.MaxData = item.MaxData / v.ConvertValue
  781. //item.MinData = item.MinData / v.ConvertValue
  782. case 3:
  783. for i, v := range newDataList {
  784. if v.Value <= 0 {
  785. err = errors.New("数据中含有负数或0,无法对数运算")
  786. return
  787. }
  788. val := math.Log(v.Value) / math.Log(req.DataConvertConf.Value)
  789. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  790. newDataList[i].Value = val
  791. }
  792. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  793. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  794. }
  795. }
  796. newEdbInfoMapping.DataList = newDataList
  797. //时间处理
  798. return
  799. }
  800. // RollingCorrelationChartDataResp 滚动区间计算图表数据
  801. type RollingCorrelationChartDataResp struct {
  802. MaxData float64
  803. MinData float64
  804. LatestDate string `description:"真实数据的最后日期"`
  805. EdbInfoCategoryType int
  806. ChartColor string
  807. ChartStyle string
  808. PredictChartColor string
  809. ChartType int
  810. ChartWidth int
  811. EdbName string
  812. EdbNameEn string
  813. Unit string
  814. UnitEn string
  815. IsAxis int
  816. DataList []models.EdbDataList
  817. }
  818. // ChartInfoRefresh 图表刷新
  819. func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
  820. var errMsg string
  821. defer func() {
  822. if err != nil {
  823. tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
  824. utils.FileLog.Info(tips)
  825. go alarm_msg.SendAlarmMsg(tips, 3)
  826. }
  827. }()
  828. var chartInfo *models.ChartInfo
  829. if chartInfoId > 0 {
  830. chartInfo, err = models.GetChartInfoById(chartInfoId)
  831. if err != nil {
  832. if err.Error() == utils.ErrNoRow() {
  833. errMsg = "图表已被删除,请刷新页面"
  834. err = errors.New(errMsg)
  835. return
  836. }
  837. errMsg = "获取图表信息失败"
  838. err = errors.New("获取图表信息失败,Err:" + err.Error())
  839. return
  840. }
  841. } else {
  842. chartInfo, err = models.GetChartInfoByUniqueCode(uniqueCode)
  843. if err != nil {
  844. if err.Error() == utils.ErrNoRow() {
  845. errMsg = "图表已被删除,请刷新页面"
  846. err = errors.New(errMsg)
  847. return
  848. }
  849. errMsg = "获取图表信息失败"
  850. err = errors.New("获取图表信息失败,Err:" + err.Error())
  851. return
  852. }
  853. }
  854. // 1.刷新图表关联的指标
  855. mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
  856. if e != nil {
  857. utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
  858. return
  859. }
  860. if len(mappings) == 0 {
  861. utils.FileLog.Info("图表无关联指标")
  862. return
  863. }
  864. var edbIds []int
  865. for _, v := range mappings {
  866. edbIds = append(edbIds, v.EdbInfoId)
  867. }
  868. if e, _ = data.EdbInfoRefreshAllFromBase(edbIds, false); e != nil {
  869. utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
  870. return
  871. }
  872. // todo 重新计算
  873. // 区间计算图表配置校验
  874. var extraConfig models.ChartRangeAnalysisExtraConf
  875. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  876. if err != nil {
  877. errMsg = "配置信息错误"
  878. err = errors.New(errMsg + ", Err: " + err.Error())
  879. return
  880. }
  881. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  882. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfo.ChartInfoId)
  883. if err != nil {
  884. if err.Error() == utils.ErrNoRow() {
  885. err = nil
  886. } else {
  887. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  888. return
  889. }
  890. } else {
  891. _, e = FactorEdbStepCalculateRange(seriesMappingItem.FactorEdbSeriesId, edbIds, extraConfig, true)
  892. if e != nil {
  893. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  894. return
  895. }
  896. }
  897. // 4.清除图表缓存
  898. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  899. _ = utils.Rc.Delete(key)
  900. return
  901. }
  902. func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*models.EdbDataList) (date string) {
  903. // 根据日期进行排序
  904. index := 0
  905. length := len(edbDataList)
  906. for i := length - 1; i >= 0; i-- {
  907. item := edbDataList[i]
  908. if item.DataTime == startDate {
  909. index += 1
  910. continue
  911. }
  912. if index >= moveForward {
  913. date = item.DataTime
  914. break
  915. }
  916. if index > 0 {
  917. index += 1
  918. date = item.DataTime
  919. }
  920. }
  921. return
  922. }
  923. // HandleEdbDateChange 处理日期变换
  924. func HandleEdbDateChange(date string, dateChange []*models.EdbDataDateChangeConf) (newDate string, err error) {
  925. newDate = date
  926. if newDate != "" {
  927. if len(dateChange) > 0 {
  928. var dateTime time.Time
  929. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  930. if err != nil {
  931. err = fmt.Errorf("日期解析失败: %s", err.Error())
  932. return
  933. }
  934. for _, v := range dateChange {
  935. if v.ChangeType == 1 {
  936. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  937. newDate = dateTime.Format(utils.FormatDate)
  938. } else if v.ChangeType == 2 {
  939. newDate, err, _ = utils.HandleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  940. if err != nil {
  941. return
  942. }
  943. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  944. if err != nil {
  945. err = fmt.Errorf("日期解析失败: %s", err.Error())
  946. return
  947. }
  948. }
  949. }
  950. }
  951. }
  952. return
  953. }
  954. // FactorEdbStepCalculateRange 因子指标-区间计算
  955. func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf models.ChartRangeAnalysisExtraConf, recalculate bool) (calculateResp data_manage.FactorEdbSeriesStepCalculateResp, err error) {
  956. // todo 如果指标已保存,则用指标数据还是图表指标数据?
  957. // 获取图表x轴y轴
  958. defer func() {
  959. if err != nil {
  960. tips := fmt.Sprintf("FactorEdbStepCalculateRange 区间计算, ErrMsg: %v", err)
  961. fmt.Println(tips)
  962. utils.FileLog.Info(tips)
  963. go alarm_msg.SendAlarmMsg(tips, 3)
  964. }
  965. /*if len(calculateResp.Fail) > 0 {
  966. tips := "StepCalculate计算失败, ErrMsg: "
  967. for _, f := range calculateResp.Fail {
  968. tips += fmt.Sprintf("code: %s, err: %s\n", f.EdbCode, f.ErrMsg)
  969. }
  970. fmt.Println(tips)
  971. utils.FileLog.Info(tips)
  972. go alarm_msg.SendAlarmMsg(tips, 2)
  973. }*/
  974. }()
  975. edbInfoMappingList, e := models.GetChartEdbMappingListByEdbInfoIdList(edbArr)
  976. if e != nil {
  977. err = fmt.Errorf("获取区间计算图表, A指标mapping信息失败, Err:%v", e)
  978. return
  979. }
  980. _, _, _, err = GetChartDataByEdbInfoList(0, 0, 0, "", "", edbInfoMappingList, &extraConf)
  981. if err != nil {
  982. err = fmt.Errorf("获取图表数据失败, ErrMsg: %v", err)
  983. return
  984. }
  985. // 重新计算-先清除原数据
  986. calculateDataOb := new(models.FactorEdbSeriesCalculateDataQjjs)
  987. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  988. pars := make([]interface{}, 0)
  989. pars = append(pars, seriesId)
  990. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  991. err = fmt.Errorf("清除原数据失败, err: %v", e)
  992. return
  993. }
  994. // 计算成功的保存结果
  995. dataArr := make([]*models.FactorEdbSeriesCalculateDataQjjs, 0)
  996. for _, v := range edbInfoMappingList {
  997. dataList := v.DataList.([]*models.EdbDataList)
  998. for _, dataItem := range dataList {
  999. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  1000. dataArr = append(dataArr, &models.FactorEdbSeriesCalculateDataQjjs{
  1001. FactorEdbSeriesId: seriesId,
  1002. EdbInfoId: v.EdbInfoId,
  1003. EdbCode: v.EdbCode,
  1004. DataTime: dataTime,
  1005. Value: dataItem.Value,
  1006. CreateTime: time.Now().Local(),
  1007. ModifyTime: time.Now().Local(),
  1008. DataTimestamp: dataItem.DataTimestamp,
  1009. })
  1010. }
  1011. }
  1012. if len(dataArr) == 0 {
  1013. err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
  1014. return
  1015. }
  1016. if e = calculateDataOb.CreateMulti(dataArr); e != nil {
  1017. err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
  1018. return
  1019. }
  1020. return
  1021. }