chart_info.go 61 KB

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