chart_info.go 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. package correlation
  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. // HandleDataByLinearRegression 线性方程插值法补全数据
  19. func HandleDataByLinearRegression(originList []*data_manage.EdbDataList, handleDataMap map[string]float64) (newList []*data_manage.EdbDataList, err error) {
  20. if len(originList) < 2 {
  21. return
  22. }
  23. var startEdbInfoData *data_manage.EdbDataList
  24. for _, v := range originList {
  25. handleDataMap[v.DataTime] = v.Value
  26. // 第一个数据就给过滤了,给后面的试用
  27. if startEdbInfoData == nil {
  28. startEdbInfoData = v
  29. newList = append(newList, &data_manage.EdbDataList{
  30. DataTime: v.DataTime,
  31. Value: v.Value,
  32. })
  33. continue
  34. }
  35. // 获取两条数据之间相差的天数
  36. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  37. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  38. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  39. betweenDay := betweenHour / 24
  40. // 如果相差一天,那么过滤
  41. if betweenDay <= 1 {
  42. startEdbInfoData = v
  43. newList = append(newList, &data_manage.EdbDataList{
  44. DataTime: v.DataTime,
  45. Value: v.Value,
  46. })
  47. continue
  48. }
  49. // 生成线性方程式
  50. var a, b float64
  51. {
  52. coordinateData := make([]utils.Coordinate, 0)
  53. tmpCoordinate1 := utils.Coordinate{
  54. X: 1,
  55. Y: startEdbInfoData.Value,
  56. }
  57. coordinateData = append(coordinateData, tmpCoordinate1)
  58. tmpCoordinate2 := utils.Coordinate{
  59. X: float64(betweenDay) + 1,
  60. Y: v.Value,
  61. }
  62. coordinateData = append(coordinateData, tmpCoordinate2)
  63. a, b = utils.GetLinearResult(coordinateData)
  64. if math.IsNaN(a) || math.IsNaN(b) {
  65. err = fmt.Errorf("线性方程公式生成失败")
  66. return
  67. }
  68. }
  69. // 生成对应的值
  70. {
  71. for i := 1; i < betweenDay; i++ {
  72. tmpDataTime := startDataTime.AddDate(0, 0, i)
  73. aDecimal := decimal.NewFromFloat(a)
  74. xDecimal := decimal.NewFromInt(int64(i) + 1)
  75. bDecimal := decimal.NewFromFloat(b)
  76. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  77. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  78. newList = append(newList, &data_manage.EdbDataList{
  79. DataTime: tmpDataTime.Format(utils.FormatDate),
  80. Value: val,
  81. })
  82. }
  83. }
  84. // 最后将自己赋值
  85. newList = append(newList, &data_manage.EdbDataList{
  86. EdbDataId: v.EdbDataId,
  87. DataTime: v.DataTime,
  88. Value: v.Value,
  89. })
  90. startEdbInfoData = v
  91. }
  92. return
  93. }
  94. // MoveDataDaysToNewDataList 平移指标数据生成新的数据序列
  95. func MoveDataDaysToNewDataList(dataList []*data_manage.EdbDataList, moveDay int) (newDataList []data_manage.EdbDataList, dateDataMap map[string]float64) {
  96. dateMap := make(map[time.Time]float64)
  97. var minDate, maxDate time.Time
  98. dateDataMap = make(map[string]float64)
  99. for _, v := range dataList {
  100. currDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  101. if minDate.IsZero() || currDate.Before(minDate) {
  102. minDate = currDate
  103. }
  104. if maxDate.IsZero() || currDate.After(maxDate) {
  105. maxDate = currDate
  106. }
  107. dateMap[currDate] = v.Value
  108. }
  109. // 处理领先、滞后数据
  110. newDateMap := make(map[time.Time]float64)
  111. for currDate, value := range dateMap {
  112. newDate := currDate.AddDate(0, 0, moveDay)
  113. newDateMap[newDate] = value
  114. }
  115. minDate = minDate.AddDate(0, 0, moveDay)
  116. maxDate = maxDate.AddDate(0, 0, moveDay)
  117. // 获取日期相差日
  118. dayNum := utils.GetTimeSubDay(minDate, maxDate)
  119. for i := 0; i <= dayNum; i++ {
  120. currDate := minDate.AddDate(0, 0, i)
  121. tmpValue, ok := newDateMap[currDate]
  122. if !ok {
  123. //找不到数据,那么就用前面的数据吧
  124. if len(newDataList)-1 < 0 {
  125. tmpValue = 0
  126. } else {
  127. tmpValue = newDataList[len(newDataList)-1].Value
  128. }
  129. }
  130. tmpData := data_manage.EdbDataList{
  131. DataTime: currDate.Format(utils.FormatDate),
  132. Value: tmpValue,
  133. }
  134. dateDataMap[tmpData.DataTime] = tmpData.Value
  135. newDataList = append(newDataList, tmpData)
  136. }
  137. return
  138. }
  139. // GetChartEdbInfoFormat 相关性图表-获取指标信息
  140. func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
  141. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  142. if edbInfoMappingA == nil || edbInfoMappingB == nil {
  143. err = fmt.Errorf("指标信息有误")
  144. return
  145. }
  146. edbInfoMappingA.FrequencyEn = data.GetFrequencyEn(edbInfoMappingA.Frequency)
  147. if edbInfoMappingA.Unit == `无` {
  148. edbInfoMappingA.Unit = ``
  149. }
  150. if edbInfoMappingB.Unit == `无` {
  151. edbInfoMappingB.Unit = ``
  152. }
  153. if chartInfoId <= 0 {
  154. edbInfoMappingA.IsAxis = 1
  155. edbInfoMappingA.LeadValue = 0
  156. edbInfoMappingA.LeadUnit = ""
  157. edbInfoMappingA.ChartEdbMappingId = 0
  158. edbInfoMappingA.ChartInfoId = 0
  159. edbInfoMappingA.IsOrder = false
  160. edbInfoMappingA.EdbInfoType = 1
  161. edbInfoMappingA.ChartStyle = ""
  162. edbInfoMappingA.ChartColor = ""
  163. edbInfoMappingA.ChartWidth = 0
  164. edbInfoMappingB.IsAxis = 1
  165. edbInfoMappingB.LeadValue = 0
  166. edbInfoMappingB.LeadUnit = ""
  167. edbInfoMappingB.ChartEdbMappingId = 0
  168. edbInfoMappingB.ChartInfoId = 0
  169. edbInfoMappingB.IsOrder = false
  170. edbInfoMappingB.EdbInfoType = 1
  171. edbInfoMappingB.ChartStyle = ""
  172. edbInfoMappingB.ChartColor = ""
  173. edbInfoMappingB.ChartWidth = 0
  174. } else {
  175. edbInfoMappingA.LeadUnitEn = data.GetLeadUnitEn(edbInfoMappingA.LeadUnit)
  176. edbInfoMappingB.LeadUnitEn = data.GetLeadUnitEn(edbInfoMappingB.LeadUnit)
  177. }
  178. edbList = append(edbList, edbInfoMappingA, edbInfoMappingB)
  179. return
  180. }
  181. // GetChartDataByEdbInfo 相关性图表-根据指标信息获取x轴和y轴
  182. func GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping, leadValue int, leadUnit, startDate, endDate string) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
  183. xData := make([]int, 0)
  184. yData := make([]float64, 0)
  185. if leadValue == 0 {
  186. xData = append(xData, 0)
  187. }
  188. if leadValue > 0 {
  189. leadMin := 0 - leadValue
  190. xLen := 2*leadValue + 1
  191. for i := 0; i < xLen; i++ {
  192. n := leadMin + i
  193. xData = append(xData, n)
  194. }
  195. }
  196. // 计算窗口,不包含第一天
  197. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  198. startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  199. //// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
  200. //baseEdbInfo := edbInfoMappingB
  201. //changeEdbInfo := edbInfoMappingA
  202. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  203. baseEdbInfo := edbInfoMappingA
  204. changeEdbInfo := edbInfoMappingB
  205. // 获取时间基准指标在时间区间内的值
  206. aDataList := make([]*data_manage.EdbDataList, 0)
  207. switch baseEdbInfo.EdbInfoCategoryType {
  208. case 0:
  209. aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
  210. case 1:
  211. _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, false)
  212. default:
  213. err = errors.New("指标base类型异常")
  214. return
  215. }
  216. // 获取变频指标所有日期的值, 插值法完善数据
  217. bDataList := make([]*data_manage.EdbDataList, 0)
  218. switch changeEdbInfo.EdbInfoCategoryType {
  219. case 0:
  220. bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
  221. case 1:
  222. _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
  223. default:
  224. err = errors.New("指标change类型异常")
  225. return
  226. }
  227. //changeDataMap := make(map[string]float64)
  228. //newChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  229. //if e != nil {
  230. // err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  231. // return
  232. //}
  233. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  234. baseDataList := make([]*data_manage.EdbDataList, 0)
  235. baseDataMap := make(map[string]float64)
  236. changeDataList := make([]*data_manage.EdbDataList, 0)
  237. changeDataMap := make(map[string]float64)
  238. // 先把低频指标升频为高频
  239. {
  240. frequencyIntMap := map[string]int{
  241. "日度": 1,
  242. "周度": 2,
  243. "旬度": 3,
  244. "月度": 4,
  245. "季度": 5,
  246. "年度": 6,
  247. }
  248. // 如果A指标是高频,那么就需要对B指标进行升频
  249. if frequencyIntMap[edbInfoMappingA.Frequency] < frequencyIntMap[edbInfoMappingB.Frequency] {
  250. tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  251. if e != nil {
  252. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  253. return
  254. }
  255. changeDataList = tmpNewChangeDataList
  256. baseDataList = aDataList
  257. for _, v := range baseDataList {
  258. baseDataMap[v.DataTime] = v.Value
  259. }
  260. } else if frequencyIntMap[edbInfoMappingA.Frequency] > frequencyIntMap[edbInfoMappingB.Frequency] {
  261. // 如果B指标是高频,那么就需要对A指标进行升频
  262. tmpNewChangeDataList, e := HandleDataByLinearRegression(aDataList, baseDataMap)
  263. if e != nil {
  264. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  265. return
  266. }
  267. baseDataList = tmpNewChangeDataList
  268. changeDataList = bDataList
  269. for _, v := range changeDataList {
  270. changeDataMap[v.DataTime] = v.Value
  271. }
  272. } else {
  273. baseDataList = aDataList
  274. for _, v := range baseDataList {
  275. baseDataMap[v.DataTime] = v.Value
  276. }
  277. changeDataList = bDataList
  278. for _, v := range changeDataList {
  279. changeDataMap[v.DataTime] = v.Value
  280. }
  281. }
  282. }
  283. // 计算不领先也不滞后时的相关系数
  284. baseCalculateData := make([]float64, 0)
  285. baseDataTimeArr := make([]string, 0)
  286. for i := range baseDataList {
  287. baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
  288. baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
  289. }
  290. //zeroBaseData := make([]float64, 0)
  291. //zeroCalculateData := make([]float64, 0)
  292. //for i := range baseDataTimeArr {
  293. // tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
  294. // tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
  295. // if ok1 && ok2 {
  296. // zeroBaseData = append(zeroBaseData, tmpBaseVal)
  297. // zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
  298. // }
  299. //}
  300. //if len(zeroBaseData) != len(zeroCalculateData) {
  301. // err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
  302. // return
  303. //}
  304. //zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
  305. //if leadValue == 0 {
  306. // yData = append(yData, zeroRatio)
  307. //}
  308. // 计算领先/滞后N期
  309. if leadValue > 0 {
  310. // 平移变频指标领先/滞后的日期(单位天)
  311. moveUnitDays := utils.FrequencyDaysMap[leadUnit]
  312. for i := range xData {
  313. //if xData[i] == 0 {
  314. // yData = append(yData, zeroRatio)
  315. // continue
  316. //}
  317. xCalculateData := make([]float64, 0)
  318. yCalculateData := make([]float64, 0)
  319. // 平移指定天数
  320. mDays := int(moveUnitDays) * xData[i]
  321. _, dMap := MoveDataDaysToNewDataList(changeDataList, mDays)
  322. // 取出对应的基准日期的值
  323. for i2 := range baseDataTimeArr {
  324. tmpDate := baseDataTimeArr[i2]
  325. if yVal, ok := dMap[tmpDate]; ok {
  326. xCalculateData = append(xCalculateData, baseCalculateData[i2])
  327. yCalculateData = append(yCalculateData, yVal)
  328. }
  329. }
  330. if len(yCalculateData) <= 0 {
  331. //err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
  332. //return
  333. // 领先滞后后,没有可以计算的数据了
  334. continue
  335. }
  336. // 公式计算出领先/滞后频度对应点的相关性系数
  337. ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
  338. yData = append(yData, ratio)
  339. }
  340. }
  341. xEdbIdValue = xData
  342. yDataList = make([]data_manage.YData, 0)
  343. yDate := "0000-00-00"
  344. yDataList = append(yDataList, data_manage.YData{
  345. Date: yDate,
  346. Value: yData,
  347. })
  348. return
  349. }
  350. // RollingCorrelationChartDataResp 滚动相关性图表数据
  351. type RollingCorrelationChartDataResp struct {
  352. MaxData float64
  353. MinData float64
  354. LatestDate string `description:"真实数据的最后日期"`
  355. EdbInfoCategoryType int
  356. ChartColor string
  357. ChartStyle string
  358. PredictChartColor string
  359. ChartType int
  360. ChartWidth int
  361. EdbName string
  362. EdbNameEn string
  363. Unit string
  364. UnitEn string
  365. IsAxis int
  366. DataList []data_manage.EdbDataList
  367. }
  368. // GetRollingCorrelationChartDataByEdbInfo 滚动相关性计算
  369. func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping, leadValue int, leadUnit string, calculateValue int, calculateUnit string, startDate, endDate, chartName, chartNameEn string) (dataResp RollingCorrelationChartDataResp, err error) {
  370. dataResp = RollingCorrelationChartDataResp{
  371. DataList: make([]data_manage.EdbDataList, 0),
  372. MaxData: 0,
  373. MinData: 0,
  374. ChartColor: "#00f",
  375. ChartStyle: `spline`,
  376. PredictChartColor: `#00f`,
  377. ChartType: 0,
  378. ChartWidth: 3,
  379. EdbName: chartName,
  380. EdbNameEn: chartNameEn,
  381. IsAxis: 1,
  382. }
  383. dataList := make([]data_manage.EdbDataList, 0)
  384. // 计算窗口,不包含第一天
  385. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  386. startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  387. baseEdbInfo := edbInfoMappingA
  388. changeEdbInfo := edbInfoMappingB
  389. // 获取时间基准指标在时间区间内的值
  390. aDataList := make([]*data_manage.EdbDataList, 0)
  391. switch baseEdbInfo.EdbInfoCategoryType {
  392. case 0:
  393. aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
  394. case 1:
  395. _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, true)
  396. default:
  397. err = errors.New("指标base类型异常")
  398. return
  399. }
  400. // 获取变频指标所有日期的值, 插值法完善数据
  401. bDataList := make([]*data_manage.EdbDataList, 0)
  402. switch changeEdbInfo.EdbInfoCategoryType {
  403. case 0:
  404. bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
  405. case 1:
  406. _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
  407. default:
  408. err = errors.New("指标change类型异常")
  409. return
  410. }
  411. // 数据平移变频指标领先/滞后的日期(单位天)
  412. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  413. //baseDataList := make([]*data_manage.EdbDataList, 0)
  414. baseDataMap := make(map[string]float64)
  415. changeDataList := make([]*data_manage.EdbDataList, 0)
  416. changeDataMap := make(map[string]float64)
  417. // A指标不管三七二十一,先变个频再说
  418. {
  419. _, e := HandleDataByLinearRegression(aDataList, baseDataMap)
  420. if e != nil {
  421. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  422. return
  423. }
  424. //baseDataList = tmpNewChangeDataList
  425. }
  426. // B指标不管三七二十一,先变个频再说
  427. {
  428. tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  429. if e != nil {
  430. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  431. return
  432. }
  433. changeDataList = tmpNewChangeDataList
  434. // 平移下日期
  435. moveUnitDays := utils.FrequencyDaysMap[leadUnit]
  436. _, changeDataMap = MoveDataDaysToNewDataList(changeDataList, leadValue*moveUnitDays)
  437. }
  438. // 计算计算时,需要多少个日期内数据
  439. calculateDay := utils.FrequencyDaysMap[calculateUnit] * calculateValue
  440. // 计算 每个日期的相关性值
  441. {
  442. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  443. if endDate == `` {
  444. endDate = baseEdbInfo.EndDate
  445. }
  446. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  447. endDateTime = endDateTime.AddDate(0, 0, -(calculateDay - 1))
  448. // 是否开始第一条数据
  449. var isStart, isNotFirst bool
  450. for currDay := startDateTime; !currDay.After(endDateTime); currDay = currDay.AddDate(0, 0, 1) {
  451. yCalculateData := make([]float64, 0)
  452. baseCalculateData := make([]float64, 0)
  453. // 取出对应的基准日期的值
  454. for i := 0; i < calculateDay; i++ {
  455. iDay := currDay.AddDate(0, 0, i).Format(utils.FormatDate)
  456. tmpBaseValue, ok1 := baseDataMap[iDay]
  457. tmpChangeValue, ok2 := changeDataMap[iDay]
  458. if ok1 && ok2 {
  459. baseCalculateData = append(baseCalculateData, tmpBaseValue)
  460. yCalculateData = append(yCalculateData, tmpChangeValue)
  461. } else {
  462. continue
  463. }
  464. }
  465. // 公式计算出领先/滞后频度对应点的相关性系数
  466. var ratio float64
  467. if len(baseCalculateData) > 0 {
  468. ratio = utils.CalculateCorrelationByIntArr(baseCalculateData, yCalculateData)
  469. } else {
  470. // 没有数据的话,那就不返回
  471. continue
  472. }
  473. // 过滤前面都是0的数据
  474. {
  475. if ratio != 0 {
  476. isStart = true
  477. }
  478. if !isStart {
  479. continue
  480. }
  481. }
  482. dataTime := currDay.AddDate(0, 0, calculateDay-1)
  483. dataList = append(dataList, data_manage.EdbDataList{
  484. //EdbDataId: 0,
  485. EdbInfoId: 0,
  486. DataTime: dataTime.Format(utils.FormatDate),
  487. DataTimestamp: dataTime.UnixNano() / 1e6,
  488. Value: ratio,
  489. })
  490. if !isNotFirst {
  491. dataResp.MinData = ratio
  492. dataResp.MaxData = ratio
  493. isNotFirst = true
  494. }
  495. if dataResp.MinData > ratio {
  496. dataResp.MinData = ratio
  497. }
  498. if dataResp.MaxData < ratio {
  499. dataResp.MaxData = ratio
  500. }
  501. }
  502. dataResp.DataList = dataList
  503. }
  504. return
  505. }
  506. // ChartInfoRefresh 图表刷新
  507. func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
  508. var errMsg string
  509. defer func() {
  510. if err != nil {
  511. tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
  512. utils.FileLog.Info(tips)
  513. go alarm_msg.SendAlarmMsg(tips, 3)
  514. }
  515. }()
  516. correlationChart := new(data_manage.ChartInfoCorrelation)
  517. if err = correlationChart.GetItemById(chartInfoId); err != nil {
  518. errMsg = "获取相关性图表失败, Err: " + err.Error()
  519. return
  520. }
  521. // 多因子刷新-异步
  522. if correlationChart.AnalysisMode == 1 {
  523. isAsync = true
  524. go func() {
  525. // 1.刷新图表关联的指标
  526. mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
  527. if e != nil {
  528. utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
  529. return
  530. }
  531. if len(mappings) == 0 {
  532. utils.FileLog.Info("图表无关联指标")
  533. return
  534. }
  535. var edbIds []int
  536. for _, v := range mappings {
  537. edbIds = append(edbIds, v.EdbInfoId)
  538. }
  539. if e, _ = data.EdbInfoRefreshAllFromBaseV3(edbIds, false, true, false); e != nil {
  540. utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
  541. return
  542. }
  543. // 2.刷新指标系列计算数据
  544. for _, v := range mappings {
  545. _, e = data.PostRefreshFactorEdbRecalculate(v.EdbInfoId, v.EdbCode)
  546. if e != nil {
  547. utils.FileLog.Info(fmt.Sprintf("PostRefreshFactorEdbRecalculate err: %v", e))
  548. continue
  549. }
  550. }
  551. // 3.刷新图表矩阵
  552. _, e = data.PostRefreshFactorEdbChartRecalculate(chartInfoId)
  553. if e != nil {
  554. utils.FileLog.Info(fmt.Sprintf("PostRefreshFactorEdbRecalculate err: %v", e))
  555. return
  556. }
  557. // 4.清除图表缓存
  558. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  559. _ = utils.Rc.Delete(key)
  560. }()
  561. return
  562. }
  563. // 批量刷新ETA指标
  564. err, _ = data.EdbInfoRefreshAllFromBaseV3([]int{correlationChart.EdbInfoIdFirst, correlationChart.EdbInfoIdSecond}, false, true, false)
  565. if err != nil {
  566. return
  567. }
  568. // 重新生成数据并更新
  569. edbInfoMappingA, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  570. if err != nil {
  571. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + err.Error()
  572. return
  573. }
  574. edbInfoMappingB, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  575. if err != nil {
  576. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + err.Error()
  577. return
  578. }
  579. periodData, correlationData, err := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
  580. if err != nil {
  581. errMsg = "获取相关性图表, 图表计算值失败, Err:" + err.Error()
  582. return
  583. }
  584. periodDataByte, err := json.Marshal(periodData)
  585. if err != nil {
  586. errMsg = "相关性图表, X轴信息有误, Err:" + err.Error()
  587. return
  588. }
  589. correlationDataByte, err := json.Marshal(correlationData[0].Value)
  590. if err != nil {
  591. errMsg = "相关性图表, Y轴信息有误, Err:" + err.Error()
  592. return
  593. }
  594. correlationChart.PeriodData = string(periodDataByte)
  595. correlationChart.CorrelationData = string(correlationDataByte)
  596. correlationChart.ModifyTime = time.Now().Local()
  597. correlationUpdateCols := []string{"PeriodData", "CorrelationData", "ModifyTime"}
  598. if err = correlationChart.Update(correlationUpdateCols); err != nil {
  599. errMsg = "更新相关性图表失败, Err:" + err.Error()
  600. return
  601. }
  602. return
  603. }
  604. // GetChartAndCorrelationInfo 获取图表信息和相关信息信息
  605. func GetChartAndCorrelationInfo(chartInfoId int) (chartInfo *data_manage.ChartInfo, correlationInfo *data_manage.ChartInfoCorrelation, tips string, err error) {
  606. item, e := data_manage.GetChartInfoById(chartInfoId)
  607. if e != nil {
  608. if e.Error() == utils.ErrNoRow() {
  609. tips = "图表已被删除, 请刷新页面"
  610. err = fmt.Errorf("图表已被删除, 请刷新页面")
  611. return
  612. }
  613. err = fmt.Errorf("获取图表信息失败, Err: %s", e.Error())
  614. return
  615. }
  616. if item.Source != utils.CHART_SOURCE_CORRELATION {
  617. tips = "该图不是相关性图表"
  618. err = fmt.Errorf("该图不是相关性图表")
  619. return
  620. }
  621. chartInfo = item
  622. correlationInfo = new(data_manage.ChartInfoCorrelation)
  623. if e = correlationInfo.GetItemById(chartInfo.ChartInfoId); e != nil {
  624. err = fmt.Errorf("获取图表相关性信息失败, Err: %s", e.Error())
  625. return
  626. }
  627. return
  628. }
  629. // AddChartInfo 添加图表
  630. func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  631. isSendEmail = true
  632. req.ChartName = strings.Trim(req.ChartName, " ")
  633. if req.ChartName == "" {
  634. errMsg = "请填写图表名称!"
  635. err = errors.New(errMsg)
  636. isSendEmail = false
  637. return
  638. }
  639. if req.ChartClassifyId <= 0 {
  640. errMsg = "分类参数错误!"
  641. err = errors.New(errMsg)
  642. isSendEmail = false
  643. return
  644. }
  645. // 相关性图表配置
  646. if req.CorrelationChartInfo.LeadValue == 0 && source == utils.CHART_SOURCE_CORRELATION {
  647. errMsg = "请输入领先期数"
  648. err = errors.New(errMsg)
  649. isSendEmail = false
  650. return
  651. }
  652. if req.CorrelationChartInfo.LeadUnit == "" {
  653. errMsg = "请填写领先单位"
  654. err = errors.New(errMsg)
  655. isSendEmail = false
  656. return
  657. }
  658. //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
  659. // errMsg = "请填写开始结束日期"
  660. // err = errors.New(errMsg)
  661. // isSendEmail = false
  662. // return
  663. //}
  664. //startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
  665. //if e != nil {
  666. // errMsg = "开始日期格式有误"
  667. // err = errors.New(errMsg)
  668. // isSendEmail = false
  669. // return
  670. //}
  671. //endDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
  672. //if e != nil {
  673. // errMsg = "结束日期格式有误"
  674. // err = errors.New(errMsg)
  675. // isSendEmail = false
  676. // return
  677. //}
  678. if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
  679. errMsg = "请选择AB指标"
  680. err = errors.New(errMsg)
  681. isSendEmail = false
  682. return
  683. }
  684. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  685. if err != nil {
  686. if err.Error() == utils.ErrNoRow() {
  687. errMsg = "分类不存在"
  688. err = errors.New(errMsg)
  689. isSendEmail = false
  690. return
  691. }
  692. errMsg = "获取分类信息失败"
  693. err = errors.New("获取分类信息失败,Err:" + err.Error())
  694. return
  695. }
  696. if chartClassify == nil {
  697. errMsg = "分类不存在"
  698. err = errors.New(errMsg)
  699. isSendEmail = false
  700. return
  701. }
  702. var edbInfoIdArr []int
  703. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  704. edbInfoId := v.EdbInfoId
  705. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  706. if tmpErr != nil {
  707. if tmpErr.Error() == utils.ErrNoRow() {
  708. errMsg = "指标不存在!"
  709. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  710. return
  711. } else {
  712. errMsg = "获取指标信息失败!"
  713. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  714. return
  715. }
  716. }
  717. if edbInfo == nil {
  718. errMsg = "指标已被删除,请重新选择!"
  719. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  720. return
  721. } else {
  722. if edbInfo.EdbInfoId <= 0 {
  723. errMsg = "指标已被删除,请重新选择!"
  724. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  725. return
  726. }
  727. }
  728. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  729. edbInfo.EdbNameSource = edbInfo.EdbName
  730. }
  731. sort.Ints(edbInfoIdArr)
  732. var edbInfoIdArrStr []string
  733. for _, v := range edbInfoIdArr {
  734. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  735. }
  736. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  737. var chartInfoId int
  738. // 判断图表是否存在
  739. {
  740. var condition string
  741. var pars []interface{}
  742. switch lang {
  743. case utils.EnLangVersion:
  744. condition += " AND chart_name_en = ? AND source = ? "
  745. default:
  746. condition += " AND chart_name=? AND source = ? "
  747. }
  748. pars = append(pars, req.ChartName, source)
  749. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  750. if tmpErr != nil {
  751. errMsg = "判断图表名称是否存在失败"
  752. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  753. return
  754. }
  755. if count > 0 {
  756. errMsg = "图表已存在,请重新填写"
  757. err = errors.New(errMsg)
  758. isSendEmail = false
  759. return
  760. }
  761. }
  762. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  763. chartInfo = new(data_manage.ChartInfo)
  764. chartInfo.ChartName = req.ChartName
  765. chartInfo.ChartNameEn = req.ChartName
  766. chartInfo.EdbInfoIds = edbInfoIdStr
  767. chartInfo.ChartClassifyId = req.ChartClassifyId
  768. chartInfo.SysUserId = sysUser.AdminId
  769. chartInfo.SysUserRealName = sysUser.RealName
  770. chartInfo.CreateTime = time.Now()
  771. chartInfo.ModifyTime = time.Now()
  772. chartInfo.IsSetName = 0
  773. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  774. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  775. chartInfo.ChartType = 9 // 相关性图
  776. chartInfo.Calendar = "公历"
  777. chartInfo.DateType = 6
  778. chartInfo.StartDate = req.StartDate
  779. chartInfo.EndDate = req.EndDate
  780. chartInfo.SeasonStartDate = req.StartDate
  781. chartInfo.SeasonEndDate = req.EndDate
  782. chartInfo.LeftMin = req.LeftMin
  783. chartInfo.LeftMax = req.LeftMax
  784. chartInfo.RightMin = req.RightMin
  785. chartInfo.RightMax = req.RightMax
  786. chartInfo.Disabled = disableVal
  787. chartInfo.Source = source
  788. chartInfo.ChartThemeId = req.ChartThemeId
  789. chartInfo.SourcesFrom = req.SourcesFrom
  790. chartInfo.Instructions = req.Instructions
  791. chartInfo.MarkersLines = req.MarkersLines
  792. chartInfo.MarkersAreas = req.MarkersAreas
  793. // 指标信息
  794. mapList := make([]*data_manage.ChartEdbMapping, 0)
  795. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  796. mapItem := new(data_manage.ChartEdbMapping)
  797. mapItem.EdbInfoId = v.EdbInfoId
  798. mapItem.CreateTime = time.Now()
  799. mapItem.ModifyTime = time.Now()
  800. edbTimestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  801. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + edbTimestamp + "_" + strconv.Itoa(v.EdbInfoId))
  802. mapItem.IsOrder = true
  803. mapItem.IsAxis = 1
  804. mapItem.EdbInfoType = 1
  805. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  806. mapList = append(mapList, mapItem)
  807. }
  808. // 相关性图表扩展信息
  809. correlationChart := new(data_manage.ChartInfoCorrelation)
  810. correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
  811. correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
  812. correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
  813. correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
  814. correlationChart.BaseCalculateValue = req.CorrelationChartInfo.BaseCalculateValue
  815. correlationChart.BaseCalculateUnit = req.CorrelationChartInfo.BaseCalculateUnit
  816. // 滚动相关性会有日期等信息
  817. if source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  818. correlationChart.DateType = req.CorrelationChartInfo.DateType
  819. if req.CorrelationChartInfo.StartDate != `` {
  820. startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.StartDate, time.Local)
  821. if tmpErr != nil {
  822. err = tmpErr
  823. return
  824. }
  825. correlationChart.StartDate = startDateTime
  826. }
  827. if req.CorrelationChartInfo.EndDate != `` {
  828. endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.EndDate, time.Local)
  829. if tmpErr != nil {
  830. err = tmpErr
  831. return
  832. }
  833. correlationChart.EndDate = endDateTime
  834. }
  835. }
  836. correlationChart.EdbInfoIdFirst = req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId
  837. correlationChart.EdbInfoIdSecond = req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId
  838. correlationChart.CreateTime = time.Now().Local()
  839. correlationChart.ModifyTime = time.Now().Local()
  840. //// 生成图表x轴y轴数据
  841. //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
  842. //if e != nil {
  843. // errMsg = "获取失败"
  844. // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
  845. // return
  846. //}
  847. //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
  848. //if e != nil {
  849. // errMsg = "获取失败"
  850. // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
  851. // return
  852. //}
  853. //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
  854. //if e != nil {
  855. // errMsg = "获取失败"
  856. // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
  857. // return
  858. //}
  859. //periodDataByte, e := json.Marshal(periodData)
  860. //if e != nil {
  861. // errMsg = "获取失败"
  862. // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
  863. // return
  864. //}
  865. //correlationDataByte, e := json.Marshal(correlationData[0].Value)
  866. //if e != nil {
  867. // errMsg = "获取失败"
  868. // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
  869. // return
  870. //}
  871. //correlationChart.PeriodData = string(periodDataByte)
  872. //correlationChart.CorrelationData = string(correlationDataByte)
  873. // 新增图表和指标mapping
  874. chartInfoId, e := data_manage.CreateCorrelationChartAndEdb(chartInfo, mapList, correlationChart)
  875. if e != nil {
  876. errMsg = "操作失败"
  877. err = errors.New("新增相关性图表失败, Err: " + e.Error())
  878. return
  879. }
  880. //添加es数据
  881. go data.EsAddOrEditChartInfo(chartInfoId)
  882. return
  883. }
  884. // EditChartInfo 编辑图表
  885. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  886. isSendEmail = true
  887. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  888. if err != nil {
  889. if err.Error() == utils.ErrNoRow() {
  890. errMsg = "图表已被删除,请刷新页面"
  891. err = errors.New(errMsg)
  892. isSendEmail = false
  893. return
  894. }
  895. errMsg = "获取图表信息失败"
  896. err = errors.New("获取图表信息失败,Err:" + err.Error())
  897. return
  898. }
  899. if chartItem.Source != utils.CHART_SOURCE_CORRELATION && chartItem.Source != utils.CHART_SOURCE_ROLLING_CORRELATION {
  900. errMsg = "该图不是相关性图表!"
  901. err = errors.New(errMsg)
  902. isSendEmail = false
  903. return
  904. }
  905. req.ChartName = strings.Trim(req.ChartName, " ")
  906. if req.ChartClassifyId <= 0 {
  907. errMsg = "分类参数错误!"
  908. err = errors.New(errMsg)
  909. isSendEmail = false
  910. return
  911. }
  912. // 相关性图表配置
  913. if req.CorrelationChartInfo.LeadValue == 0 && chartItem.Source == utils.CHART_SOURCE_CORRELATION {
  914. errMsg = "请输入领先期数"
  915. err = errors.New(errMsg)
  916. isSendEmail = false
  917. return
  918. }
  919. if req.CorrelationChartInfo.LeadUnit == "" {
  920. errMsg = "请填写领先单位"
  921. err = errors.New(errMsg)
  922. isSendEmail = false
  923. return
  924. }
  925. //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
  926. // errMsg = "请填写开始结束日期"
  927. // err = errors.New(errMsg)
  928. // isSendEmail = false
  929. // return
  930. //}
  931. startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
  932. if e != nil {
  933. errMsg = "开始日期格式有误"
  934. err = errors.New(errMsg)
  935. isSendEmail = false
  936. return
  937. }
  938. var endDate time.Time
  939. if req.CorrelationChartInfo.EndDate != `` {
  940. endDate, e = time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
  941. if e != nil {
  942. errMsg = "结束日期格式有误"
  943. err = errors.New(errMsg)
  944. isSendEmail = false
  945. return
  946. }
  947. }
  948. if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
  949. errMsg = "请选择AB指标"
  950. err = errors.New(errMsg)
  951. isSendEmail = false
  952. return
  953. }
  954. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  955. if err != nil {
  956. if err.Error() == utils.ErrNoRow() {
  957. errMsg = "分类不存在"
  958. err = errors.New(errMsg)
  959. isSendEmail = false
  960. return
  961. }
  962. errMsg = "获取分类信息失败"
  963. err = errors.New("获取分类信息失败,Err:" + err.Error())
  964. return
  965. }
  966. if chartClassify == nil {
  967. errMsg = "分类不存在"
  968. err = errors.New(errMsg)
  969. isSendEmail = false
  970. return
  971. }
  972. // 图表操作权限
  973. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  974. if !ok {
  975. errMsg = "没有该图表的操作权限"
  976. err = errors.New(errMsg)
  977. isSendEmail = false
  978. return
  979. }
  980. var edbInfoIdArr []int
  981. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  982. edbInfoId := v.EdbInfoId
  983. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  984. if tmpErr != nil {
  985. if tmpErr.Error() == utils.ErrNoRow() {
  986. errMsg = "图表不存在!"
  987. err = errors.New("图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  988. return
  989. } else {
  990. errMsg = "获取图表信息失败!"
  991. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  992. return
  993. }
  994. }
  995. if edbInfo == nil {
  996. errMsg = "指标不存在!"
  997. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  998. return
  999. }
  1000. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1001. }
  1002. sort.Ints(edbInfoIdArr)
  1003. var edbInfoIdArrStr []string
  1004. for _, v := range edbInfoIdArr {
  1005. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1006. }
  1007. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1008. //判断图表是否存在
  1009. {
  1010. var condition string
  1011. var pars []interface{}
  1012. condition += " AND chart_info_id <> ? "
  1013. pars = append(pars, req.ChartInfoId)
  1014. switch lang {
  1015. case utils.EnLangVersion:
  1016. condition += " AND chart_name_en = ? AND source = ? "
  1017. default:
  1018. condition += " AND chart_name=? AND source = ? "
  1019. }
  1020. pars = append(pars, req.ChartName, chartItem.Source)
  1021. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  1022. if tmpErr != nil {
  1023. errMsg = "判断图表名称是否存在失败"
  1024. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  1025. return
  1026. }
  1027. if count > 0 {
  1028. errMsg = "图表已存在,请重新填写"
  1029. err = errors.New(errMsg)
  1030. isSendEmail = false
  1031. return
  1032. }
  1033. }
  1034. correlationChart := new(data_manage.ChartInfoCorrelation)
  1035. if e := correlationChart.GetItemById(chartItem.ChartInfoId); e != nil {
  1036. errMsg = "操作失败"
  1037. err = errors.New("图表相关性信息不存在, Err: " + e.Error())
  1038. return
  1039. }
  1040. // 图表启用与否
  1041. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  1042. // 重新生成图表值, 并修改相关性图表扩展信息
  1043. //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
  1044. //if e != nil {
  1045. // errMsg = "获取失败"
  1046. // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
  1047. // return
  1048. //}
  1049. //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
  1050. //if e != nil {
  1051. // errMsg = "获取失败"
  1052. // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
  1053. // return
  1054. //}
  1055. //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
  1056. //if e != nil {
  1057. // errMsg = "获取失败"
  1058. // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
  1059. // return
  1060. //}
  1061. //periodDataByte, e := json.Marshal(periodData)
  1062. //if e != nil {
  1063. // errMsg = "获取失败"
  1064. // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
  1065. // return
  1066. //}
  1067. //correlationDataByte, e := json.Marshal(correlationData[0].Value)
  1068. //if e != nil {
  1069. // errMsg = "获取失败"
  1070. // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
  1071. // return
  1072. //}
  1073. correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
  1074. correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
  1075. correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
  1076. correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
  1077. correlationChart.StartDate = startDate
  1078. correlationChart.EndDate = endDate
  1079. correlationChart.EdbInfoIdFirst = req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId
  1080. correlationChart.EdbInfoIdSecond = req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId
  1081. // 滚动相关性会有日期等信息
  1082. if chartItem.Source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  1083. correlationChart.DateType = req.CorrelationChartInfo.DateType
  1084. }
  1085. //correlationChart.PeriodData = string(periodDataByte)
  1086. //correlationChart.CorrelationData = string(correlationDataByte)
  1087. correlationChart.ModifyTime = time.Now().Local()
  1088. //correlationUpdateCols := []string{"LeadValue", "LeadUnit", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "PeriodData","CorrelationData", "ModifyTime"}
  1089. correlationUpdateCols := []string{"LeadValue", "LeadUnit", "CalculateValue", "CalculateUnit", "DateType", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "ModifyTime"}
  1090. // 修改图表与指标mapping
  1091. req.ChartType = 9
  1092. err = data_manage.EditCorrelationChartInfoAndMapping(&req, edbInfoIdStr, "公历", 6, disableVal, ``,
  1093. correlationChart, correlationUpdateCols)
  1094. if err != nil {
  1095. errMsg = "保存失败"
  1096. err = errors.New("保存失败,Err:" + err.Error())
  1097. return
  1098. }
  1099. resp := new(data_manage.AddChartInfoResp)
  1100. resp.ChartInfoId = chartItem.ChartInfoId
  1101. resp.UniqueCode = chartItem.UniqueCode
  1102. resp.ChartType = req.ChartType
  1103. //添加es数据
  1104. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1105. //修改my eta es数据
  1106. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1107. return
  1108. }
  1109. // CopyChartInfo 复制图表
  1110. func CopyChartInfo(configId, classifyId int, chartName string, correlationChartInfoReq data_manage.CorrelationChartInfoReq, oldChartInfo *data_manage.ChartInfo, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1111. configSource := 2
  1112. isSendEmail = true
  1113. // 获取相关性图的配置
  1114. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(configId, configSource)
  1115. if err != nil {
  1116. return
  1117. }
  1118. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(configId)
  1119. if err != nil {
  1120. return
  1121. }
  1122. multipleGraphConfig.MultipleGraphConfigId = 0
  1123. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1124. if err != nil {
  1125. return
  1126. }
  1127. // 添加图
  1128. addChartReq := data_manage.AddChartInfoReq{
  1129. ChartClassifyId: classifyId,
  1130. ChartName: chartName,
  1131. ChartType: utils.CHART_TYPE_CURVE,
  1132. Calendar: "公历",
  1133. CorrelationChartInfo: correlationChartInfoReq,
  1134. ChartThemeId: oldChartInfo.ChartThemeId,
  1135. SourcesFrom: oldChartInfo.SourcesFrom,
  1136. Instructions: oldChartInfo.Instructions,
  1137. MarkersLines: oldChartInfo.MarkersLines,
  1138. MarkersAreas: oldChartInfo.MarkersAreas,
  1139. }
  1140. chartSource := utils.CHART_SOURCE_CORRELATION // 默认是相关性图
  1141. chartInfo, err, errMsg, isSendEmail = AddChartInfo(addChartReq, chartSource, sysUser, lang)
  1142. if err != nil {
  1143. return
  1144. }
  1145. // 添加关系
  1146. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  1147. //Id: 0,
  1148. MultipleGraphConfigId: multipleGraphConfig.MultipleGraphConfigId,
  1149. ChartInfoId: chartInfo.ChartInfoId,
  1150. Source: configSource,
  1151. ModifyTime: time.Now(),
  1152. CreateTime: time.Now(),
  1153. }
  1154. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1155. if err != nil {
  1156. return
  1157. }
  1158. //添加es数据
  1159. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1160. return
  1161. }
  1162. // GetFactorChartDataByChartId 获取多因子相关性图表数据
  1163. func GetFactorChartDataByChartId(chartInfoId int, extraConfig string) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
  1164. if chartInfoId <= 0 {
  1165. return
  1166. }
  1167. // 指标对应的图例
  1168. extra := new(data_manage.CorrelationChartInfoExtraConfig)
  1169. if extraConfig != "" {
  1170. if e := json.Unmarshal([]byte(extraConfig), extra); e != nil {
  1171. err = fmt.Errorf("解析图表额外配置失败, err: %v", e)
  1172. return
  1173. }
  1174. }
  1175. legends := make(map[string]*data_manage.CorrelationChartLegend)
  1176. if extra != nil {
  1177. for _, v := range extra.LegendConfig {
  1178. s := fmt.Sprintf("%d-%d", v.SeriesId, v.EdbInfoId)
  1179. legends[s] = v
  1180. }
  1181. }
  1182. // 获取图表引用到的系列指标
  1183. chartMappingOb := new(data_manage.FactorEdbSeriesChartMapping)
  1184. cond := fmt.Sprintf(" AND %s = ? AND %s = 1", chartMappingOb.Cols().ChartInfoId, chartMappingOb.Cols().EdbUsed)
  1185. pars := make([]interface{}, 0)
  1186. pars = append(pars, chartInfoId)
  1187. chartMappings, e := chartMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
  1188. if e != nil {
  1189. err = fmt.Errorf("获取图表引用系列指标失败")
  1190. return
  1191. }
  1192. // 取出计算结果
  1193. yDataList = make([]data_manage.YData, 0)
  1194. yDate := "0000-00-00"
  1195. for k, m := range chartMappings {
  1196. var values []data_manage.FactorEdbSeriesCorrelationMatrixValues
  1197. if m.CalculateData != "" {
  1198. e = json.Unmarshal([]byte(m.CalculateData), &values)
  1199. if e != nil {
  1200. err = fmt.Errorf("系列指标计算数据有误, err: %v", e)
  1201. return
  1202. }
  1203. }
  1204. var y []float64
  1205. for _, v := range values {
  1206. if k == 0 {
  1207. xEdbIdValue = append(xEdbIdValue, v.XData)
  1208. }
  1209. y = append(y, v.YData)
  1210. }
  1211. var yData data_manage.YData
  1212. yData.Date = yDate
  1213. yData.Value = y
  1214. yData.SeriesEdb.SeriesId = m.FactorEdbSeriesId
  1215. yData.SeriesEdb.EdbInfoId = m.EdbInfoId
  1216. // 图例
  1217. s := fmt.Sprintf("%d-%d", m.FactorEdbSeriesId, m.EdbInfoId)
  1218. legend := legends[s]
  1219. if legend != nil {
  1220. yData.Name = legend.LegendName
  1221. yData.Color = legend.Color
  1222. }
  1223. yDataList = append(yDataList, yData)
  1224. }
  1225. return
  1226. }