multiple_graph_config.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/request"
  7. "eta/eta_api/models/data_manage/response"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/data"
  10. correlationServ "eta/eta_api/services/data/correlation"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "math"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. //多图配置
  19. // MultipleGraphConfigSave
  20. // @Title 保存配置接口
  21. // @Description 保存配置接口
  22. // @Param request body request.SaveMultipleGraphConfigReq true "type json string"
  23. // @Success Ret=200 返回图表id
  24. // @router /multiple_graph/config/save [post]
  25. func (this *ChartInfoController) MultipleGraphConfigSave() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. sysUser := this.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. var req request.SaveMultipleGraphConfigReq
  39. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  40. if err != nil {
  41. br.Msg = "参数解析异常!"
  42. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  43. return
  44. }
  45. if req.EdbInfoIdA <= 0 {
  46. if this.Lang == utils.EnLangVersion {
  47. br.Msg = "Indicator A not selected"
  48. } else {
  49. br.Msg = "指标A未选择"
  50. }
  51. br.IsSendEmail = false
  52. return
  53. }
  54. _, err = data_manage.GetEdbInfoById(req.EdbInfoIdA)
  55. if err != nil {
  56. br.Msg = "指标A异常"
  57. br.ErrMsg = "指标A异常,err:" + err.Error()
  58. if err.Error() == utils.ErrNoRow() {
  59. br.Msg = `指标A不存在`
  60. br.IsSendEmail = false
  61. }
  62. return
  63. }
  64. if req.EdbInfoIdB <= 0 {
  65. if this.Lang == utils.EnLangVersion {
  66. br.Msg = "Indicator B not selected"
  67. } else {
  68. br.Msg = "指标B未选择"
  69. }
  70. br.IsSendEmail = false
  71. return
  72. }
  73. _, err = data_manage.GetEdbInfoById(req.EdbInfoIdB)
  74. if err != nil {
  75. br.Msg = "指标B异常"
  76. br.ErrMsg = "指标B异常,err:" + err.Error()
  77. if err.Error() == utils.ErrNoRow() {
  78. br.Msg = `指标B不存在`
  79. br.IsSendEmail = false
  80. }
  81. return
  82. }
  83. curveStrByte, err := json.Marshal(req.Curve)
  84. if err != nil {
  85. br.Msg = `曲线图配置异常`
  86. br.ErrMsg = `曲线图配置异常,ERR:` + err.Error()
  87. return
  88. }
  89. if req.Correlation.LeadValue <= 0 {
  90. br.Msg = `分析周期不允许设置为负数或0`
  91. br.IsSendEmail = false
  92. return
  93. }
  94. correlationStrByte, err := json.Marshal(req.Correlation)
  95. if err != nil {
  96. br.Msg = `相关性配置异常`
  97. br.ErrMsg = `相关性配置异常,ERR:` + err.Error()
  98. return
  99. }
  100. rollingCorrelationStrByte, err := json.Marshal(req.RollingCorrelation)
  101. if err != nil {
  102. br.Msg = `滚动相关性配置异常`
  103. br.ErrMsg = `滚动相关性配置异常,ERR:` + err.Error()
  104. return
  105. }
  106. var multipleGraphConfig *data_manage.MultipleGraphConfig
  107. if req.MultipleGraphConfigId > 0 {
  108. multipleGraphConfig, err = data_manage.GetMultipleGraphConfigById(req.MultipleGraphConfigId)
  109. if err != nil {
  110. br.Msg = `不存在该配置`
  111. br.ErrMsg = `不存在该配置`
  112. return
  113. }
  114. multipleGraphConfig.EdbInfoIdA = req.EdbInfoIdA
  115. multipleGraphConfig.EdbInfoIdB = req.EdbInfoIdB
  116. multipleGraphConfig.Curve = string(curveStrByte)
  117. multipleGraphConfig.Correlation = string(correlationStrByte)
  118. multipleGraphConfig.RollingCorrelation = string(rollingCorrelationStrByte)
  119. multipleGraphConfig.ModifyTime = time.Now()
  120. err = multipleGraphConfig.Update([]string{"EdbInfoIdA", "EdbInfoIdB", "Curve", "Correlation", "RollingCorrelation", "ModifyTime"})
  121. } else {
  122. multipleGraphConfig = &data_manage.MultipleGraphConfig{
  123. //MultipleGraphConfigId: 0,
  124. EdbInfoIdA: req.EdbInfoIdA,
  125. EdbInfoIdB: req.EdbInfoIdB,
  126. Curve: string(curveStrByte),
  127. Correlation: string(correlationStrByte),
  128. RollingCorrelation: string(rollingCorrelationStrByte),
  129. SysUserId: this.SysUser.AdminId,
  130. SysUserRealName: this.SysUser.RealName,
  131. ModifyTime: time.Now(),
  132. CreateTime: time.Now(),
  133. }
  134. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  135. }
  136. if err != nil {
  137. br.Msg = `保存失败`
  138. br.ErrMsg = `保存失败,Err:` + err.Error()
  139. return
  140. }
  141. br.Ret = 200
  142. br.Success = true
  143. br.Msg = "保存成功"
  144. br.Data = multipleGraphConfig
  145. br.IsAddLog = true
  146. }
  147. // MultipleGraphPreview
  148. // @Title 批量添加图表的预览接口
  149. // @Description 批量添加图表的预览接口
  150. // @Param request body request.SaveMultipleGraphConfigReq true "type json string"
  151. // @Success Ret=200 返回图表id
  152. // @router /multiple_graph/preview [post]
  153. func (this *ChartInfoController) MultipleGraphPreview() {
  154. br := new(models.BaseResponse).Init()
  155. defer func() {
  156. this.Data["json"] = br
  157. this.ServeJSON()
  158. }()
  159. sysUser := this.SysUser
  160. if sysUser == nil {
  161. br.Msg = "请登录"
  162. br.ErrMsg = "请登录,SysUser Is Empty"
  163. br.Ret = 408
  164. return
  165. }
  166. var req request.SaveMultipleGraphConfigReq
  167. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  168. if err != nil {
  169. br.Msg = "参数解析异常!"
  170. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  171. return
  172. }
  173. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  174. if err != nil {
  175. br.Msg = "获取失败"
  176. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  177. return
  178. }
  179. var maxDate time.Time
  180. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  181. for _, v := range mappingList {
  182. if v.EdbInfoId == req.EdbInfoIdA {
  183. edbInfoMappingA = v
  184. }
  185. if v.EdbInfoId == req.EdbInfoIdB {
  186. edbInfoMappingB = v
  187. }
  188. if v.LatestDate != "" {
  189. latestDateT, _ := time.Parse(utils.FormatDate, v.LatestDate)
  190. maxDate = latestDateT
  191. }
  192. }
  193. if edbInfoMappingA == nil {
  194. br.Msg = "指标A不存在"
  195. br.IsSendEmail = false
  196. return
  197. }
  198. if edbInfoMappingB == nil {
  199. br.Msg = "指标B不存在"
  200. br.IsSendEmail = false
  201. return
  202. }
  203. resp := response.PreviewMultipleGraphConfigResp{}
  204. chartInfo := new(data_manage.ChartInfoView)
  205. chartInfo.Calendar = `公历`
  206. chartInfo.ChartName = edbInfoMappingA.EdbName + "与" + edbInfoMappingB.EdbName
  207. // 曲线图表信息
  208. {
  209. tmpChartInfo := *chartInfo
  210. curveConf := req.Curve
  211. edbInfoType := 0
  212. if curveConf.EdbInfoType {
  213. edbInfoType = 1
  214. }
  215. for _, v := range mappingList {
  216. v.EdbInfoType = 1
  217. v.IsOrder = false
  218. if v.EdbInfoId == req.EdbInfoIdB {
  219. v.LeadValue = curveConf.LeadValue
  220. v.LeadUnit = curveConf.LeadUnit
  221. v.EdbInfoType = edbInfoType
  222. v.IsOrder = curveConf.IsOrder
  223. }
  224. }
  225. startDate, endDate := utils.GetDateByDateTypeV2(curveConf.DateType, curveConf.StartDate, curveConf.EndDate, curveConf.StartYear, maxDate)
  226. // 获取图表中的指标数据
  227. edbList, _, _, _, err, errMsg := data.GetChartEdbData(tmpChartInfo.ChartInfoId, tmpChartInfo.ChartType, tmpChartInfo.Calendar, startDate, endDate, mappingList, tmpChartInfo.ExtraConfig, tmpChartInfo.SeasonExtraConfig)
  228. if err != nil {
  229. br.Msg = "获取失败"
  230. if errMsg != `` {
  231. br.Msg = errMsg
  232. }
  233. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  234. return
  235. }
  236. warnEdbList := make([]string, 0)
  237. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  238. for _, v := range edbList {
  239. if v.IsNullData {
  240. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  241. }
  242. chartEdbInfoMapping := *v
  243. chartEdbInfoMapping.IsOrder = false
  244. if v.EdbInfoId == req.EdbInfoIdA {
  245. chartEdbInfoMapping.IsAxis = 1
  246. chartEdbInfoMapping.MinData = curveConf.LeftMin
  247. chartEdbInfoMapping.MaxData = curveConf.LeftMax
  248. chartEdbInfoMapping.ChartColor = `#00F`
  249. chartEdbInfoMapping.LeadValue = curveConf.LeadValue
  250. chartEdbInfoMapping.LeadUnit = curveConf.LeadUnit
  251. chartEdbInfoMapping.ChartWidth = 3
  252. chartEdbInfoMapping.EdbInfoType = edbInfoType
  253. } else {
  254. chartEdbInfoMapping.IsAxis = 0
  255. chartEdbInfoMapping.MinData = curveConf.RightMin
  256. chartEdbInfoMapping.MaxData = curveConf.RightMax
  257. chartEdbInfoMapping.ChartColor = `#F00`
  258. chartEdbInfoMapping.LeadValue = curveConf.LeadValue
  259. chartEdbInfoMapping.LeadUnit = curveConf.LeadUnit
  260. chartEdbInfoMapping.ChartWidth = 1
  261. chartEdbInfoMapping.IsOrder = curveConf.IsOrder
  262. chartEdbInfoMapping.EdbInfoType = edbInfoType
  263. }
  264. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  265. }
  266. if len(warnEdbList) > 0 {
  267. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  268. }
  269. tmpChartInfo.Source = utils.CHART_SOURCE_DEFAULT
  270. tmpChartInfo.DateType = curveConf.DateType
  271. tmpChartInfo.StartDate = startDate
  272. tmpChartInfo.EndDate = endDate
  273. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  274. resp.CurveData = response.CurveResp{
  275. ChartInfo: tmpChartInfo,
  276. EdbInfoList: tmpEdbList,
  277. }
  278. }
  279. // 相关性图表信息
  280. {
  281. correlationConf := req.Correlation
  282. // 配置了数据才有返回
  283. if correlationConf.LeadValue > 0 {
  284. tmpChartInfo := *chartInfo
  285. moveUnitDays, ok := utils.FrequencyDaysMap[correlationConf.CalculateUnit]
  286. if !ok {
  287. br.Msg = "错误的分析周期"
  288. br.IsSendEmail = false
  289. return
  290. }
  291. startDate := time.Now().AddDate(0, 0, -correlationConf.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  292. endDate := time.Now().Format(utils.FormatDate)
  293. xEdbIdValue, yDataList, e := correlationServ.GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationConf.LeadValue, correlationConf.LeadUnit, startDate, endDate, "")
  294. if e != nil {
  295. br.Msg = "获取失败"
  296. br.ErrMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  297. return
  298. }
  299. tmpChartInfo.ChartName += fmt.Sprint(`(`, correlationConf.CalculateValue, correlationConf.CalculateUnit, `)`)
  300. tmpChartInfo.Source = utils.CHART_SOURCE_CORRELATION
  301. resp.CorrelationData = response.CorrelationResp{
  302. ChartInfo: tmpChartInfo,
  303. EdbInfoList: mappingList,
  304. XEdbIdValue: xEdbIdValue,
  305. YDataList: yDataList,
  306. CorrelationChartInfo: data_manage.CorrelationInfo{
  307. LeadValue: correlationConf.LeadValue,
  308. LeadUnit: correlationConf.LeadUnit,
  309. StartDate: startDate,
  310. EndDate: endDate,
  311. EdbInfoIdFirst: edbInfoMappingA.EdbInfoId,
  312. EdbInfoIdSecond: edbInfoMappingB.EdbInfoId,
  313. },
  314. }
  315. }
  316. }
  317. // 滚动相关性图表信息
  318. {
  319. correlationConf := req.Correlation
  320. rollingCorrelationConfList := req.RollingCorrelation
  321. // 配置了数据才有返回
  322. if correlationConf.LeadValue > 0 {
  323. //moveUnitDays, ok := utils.FrequencyDaysMap[correlationConf.CalculateUnit]
  324. //if !ok {
  325. // br.Msg = "错误的分析周期"
  326. // br.IsSendEmail = false
  327. // return
  328. //}
  329. //startDate := time.Now().AddDate(0, 0, -correlationConf.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  330. //endDate := time.Now().Format(utils.FormatDate)
  331. // 数据的开始/结束日期
  332. curveConf := req.Curve
  333. startDate, endDate := utils.GetDateByDateTypeV2(curveConf.DateType, curveConf.StartDate, curveConf.EndDate, curveConf.StartYear, maxDate)
  334. rollingCorrelationData := make([]interface{}, 0)
  335. for _, rollingCorrelationConf := range rollingCorrelationConfList {
  336. if rollingCorrelationConf.CalculateValue > 0 && rollingCorrelationConf.CalculateUnit != `` {
  337. tmpChartInfo := *chartInfo
  338. tmpChartInfo.Source = utils.CHART_SOURCE_ROLLING_CORRELATION
  339. // ”指标A名称“+”指标B名称“+"滚动相关性"+”滚动相关性计算窗口值“+”滚动相关性计算窗口单位“
  340. // 若滚动相关性的计算周期为30天
  341. // 1、B即不领先也不滞后时:“指标A名称”与“指标B名称"30天滚动相关性;
  342. // 2、B有领先时:“指标A名称”与“指标B名称(领先15天)"30天滚动相关性;
  343. // 3、B滞后时:“指标A名称”与“指标B名称(滞后15天)"30天滚动相关性
  344. if rollingCorrelationConf.LeadValue == 0 {
  345. tmpChartInfo.ChartName = fmt.Sprintf(`%s与%s%d%s滚动相关性`, edbInfoMappingA.EdbName, edbInfoMappingB.EdbName, rollingCorrelationConf.CalculateValue, rollingCorrelationConf.CalculateUnit)
  346. } else if rollingCorrelationConf.LeadValue < 0 {
  347. tmpChartInfo.ChartName = fmt.Sprintf(`%s与%s(滞后%d%s)%d%s滚动相关性`, edbInfoMappingA.EdbName, edbInfoMappingB.EdbName, int(math.Abs(float64(rollingCorrelationConf.LeadValue))), rollingCorrelationConf.LeadUnit, rollingCorrelationConf.CalculateValue, rollingCorrelationConf.CalculateUnit)
  348. } else {
  349. tmpChartInfo.ChartName = fmt.Sprintf(`%s与%s(领先%d%s)%d%s滚动相关性`, edbInfoMappingA.EdbName, edbInfoMappingB.EdbName, int(math.Abs(float64(rollingCorrelationConf.LeadValue))), rollingCorrelationConf.LeadUnit, rollingCorrelationConf.CalculateValue, rollingCorrelationConf.CalculateUnit)
  350. }
  351. dataResp, e := correlationServ.GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, rollingCorrelationConf.LeadValue, rollingCorrelationConf.LeadUnit, rollingCorrelationConf.CalculateValue, rollingCorrelationConf.CalculateUnit, startDate, endDate, tmpChartInfo.ChartName, tmpChartInfo.ChartNameEn)
  352. if e != nil {
  353. br.Msg = "获取失败"
  354. br.ErrMsg = "获取滚动相关性图表, 图表计算值失败, Err:" + e.Error()
  355. return
  356. }
  357. rollingCorrelationData = append(rollingCorrelationData, response.RollingCorrelationResp{
  358. ChartInfo: tmpChartInfo,
  359. EdbInfoList: mappingList,
  360. DataResp: dataResp,
  361. CorrelationChartInfo: data_manage.CorrelationInfo{
  362. LeadValue: rollingCorrelationConf.LeadValue,
  363. LeadUnit: rollingCorrelationConf.LeadUnit,
  364. CalculateValue: rollingCorrelationConf.CalculateValue,
  365. CalculateUnit: rollingCorrelationConf.CalculateUnit,
  366. StartDate: startDate,
  367. EndDate: endDate,
  368. EdbInfoIdFirst: edbInfoMappingA.EdbInfoId,
  369. EdbInfoIdSecond: edbInfoMappingB.EdbInfoId,
  370. },
  371. })
  372. } else {
  373. rollingCorrelationData = append(rollingCorrelationData, nil)
  374. }
  375. }
  376. resp.RollingCorrelationData = rollingCorrelationData
  377. } else {
  378. resp.RollingCorrelationData = []interface{}{nil, nil}
  379. }
  380. }
  381. br.Ret = 200
  382. br.Success = true
  383. br.Msg = "获取成功"
  384. br.Data = resp
  385. }
  386. // MultipleGraphPreviewCurve
  387. // @Title 批量添加图表的预览曲线图接口(只返回曲线图的数据)
  388. // @Description 批量添加图表的预览曲线图接口
  389. // @Param request body request.SaveMultipleGraphConfigReq true "type json string"
  390. // @Success Ret=200 返回图表id
  391. // @router /multiple_graph/preview_cure [post]
  392. func (this *ChartInfoController) MultipleGraphPreviewCurve() {
  393. br := new(models.BaseResponse).Init()
  394. defer func() {
  395. this.Data["json"] = br
  396. this.ServeJSON()
  397. }()
  398. sysUser := this.SysUser
  399. if sysUser == nil {
  400. br.Msg = "请登录"
  401. br.ErrMsg = "请登录,SysUser Is Empty"
  402. br.Ret = 408
  403. return
  404. }
  405. var req request.SaveMultipleGraphConfigReq
  406. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  407. if err != nil {
  408. br.Msg = "参数解析异常!"
  409. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  410. return
  411. }
  412. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  413. if err != nil {
  414. br.Msg = "获取失败"
  415. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  416. return
  417. }
  418. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  419. for _, v := range mappingList {
  420. if v.EdbInfoId == req.EdbInfoIdA {
  421. edbInfoMappingA = v
  422. }
  423. if v.EdbInfoId == req.EdbInfoIdB {
  424. edbInfoMappingB = v
  425. }
  426. }
  427. if edbInfoMappingA == nil {
  428. br.Msg = "指标A不存在"
  429. br.IsSendEmail = false
  430. return
  431. }
  432. if edbInfoMappingB == nil {
  433. br.Msg = "指标B不存在"
  434. br.IsSendEmail = false
  435. return
  436. }
  437. resp := response.PreviewMultipleGraphConfigResp{}
  438. chartInfo := new(data_manage.ChartInfoView)
  439. chartInfo.Calendar = `公历`
  440. chartInfo.ChartName = edbInfoMappingA.EdbName + "与" + edbInfoMappingB.EdbName
  441. // 曲线图表信息
  442. {
  443. tmpChartInfo := *chartInfo
  444. curveConf := req.Curve
  445. startDate := curveConf.StartDate
  446. endDate := curveConf.EndDate
  447. switch curveConf.DateType {
  448. case 1:
  449. startDate = "2000-01-01"
  450. case 2:
  451. startDate = "2010-01-01"
  452. case 3:
  453. startDate = "2015-01-01"
  454. case 4:
  455. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  456. startDate = "2021-01-01"
  457. case 5:
  458. startDate = startDate + "-01"
  459. endDate = endDate + "-01"
  460. case 6:
  461. startDate = startDate + "-01"
  462. case 7:
  463. startDate = "2018-01-01"
  464. case 8:
  465. startDate = "2019-01-01"
  466. case 9:
  467. startDate = "2020-01-01"
  468. case 11:
  469. startDate = "2022-01-01"
  470. case 12:
  471. startDate = "2023-01-01"
  472. case 13:
  473. startDate = "2024-01-01"
  474. }
  475. edbInfoType := 0
  476. if curveConf.EdbInfoType {
  477. edbInfoType = 1
  478. }
  479. for _, v := range mappingList {
  480. v.EdbInfoType = 1
  481. v.IsOrder = false
  482. if v.EdbInfoId == req.EdbInfoIdB {
  483. v.LeadValue = curveConf.LeadValue
  484. v.LeadUnit = curveConf.LeadUnit
  485. v.EdbInfoType = edbInfoType
  486. v.IsOrder = curveConf.IsOrder
  487. }
  488. }
  489. // 获取图表中的指标数据
  490. edbList, _, _, _, err, errMsg := data.GetChartEdbData(tmpChartInfo.ChartInfoId, tmpChartInfo.ChartType, tmpChartInfo.Calendar, startDate, endDate, mappingList, tmpChartInfo.ExtraConfig, tmpChartInfo.SeasonExtraConfig)
  491. if err != nil {
  492. br.Msg = "获取失败"
  493. if errMsg != `` {
  494. br.Msg = errMsg
  495. }
  496. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  497. return
  498. }
  499. warnEdbList := make([]string, 0)
  500. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  501. for _, v := range edbList {
  502. if v.IsNullData {
  503. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  504. }
  505. chartEdbInfoMapping := *v
  506. chartEdbInfoMapping.IsOrder = false
  507. chartEdbInfoMapping.EdbInfoType = 1
  508. if v.EdbInfoId == req.EdbInfoIdA {
  509. chartEdbInfoMapping.IsAxis = 1
  510. chartEdbInfoMapping.MinData = curveConf.LeftMin
  511. chartEdbInfoMapping.MaxData = curveConf.LeftMax
  512. chartEdbInfoMapping.ChartColor = `#00F`
  513. //chartEdbInfoMapping.LeadValue = curveConf.LeadValue
  514. //chartEdbInfoMapping.LeadUnit = curveConf.LeadUnit
  515. chartEdbInfoMapping.ChartWidth = 3
  516. } else {
  517. chartEdbInfoMapping.IsAxis = 0
  518. chartEdbInfoMapping.MinData = curveConf.RightMin
  519. chartEdbInfoMapping.MaxData = curveConf.RightMax
  520. chartEdbInfoMapping.ChartColor = `#F00`
  521. chartEdbInfoMapping.LeadValue = curveConf.LeadValue
  522. chartEdbInfoMapping.LeadUnit = curveConf.LeadUnit
  523. chartEdbInfoMapping.ChartWidth = 1
  524. chartEdbInfoMapping.IsOrder = curveConf.IsOrder
  525. chartEdbInfoMapping.EdbInfoType = edbInfoType
  526. }
  527. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  528. }
  529. if len(warnEdbList) > 0 {
  530. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  531. }
  532. tmpChartInfo.Source = utils.CHART_SOURCE_DEFAULT
  533. tmpChartInfo.DateType = curveConf.DateType
  534. tmpChartInfo.StartDate = startDate
  535. tmpChartInfo.EndDate = endDate
  536. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  537. resp.CurveData = response.CurveResp{
  538. ChartInfo: tmpChartInfo,
  539. EdbInfoList: tmpEdbList,
  540. }
  541. }
  542. br.Ret = 200
  543. br.Success = true
  544. br.Msg = "获取成功"
  545. br.Data = resp
  546. }
  547. // MultipleGraphConfigSaveChart
  548. // @Title 保存图表接口
  549. // @Description 保存图表接口
  550. // @Param request body request.SaveMultipleGraphChartReq true "type json string"
  551. // @Success Ret=200 返回图表id
  552. // @router /multiple_graph/chart/save [post]
  553. func (this *ChartInfoController) MultipleGraphConfigSaveChart() {
  554. br := new(models.BaseResponse).Init()
  555. defer func() {
  556. this.Data["json"] = br
  557. this.ServeJSON()
  558. }()
  559. sysUser := this.SysUser
  560. if sysUser == nil {
  561. br.Msg = "请登录"
  562. br.ErrMsg = "请登录,SysUser Is Empty"
  563. br.Ret = 408
  564. return
  565. }
  566. var req request.SaveMultipleGraphChartReq
  567. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  568. if err != nil {
  569. br.Msg = "参数解析异常!"
  570. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  571. return
  572. }
  573. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  574. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  575. br.Msg = "系统处理中,请稍后重试!"
  576. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  577. return
  578. }
  579. defer func() {
  580. _ = utils.Rc.Delete(cacheKey)
  581. }()
  582. if req.MultipleGraphConfigId <= 0 {
  583. br.Msg = "配置未保存"
  584. br.IsSendEmail = false
  585. return
  586. }
  587. if req.IsSaveAs {
  588. CopyMultipleGraphConfigSaveChart(req, this, br, sysUser)
  589. return
  590. }
  591. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  592. if err != nil {
  593. br.Msg = "获取失败"
  594. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  595. return
  596. }
  597. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  598. for _, v := range mappingList {
  599. if v.EdbInfoId == req.EdbInfoIdA {
  600. edbInfoMappingA = v
  601. }
  602. if v.EdbInfoId == req.EdbInfoIdB {
  603. edbInfoMappingB = v
  604. }
  605. }
  606. if edbInfoMappingA == nil {
  607. br.Msg = "指标A不存在"
  608. br.IsSendEmail = false
  609. return
  610. }
  611. if edbInfoMappingB == nil {
  612. br.Msg = "指标B不存在"
  613. br.IsSendEmail = false
  614. return
  615. }
  616. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  617. if err != nil && err.Error() != utils.ErrNoRow() {
  618. br.Msg = `保存失败`
  619. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  620. return
  621. }
  622. err = nil
  623. var isAdd bool
  624. var chartInfoId int
  625. if multipleGraphConfigChartMapping == nil {
  626. isAdd = true
  627. } else {
  628. chartInfo, err := data_manage.GetChartInfoById(multipleGraphConfigChartMapping.ChartInfoId)
  629. if err != nil && err.Error() != utils.ErrNoRow() {
  630. br.Msg = `保存失败`
  631. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  632. return
  633. }
  634. // 说明图还在,没有被删除
  635. if chartInfo != nil {
  636. chartInfoId = multipleGraphConfigChartMapping.ChartInfoId
  637. req.ChartName = chartInfo.ChartName
  638. req.ClassifyId = chartInfo.ChartClassifyId
  639. }
  640. }
  641. var chartInfo *data_manage.ChartInfo
  642. var errMsg string
  643. isSendEmail := true
  644. switch req.Source {
  645. case 1: // 曲线图
  646. curveConf := req.Curve
  647. edbInfoType := 1 //1:标准指标,0:领先指标
  648. if !curveConf.EdbInfoType {
  649. edbInfoType = 0
  650. }
  651. chartSaveItemList := []*data_manage.ChartSaveItem{
  652. {
  653. EdbInfoId: edbInfoMappingA.EdbInfoId,
  654. MaxData: curveConf.LeftMax,
  655. MinData: curveConf.LeftMin,
  656. IsOrder: false,
  657. IsAxis: 1,
  658. EdbInfoType: 1,
  659. //LeadValue: curveConf.LeadValue,
  660. //LeadUnit: curveConf.LeadUnit,
  661. ChartStyle: "spline",
  662. ChartColor: `#00F`,
  663. PredictChartColor: "",
  664. ChartWidth: 3,
  665. Source: utils.CHART_SOURCE_DEFAULT,
  666. }, {
  667. EdbInfoId: edbInfoMappingB.EdbInfoId,
  668. MaxData: curveConf.RightMax,
  669. MinData: curveConf.RightMin,
  670. IsOrder: curveConf.IsOrder,
  671. IsAxis: 0,
  672. EdbInfoType: edbInfoType,
  673. LeadValue: curveConf.LeadValue,
  674. LeadUnit: curveConf.LeadUnit,
  675. ChartStyle: "spline",
  676. ChartColor: `#F00`,
  677. PredictChartColor: "",
  678. ChartWidth: 1,
  679. Source: utils.CHART_SOURCE_DEFAULT,
  680. },
  681. }
  682. if chartInfoId <= 0 {
  683. addChartReq := data_manage.AddChartInfoReq{
  684. ChartEdbInfoList: chartSaveItemList,
  685. ChartClassifyId: req.ClassifyId,
  686. ChartName: req.ChartName,
  687. ChartType: utils.CHART_TYPE_CURVE,
  688. DateType: curveConf.DateType,
  689. StartDate: curveConf.StartDate,
  690. EndDate: curveConf.EndDate,
  691. StartYear: curveConf.StartYear,
  692. Calendar: "公历",
  693. LeftMin: fmt.Sprint(curveConf.LeftMin),
  694. LeftMax: fmt.Sprint(curveConf.LeftMax),
  695. RightMin: fmt.Sprint(curveConf.RightMin),
  696. RightMax: fmt.Sprint(curveConf.RightMax),
  697. Right2Min: fmt.Sprint(curveConf.Right2Min),
  698. Right2Max: fmt.Sprint(curveConf.Right2Max),
  699. }
  700. chartInfo, err, errMsg, isSendEmail = data.AddChartInfo(addChartReq, sysUser.AdminId, sysUser.RealName, this.Lang)
  701. } else {
  702. editChartReq := data_manage.EditChartInfoReq{
  703. ChartInfoId: multipleGraphConfigChartMapping.ChartInfoId,
  704. ChartName: req.ChartName,
  705. ChartClassifyId: req.ClassifyId,
  706. ChartEdbInfoList: chartSaveItemList,
  707. ChartType: utils.CHART_TYPE_CURVE,
  708. DateType: curveConf.DateType,
  709. StartDate: curveConf.StartDate,
  710. EndDate: curveConf.EndDate,
  711. StartYear: curveConf.StartYear,
  712. Calendar: "公历",
  713. LeftMin: fmt.Sprint(curveConf.LeftMin),
  714. LeftMax: fmt.Sprint(curveConf.LeftMax),
  715. RightMin: fmt.Sprint(curveConf.RightMin),
  716. RightMax: fmt.Sprint(curveConf.RightMax),
  717. Right2Min: fmt.Sprint(curveConf.Right2Min),
  718. Right2Max: fmt.Sprint(curveConf.Right2Max),
  719. }
  720. chartInfo, err, errMsg, isSendEmail = data.EditChartInfo(editChartReq, sysUser, this.Lang)
  721. if err != nil {
  722. br.Msg = "保存失败"
  723. if errMsg != `` {
  724. br.Msg = errMsg
  725. }
  726. br.ErrMsg = err.Error()
  727. br.IsSendEmail = isSendEmail
  728. return
  729. }
  730. }
  731. case 2, 3, 4: // 相关性图(滚动相关性)
  732. correlationConf := req.Correlation
  733. // 配置了数据才有返回
  734. if correlationConf.LeadValue > 0 {
  735. leadValue := correlationConf.LeadValue
  736. leadUnit := correlationConf.LeadUnit
  737. baseCalculateValue := correlationConf.CalculateValue
  738. baseCalculateUnit := correlationConf.CalculateUnit
  739. calculateValue := baseCalculateValue
  740. calculateUnit := baseCalculateUnit
  741. chartSource := utils.CHART_SOURCE_CORRELATION // 默认是相关性图
  742. moveUnitDays, ok := utils.FrequencyDaysMap[calculateUnit]
  743. if !ok {
  744. br.Msg = "错误的分析周期"
  745. br.IsSendEmail = false
  746. return
  747. }
  748. startDate := time.Now().AddDate(0, 0, -correlationConf.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  749. endDate := time.Now().Format(utils.FormatDate)
  750. // 日期类型
  751. var dateType int
  752. if req.Source != 2 {
  753. chartSource = utils.CHART_SOURCE_ROLLING_CORRELATION
  754. var rollingCorrelation data_manage.RollingCorrelationConfig
  755. if req.Source == 3 {
  756. rollingCorrelation = req.RollingCorrelation[0]
  757. } else {
  758. rollingCorrelation = req.RollingCorrelation[1]
  759. }
  760. leadValue = rollingCorrelation.LeadValue
  761. leadUnit = rollingCorrelation.LeadUnit
  762. calculateValue = rollingCorrelation.CalculateValue
  763. calculateUnit = rollingCorrelation.CalculateUnit
  764. // 开始日期,结束日期
  765. dateType = req.Curve.DateType
  766. startDate, endDate = utils.GetDateByDateType(req.Curve.DateType, req.Curve.StartDate, req.Curve.EndDate)
  767. }
  768. // 图例, 图表来源
  769. var sourceFrom, extraConfig string
  770. if req.CorrelationExtraConfig != nil {
  771. b, e := json.Marshal(req.CorrelationExtraConfig)
  772. if e != nil {
  773. br.Msg = "保存失败"
  774. br.ErrMsg = fmt.Sprintf("相关性图表图例信息JSON格式化失败, Err: %v", e)
  775. return
  776. }
  777. extraConfig = string(b)
  778. }
  779. if req.SourcesFrom != nil {
  780. b, e := json.Marshal(req.SourcesFrom)
  781. if e != nil {
  782. br.Msg = "保存失败"
  783. br.ErrMsg = fmt.Sprintf("图表来源JSON格式化失败, Err: %v", e)
  784. return
  785. }
  786. sourceFrom = string(b)
  787. }
  788. correlationChartInfoReq := data_manage.CorrelationChartInfoReq{
  789. LeadValue: leadValue,
  790. LeadUnit: leadUnit,
  791. CalculateValue: calculateValue,
  792. CalculateUnit: calculateUnit,
  793. BaseCalculateValue: baseCalculateValue,
  794. BaseCalculateUnit: baseCalculateUnit,
  795. DateType: dateType,
  796. StartDate: startDate,
  797. EndDate: endDate,
  798. EdbInfoIdList: []data_manage.CorrelationChartInfoEdbItemReq{
  799. {
  800. EdbInfoId: edbInfoMappingA.EdbInfoId,
  801. Name: "",
  802. NameEn: "",
  803. }, {
  804. EdbInfoId: edbInfoMappingB.EdbInfoId,
  805. Name: "",
  806. NameEn: "",
  807. },
  808. },
  809. }
  810. if chartInfoId <= 0 {
  811. addChartReq := data_manage.AddChartInfoReq{
  812. ChartClassifyId: req.ClassifyId,
  813. ChartName: req.ChartName,
  814. ChartType: utils.CHART_TYPE_CURVE,
  815. Calendar: "公历",
  816. CorrelationChartInfo: correlationChartInfoReq,
  817. ExtraConfig: extraConfig,
  818. SourcesFrom: sourceFrom,
  819. }
  820. chartInfo, err, errMsg, isSendEmail = correlationServ.AddChartInfo(addChartReq, chartSource, sysUser, this.Lang)
  821. } else {
  822. editChartReq := data_manage.EditChartInfoReq{
  823. ChartInfoId: multipleGraphConfigChartMapping.ChartInfoId,
  824. ChartName: req.ChartName,
  825. ChartClassifyId: req.ClassifyId,
  826. ChartType: utils.CHART_TYPE_CURVE,
  827. Calendar: "公历",
  828. CorrelationChartInfo: correlationChartInfoReq,
  829. ExtraConfig: extraConfig,
  830. SourcesFrom: sourceFrom,
  831. }
  832. chartInfo, err, errMsg, isSendEmail = correlationServ.EditChartInfo(editChartReq, sysUser, this.Lang)
  833. if err != nil {
  834. br.Msg = "保存失败"
  835. if errMsg != `` {
  836. br.Msg = errMsg
  837. }
  838. br.ErrMsg = err.Error()
  839. br.IsSendEmail = isSendEmail
  840. return
  841. }
  842. }
  843. }
  844. default:
  845. br.Msg = "错误的来源"
  846. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  847. br.IsSendEmail = false
  848. return
  849. }
  850. if err != nil {
  851. br.Msg = "保存失败"
  852. if errMsg != `` {
  853. br.Msg = errMsg
  854. }
  855. br.ErrMsg = err.Error()
  856. br.IsSendEmail = isSendEmail
  857. return
  858. }
  859. // 如果图没有建立关联关系,那么就需要添加关系
  860. if isAdd {
  861. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  862. //Id: 0,
  863. MultipleGraphConfigId: req.MultipleGraphConfigId,
  864. ChartInfoId: chartInfo.ChartInfoId,
  865. Source: req.Source,
  866. ModifyTime: time.Now(),
  867. CreateTime: time.Now(),
  868. }
  869. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  870. if err != nil {
  871. br.Msg = "保存失败"
  872. br.ErrMsg = "保存配置与图表的关联关系失败,ERR:" + err.Error()
  873. return
  874. }
  875. } else if multipleGraphConfigChartMapping != nil {
  876. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  877. multipleGraphConfigChartMapping.ModifyTime = time.Now()
  878. err = multipleGraphConfigChartMapping.Update([]string{"ChartInfoId", "ModifyTime"})
  879. if err != nil {
  880. br.Msg = "保存失败"
  881. br.ErrMsg = "保存配置与图表的关联关系失败,ERR:" + err.Error()
  882. return
  883. }
  884. }
  885. //新增操作日志
  886. {
  887. chartLog := new(data_manage.ChartInfoLog)
  888. chartLog.ChartInfoId = chartInfo.ChartInfoId
  889. chartLog.ChartName = req.ChartName
  890. chartLog.ChartClassifyId = req.ClassifyId
  891. chartLog.SysUserId = sysUser.AdminId
  892. chartLog.SysUserRealName = sysUser.RealName
  893. chartLog.UniqueCode = chartInfo.UniqueCode
  894. chartLog.CreateTime = time.Now()
  895. chartLog.Content = string(this.Ctx.Input.RequestBody)
  896. chartLog.Status = "多图配置保存图表"
  897. chartLog.Method = this.Ctx.Input.URI()
  898. go data_manage.AddChartInfoLog(chartLog)
  899. }
  900. resp := new(data_manage.AddChartInfoResp)
  901. resp.ChartInfoId = chartInfo.ChartInfoId
  902. resp.UniqueCode = chartInfo.UniqueCode
  903. resp.ChartType = chartInfo.ChartType
  904. br.Ret = 200
  905. br.Success = true
  906. br.Msg = "保存成功"
  907. br.Data = resp
  908. br.IsAddLog = true
  909. }
  910. // CopyMultipleGraphConfigSaveChart 图表另存为
  911. func CopyMultipleGraphConfigSaveChart(req request.SaveMultipleGraphChartReq, this *ChartInfoController, br *models.BaseResponse, sysUser *system.Admin) {
  912. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  913. if err != nil {
  914. br.Msg = "获取失败"
  915. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  916. return
  917. }
  918. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  919. for _, v := range mappingList {
  920. if v.EdbInfoId == req.EdbInfoIdA {
  921. edbInfoMappingA = v
  922. }
  923. if v.EdbInfoId == req.EdbInfoIdB {
  924. edbInfoMappingB = v
  925. }
  926. }
  927. if edbInfoMappingA == nil {
  928. br.Msg = "指标A不存在"
  929. br.IsSendEmail = false
  930. return
  931. }
  932. if edbInfoMappingB == nil {
  933. br.Msg = "指标B不存在"
  934. br.IsSendEmail = false
  935. return
  936. }
  937. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  938. if err != nil && err.Error() != utils.ErrNoRow() {
  939. br.Msg = `保存失败`
  940. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  941. return
  942. }
  943. // 原图
  944. oldChartInfo, err := data_manage.GetChartInfoById(multipleGraphConfigChartMapping.ChartInfoId)
  945. if err != nil && err.Error() != utils.ErrNoRow() {
  946. br.Msg = `保存失败`
  947. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  948. return
  949. }
  950. // 说明图还在,没有被删除
  951. if oldChartInfo == nil {
  952. br.Msg = "原图不存在"
  953. br.IsSendEmail = false
  954. }
  955. var chartInfo *data_manage.ChartInfo
  956. var errMsg string
  957. isSendEmail := true
  958. switch req.Source {
  959. case 1: // 曲线图
  960. curveConf := req.Curve
  961. edbInfoType := 1 //1:标准指标,0:领先指标
  962. if !curveConf.EdbInfoType {
  963. edbInfoType = 0
  964. }
  965. chartSaveItemList := []*data_manage.ChartSaveItem{
  966. {
  967. EdbInfoId: edbInfoMappingA.EdbInfoId,
  968. MaxData: curveConf.LeftMax,
  969. MinData: curveConf.LeftMin,
  970. IsOrder: false,
  971. IsAxis: 1,
  972. EdbInfoType: 1,
  973. //LeadValue: curveConf.LeadValue,
  974. //LeadUnit: curveConf.LeadUnit,
  975. ChartStyle: "spline",
  976. ChartColor: `#00F`,
  977. PredictChartColor: "",
  978. ChartWidth: 3,
  979. Source: utils.CHART_SOURCE_DEFAULT,
  980. }, {
  981. EdbInfoId: edbInfoMappingB.EdbInfoId,
  982. MaxData: curveConf.RightMax,
  983. MinData: curveConf.RightMin,
  984. IsOrder: curveConf.IsOrder,
  985. IsAxis: 0,
  986. EdbInfoType: edbInfoType,
  987. LeadValue: curveConf.LeadValue,
  988. LeadUnit: curveConf.LeadUnit,
  989. ChartStyle: "spline",
  990. ChartColor: `#F00`,
  991. PredictChartColor: "",
  992. ChartWidth: 1,
  993. Source: utils.CHART_SOURCE_DEFAULT,
  994. },
  995. }
  996. addChartReq := data_manage.AddChartInfoReq{
  997. ChartEdbInfoList: chartSaveItemList,
  998. ChartClassifyId: req.ClassifyId,
  999. ChartName: req.ChartName,
  1000. ChartType: utils.CHART_TYPE_CURVE,
  1001. DateType: curveConf.DateType,
  1002. StartDate: curveConf.StartDate,
  1003. EndDate: curveConf.EndDate,
  1004. StartYear: curveConf.StartYear,
  1005. Calendar: "公历",
  1006. LeftMin: fmt.Sprint(curveConf.LeftMin),
  1007. LeftMax: fmt.Sprint(curveConf.LeftMax),
  1008. RightMin: fmt.Sprint(curveConf.RightMin),
  1009. RightMax: fmt.Sprint(curveConf.RightMax),
  1010. }
  1011. chartInfo, err, errMsg, isSendEmail = data.AddChartInfo(addChartReq, sysUser.AdminId, sysUser.RealName, this.Lang)
  1012. case 2: // 相关性图
  1013. correlationConf := req.Correlation
  1014. // 配置了数据才有返回
  1015. if correlationConf.LeadValue > 0 {
  1016. leadValue := correlationConf.LeadValue
  1017. leadUnit := correlationConf.LeadUnit
  1018. calculateValue := correlationConf.CalculateValue
  1019. calculateUnit := correlationConf.CalculateUnit
  1020. moveUnitDays, ok := utils.FrequencyDaysMap[calculateUnit]
  1021. if !ok {
  1022. br.Msg = "错误的分析周期"
  1023. br.IsSendEmail = false
  1024. return
  1025. }
  1026. startDate := time.Now().AddDate(0, 0, -correlationConf.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  1027. endDate := time.Now().Format(utils.FormatDate)
  1028. correlationChartInfoReq := data_manage.CorrelationChartInfoReq{
  1029. LeadValue: leadValue,
  1030. LeadUnit: leadUnit,
  1031. CalculateValue: calculateValue,
  1032. CalculateUnit: calculateUnit,
  1033. StartDate: startDate,
  1034. EndDate: endDate,
  1035. EdbInfoIdList: []data_manage.CorrelationChartInfoEdbItemReq{
  1036. {
  1037. EdbInfoId: edbInfoMappingA.EdbInfoId,
  1038. Name: "",
  1039. NameEn: "",
  1040. }, {
  1041. EdbInfoId: edbInfoMappingB.EdbInfoId,
  1042. Name: "",
  1043. NameEn: "",
  1044. },
  1045. },
  1046. }
  1047. chartInfo, err, errMsg, isSendEmail = correlationServ.CopyChartInfo(multipleGraphConfigChartMapping.MultipleGraphConfigId, req.ClassifyId, req.ChartName, correlationChartInfoReq, oldChartInfo, sysUser, this.Lang)
  1048. }
  1049. default:
  1050. br.Msg = "错误的来源"
  1051. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  1052. br.IsSendEmail = false
  1053. return
  1054. }
  1055. if err != nil {
  1056. br.Msg = "保存失败"
  1057. if errMsg != `` {
  1058. br.Msg = errMsg
  1059. }
  1060. br.ErrMsg = err.Error()
  1061. br.IsSendEmail = isSendEmail
  1062. return
  1063. }
  1064. //新增操作日志
  1065. {
  1066. chartLog := new(data_manage.ChartInfoLog)
  1067. chartLog.ChartInfoId = chartInfo.ChartInfoId
  1068. chartLog.ChartName = req.ChartName
  1069. chartLog.ChartClassifyId = req.ClassifyId
  1070. chartLog.SysUserId = sysUser.AdminId
  1071. chartLog.SysUserRealName = sysUser.RealName
  1072. chartLog.UniqueCode = chartInfo.UniqueCode
  1073. chartLog.CreateTime = time.Now()
  1074. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1075. chartLog.Status = "多图配置另存为图表"
  1076. chartLog.Method = this.Ctx.Input.URI()
  1077. go data_manage.AddChartInfoLog(chartLog)
  1078. }
  1079. resp := new(data_manage.AddChartInfoResp)
  1080. resp.ChartInfoId = chartInfo.ChartInfoId
  1081. resp.UniqueCode = chartInfo.UniqueCode
  1082. resp.ChartType = chartInfo.ChartType
  1083. br.Ret = 200
  1084. br.Success = true
  1085. br.Msg = "保存成功"
  1086. br.Data = resp
  1087. br.IsAddLog = true
  1088. }
  1089. // MultipleGraphConfigSaveEdb
  1090. // @Title 保存指标接口
  1091. // @Description 保存指标接口
  1092. // @Param request body request.SaveMultipleGraphEdbReq true "type json string"
  1093. // @Success Ret=200 返回指标id
  1094. // @router /multiple_graph/edb/save [post]
  1095. func (this *ChartInfoController) MultipleGraphConfigSaveEdb() {
  1096. br := new(models.BaseResponse).Init()
  1097. defer func() {
  1098. this.Data["json"] = br
  1099. this.ServeJSON()
  1100. }()
  1101. sysUser := this.SysUser
  1102. if sysUser == nil {
  1103. br.Msg = "请登录"
  1104. br.ErrMsg = "请登录,SysUser Is Empty"
  1105. br.Ret = 408
  1106. return
  1107. }
  1108. var req request.SaveMultipleGraphEdbReq
  1109. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1110. if err != nil {
  1111. br.Msg = "参数解析异常!"
  1112. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1113. return
  1114. }
  1115. cacheKey := "CACHE_CHART_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1116. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1117. br.Msg = "系统处理中,请稍后重试!"
  1118. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1119. return
  1120. }
  1121. defer func() {
  1122. _ = utils.Rc.Delete(cacheKey)
  1123. }()
  1124. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  1125. if err != nil {
  1126. br.Msg = "获取失败"
  1127. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1128. return
  1129. }
  1130. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  1131. for _, v := range mappingList {
  1132. if v.EdbInfoId == req.EdbInfoIdA {
  1133. edbInfoMappingA = v
  1134. }
  1135. if v.EdbInfoId == req.EdbInfoIdB {
  1136. edbInfoMappingB = v
  1137. }
  1138. }
  1139. if edbInfoMappingA == nil {
  1140. br.Msg = "指标A不存在"
  1141. br.IsSendEmail = false
  1142. return
  1143. }
  1144. if edbInfoMappingB == nil {
  1145. br.Msg = "指标B不存在"
  1146. br.IsSendEmail = false
  1147. return
  1148. }
  1149. correlationConf := req.Correlation
  1150. // 配置了数据才有返回
  1151. if correlationConf.LeadValue <= 0 {
  1152. br.Msg = "相关性计算窗口必须大于0"
  1153. br.IsSendEmail = false
  1154. return
  1155. }
  1156. // 滚动相关性配置
  1157. var rollingCorrelation data_manage.RollingCorrelationConfig
  1158. switch req.Source {
  1159. case 3:
  1160. rollingCorrelation = req.RollingCorrelation[0]
  1161. case 4:
  1162. rollingCorrelation = req.RollingCorrelation[1]
  1163. default:
  1164. br.Msg = "错误的来源"
  1165. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  1166. br.IsSendEmail = false
  1167. return
  1168. }
  1169. // 配置了数据才有返回
  1170. if rollingCorrelation.CalculateValue <= 0 {
  1171. br.Msg = "滚动相关性计算窗口必须大于0"
  1172. br.IsSendEmail = false
  1173. return
  1174. }
  1175. multipleGraphConfigEdbMapping, err := data_manage.GetMultipleGraphConfigEdbMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  1176. if err != nil && err.Error() != utils.ErrNoRow() {
  1177. br.Msg = `保存失败`
  1178. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  1179. return
  1180. }
  1181. err = nil
  1182. var isAdd bool
  1183. var edbInfoId int
  1184. if multipleGraphConfigEdbMapping == nil {
  1185. isAdd = true
  1186. } else {
  1187. edbInfo, err := data_manage.GetEdbInfoById(multipleGraphConfigEdbMapping.EdbInfoId)
  1188. if err != nil && err.Error() != utils.ErrNoRow() {
  1189. br.Msg = `保存失败`
  1190. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  1191. return
  1192. }
  1193. // 说明指标还在,没有被删除
  1194. if edbInfo != nil && !req.IsSaveAs {
  1195. edbInfoId = multipleGraphConfigEdbMapping.EdbInfoId
  1196. req.EdbName = edbInfo.EdbName
  1197. req.ClassifyId = edbInfo.ClassifyId
  1198. req.Frequency = edbInfo.Frequency
  1199. req.Unit = edbInfo.Unit
  1200. }
  1201. }
  1202. // 关联指标
  1203. edbInfoIdArr := []data_manage.EdbInfoFromTag{
  1204. {
  1205. EdbInfoId: edbInfoMappingA.EdbInfoId,
  1206. FromTag: "A",
  1207. MoveValue: 0,
  1208. }, {
  1209. EdbInfoId: edbInfoMappingB.EdbInfoId,
  1210. FromTag: "B",
  1211. MoveValue: 0,
  1212. },
  1213. }
  1214. calculateFormula := request.EdbCalculateFormula{
  1215. BaseCalculateValue: correlationConf.CalculateValue,
  1216. BaseCalculateUnit: correlationConf.CalculateUnit,
  1217. LeadValue: rollingCorrelation.LeadValue,
  1218. LeadUnit: rollingCorrelation.LeadUnit,
  1219. CalculateValue: rollingCorrelation.CalculateValue,
  1220. CalculateUnit: rollingCorrelation.CalculateUnit,
  1221. }
  1222. calculateFormulaByte, err := json.Marshal(calculateFormula)
  1223. if err != nil {
  1224. br.Msg = "参数解析异常!"
  1225. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1226. return
  1227. }
  1228. var respItem *data.AddPredictEdbDataResponse
  1229. if edbInfoId <= 0 || req.IsSaveAs {
  1230. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1231. AdminId: sysUser.AdminId,
  1232. AdminName: sysUser.RealName,
  1233. EdbName: req.EdbName,
  1234. Frequency: req.Frequency,
  1235. Unit: req.Unit,
  1236. ClassifyId: req.ClassifyId,
  1237. Formula: string(calculateFormulaByte),
  1238. Source: utils.DATA_SOURCE_CALCULATE_CORRELATION,
  1239. EdbInfoIdArr: edbInfoIdArr,
  1240. Calendar: `公历`,
  1241. }
  1242. // 调用指标库去添加
  1243. reqJson, err := json.Marshal(req2)
  1244. if err != nil {
  1245. br.Msg = "参数解析异常!"
  1246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1247. return
  1248. }
  1249. respItem, err = data.BatchSaveEdbCalculateData(string(reqJson), this.Lang)
  1250. if err != nil {
  1251. br.Msg = "新增失败"
  1252. br.ErrMsg = "新增失败,Err:" + err.Error()
  1253. return
  1254. }
  1255. } else {
  1256. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  1257. EdbInfoId: edbInfoId,
  1258. EdbName: req.EdbName,
  1259. Frequency: req.Frequency,
  1260. Unit: req.Unit,
  1261. ClassifyId: req.ClassifyId,
  1262. Formula: string(calculateFormulaByte),
  1263. Source: utils.DATA_SOURCE_CALCULATE_CORRELATION,
  1264. EdbInfoIdArr: edbInfoIdArr,
  1265. Calendar: `公历`,
  1266. }
  1267. // 调用指标库去更新
  1268. reqJson, err := json.Marshal(req2)
  1269. if err != nil {
  1270. br.Msg = "参数解析异常!"
  1271. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1272. return
  1273. }
  1274. respItem, err = data.BatchEditEdbCalculateData(string(reqJson), this.Lang)
  1275. if err != nil {
  1276. br.Msg = "编辑失败"
  1277. br.ErrMsg = "编辑失败,Err:" + err.Error()
  1278. return
  1279. }
  1280. }
  1281. if respItem == nil {
  1282. br.Msg = "保存失败"
  1283. br.ErrMsg = "保存失败"
  1284. return
  1285. }
  1286. if respItem.Ret != 200 {
  1287. br.Msg = respItem.Msg
  1288. br.ErrMsg = respItem.ErrMsg
  1289. return
  1290. }
  1291. resp := respItem.Data
  1292. //添加es
  1293. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  1294. // 如果不是另存为的话,那么需要建立配置与指标的关系
  1295. if !req.IsSaveAs {
  1296. // 如果指标没有建立关联关系,那么就需要添加关系
  1297. if isAdd {
  1298. multipleGraphConfigEdbMapping = &data_manage.MultipleGraphConfigEdbMapping{
  1299. //Id: 0,
  1300. MultipleGraphConfigId: req.MultipleGraphConfigId,
  1301. EdbInfoId: resp.EdbInfoId,
  1302. Source: req.Source,
  1303. ModifyTime: time.Now(),
  1304. CreateTime: time.Now(),
  1305. }
  1306. err = data_manage.AddMultipleGraphConfigEdbMapping(multipleGraphConfigEdbMapping)
  1307. if err != nil {
  1308. br.Msg = "保存失败"
  1309. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1310. return
  1311. }
  1312. } else if multipleGraphConfigEdbMapping != nil {
  1313. multipleGraphConfigEdbMapping.EdbInfoId = resp.EdbInfoId
  1314. multipleGraphConfigEdbMapping.ModifyTime = time.Now()
  1315. err = multipleGraphConfigEdbMapping.Update([]string{"EdbInfoId", "ModifyTime"})
  1316. if err != nil {
  1317. br.Msg = "保存失败"
  1318. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1319. return
  1320. }
  1321. }
  1322. }
  1323. //新增操作日志
  1324. //{
  1325. // edbLog := new(data_manage.EdbInfoLog)
  1326. // edbLog.EdbInfoId = resp.EdbInfoId
  1327. // edbLog.SourceName = edbInfo.SourceName
  1328. // edbLog.Source = edbInfo.Source
  1329. // edbLog.EdbCode = edbInfo.EdbCode
  1330. // edbLog.EdbName = req2.EdbName
  1331. // edbLog.ClassifyId = req2.ClassifyId
  1332. // edbLog.SysUserId = sysUser.AdminId
  1333. // edbLog.SysUserRealName = sysUser.RealName
  1334. // edbLog.CreateTime = time.Now()
  1335. // edbLog.Content = string(this.Ctx.Input.RequestBody)
  1336. // edbLog.Status = "多图配置保存指标"
  1337. // edbLog.Method = this.Ctx.Input.URI()
  1338. // go data_manage.AddEdbInfoLog(edbLog)
  1339. //}
  1340. br.Ret = 200
  1341. br.Success = true
  1342. br.Msg = "保存成功"
  1343. br.Data = resp
  1344. br.IsAddLog = true
  1345. }
  1346. // GetMultipleGraphConfig
  1347. // @Title 保存配置接口
  1348. // @Description 保存配置接口
  1349. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  1350. // @Success Ret=200 返回图表id
  1351. // @router /multiple_graph/config/detail [get]
  1352. func (this *ChartInfoController) GetMultipleGraphConfig() {
  1353. br := new(models.BaseResponse).Init()
  1354. defer func() {
  1355. this.Data["json"] = br
  1356. this.ServeJSON()
  1357. }()
  1358. sysUser := this.SysUser
  1359. if sysUser == nil {
  1360. br.Msg = "请登录"
  1361. br.ErrMsg = "请登录,SysUser Is Empty"
  1362. br.Ret = 408
  1363. return
  1364. }
  1365. uniqueCode := this.GetString("UniqueCode")
  1366. if uniqueCode == "" {
  1367. br.Msg = "参数错误"
  1368. br.ErrMsg = "参数错误,uniqueCode is empty"
  1369. return
  1370. }
  1371. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  1372. if err != nil {
  1373. br.Msg = "获取失败"
  1374. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1375. return
  1376. }
  1377. if chartInfo == nil {
  1378. br.Msg = "获取失败"
  1379. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1380. return
  1381. }
  1382. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
  1383. if err != nil {
  1384. br.Msg = "获取失败"
  1385. br.ErrMsg = "获取滚动相关性图表的配置信息失败,Err:" + err.Error()
  1386. return
  1387. }
  1388. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1389. if err != nil {
  1390. br.Msg = `获取失败`
  1391. br.ErrMsg = `获取配置失败,ERR:` + err.Error()
  1392. return
  1393. }
  1394. // 生成的图表
  1395. multipleGraphConfigChartMappingList := make([]response.MultipleGraphConfigChartMapping, 0)
  1396. {
  1397. chartMappingList, err := data_manage.GetMultipleGraphConfigChartMappingListById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1398. if err != nil {
  1399. br.Msg = `获取失败`
  1400. br.ErrMsg = `获取关联图表配置失败,ERR:` + err.Error()
  1401. return
  1402. }
  1403. chartInfoIdList := make([]int, 0)
  1404. chartMappingMap := make(map[int]*data_manage.MultipleGraphConfigChartMapping)
  1405. for _, v := range chartMappingList {
  1406. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  1407. chartMappingMap[v.ChartInfoId] = v
  1408. }
  1409. chartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  1410. if err != nil {
  1411. br.Msg = `获取失败`
  1412. br.ErrMsg = `获取关联图表列表失败,ERR:` + err.Error()
  1413. return
  1414. }
  1415. for _, v := range chartList {
  1416. var mapping response.MultipleGraphConfigChartMapping
  1417. if tmpInfo, ok := chartMappingMap[v.ChartInfoId]; ok {
  1418. mapping.ChartInfoId = tmpInfo.ChartInfoId
  1419. mapping.Source = tmpInfo.Source
  1420. mapping.MultipleLocationSource = tmpInfo.Source
  1421. if v.ChartInfoId == chartInfo.ChartInfoId {
  1422. mapping.CorrelationExtraConfig = chartInfo.ExtraConfig
  1423. mapping.SourcesFrom = chartInfo.SourcesFrom
  1424. }
  1425. multipleGraphConfigChartMappingList = append(multipleGraphConfigChartMappingList, mapping)
  1426. }
  1427. }
  1428. }
  1429. // 生成的指标
  1430. multipleGraphConfigEdbMappingList := make([]response.MultipleGraphConfigEdbMapping, 0)
  1431. {
  1432. edbMappingList, err := data_manage.GetMultipleGraphConfigEdbMappingListById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1433. if err != nil {
  1434. br.Msg = `获取失败`
  1435. br.ErrMsg = `获取关联指标配置失败,ERR:` + err.Error()
  1436. return
  1437. }
  1438. edbInfoIdList := make([]int, 0)
  1439. edbMappingMap := make(map[int]*data_manage.MultipleGraphConfigEdbMapping)
  1440. for _, v := range edbMappingList {
  1441. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  1442. edbMappingMap[v.EdbInfoId] = v
  1443. }
  1444. edbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1445. if err != nil {
  1446. br.Msg = `获取失败`
  1447. br.ErrMsg = `获取关联图表列表失败,ERR:` + err.Error()
  1448. return
  1449. }
  1450. for _, v := range edbList {
  1451. if tmpInfo, ok := edbMappingMap[v.EdbInfoId]; ok {
  1452. multipleGraphConfigEdbMappingList = append(multipleGraphConfigEdbMappingList, response.MultipleGraphConfigEdbMapping{
  1453. EdbInfoId: tmpInfo.EdbInfoId,
  1454. Source: tmpInfo.Source,
  1455. MultipleLocationSource: tmpInfo.Source,
  1456. })
  1457. }
  1458. }
  1459. }
  1460. // 关联指标
  1461. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{multipleGraphConfig.EdbInfoIdA, multipleGraphConfig.EdbInfoIdB})
  1462. if err != nil {
  1463. br.Msg = "获取失败"
  1464. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1465. return
  1466. }
  1467. resp := response.MultipleGraphConfigDetailResp{
  1468. EdbInfoList: mappingList,
  1469. MultipleGraphConfig: *multipleGraphConfig,
  1470. ChartMappingList: multipleGraphConfigChartMappingList,
  1471. EdbMappingList: multipleGraphConfigEdbMappingList,
  1472. }
  1473. br.Ret = 200
  1474. br.Success = true
  1475. br.Msg = "保存成功"
  1476. br.Data = resp
  1477. br.IsAddLog = true
  1478. }
  1479. // func init() {
  1480. // //correlationChart := new(data_manage.ChartInfoCorrelation)
  1481. //
  1482. // condition := ` AND source = 3 `
  1483. // list, err := data_manage.GetChartListByCondition(condition, []interface{}{}, 0, 1000)
  1484. // if err != nil {
  1485. // fmt.Println("err:", err)
  1486. // return
  1487. // }
  1488. // timeNowFormat := time.Now().Format(utils.FormatDate)
  1489. // for _, v := range list {
  1490. // item, tmpErr := data_manage.GetMultipleGraphConfigChartMappingByChartIdAndSource(v.ChartInfoId)
  1491. // if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1492. // fmt.Println("tmpErr:", tmpErr)
  1493. // continue
  1494. // }
  1495. // if item == nil {
  1496. // correlationChart := new(data_manage.ChartInfoCorrelation)
  1497. // e := correlationChart.GetItemById(v.ChartInfoId)
  1498. // if e != nil {
  1499. // fmt.Println("获取历史配置失败:", e)
  1500. // continue
  1501. // }
  1502. // day, _ := utils.GetDaysBetween2Date(utils.FormatDate, timeNowFormat, correlationChart.StartDate.Format(utils.FormatDate))
  1503. // req := request.SaveMultipleGraphConfigReq{
  1504. // MultipleGraphConfigId: 0,
  1505. // EdbInfoIdA: correlationChart.EdbInfoIdFirst,
  1506. // EdbInfoIdB: correlationChart.EdbInfoIdSecond,
  1507. // Curve: data_manage.CurveConfig{},
  1508. // Correlation: data_manage.CorrelationConfig{
  1509. // LeadValue: correlationChart.LeadValue,
  1510. // LeadUnit: correlationChart.LeadUnit,
  1511. // CalculateValue: day,
  1512. // CalculateUnit: "天",
  1513. // },
  1514. // RollingCorrelation: []data_manage.RollingCorrelationConfig{
  1515. // {}, {},
  1516. // },
  1517. // }
  1518. // curveStrByte, _ := json.Marshal(req.Curve)
  1519. // correlationStrByte, _ := json.Marshal(req.Correlation)
  1520. // rollingCorrelationStrByte, _ := json.Marshal(req.RollingCorrelation)
  1521. //
  1522. // multipleGraphConfig := &data_manage.MultipleGraphConfig{
  1523. // //MultipleGraphConfigId: 0,
  1524. // EdbInfoIdA: req.EdbInfoIdA,
  1525. // EdbInfoIdB: req.EdbInfoIdB,
  1526. // Curve: string(curveStrByte),
  1527. // Correlation: string(correlationStrByte),
  1528. // RollingCorrelation: string(rollingCorrelationStrByte),
  1529. // SysUserId: v.SysUserId,
  1530. // SysUserRealName: v.SysUserRealName,
  1531. // ModifyTime: v.ModifyTime,
  1532. // CreateTime: v.CreateTime,
  1533. // }
  1534. // err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1535. //
  1536. // if err != nil {
  1537. // fmt.Println(v.ChartInfoId, "创建配置失败:", err)
  1538. // continue
  1539. // }
  1540. //
  1541. // multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  1542. // //Id: 0,
  1543. // MultipleGraphConfigId: multipleGraphConfig.MultipleGraphConfigId,
  1544. // ChartInfoId: v.ChartInfoId,
  1545. // Source: 2,
  1546. // ModifyTime: time.Now(),
  1547. // CreateTime: time.Now(),
  1548. // }
  1549. // err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1550. // if err != nil {
  1551. // fmt.Println(v.ChartInfoId, "保存配置与图表的关联关系失败:", err)
  1552. // }
  1553. //
  1554. // correlationChart.CalculateUnit = "天"
  1555. // correlationChart.CalculateValue = day
  1556. // err = correlationChart.Update([]string{"CalculateUnit", "CalculateValue"})
  1557. // if err != nil {
  1558. // fmt.Println(v.ChartInfoId, "修复历史数据失败:", err)
  1559. // }
  1560. // }
  1561. // }
  1562. // }
  1563. // 修复相关性图表与滚动相关性图表的强关联关系
  1564. //func init() {
  1565. // condition := ` AND source = ? `
  1566. // pars := []interface{}{utils.CHART_SOURCE_ROLLING_CORRELATION}
  1567. // list, err := data_manage.GetChartListByCondition(condition, pars, 0, 1000)
  1568. // if err != nil {
  1569. // fmt.Println("err:", err)
  1570. // return
  1571. // }
  1572. // for _, chartInfo := range list {
  1573. // multipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
  1574. // if e != nil {
  1575. // fmt.Println(chartInfo.ChartInfoId, ";获取滚动相关性图表的配置信息失败, Err:"+e.Error())
  1576. // continue
  1577. // }
  1578. // baseChartMultipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(multipleGraphConfigChartMapping.MultipleGraphConfigId, 2)
  1579. // if e != nil {
  1580. // fmt.Println(chartInfo.ChartInfoId, ";获取相关性图表的配置信息失败, Err:"+e.Error())
  1581. // continue
  1582. // }
  1583. // baseChartCorrelationChart := new(data_manage.ChartInfoCorrelation)
  1584. // if e := baseChartCorrelationChart.GetItemById(baseChartMultipleGraphConfigChartMapping.ChartInfoId); e != nil {
  1585. // fmt.Println(chartInfo.ChartInfoId, ";获取基础相关性图表信息失败, Err:"+e.Error())
  1586. // continue
  1587. // }
  1588. //
  1589. // // 自己的配置
  1590. // selfChartCorrelationChart := new(data_manage.ChartInfoCorrelation)
  1591. // if e := selfChartCorrelationChart.GetItemById(chartInfo.ChartInfoId); e != nil {
  1592. // fmt.Println(chartInfo.ChartInfoId, ";获取自己的相关性图表信息失败, Err:"+e.Error())
  1593. // continue
  1594. // }
  1595. //
  1596. // selfChartCorrelationChart.BaseCalculateUnit = baseChartCorrelationChart.CalculateUnit
  1597. // selfChartCorrelationChart.BaseCalculateValue = baseChartCorrelationChart.CalculateValue
  1598. // err = selfChartCorrelationChart.Update([]string{"BaseCalculateUnit", "BaseCalculateValue"})
  1599. // if err != nil {
  1600. // fmt.Println(chartInfo.ChartInfoId, ";更新相关性图表信息失败, Err:"+err.Error())
  1601. // continue
  1602. // }
  1603. // }
  1604. //
  1605. //}
  1606. // 修复相关性图表与滚动相关性图表的强关联关系
  1607. //func init() {
  1608. // {
  1609. // condition := ` AND source = ? `
  1610. // pars := []interface{}{utils.CHART_SOURCE_ROLLING_CORRELATION}
  1611. // list, err := data_manage.GetChartListByCondition(condition, pars, 0, 1000)
  1612. // if err != nil {
  1613. // fmt.Println("err:", err)
  1614. // return
  1615. // }
  1616. // for _, chartInfo := range list {
  1617. // multipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
  1618. // if e != nil {
  1619. // fmt.Println(chartInfo.ChartInfoId, ";获取滚动相关性图表的配置信息失败, Err:"+e.Error())
  1620. // continue
  1621. // }
  1622. // multipleGraphConfig, e := data_manage.GetMultipleGraphConfigById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1623. // if e != nil {
  1624. // fmt.Println(chartInfo.ChartInfoId, ";获取相关性图表的配置信息失败, Err:"+e.Error())
  1625. // continue
  1626. // }
  1627. // var curveConf data_manage.CurveConfig
  1628. // e = json.Unmarshal([]byte(multipleGraphConfig.Curve), &curveConf)
  1629. // if e != nil {
  1630. // fmt.Println(chartInfo.ChartInfoId, ";格式化曲线图数据失败, Err:"+e.Error())
  1631. // continue
  1632. // }
  1633. //
  1634. // // 自己的配置
  1635. // selfChartCorrelationChart := new(data_manage.ChartInfoCorrelation)
  1636. // if e := selfChartCorrelationChart.GetItemById(chartInfo.ChartInfoId); e != nil {
  1637. // fmt.Println(chartInfo.ChartInfoId, ";获取自己的相关性图表信息失败, Err:"+e.Error())
  1638. // continue
  1639. // }
  1640. //
  1641. // selfChartCorrelationChart.DateType = curveConf.DateType
  1642. //
  1643. // startDate, endDate := utils.GetDateByDateType(curveConf.DateType, curveConf.StartDate, curveConf.EndDate)
  1644. // if startDate != `` {
  1645. // startDateTime, e := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  1646. // if e != nil {
  1647. // fmt.Println(chartInfo.ChartInfoId, ";开始日期格式化失败,开始日期:", startDate, ", Err:"+e.Error())
  1648. // continue
  1649. // }
  1650. // selfChartCorrelationChart.StartDate = startDateTime
  1651. // }
  1652. // if endDate != `` {
  1653. // endDateTime, e := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1654. // if e != nil {
  1655. // fmt.Println(chartInfo.ChartInfoId, ";开始日期格式化失败,开始日期:", startDate, ", Err:"+e.Error())
  1656. // continue
  1657. // }
  1658. // selfChartCorrelationChart.EndDate = endDateTime
  1659. // }
  1660. // err = selfChartCorrelationChart.Update([]string{"DateType", "StartDate", "EndDate"})
  1661. // if err != nil {
  1662. // fmt.Println(chartInfo.ChartInfoId, ";更新相关性图表信息失败, Err:"+err.Error())
  1663. // continue
  1664. // }
  1665. // }
  1666. //
  1667. // fmt.Println("图表更新完成")
  1668. // }
  1669. //
  1670. // {
  1671. // // 更新指标
  1672. // condition := ` AND source = ? `
  1673. // pars := []interface{}{utils.DATA_SOURCE_CALCULATE_CORRELATION}
  1674. // list, err := data_manage.GetEdbInfoListV2ByCondition(condition, pars, 0, 10000)
  1675. // if err != nil {
  1676. // fmt.Println("err:", err)
  1677. // return
  1678. // }
  1679. // for _, edbInfo := range list {
  1680. // multipleGraphConfigEdbMappingInfo, e := data_manage.GetMultipleGraphConfigEdbMappingByEdbInfoId(edbInfo.EdbInfoId)
  1681. // if e != nil {
  1682. // fmt.Println(edbInfo.EdbInfoId, ";获取滚动相关性指标的配置信息失败, Err:"+e.Error())
  1683. // continue
  1684. // }
  1685. // multipleGraphConfig, e := data_manage.GetMultipleGraphConfigById(multipleGraphConfigEdbMappingInfo.MultipleGraphConfigId)
  1686. // if e != nil {
  1687. // fmt.Println(edbInfo.EdbInfoId, ";获取相关性图表的配置信息失败, Err:"+e.Error())
  1688. // continue
  1689. // }
  1690. //
  1691. // // 曲线图配置
  1692. // var curveConf data_manage.CurveConfig
  1693. // e = json.Unmarshal([]byte(multipleGraphConfig.Curve), &curveConf)
  1694. // if e != nil {
  1695. // fmt.Println(edbInfo.EdbInfoId, ";格式化曲线图数据失败, Err:"+e.Error())
  1696. // continue
  1697. // }
  1698. //
  1699. // var formulaConf request.EdbCalculateFormula
  1700. // e = json.Unmarshal([]byte(edbInfo.CalculateFormula), &formulaConf)
  1701. // if e != nil {
  1702. // fmt.Println(edbInfo.EdbInfoId, ";格式化指标计算配置数据失败, Err:"+e.Error())
  1703. // continue
  1704. // }
  1705. // formulaConf.DateType = curveConf.DateType
  1706. // formulaConf.StartDate = curveConf.StartDate
  1707. // formulaConf.EndDate = curveConf.EndDate
  1708. // calculateFormulaByte, err := json.Marshal(formulaConf)
  1709. // edbInfo.CalculateFormula = string(calculateFormulaByte)
  1710. // err = edbInfo.Update([]string{"CalculateFormula"})
  1711. // if err != nil {
  1712. // fmt.Println(edbInfo.EdbInfoId, ";更新指标信息失败, Err:"+err.Error())
  1713. // continue
  1714. // }
  1715. // }
  1716. //
  1717. // fmt.Println("指标更新完成")
  1718. // }
  1719. //}