chart_info.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. package correlation
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/shopspring/decimal"
  7. "hongze/hz_crm_api/models/data_manage"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/services/data"
  11. "hongze/hz_crm_api/utils"
  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.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.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(aDataList, baseDataMap)
  251. if e != nil {
  252. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  253. return
  254. }
  255. baseDataList = tmpNewChangeDataList
  256. } else {
  257. baseDataList = aDataList
  258. for _, v := range baseDataList {
  259. baseDataMap[v.DataTime] = v.Value
  260. }
  261. }
  262. // 如果B指标是高频,那么就需要对A指标进行升频
  263. if frequencyIntMap[edbInfoMappingA.Frequency] > frequencyIntMap[edbInfoMappingB.Frequency] {
  264. tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  265. if e != nil {
  266. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  267. return
  268. }
  269. changeDataList = tmpNewChangeDataList
  270. } else {
  271. changeDataList = bDataList
  272. for _, v := range changeDataList {
  273. changeDataMap[v.DataTime] = v.Value
  274. }
  275. }
  276. }
  277. // 计算不领先也不滞后时的相关系数
  278. baseCalculateData := make([]float64, 0)
  279. baseDataTimeArr := make([]string, 0)
  280. for i := range baseDataList {
  281. baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
  282. baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
  283. }
  284. zeroBaseData := make([]float64, 0)
  285. zeroCalculateData := make([]float64, 0)
  286. for i := range baseDataTimeArr {
  287. tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
  288. tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
  289. if ok1 && ok2 {
  290. zeroBaseData = append(zeroBaseData, tmpBaseVal)
  291. zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
  292. }
  293. }
  294. if len(zeroBaseData) != len(zeroCalculateData) {
  295. err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
  296. return
  297. }
  298. zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
  299. if leadValue == 0 {
  300. yData = append(yData, zeroRatio)
  301. }
  302. // 计算领先/滞后N期
  303. if leadValue > 0 {
  304. // 平移变频指标领先/滞后的日期(单位天)
  305. moveUnitDays := utils.FrequencyDaysMap[leadUnit]
  306. for i := range xData {
  307. if xData[i] == 0 {
  308. yData = append(yData, zeroRatio)
  309. continue
  310. }
  311. xCalculateData := make([]float64, 0)
  312. yCalculateData := make([]float64, 0)
  313. // 平移指定天数
  314. mDays := int(moveUnitDays) * xData[i]
  315. _, dMap := MoveDataDaysToNewDataList(changeDataList, mDays)
  316. // 取出对应的基准日期的值
  317. for i2 := range baseDataTimeArr {
  318. tmpDate := baseDataTimeArr[i2]
  319. if yVal, ok := dMap[tmpDate]; ok {
  320. xCalculateData = append(xCalculateData, baseCalculateData[i2])
  321. yCalculateData = append(yCalculateData, yVal)
  322. }
  323. }
  324. if len(yCalculateData) <= 0 {
  325. //err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
  326. //return
  327. // 领先滞后后,没有可以计算的数据了
  328. continue
  329. }
  330. // 公式计算出领先/滞后频度对应点的相关性系数
  331. ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
  332. yData = append(yData, ratio)
  333. }
  334. }
  335. xEdbIdValue = xData
  336. yDataList = make([]data_manage.YData, 0)
  337. yDate := "0000-00-00"
  338. yDataList = append(yDataList, data_manage.YData{
  339. Date: yDate,
  340. Value: yData,
  341. })
  342. return
  343. }
  344. // GetRollingCorrelationChartDataByEdbInfo 滚动相关性计算
  345. func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping, leadValue int, leadUnit string, calculateValue int, calculateUnit string, startDate, endDate string) (xDateTimeValue []string, yDataList []data_manage.YData, err error) {
  346. xDateTimeValue = make([]string, 0)
  347. yData := make([]float64, 0)
  348. // 计算窗口,不包含第一天
  349. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  350. startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  351. baseEdbInfo := edbInfoMappingA
  352. changeEdbInfo := edbInfoMappingB
  353. // 获取时间基准指标在时间区间内的值
  354. aDataList := make([]*data_manage.EdbDataList, 0)
  355. switch baseEdbInfo.EdbInfoCategoryType {
  356. case 0:
  357. aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.EdbInfoId, startDate, endDate)
  358. case 1:
  359. _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, true)
  360. default:
  361. err = errors.New("指标base类型异常")
  362. return
  363. }
  364. // 获取变频指标所有日期的值, 插值法完善数据
  365. bDataList := make([]*data_manage.EdbDataList, 0)
  366. switch changeEdbInfo.EdbInfoCategoryType {
  367. case 0:
  368. bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.EdbInfoId, "", "")
  369. case 1:
  370. _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
  371. default:
  372. err = errors.New("指标change类型异常")
  373. return
  374. }
  375. // 数据平移变频指标领先/滞后的日期(单位天)
  376. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  377. //baseDataList := make([]*data_manage.EdbDataList, 0)
  378. baseDataMap := make(map[string]float64)
  379. changeDataList := make([]*data_manage.EdbDataList, 0)
  380. changeDataMap := make(map[string]float64)
  381. // A指标不管三七二十一,先变个频再说
  382. {
  383. _, e := HandleDataByLinearRegression(aDataList, baseDataMap)
  384. if e != nil {
  385. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  386. return
  387. }
  388. //baseDataList = tmpNewChangeDataList
  389. }
  390. // B指标不管三七二十一,先变个频再说
  391. {
  392. tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  393. if e != nil {
  394. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  395. return
  396. }
  397. changeDataList = tmpNewChangeDataList
  398. // 平移下日期
  399. moveUnitDays := utils.FrequencyDaysMap[leadUnit]
  400. _, changeDataMap = MoveDataDaysToNewDataList(changeDataList, leadValue*moveUnitDays)
  401. }
  402. // 计算计算时,需要多少个日期内数据
  403. calculateDay := utils.FrequencyDaysMap[calculateUnit] * calculateValue
  404. // 计算 每个日期的相关性值
  405. {
  406. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  407. if endDate == `` {
  408. endDate = baseEdbInfo.EndDate
  409. }
  410. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  411. endDateTime = endDateTime.AddDate(0, 0, -(calculateDay - 1))
  412. for currDay := startDateTime; !currDay.After(endDateTime); currDay = currDay.AddDate(0, 0, 1) {
  413. yCalculateData := make([]float64, 0)
  414. baseCalculateData := make([]float64, 0)
  415. // 取出对应的基准日期的值
  416. for i := 0; i < calculateDay; i++ {
  417. iDay := currDay.AddDate(0, 0, i).Format(utils.FormatDate)
  418. tmpBaseValue, ok1 := baseDataMap[iDay]
  419. tmpChangeValue, ok2 := changeDataMap[iDay]
  420. if ok1 && ok2 {
  421. baseCalculateData = append(baseCalculateData, tmpBaseValue)
  422. yCalculateData = append(yCalculateData, tmpChangeValue)
  423. } else {
  424. continue
  425. }
  426. }
  427. // 公式计算出领先/滞后频度对应点的相关性系数
  428. var ratio float64
  429. if len(baseCalculateData) > 0 {
  430. ratio = utils.CalculateCorrelationByIntArr(baseCalculateData, yCalculateData)
  431. }
  432. yData = append(yData, ratio)
  433. xDateTimeValue = append(xDateTimeValue, currDay.AddDate(0, 0, calculateDay-1).Format(utils.FormatDate))
  434. }
  435. }
  436. yDataList = make([]data_manage.YData, 0)
  437. yDate := "0000-00-00"
  438. yDataList = append(yDataList, data_manage.YData{
  439. Date: yDate,
  440. Value: yData,
  441. })
  442. return
  443. }
  444. // ChartInfoRefresh 图表刷新
  445. func ChartInfoRefresh(chartInfoId int) (err error) {
  446. var errMsg string
  447. defer func() {
  448. if err != nil {
  449. go alarm_msg.SendAlarmMsg("CorrelationChartInfoRefresh: "+errMsg, 3)
  450. }
  451. }()
  452. correlationChart := new(data_manage.ChartInfoCorrelation)
  453. if err = correlationChart.GetItemById(chartInfoId); err != nil {
  454. errMsg = "获取相关性图表失败, Err: " + err.Error()
  455. return
  456. }
  457. // 批量刷新ETA指标
  458. err = data.EdbInfoRefreshAllFromBaseV3([]int{correlationChart.EdbInfoIdFirst, correlationChart.EdbInfoIdSecond}, false)
  459. if err != nil {
  460. return
  461. }
  462. // 重新生成数据并更新
  463. edbInfoMappingA, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  464. if err != nil {
  465. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + err.Error()
  466. return
  467. }
  468. edbInfoMappingB, err := data_manage.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  469. if err != nil {
  470. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + err.Error()
  471. return
  472. }
  473. periodData, correlationData, err := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
  474. if err != nil {
  475. errMsg = "获取相关性图表, 图表计算值失败, Err:" + err.Error()
  476. return
  477. }
  478. periodDataByte, err := json.Marshal(periodData)
  479. if err != nil {
  480. errMsg = "相关性图表, X轴信息有误, Err:" + err.Error()
  481. return
  482. }
  483. correlationDataByte, err := json.Marshal(correlationData[0].Value)
  484. if err != nil {
  485. errMsg = "相关性图表, Y轴信息有误, Err:" + err.Error()
  486. return
  487. }
  488. correlationChart.PeriodData = string(periodDataByte)
  489. correlationChart.CorrelationData = string(correlationDataByte)
  490. correlationChart.ModifyTime = time.Now().Local()
  491. correlationUpdateCols := []string{"PeriodData", "CorrelationData", "ModifyTime"}
  492. if err = correlationChart.Update(correlationUpdateCols); err != nil {
  493. errMsg = "更新相关性图表失败, Err:" + err.Error()
  494. return
  495. }
  496. return
  497. }
  498. // GetChartAndCorrelationInfo 获取图表信息和相关信息信息
  499. func GetChartAndCorrelationInfo(chartInfoId int) (chartInfo *data_manage.ChartInfo, correlationInfo *data_manage.ChartInfoCorrelation, tips string, err error) {
  500. item, e := data_manage.GetChartInfoById(chartInfoId)
  501. if e != nil {
  502. if e.Error() == utils.ErrNoRow() {
  503. tips = "图表已被删除, 请刷新页面"
  504. err = fmt.Errorf("图表已被删除, 请刷新页面")
  505. return
  506. }
  507. err = fmt.Errorf("获取图表信息失败, Err: %s", e.Error())
  508. return
  509. }
  510. if item.Source != utils.CHART_SOURCE_CORRELATION {
  511. tips = "该图不是相关性图表"
  512. err = fmt.Errorf("该图不是相关性图表")
  513. return
  514. }
  515. chartInfo = item
  516. correlationInfo = new(data_manage.ChartInfoCorrelation)
  517. if e = correlationInfo.GetItemById(chartInfo.ChartInfoId); e != nil {
  518. err = fmt.Errorf("获取图表相关性信息失败, Err: %s", e.Error())
  519. return
  520. }
  521. return
  522. }
  523. // AddChartInfo 添加图表
  524. func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.Admin) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  525. isSendEmail = true
  526. req.ChartName = strings.Trim(req.ChartName, " ")
  527. if req.ChartName == "" {
  528. errMsg = "请填写图表名称!"
  529. err = errors.New(errMsg)
  530. isSendEmail = false
  531. return
  532. }
  533. if req.ChartClassifyId <= 0 {
  534. errMsg = "分类参数错误!"
  535. err = errors.New(errMsg)
  536. isSendEmail = false
  537. return
  538. }
  539. // 相关性图表配置
  540. if req.CorrelationChartInfo.LeadValue == 0 && source == utils.CHART_SOURCE_CORRELATION {
  541. errMsg = "请输入领先期数"
  542. err = errors.New(errMsg)
  543. isSendEmail = false
  544. return
  545. }
  546. if req.CorrelationChartInfo.LeadUnit == "" {
  547. errMsg = "请填写领先单位"
  548. err = errors.New(errMsg)
  549. isSendEmail = false
  550. return
  551. }
  552. //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
  553. // errMsg = "请填写开始结束日期"
  554. // err = errors.New(errMsg)
  555. // isSendEmail = false
  556. // return
  557. //}
  558. //startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
  559. //if e != nil {
  560. // errMsg = "开始日期格式有误"
  561. // err = errors.New(errMsg)
  562. // isSendEmail = false
  563. // return
  564. //}
  565. //endDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
  566. //if e != nil {
  567. // errMsg = "结束日期格式有误"
  568. // err = errors.New(errMsg)
  569. // isSendEmail = false
  570. // return
  571. //}
  572. if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
  573. errMsg = "请选择AB指标"
  574. err = errors.New(errMsg)
  575. isSendEmail = false
  576. return
  577. }
  578. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  579. if err != nil {
  580. if err.Error() == utils.ErrNoRow() {
  581. errMsg = "分类不存在"
  582. err = errors.New(errMsg)
  583. isSendEmail = false
  584. return
  585. }
  586. errMsg = "获取分类信息失败"
  587. err = errors.New("获取分类信息失败,Err:" + err.Error())
  588. return
  589. }
  590. if chartClassify == nil {
  591. errMsg = "分类不存在"
  592. err = errors.New(errMsg)
  593. isSendEmail = false
  594. return
  595. }
  596. var edbInfoIdArr []int
  597. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  598. edbInfoId := v.EdbInfoId
  599. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  600. if tmpErr != nil {
  601. if tmpErr.Error() == utils.ErrNoRow() {
  602. errMsg = "指标不存在!"
  603. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  604. return
  605. } else {
  606. errMsg = "获取指标信息失败!"
  607. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  608. return
  609. }
  610. }
  611. if edbInfo == nil {
  612. errMsg = "指标已被删除,请重新选择!"
  613. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  614. return
  615. } else {
  616. if edbInfo.EdbInfoId <= 0 {
  617. errMsg = "指标已被删除,请重新选择!"
  618. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  619. return
  620. }
  621. }
  622. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  623. edbInfo.EdbNameSource = edbInfo.EdbName
  624. }
  625. sort.Ints(edbInfoIdArr)
  626. var edbInfoIdArrStr []string
  627. for _, v := range edbInfoIdArr {
  628. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  629. }
  630. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  631. var chartInfoId int
  632. // 判断图表是否存在
  633. var condition string
  634. var pars []interface{}
  635. condition += " AND chart_name=? AND source = ? "
  636. pars = append(pars, req.ChartName, source)
  637. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  638. if err != nil {
  639. errMsg = "判断图表名称是否存在失败"
  640. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  641. return
  642. }
  643. if count > 0 {
  644. errMsg = "图表已存在,请重新填写"
  645. err = errors.New(errMsg)
  646. isSendEmail = false
  647. return
  648. }
  649. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  650. chartInfo = new(data_manage.ChartInfo)
  651. chartInfo.ChartName = req.ChartName
  652. chartInfo.EdbInfoIds = edbInfoIdStr
  653. chartInfo.ChartClassifyId = req.ChartClassifyId
  654. chartInfo.SysUserId = sysUser.AdminId
  655. chartInfo.SysUserRealName = sysUser.RealName
  656. chartInfo.CreateTime = time.Now()
  657. chartInfo.ModifyTime = time.Now()
  658. chartInfo.IsSetName = 0
  659. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  660. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  661. chartInfo.ChartType = 9 // 相关性图
  662. chartInfo.Calendar = "公历"
  663. chartInfo.DateType = 6
  664. chartInfo.StartDate = req.StartDate
  665. chartInfo.EndDate = req.EndDate
  666. chartInfo.SeasonStartDate = req.SeasonStartDate
  667. chartInfo.SeasonEndDate = req.SeasonEndDate
  668. chartInfo.LeftMin = req.LeftMin
  669. chartInfo.LeftMax = req.LeftMax
  670. chartInfo.RightMin = req.RightMin
  671. chartInfo.RightMax = req.RightMax
  672. chartInfo.Disabled = disableVal
  673. chartInfo.Source = source
  674. // 指标信息
  675. mapList := make([]*data_manage.ChartEdbMapping, 0)
  676. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  677. mapItem := new(data_manage.ChartEdbMapping)
  678. mapItem.EdbInfoId = v.EdbInfoId
  679. mapItem.CreateTime = time.Now()
  680. mapItem.ModifyTime = time.Now()
  681. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  682. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  683. mapItem.IsOrder = true
  684. mapItem.IsAxis = 1
  685. mapItem.EdbInfoType = 1
  686. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  687. mapList = append(mapList, mapItem)
  688. }
  689. // 相关性图表扩展信息
  690. correlationChart := new(data_manage.ChartInfoCorrelation)
  691. correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
  692. correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
  693. correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
  694. correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
  695. correlationChart.BaseCalculateValue = req.CorrelationChartInfo.BaseCalculateValue
  696. correlationChart.BaseCalculateUnit = req.CorrelationChartInfo.BaseCalculateUnit
  697. // 滚动相关性会有日期等信息
  698. if source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  699. correlationChart.DateType = req.CorrelationChartInfo.DateType
  700. if req.CorrelationChartInfo.StartDate != `` {
  701. startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.StartDate, time.Local)
  702. if tmpErr != nil {
  703. err = tmpErr
  704. return
  705. }
  706. correlationChart.StartDate = startDateTime
  707. }
  708. if req.CorrelationChartInfo.EndDate != `` {
  709. endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, req.CorrelationChartInfo.EndDate, time.Local)
  710. if tmpErr != nil {
  711. err = tmpErr
  712. return
  713. }
  714. correlationChart.EndDate = endDateTime
  715. }
  716. }
  717. correlationChart.EdbInfoIdFirst = req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId
  718. correlationChart.EdbInfoIdSecond = req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId
  719. correlationChart.CreateTime = time.Now().Local()
  720. correlationChart.ModifyTime = time.Now().Local()
  721. //// 生成图表x轴y轴数据
  722. //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
  723. //if e != nil {
  724. // errMsg = "获取失败"
  725. // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
  726. // return
  727. //}
  728. //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
  729. //if e != nil {
  730. // errMsg = "获取失败"
  731. // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
  732. // return
  733. //}
  734. //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
  735. //if e != nil {
  736. // errMsg = "获取失败"
  737. // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
  738. // return
  739. //}
  740. //periodDataByte, e := json.Marshal(periodData)
  741. //if e != nil {
  742. // errMsg = "获取失败"
  743. // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
  744. // return
  745. //}
  746. //correlationDataByte, e := json.Marshal(correlationData[0].Value)
  747. //if e != nil {
  748. // errMsg = "获取失败"
  749. // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
  750. // return
  751. //}
  752. //correlationChart.PeriodData = string(periodDataByte)
  753. //correlationChart.CorrelationData = string(correlationDataByte)
  754. // 新增图表和指标mapping
  755. chartInfoId, e := data_manage.CreateCorrelationChartAndEdb(chartInfo, mapList, correlationChart)
  756. if e != nil {
  757. errMsg = "操作失败"
  758. err = errors.New("新增相关性图表失败, Err: " + e.Error())
  759. return
  760. }
  761. //添加es数据
  762. go data.EsAddOrEditChartInfo(chartInfoId)
  763. return
  764. }
  765. // EditChartInfo 编辑图表
  766. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  767. isSendEmail = true
  768. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  769. if err != nil {
  770. if err.Error() == utils.ErrNoRow() {
  771. errMsg = "图表已被删除,请刷新页面"
  772. err = errors.New(errMsg)
  773. isSendEmail = false
  774. return
  775. }
  776. errMsg = "获取图表信息失败"
  777. err = errors.New("获取图表信息失败,Err:" + err.Error())
  778. return
  779. }
  780. if chartItem.Source != utils.CHART_SOURCE_CORRELATION && chartItem.Source != utils.CHART_SOURCE_ROLLING_CORRELATION {
  781. errMsg = "该图不是相关性图表!"
  782. err = errors.New(errMsg)
  783. isSendEmail = false
  784. return
  785. }
  786. req.ChartName = strings.Trim(req.ChartName, " ")
  787. if req.ChartClassifyId <= 0 {
  788. errMsg = "分类参数错误!"
  789. err = errors.New(errMsg)
  790. isSendEmail = false
  791. return
  792. }
  793. // 相关性图表配置
  794. if req.CorrelationChartInfo.LeadValue == 0 && chartItem.Source == utils.CHART_SOURCE_CORRELATION {
  795. errMsg = "请输入领先期数"
  796. err = errors.New(errMsg)
  797. isSendEmail = false
  798. return
  799. }
  800. if req.CorrelationChartInfo.LeadUnit == "" {
  801. errMsg = "请填写领先单位"
  802. err = errors.New(errMsg)
  803. isSendEmail = false
  804. return
  805. }
  806. //if req.CorrelationChartInfo.StartDate == "" || req.CorrelationChartInfo.EndDate == "" {
  807. // errMsg = "请填写开始结束日期"
  808. // err = errors.New(errMsg)
  809. // isSendEmail = false
  810. // return
  811. //}
  812. startDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.StartDate)
  813. if e != nil {
  814. errMsg = "开始日期格式有误"
  815. err = errors.New(errMsg)
  816. isSendEmail = false
  817. return
  818. }
  819. endDate, e := time.Parse(utils.FormatDate, req.CorrelationChartInfo.EndDate)
  820. if e != nil {
  821. errMsg = "结束日期格式有误"
  822. err = errors.New(errMsg)
  823. isSendEmail = false
  824. return
  825. }
  826. if len(req.CorrelationChartInfo.EdbInfoIdList) != 2 {
  827. errMsg = "请选择AB指标"
  828. err = errors.New(errMsg)
  829. isSendEmail = false
  830. return
  831. }
  832. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  833. if err != nil {
  834. if err.Error() == utils.ErrNoRow() {
  835. errMsg = "分类不存在"
  836. err = errors.New(errMsg)
  837. isSendEmail = false
  838. return
  839. }
  840. errMsg = "获取分类信息失败"
  841. err = errors.New("获取分类信息失败,Err:" + err.Error())
  842. return
  843. }
  844. if chartClassify == nil {
  845. errMsg = "分类不存在"
  846. err = errors.New(errMsg)
  847. isSendEmail = false
  848. return
  849. }
  850. // 图表操作权限
  851. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId)
  852. if !ok {
  853. errMsg = "没有该图表的操作权限"
  854. err = errors.New(errMsg)
  855. isSendEmail = false
  856. return
  857. }
  858. var edbInfoIdArr []int
  859. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  860. edbInfoId := v.EdbInfoId
  861. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  862. if tmpErr != nil {
  863. if tmpErr.Error() == utils.ErrNoRow() {
  864. errMsg = "图表不存在!"
  865. err = errors.New("图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  866. return
  867. } else {
  868. errMsg = "获取图表信息失败!"
  869. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  870. return
  871. }
  872. }
  873. if edbInfo == nil {
  874. errMsg = "指标不存在!"
  875. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  876. return
  877. }
  878. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  879. }
  880. sort.Ints(edbInfoIdArr)
  881. var edbInfoIdArrStr []string
  882. for _, v := range edbInfoIdArr {
  883. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  884. }
  885. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  886. //判断图表是否存在
  887. var condition string
  888. var pars []interface{}
  889. condition += " AND chart_info_id <> ? "
  890. pars = append(pars, req.ChartInfoId)
  891. condition += " AND chart_name=? AND source = ? "
  892. pars = append(pars, req.ChartName, chartItem.Source)
  893. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  894. if err != nil {
  895. errMsg = "判断图表名称是否存在失败"
  896. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  897. return
  898. }
  899. if count > 0 {
  900. errMsg = "图表已存在,请重新填写"
  901. err = errors.New(errMsg)
  902. isSendEmail = false
  903. return
  904. }
  905. correlationChart := new(data_manage.ChartInfoCorrelation)
  906. if e := correlationChart.GetItemById(chartItem.ChartInfoId); e != nil {
  907. errMsg = "操作失败"
  908. err = errors.New("图表相关性信息不存在, Err: " + e.Error())
  909. return
  910. }
  911. // 图表启用与否
  912. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  913. // 重新生成图表值, 并修改相关性图表扩展信息
  914. //edbInfoMappingA, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[0].EdbInfoId)
  915. //if e != nil {
  916. // errMsg = "获取失败"
  917. // err = errors.New("获取相关性图表, A指标mapping信息失败, Err:" + e.Error())
  918. // return
  919. //}
  920. //edbInfoMappingB, e := data_manage.GetChartEdbMappingByEdbInfoId(req.CorrelationChartInfo.EdbInfoIdList[1].EdbInfoId)
  921. //if e != nil {
  922. // errMsg = "获取失败"
  923. // err = errors.New("获取相关性图表, B指标mapping信息失败, Err:" + e.Error())
  924. // return
  925. //}
  926. //periodData, correlationData, e := GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.CorrelationChartInfo.LeadValue, req.CorrelationChartInfo.LeadUnit, req.CorrelationChartInfo.StartDate, req.CorrelationChartInfo.EndDate)
  927. //if e != nil {
  928. // errMsg = "获取失败"
  929. // err = errors.New("获取相关性图表, 图表计算值失败, Err:" + e.Error())
  930. // return
  931. //}
  932. //periodDataByte, e := json.Marshal(periodData)
  933. //if e != nil {
  934. // errMsg = "获取失败"
  935. // err = errors.New("相关性图表, X轴信息有误, Err:" + e.Error())
  936. // return
  937. //}
  938. //correlationDataByte, e := json.Marshal(correlationData[0].Value)
  939. //if e != nil {
  940. // errMsg = "获取失败"
  941. // err = errors.New("相关性图表, Y轴信息有误, Err:" + e.Error())
  942. // return
  943. //}
  944. correlationChart.LeadValue = req.CorrelationChartInfo.LeadValue
  945. correlationChart.LeadUnit = req.CorrelationChartInfo.LeadUnit
  946. correlationChart.CalculateValue = req.CorrelationChartInfo.CalculateValue
  947. correlationChart.CalculateUnit = req.CorrelationChartInfo.CalculateUnit
  948. correlationChart.StartDate = startDate
  949. correlationChart.EndDate = endDate
  950. correlationChart.EdbInfoIdFirst = edbInfoIdArr[0]
  951. correlationChart.EdbInfoIdSecond = edbInfoIdArr[1]
  952. // 滚动相关性会有日期等信息
  953. if chartItem.Source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  954. correlationChart.DateType = req.CorrelationChartInfo.DateType
  955. }
  956. //correlationChart.PeriodData = string(periodDataByte)
  957. //correlationChart.CorrelationData = string(correlationDataByte)
  958. correlationChart.ModifyTime = time.Now().Local()
  959. //correlationUpdateCols := []string{"LeadValue", "LeadUnit", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "PeriodData","CorrelationData", "ModifyTime"}
  960. correlationUpdateCols := []string{"LeadValue", "LeadUnit", "CalculateValue", "CalculateUnit", "DateType", "StartDate", "EndDate", "EdbInfoIdFirst", "EdbInfoIdSecond", "ModifyTime"}
  961. // 修改图表与指标mapping
  962. req.ChartType = 9
  963. err = data_manage.EditCorrelationChartInfoAndMapping(&req, edbInfoIdStr, "公历", 6, disableVal, ``,
  964. correlationChart, correlationUpdateCols)
  965. if err != nil {
  966. errMsg = "保存失败"
  967. err = errors.New("保存失败,Err:" + err.Error())
  968. return
  969. }
  970. resp := new(data_manage.AddChartInfoResp)
  971. resp.ChartInfoId = chartItem.ChartInfoId
  972. resp.UniqueCode = chartItem.UniqueCode
  973. resp.ChartType = req.ChartType
  974. //添加es数据
  975. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  976. //修改my eta es数据
  977. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  978. return
  979. }
  980. // CopyChartInfo 复制图表
  981. func CopyChartInfo(configId, classifyId int, chartName string, correlationChartInfoReq data_manage.CorrelationChartInfoReq, sysUser *system.Admin) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  982. configSource := 2
  983. isSendEmail = true
  984. // 获取相关性图的配置
  985. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(configId, configSource)
  986. if err != nil {
  987. return
  988. }
  989. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(configId)
  990. if err != nil {
  991. return
  992. }
  993. multipleGraphConfig.MultipleGraphConfigId = 0
  994. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  995. if err != nil {
  996. return
  997. }
  998. // 添加图
  999. addChartReq := data_manage.AddChartInfoReq{
  1000. ChartClassifyId: classifyId,
  1001. ChartName: chartName,
  1002. ChartType: utils.CHART_TYPE_CURVE,
  1003. Calendar: "公历",
  1004. CorrelationChartInfo: correlationChartInfoReq,
  1005. }
  1006. chartSource := utils.CHART_SOURCE_CORRELATION // 默认是相关性图
  1007. chartInfo, err, errMsg, isSendEmail = AddChartInfo(addChartReq, chartSource, sysUser)
  1008. if err != nil {
  1009. return
  1010. }
  1011. // 添加关系
  1012. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  1013. //Id: 0,
  1014. MultipleGraphConfigId: multipleGraphConfig.MultipleGraphConfigId,
  1015. ChartInfoId: chartInfo.ChartInfoId,
  1016. Source: configSource,
  1017. ModifyTime: time.Now(),
  1018. CreateTime: time.Now(),
  1019. }
  1020. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1021. if err != nil {
  1022. return
  1023. }
  1024. //添加es数据
  1025. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1026. return
  1027. }