chart_info.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. package correlation
  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. // 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) (err error) {
  508. var errMsg string
  509. defer func() {
  510. if err != nil {
  511. go alarm_msg.SendAlarmMsg("CorrelationChartInfoRefresh: "+errMsg, 3)
  512. }
  513. }()
  514. correlationChart := new(data_manage.ChartInfoCorrelation)
  515. if err = correlationChart.GetItemById(chartInfoId); err != nil {
  516. errMsg = "获取相关性图表失败, Err: " + err.Error()
  517. return
  518. }
  519. // 批量刷新ETA指标
  520. err, _ = data.EdbInfoRefreshAllFromBaseV3([]int{correlationChart.EdbInfoIdFirst, correlationChart.EdbInfoIdSecond}, false, true, false)
  521. if err != nil {
  522. return
  523. }
  524. // 重新生成数据并更新
  525. edbInfoMappingA, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  526. if err != nil {
  527. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + err.Error()
  528. return
  529. }
  530. edbInfoMappingB, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  531. if err != nil {
  532. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + err.Error()
  533. return
  534. }
  535. periodData, correlationData, err := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
  536. if err != nil {
  537. errMsg = "获取相关性图表, 图表计算值失败, Err:" + err.Error()
  538. return
  539. }
  540. periodDataByte, err := json.Marshal(periodData)
  541. if err != nil {
  542. errMsg = "相关性图表, X轴信息有误, Err:" + err.Error()
  543. return
  544. }
  545. correlationDataByte, err := json.Marshal(correlationData[0].Value)
  546. if err != nil {
  547. errMsg = "相关性图表, Y轴信息有误, Err:" + err.Error()
  548. return
  549. }
  550. correlationChart.PeriodData = string(periodDataByte)
  551. correlationChart.CorrelationData = string(correlationDataByte)
  552. correlationChart.ModifyTime = time.Now().Local()
  553. correlationUpdateCols := []string{"PeriodData", "CorrelationData", "ModifyTime"}
  554. if err = correlationChart.Update(correlationUpdateCols); err != nil {
  555. errMsg = "更新相关性图表失败, Err:" + err.Error()
  556. return
  557. }
  558. return
  559. }
  560. // GetChartAndCorrelationInfo 获取图表信息和相关信息信息
  561. func GetChartAndCorrelationInfo(chartInfoId int) (chartInfo *data_manage.ChartInfo, correlationInfo *data_manage.ChartInfoCorrelation, tips string, err error) {
  562. item, e := data_manage.GetChartInfoById(chartInfoId)
  563. if e != nil {
  564. if e.Error() == utils.ErrNoRow() {
  565. tips = "图表已被删除, 请刷新页面"
  566. err = fmt.Errorf("图表已被删除, 请刷新页面")
  567. return
  568. }
  569. err = fmt.Errorf("获取图表信息失败, Err: %s", e.Error())
  570. return
  571. }
  572. if item.Source != utils.CHART_SOURCE_CORRELATION {
  573. tips = "该图不是相关性图表"
  574. err = fmt.Errorf("该图不是相关性图表")
  575. return
  576. }
  577. chartInfo = item
  578. correlationInfo = new(data_manage.ChartInfoCorrelation)
  579. if e = correlationInfo.GetItemById(chartInfo.ChartInfoId); e != nil {
  580. err = fmt.Errorf("获取图表相关性信息失败, Err: %s", e.Error())
  581. return
  582. }
  583. return
  584. }
  585. // AddChartInfo 添加图表
  586. func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.Admin) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  587. isSendEmail = true
  588. req.ChartName = strings.Trim(req.ChartName, " ")
  589. if req.ChartName == "" {
  590. errMsg = "请填写图表名称!"
  591. err = errors.New(errMsg)
  592. isSendEmail = false
  593. return
  594. }
  595. if req.ChartClassifyId <= 0 {
  596. errMsg = "分类参数错误!"
  597. err = errors.New(errMsg)
  598. isSendEmail = false
  599. return
  600. }
  601. // 相关性图表配置
  602. if req.CorrelationChartInfo.LeadValue == 0 && source == utils.CHART_SOURCE_CORRELATION {
  603. errMsg = "请输入领先期数"
  604. err = errors.New(errMsg)
  605. isSendEmail = false
  606. return
  607. }
  608. if req.CorrelationChartInfo.LeadUnit == "" {
  609. errMsg = "请填写领先单位"
  610. err = errors.New(errMsg)
  611. isSendEmail = false
  612. return
  613. }
  614. //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
  615. // errMsg = "请填写开始结束日期"
  616. // err = errors.New(errMsg)
  617. // isSendEmail = false
  618. // return
  619. //}
  620. //startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
  621. //if e != nil {
  622. // errMsg = "开始日期格式有误"
  623. // err = errors.New(errMsg)
  624. // isSendEmail = false
  625. // return
  626. //}
  627. //endDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
  628. //if e != nil {
  629. // errMsg = "结束日期格式有误"
  630. // err = errors.New(errMsg)
  631. // isSendEmail = false
  632. // return
  633. //}
  634. if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
  635. errMsg = "请选择AB指标"
  636. err = errors.New(errMsg)
  637. isSendEmail = false
  638. return
  639. }
  640. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  641. if err != nil {
  642. if err.Error() == utils.ErrNoRow() {
  643. errMsg = "分类不存在"
  644. err = errors.New(errMsg)
  645. isSendEmail = false
  646. return
  647. }
  648. errMsg = "获取分类信息失败"
  649. err = errors.New("获取分类信息失败,Err:" + err.Error())
  650. return
  651. }
  652. if chartClassify == nil {
  653. errMsg = "分类不存在"
  654. err = errors.New(errMsg)
  655. isSendEmail = false
  656. return
  657. }
  658. var edbInfoIdArr []int
  659. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  660. edbInfoId := v.EdbInfoId
  661. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  662. if tmpErr != nil {
  663. if tmpErr.Error() == utils.ErrNoRow() {
  664. errMsg = "指标不存在!"
  665. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  666. return
  667. } else {
  668. errMsg = "获取指标信息失败!"
  669. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  670. return
  671. }
  672. }
  673. if edbInfo == nil {
  674. errMsg = "指标已被删除,请重新选择!"
  675. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  676. return
  677. } else {
  678. if edbInfo.EdbInfoId <= 0 {
  679. errMsg = "指标已被删除,请重新选择!"
  680. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  681. return
  682. }
  683. }
  684. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  685. edbInfo.EdbNameSource = edbInfo.EdbName
  686. }
  687. sort.Ints(edbInfoIdArr)
  688. var edbInfoIdArrStr []string
  689. for _, v := range edbInfoIdArr {
  690. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  691. }
  692. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  693. var chartInfoId int
  694. // 判断图表是否存在
  695. var condition string
  696. var pars []interface{}
  697. condition += " AND chart_name=? AND source = ? "
  698. pars = append(pars, req.ChartName, source)
  699. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  700. if err != nil {
  701. errMsg = "判断图表名称是否存在失败"
  702. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  703. return
  704. }
  705. if count > 0 {
  706. errMsg = "图表已存在,请重新填写"
  707. err = errors.New(errMsg)
  708. isSendEmail = false
  709. return
  710. }
  711. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  712. chartInfo = new(data_manage.ChartInfo)
  713. chartInfo.ChartName = req.ChartName
  714. chartInfo.EdbInfoIds = edbInfoIdStr
  715. chartInfo.ChartClassifyId = req.ChartClassifyId
  716. chartInfo.SysUserId = sysUser.AdminId
  717. chartInfo.SysUserRealName = sysUser.RealName
  718. chartInfo.CreateTime = time.Now()
  719. chartInfo.ModifyTime = time.Now()
  720. chartInfo.IsSetName = 0
  721. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  722. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  723. chartInfo.ChartType = 9 // 相关性图
  724. chartInfo.Calendar = "公历"
  725. chartInfo.DateType = 6
  726. chartInfo.StartDate = req.StartDate
  727. chartInfo.EndDate = req.EndDate
  728. chartInfo.SeasonStartDate = req.StartDate
  729. chartInfo.SeasonEndDate = req.EndDate
  730. chartInfo.LeftMin = req.LeftMin
  731. chartInfo.LeftMax = req.LeftMax
  732. chartInfo.RightMin = req.RightMin
  733. chartInfo.RightMax = req.RightMax
  734. chartInfo.Disabled = disableVal
  735. chartInfo.Source = source
  736. chartInfo.ChartThemeId = req.ChartThemeId
  737. chartInfo.SourcesFrom = req.SourcesFrom
  738. chartInfo.Instructions = req.Instructions
  739. chartInfo.MarkersLines = req.MarkersLines
  740. chartInfo.MarkersAreas = req.MarkersAreas
  741. // 指标信息
  742. mapList := make([]*data_manage.ChartEdbMapping, 0)
  743. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  744. mapItem := new(data_manage.ChartEdbMapping)
  745. mapItem.EdbInfoId = v.EdbInfoId
  746. mapItem.CreateTime = time.Now()
  747. mapItem.ModifyTime = time.Now()
  748. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  749. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  750. mapItem.IsOrder = true
  751. mapItem.IsAxis = 1
  752. mapItem.EdbInfoType = 1
  753. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  754. mapList = append(mapList, mapItem)
  755. }
  756. // 相关性图表扩展信息
  757. correlationChart := new(data_manage.ChartInfoCorrelation)
  758. correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
  759. correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
  760. correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
  761. correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
  762. correlationChart.BaseCalculateValue = req.CorrelationChartInfo.BaseCalculateValue
  763. correlationChart.BaseCalculateUnit = req.CorrelationChartInfo.BaseCalculateUnit
  764. // 滚动相关性会有日期等信息
  765. if source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  766. correlationChart.DateType = req.CorrelationChartInfo.DateType
  767. if req.CorrelationChartInfo.StartDate != `` {
  768. startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.StartDate, time.Local)
  769. if tmpErr != nil {
  770. err = tmpErr
  771. return
  772. }
  773. correlationChart.StartDate = startDateTime
  774. }
  775. if req.CorrelationChartInfo.EndDate != `` {
  776. endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.EndDate, time.Local)
  777. if tmpErr != nil {
  778. err = tmpErr
  779. return
  780. }
  781. correlationChart.EndDate = endDateTime
  782. }
  783. }
  784. correlationChart.EdbInfoIdFirst = req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId
  785. correlationChart.EdbInfoIdSecond = req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId
  786. correlationChart.CreateTime = time.Now().Local()
  787. correlationChart.ModifyTime = time.Now().Local()
  788. //// 生成图表x轴y轴数据
  789. //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
  790. //if e != nil {
  791. // errMsg = "获取失败"
  792. // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
  793. // return
  794. //}
  795. //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
  796. //if e != nil {
  797. // errMsg = "获取失败"
  798. // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
  799. // return
  800. //}
  801. //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
  802. //if e != nil {
  803. // errMsg = "获取失败"
  804. // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
  805. // return
  806. //}
  807. //periodDataByte, e := json.Marshal(periodData)
  808. //if e != nil {
  809. // errMsg = "获取失败"
  810. // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
  811. // return
  812. //}
  813. //correlationDataByte, e := json.Marshal(correlationData[0].Value)
  814. //if e != nil {
  815. // errMsg = "获取失败"
  816. // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
  817. // return
  818. //}
  819. //correlationChart.PeriodData = string(periodDataByte)
  820. //correlationChart.CorrelationData = string(correlationDataByte)
  821. // 新增图表和指标mapping
  822. chartInfoId, e := data_manage.CreateCorrelationChartAndEdb(chartInfo, mapList, correlationChart)
  823. if e != nil {
  824. errMsg = "操作失败"
  825. err = errors.New("新增相关性图表失败, Err: " + e.Error())
  826. return
  827. }
  828. //添加es数据
  829. go data.EsAddOrEditChartInfo(chartInfoId)
  830. return
  831. }
  832. // EditChartInfo 编辑图表
  833. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  834. isSendEmail = true
  835. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  836. if err != nil {
  837. if err.Error() == utils.ErrNoRow() {
  838. errMsg = "图表已被删除,请刷新页面"
  839. err = errors.New(errMsg)
  840. isSendEmail = false
  841. return
  842. }
  843. errMsg = "获取图表信息失败"
  844. err = errors.New("获取图表信息失败,Err:" + err.Error())
  845. return
  846. }
  847. if chartItem.Source != utils.CHART_SOURCE_CORRELATION && chartItem.Source != utils.CHART_SOURCE_ROLLING_CORRELATION {
  848. errMsg = "该图不是相关性图表!"
  849. err = errors.New(errMsg)
  850. isSendEmail = false
  851. return
  852. }
  853. req.ChartName = strings.Trim(req.ChartName, " ")
  854. if req.ChartClassifyId <= 0 {
  855. errMsg = "分类参数错误!"
  856. err = errors.New(errMsg)
  857. isSendEmail = false
  858. return
  859. }
  860. // 相关性图表配置
  861. if req.CorrelationChartInfo.LeadValue == 0 && chartItem.Source == utils.CHART_SOURCE_CORRELATION {
  862. errMsg = "请输入领先期数"
  863. err = errors.New(errMsg)
  864. isSendEmail = false
  865. return
  866. }
  867. if req.CorrelationChartInfo.LeadUnit == "" {
  868. errMsg = "请填写领先单位"
  869. err = errors.New(errMsg)
  870. isSendEmail = false
  871. return
  872. }
  873. //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
  874. // errMsg = "请填写开始结束日期"
  875. // err = errors.New(errMsg)
  876. // isSendEmail = false
  877. // return
  878. //}
  879. startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
  880. if e != nil {
  881. errMsg = "开始日期格式有误"
  882. err = errors.New(errMsg)
  883. isSendEmail = false
  884. return
  885. }
  886. var endDate time.Time
  887. if req.CorrelationChartInfo.EndDate != `` {
  888. endDate, e = time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
  889. if e != nil {
  890. errMsg = "结束日期格式有误"
  891. err = errors.New(errMsg)
  892. isSendEmail = false
  893. return
  894. }
  895. }
  896. if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
  897. errMsg = "请选择AB指标"
  898. err = errors.New(errMsg)
  899. isSendEmail = false
  900. return
  901. }
  902. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  903. if err != nil {
  904. if err.Error() == utils.ErrNoRow() {
  905. errMsg = "分类不存在"
  906. err = errors.New(errMsg)
  907. isSendEmail = false
  908. return
  909. }
  910. errMsg = "获取分类信息失败"
  911. err = errors.New("获取分类信息失败,Err:" + err.Error())
  912. return
  913. }
  914. if chartClassify == nil {
  915. errMsg = "分类不存在"
  916. err = errors.New(errMsg)
  917. isSendEmail = false
  918. return
  919. }
  920. // 图表操作权限
  921. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId)
  922. if !ok {
  923. errMsg = "没有该图表的操作权限"
  924. err = errors.New(errMsg)
  925. isSendEmail = false
  926. return
  927. }
  928. var edbInfoIdArr []int
  929. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  930. edbInfoId := v.EdbInfoId
  931. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  932. if tmpErr != nil {
  933. if tmpErr.Error() == utils.ErrNoRow() {
  934. errMsg = "图表不存在!"
  935. err = errors.New("图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  936. return
  937. } else {
  938. errMsg = "获取图表信息失败!"
  939. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  940. return
  941. }
  942. }
  943. if edbInfo == nil {
  944. errMsg = "指标不存在!"
  945. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  946. return
  947. }
  948. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  949. }
  950. sort.Ints(edbInfoIdArr)
  951. var edbInfoIdArrStr []string
  952. for _, v := range edbInfoIdArr {
  953. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  954. }
  955. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  956. //判断图表是否存在
  957. var condition string
  958. var pars []interface{}
  959. condition += " AND chart_info_id <> ? "
  960. pars = append(pars, req.ChartInfoId)
  961. condition += " AND chart_name=? AND source = ? "
  962. pars = append(pars, req.ChartName, chartItem.Source)
  963. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  964. if err != nil {
  965. errMsg = "判断图表名称是否存在失败"
  966. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  967. return
  968. }
  969. if count > 0 {
  970. errMsg = "图表已存在,请重新填写"
  971. err = errors.New(errMsg)
  972. isSendEmail = false
  973. return
  974. }
  975. correlationChart := new(data_manage.ChartInfoCorrelation)
  976. if e := correlationChart.GetItemById(chartItem.ChartInfoId); e != nil {
  977. errMsg = "操作失败"
  978. err = errors.New("图表相关性信息不存在, Err: " + e.Error())
  979. return
  980. }
  981. // 图表启用与否
  982. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  983. // 重新生成图表值, 并修改相关性图表扩展信息
  984. //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
  985. //if e != nil {
  986. // errMsg = "获取失败"
  987. // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
  988. // return
  989. //}
  990. //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
  991. //if e != nil {
  992. // errMsg = "获取失败"
  993. // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
  994. // return
  995. //}
  996. //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
  997. //if e != nil {
  998. // errMsg = "获取失败"
  999. // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
  1000. // return
  1001. //}
  1002. //periodDataByte, e := json.Marshal(periodData)
  1003. //if e != nil {
  1004. // errMsg = "获取失败"
  1005. // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
  1006. // return
  1007. //}
  1008. //correlationDataByte, e := json.Marshal(correlationData[0].Value)
  1009. //if e != nil {
  1010. // errMsg = "获取失败"
  1011. // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
  1012. // return
  1013. //}
  1014. correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
  1015. correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
  1016. correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
  1017. correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
  1018. correlationChart.StartDate = startDate
  1019. correlationChart.EndDate = endDate
  1020. correlationChart.EdbInfoIdFirst = req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId
  1021. correlationChart.EdbInfoIdSecond = req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId
  1022. // 滚动相关性会有日期等信息
  1023. if chartItem.Source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  1024. correlationChart.DateType = req.CorrelationChartInfo.DateType
  1025. }
  1026. //correlationChart.PeriodData = string(periodDataByte)
  1027. //correlationChart.CorrelationData = string(correlationDataByte)
  1028. correlationChart.ModifyTime = time.Now().Local()
  1029. //correlationUpdateCols := []string{"LeadValue", "LeadUnit", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "PeriodData","CorrelationData", "ModifyTime"}
  1030. correlationUpdateCols := []string{"LeadValue", "LeadUnit", "CalculateValue", "CalculateUnit", "DateType", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "ModifyTime"}
  1031. // 修改图表与指标mapping
  1032. req.ChartType = 9
  1033. err = data_manage.EditCorrelationChartInfoAndMapping(&req, edbInfoIdStr, "公历", 6, disableVal, ``,
  1034. correlationChart, correlationUpdateCols)
  1035. if err != nil {
  1036. errMsg = "保存失败"
  1037. err = errors.New("保存失败,Err:" + err.Error())
  1038. return
  1039. }
  1040. resp := new(data_manage.AddChartInfoResp)
  1041. resp.ChartInfoId = chartItem.ChartInfoId
  1042. resp.UniqueCode = chartItem.UniqueCode
  1043. resp.ChartType = req.ChartType
  1044. //添加es数据
  1045. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1046. //修改my eta es数据
  1047. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1048. return
  1049. }
  1050. // CopyChartInfo 复制图表
  1051. func CopyChartInfo(configId, classifyId int, chartName string, correlationChartInfoReq data_manage.CorrelationChartInfoReq, oldChartInfo *data_manage.ChartInfo, sysUser *system.Admin) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1052. configSource := 2
  1053. isSendEmail = true
  1054. // 获取相关性图的配置
  1055. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(configId, configSource)
  1056. if err != nil {
  1057. return
  1058. }
  1059. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(configId)
  1060. if err != nil {
  1061. return
  1062. }
  1063. multipleGraphConfig.MultipleGraphConfigId = 0
  1064. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1065. if err != nil {
  1066. return
  1067. }
  1068. // 添加图
  1069. addChartReq := data_manage.AddChartInfoReq{
  1070. ChartClassifyId: classifyId,
  1071. ChartName: chartName,
  1072. ChartType: utils.CHART_TYPE_CURVE,
  1073. Calendar: "公历",
  1074. CorrelationChartInfo: correlationChartInfoReq,
  1075. ChartThemeId: oldChartInfo.ChartThemeId,
  1076. SourcesFrom: oldChartInfo.SourcesFrom,
  1077. Instructions: oldChartInfo.Instructions,
  1078. MarkersLines: oldChartInfo.MarkersLines,
  1079. MarkersAreas: oldChartInfo.MarkersAreas,
  1080. }
  1081. chartSource := utils.CHART_SOURCE_CORRELATION // 默认是相关性图
  1082. chartInfo, err, errMsg, isSendEmail = AddChartInfo(addChartReq, chartSource, sysUser)
  1083. if err != nil {
  1084. return
  1085. }
  1086. // 添加关系
  1087. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  1088. //Id: 0,
  1089. MultipleGraphConfigId: multipleGraphConfig.MultipleGraphConfigId,
  1090. ChartInfoId: chartInfo.ChartInfoId,
  1091. Source: configSource,
  1092. ModifyTime: time.Now(),
  1093. CreateTime: time.Now(),
  1094. }
  1095. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1096. if err != nil {
  1097. return
  1098. }
  1099. //添加es数据
  1100. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1101. return
  1102. }