chart_info.go 55 KB

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