multiple_graph_config.go 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/data_manage/request"
  7. "eta_gn/eta_api/models/data_manage/response"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/services/data"
  10. correlationServ "eta_gn/eta_api/services/data/correlation"
  11. "eta_gn/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 utils.IsErrNoRow(err) {
  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 utils.IsErrNoRow(err) {
  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. maxYear := 0
  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. maxYear = latestDateT.Year()
  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, maxYear)
  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, maxYear)
  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. }
  471. edbInfoType := 0
  472. if curveConf.EdbInfoType {
  473. edbInfoType = 1
  474. }
  475. for _, v := range mappingList {
  476. v.EdbInfoType = 1
  477. v.IsOrder = false
  478. if v.EdbInfoId == req.EdbInfoIdB {
  479. v.LeadValue = curveConf.LeadValue
  480. v.LeadUnit = curveConf.LeadUnit
  481. v.EdbInfoType = edbInfoType
  482. v.IsOrder = curveConf.IsOrder
  483. }
  484. }
  485. // 获取图表中的指标数据
  486. edbList, _, _, _, err, errMsg := data.GetChartEdbData(tmpChartInfo.ChartInfoId, tmpChartInfo.ChartType, tmpChartInfo.Calendar, startDate, endDate, mappingList, tmpChartInfo.ExtraConfig, tmpChartInfo.SeasonExtraConfig)
  487. if err != nil {
  488. br.Msg = "获取失败"
  489. if errMsg != `` {
  490. br.Msg = errMsg
  491. }
  492. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  493. return
  494. }
  495. warnEdbList := make([]string, 0)
  496. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  497. for _, v := range edbList {
  498. if v.IsNullData {
  499. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  500. }
  501. chartEdbInfoMapping := *v
  502. chartEdbInfoMapping.IsOrder = false
  503. chartEdbInfoMapping.EdbInfoType = 1
  504. if v.EdbInfoId == req.EdbInfoIdA {
  505. chartEdbInfoMapping.IsAxis = 1
  506. chartEdbInfoMapping.MinData = curveConf.LeftMin
  507. chartEdbInfoMapping.MaxData = curveConf.LeftMax
  508. chartEdbInfoMapping.ChartColor = `#00F`
  509. //chartEdbInfoMapping.LeadValue = curveConf.LeadValue
  510. //chartEdbInfoMapping.LeadUnit = curveConf.LeadUnit
  511. chartEdbInfoMapping.ChartWidth = 3
  512. } else {
  513. chartEdbInfoMapping.IsAxis = 0
  514. chartEdbInfoMapping.MinData = curveConf.RightMin
  515. chartEdbInfoMapping.MaxData = curveConf.RightMax
  516. chartEdbInfoMapping.ChartColor = `#F00`
  517. chartEdbInfoMapping.LeadValue = curveConf.LeadValue
  518. chartEdbInfoMapping.LeadUnit = curveConf.LeadUnit
  519. chartEdbInfoMapping.ChartWidth = 1
  520. chartEdbInfoMapping.IsOrder = curveConf.IsOrder
  521. chartEdbInfoMapping.EdbInfoType = edbInfoType
  522. }
  523. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  524. }
  525. if len(warnEdbList) > 0 {
  526. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  527. }
  528. tmpChartInfo.Source = utils.CHART_SOURCE_DEFAULT
  529. tmpChartInfo.DateType = curveConf.DateType
  530. tmpChartInfo.StartDate = startDate
  531. tmpChartInfo.EndDate = endDate
  532. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  533. resp.CurveData = response.CurveResp{
  534. ChartInfo: tmpChartInfo,
  535. EdbInfoList: tmpEdbList,
  536. }
  537. }
  538. br.Ret = 200
  539. br.Success = true
  540. br.Msg = "获取成功"
  541. br.Data = resp
  542. }
  543. // MultipleGraphConfigSaveChart
  544. // @Title 保存图表接口
  545. // @Description 保存图表接口
  546. // @Param request body request.SaveMultipleGraphChartReq true "type json string"
  547. // @Success Ret=200 返回图表id
  548. // @router /multiple_graph/chart/save [post]
  549. func (this *ChartInfoController) MultipleGraphConfigSaveChart() {
  550. br := new(models.BaseResponse).Init()
  551. defer func() {
  552. this.Data["json"] = br
  553. this.ServeJSON()
  554. }()
  555. sysUser := this.SysUser
  556. if sysUser == nil {
  557. br.Msg = "请登录"
  558. br.ErrMsg = "请登录,SysUser Is Empty"
  559. br.Ret = 408
  560. return
  561. }
  562. var req request.SaveMultipleGraphChartReq
  563. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  564. if err != nil {
  565. br.Msg = "参数解析异常!"
  566. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  567. return
  568. }
  569. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  570. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  571. br.Msg = "系统处理中,请稍后重试!"
  572. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  573. return
  574. }
  575. defer func() {
  576. _ = utils.Rc.Delete(cacheKey)
  577. }()
  578. if req.MultipleGraphConfigId <= 0 {
  579. br.Msg = "配置未保存"
  580. br.IsSendEmail = false
  581. return
  582. }
  583. if req.IsSaveAs {
  584. CopyMultipleGraphConfigSaveChart(req, this, br, sysUser)
  585. return
  586. }
  587. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  588. if err != nil {
  589. br.Msg = "获取失败"
  590. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  591. return
  592. }
  593. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  594. for _, v := range mappingList {
  595. if v.EdbInfoId == req.EdbInfoIdA {
  596. edbInfoMappingA = v
  597. }
  598. if v.EdbInfoId == req.EdbInfoIdB {
  599. edbInfoMappingB = v
  600. }
  601. }
  602. if edbInfoMappingA == nil {
  603. br.Msg = "指标A不存在"
  604. br.IsSendEmail = false
  605. return
  606. }
  607. if edbInfoMappingB == nil {
  608. br.Msg = "指标B不存在"
  609. br.IsSendEmail = false
  610. return
  611. }
  612. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  613. if err != nil && !utils.IsErrNoRow(err) {
  614. br.Msg = `保存失败`
  615. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  616. return
  617. }
  618. err = nil
  619. var isAdd bool
  620. var chartInfoId int
  621. if multipleGraphConfigChartMapping == nil || multipleGraphConfigChartMapping.Id <= 0 {
  622. isAdd = true
  623. } else {
  624. chartInfo, err := data_manage.GetChartInfoById(multipleGraphConfigChartMapping.ChartInfoId)
  625. if err != nil && !utils.IsErrNoRow(err) {
  626. br.Msg = `保存失败`
  627. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  628. return
  629. }
  630. // 说明图还在,没有被删除
  631. if chartInfo != nil && chartInfo.ChartInfoId > 0 {
  632. chartInfoId = multipleGraphConfigChartMapping.ChartInfoId
  633. req.ChartName = chartInfo.ChartName
  634. req.ClassifyId = chartInfo.ChartClassifyId
  635. }
  636. }
  637. var chartInfo *data_manage.ChartInfo
  638. var errMsg string
  639. isSendEmail := true
  640. switch req.Source {
  641. case 1: // 曲线图
  642. curveConf := req.Curve
  643. edbInfoType := 1 //1:标准指标,0:领先指标
  644. if !curveConf.EdbInfoType {
  645. edbInfoType = 0
  646. }
  647. chartSaveItemList := []*data_manage.ChartSaveItem{
  648. {
  649. EdbInfoId: edbInfoMappingA.EdbInfoId,
  650. MaxData: curveConf.LeftMax,
  651. MinData: curveConf.LeftMin,
  652. IsOrder: false,
  653. IsAxis: 1,
  654. EdbInfoType: 1,
  655. //LeadValue: curveConf.LeadValue,
  656. //LeadUnit: curveConf.LeadUnit,
  657. ChartStyle: "spline",
  658. ChartColor: `#00F`,
  659. PredictChartColor: "",
  660. ChartWidth: 3,
  661. Source: utils.CHART_SOURCE_DEFAULT,
  662. }, {
  663. EdbInfoId: edbInfoMappingB.EdbInfoId,
  664. MaxData: curveConf.RightMax,
  665. MinData: curveConf.RightMin,
  666. IsOrder: curveConf.IsOrder,
  667. IsAxis: 0,
  668. EdbInfoType: edbInfoType,
  669. LeadValue: curveConf.LeadValue,
  670. LeadUnit: curveConf.LeadUnit,
  671. ChartStyle: "spline",
  672. ChartColor: `#F00`,
  673. PredictChartColor: "",
  674. ChartWidth: 1,
  675. Source: utils.CHART_SOURCE_DEFAULT,
  676. },
  677. }
  678. if chartInfoId <= 0 {
  679. addChartReq := data_manage.AddChartInfoReq{
  680. ChartEdbInfoList: chartSaveItemList,
  681. ChartClassifyId: req.ClassifyId,
  682. ChartName: req.ChartName,
  683. ChartType: utils.CHART_TYPE_CURVE,
  684. DateType: curveConf.DateType,
  685. StartDate: curveConf.StartDate,
  686. EndDate: curveConf.EndDate,
  687. StartYear: curveConf.StartYear,
  688. Calendar: "公历",
  689. LeftMin: fmt.Sprint(curveConf.LeftMin),
  690. LeftMax: fmt.Sprint(curveConf.LeftMax),
  691. RightMin: fmt.Sprint(curveConf.RightMin),
  692. RightMax: fmt.Sprint(curveConf.RightMax),
  693. Right2Min: fmt.Sprint(curveConf.Right2Min),
  694. Right2Max: fmt.Sprint(curveConf.Right2Max),
  695. }
  696. chartInfo, err, errMsg, isSendEmail = data.AddChartInfo(addChartReq, sysUser.AdminId, sysUser.RealName, this.Lang)
  697. } else {
  698. editChartReq := data_manage.EditChartInfoReq{
  699. ChartInfoId: multipleGraphConfigChartMapping.ChartInfoId,
  700. ChartName: req.ChartName,
  701. ChartClassifyId: req.ClassifyId,
  702. ChartEdbInfoList: chartSaveItemList,
  703. ChartType: utils.CHART_TYPE_CURVE,
  704. DateType: curveConf.DateType,
  705. StartDate: curveConf.StartDate,
  706. EndDate: curveConf.EndDate,
  707. StartYear: curveConf.StartYear,
  708. Calendar: "公历",
  709. LeftMin: fmt.Sprint(curveConf.LeftMin),
  710. LeftMax: fmt.Sprint(curveConf.LeftMax),
  711. RightMin: fmt.Sprint(curveConf.RightMin),
  712. RightMax: fmt.Sprint(curveConf.RightMax),
  713. Right2Min: fmt.Sprint(curveConf.Right2Min),
  714. Right2Max: fmt.Sprint(curveConf.Right2Max),
  715. }
  716. chartInfo, err, errMsg, isSendEmail = data.EditChartInfo(editChartReq, sysUser, this.Lang)
  717. if err != nil {
  718. br.Msg = "保存失败"
  719. if errMsg != `` {
  720. br.Msg = errMsg
  721. }
  722. br.ErrMsg = err.Error()
  723. br.IsSendEmail = isSendEmail
  724. return
  725. }
  726. }
  727. case 2, 3, 4: // 相关性图(滚动相关性)
  728. correlationConf := req.Correlation
  729. // 配置了数据才有返回
  730. if correlationConf.LeadValue > 0 {
  731. leadValue := correlationConf.LeadValue
  732. leadUnit := correlationConf.LeadUnit
  733. baseCalculateValue := correlationConf.CalculateValue
  734. baseCalculateUnit := correlationConf.CalculateUnit
  735. calculateValue := baseCalculateValue
  736. calculateUnit := baseCalculateUnit
  737. chartSource := utils.CHART_SOURCE_CORRELATION // 默认是相关性图
  738. moveUnitDays, ok := utils.FrequencyDaysMap[calculateUnit]
  739. if !ok {
  740. br.Msg = "错误的分析周期"
  741. br.IsSendEmail = false
  742. return
  743. }
  744. startDate := time.Now().AddDate(0, 0, -correlationConf.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  745. endDate := time.Now().Format(utils.FormatDate)
  746. // 日期类型
  747. var dateType int
  748. if req.Source != 2 {
  749. chartSource = utils.CHART_SOURCE_ROLLING_CORRELATION
  750. var rollingCorrelation data_manage.RollingCorrelationConfig
  751. if req.Source == 3 {
  752. rollingCorrelation = req.RollingCorrelation[0]
  753. } else {
  754. rollingCorrelation = req.RollingCorrelation[1]
  755. }
  756. leadValue = rollingCorrelation.LeadValue
  757. leadUnit = rollingCorrelation.LeadUnit
  758. calculateValue = rollingCorrelation.CalculateValue
  759. calculateUnit = rollingCorrelation.CalculateUnit
  760. // 开始日期,结束日期
  761. dateType = req.Curve.DateType
  762. startDate, endDate = utils.GetDateByDateType(req.Curve.DateType, req.Curve.StartDate, req.Curve.EndDate)
  763. }
  764. // 图例, 图表来源
  765. var sourceFrom, extraConfig string
  766. if req.CorrelationExtraConfig != nil {
  767. b, e := json.Marshal(req.CorrelationExtraConfig)
  768. if e != nil {
  769. br.Msg = "保存失败"
  770. br.ErrMsg = fmt.Sprintf("相关性图表图例信息JSON格式化失败, Err: %v", e)
  771. return
  772. }
  773. extraConfig = string(b)
  774. }
  775. if req.SourcesFrom != nil {
  776. b, e := json.Marshal(req.SourcesFrom)
  777. if e != nil {
  778. br.Msg = "保存失败"
  779. br.ErrMsg = fmt.Sprintf("图表来源JSON格式化失败, Err: %v", e)
  780. return
  781. }
  782. sourceFrom = string(b)
  783. }
  784. correlationChartInfoReq := data_manage.CorrelationChartInfoReq{
  785. LeadValue: leadValue,
  786. LeadUnit: leadUnit,
  787. CalculateValue: calculateValue,
  788. CalculateUnit: calculateUnit,
  789. BaseCalculateValue: baseCalculateValue,
  790. BaseCalculateUnit: baseCalculateUnit,
  791. DateType: dateType,
  792. StartDate: startDate,
  793. EndDate: endDate,
  794. EdbInfoIdList: []data_manage.CorrelationChartInfoEdbItemReq{
  795. {
  796. EdbInfoId: edbInfoMappingA.EdbInfoId,
  797. Name: "",
  798. NameEn: "",
  799. }, {
  800. EdbInfoId: edbInfoMappingB.EdbInfoId,
  801. Name: "",
  802. NameEn: "",
  803. },
  804. },
  805. }
  806. if chartInfoId <= 0 {
  807. addChartReq := data_manage.AddChartInfoReq{
  808. ChartClassifyId: req.ClassifyId,
  809. ChartName: req.ChartName,
  810. ChartType: utils.CHART_TYPE_CURVE,
  811. Calendar: "公历",
  812. CorrelationChartInfo: correlationChartInfoReq,
  813. ExtraConfig: extraConfig,
  814. SourcesFrom: sourceFrom,
  815. }
  816. chartInfo, err, errMsg, isSendEmail = correlationServ.AddChartInfo(addChartReq, chartSource, sysUser, this.Lang)
  817. } else {
  818. editChartReq := data_manage.EditChartInfoReq{
  819. ChartInfoId: multipleGraphConfigChartMapping.ChartInfoId,
  820. ChartName: req.ChartName,
  821. ChartClassifyId: req.ClassifyId,
  822. ChartType: utils.CHART_TYPE_CURVE,
  823. Calendar: "公历",
  824. CorrelationChartInfo: correlationChartInfoReq,
  825. ExtraConfig: extraConfig,
  826. SourcesFrom: sourceFrom,
  827. }
  828. chartInfo, err, errMsg, isSendEmail = correlationServ.EditChartInfo(editChartReq, sysUser, this.Lang)
  829. if err != nil {
  830. br.Msg = "保存失败"
  831. if errMsg != `` {
  832. br.Msg = errMsg
  833. }
  834. br.ErrMsg = err.Error()
  835. br.IsSendEmail = isSendEmail
  836. return
  837. }
  838. }
  839. }
  840. default:
  841. br.Msg = "错误的来源"
  842. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  843. br.IsSendEmail = false
  844. return
  845. }
  846. if err != nil {
  847. br.Msg = "保存失败"
  848. if errMsg != `` {
  849. br.Msg = errMsg
  850. }
  851. br.ErrMsg = err.Error()
  852. br.IsSendEmail = isSendEmail
  853. return
  854. }
  855. // 如果图没有建立关联关系,那么就需要添加关系
  856. if isAdd {
  857. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  858. //Id: 0,
  859. MultipleGraphConfigId: req.MultipleGraphConfigId,
  860. ChartInfoId: chartInfo.ChartInfoId,
  861. Source: req.Source,
  862. ModifyTime: time.Now(),
  863. CreateTime: time.Now(),
  864. }
  865. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  866. if err != nil {
  867. br.Msg = "保存失败"
  868. br.ErrMsg = "保存配置与图表的关联关系失败,ERR:" + err.Error()
  869. return
  870. }
  871. } else if multipleGraphConfigChartMapping != nil {
  872. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  873. multipleGraphConfigChartMapping.ModifyTime = time.Now()
  874. err = multipleGraphConfigChartMapping.Update([]string{"ChartInfoId", "ModifyTime"})
  875. if err != nil {
  876. br.Msg = "保存失败"
  877. br.ErrMsg = "保存配置与图表的关联关系失败,ERR:" + err.Error()
  878. return
  879. }
  880. }
  881. //新增操作日志
  882. {
  883. chartLog := new(data_manage.ChartInfoLog)
  884. chartLog.ChartInfoId = chartInfo.ChartInfoId
  885. chartLog.ChartName = req.ChartName
  886. chartLog.ChartClassifyId = req.ClassifyId
  887. chartLog.SysUserId = sysUser.AdminId
  888. chartLog.SysUserRealName = sysUser.RealName
  889. chartLog.UniqueCode = chartInfo.UniqueCode
  890. chartLog.CreateTime = time.Now()
  891. chartLog.Content = string(this.Ctx.Input.RequestBody)
  892. chartLog.Status = "多图配置保存图表"
  893. chartLog.Method = this.Ctx.Input.URI()
  894. go data_manage.AddChartInfoLog(chartLog)
  895. }
  896. resp := new(data_manage.AddChartInfoResp)
  897. resp.ChartInfoId = chartInfo.ChartInfoId
  898. resp.UniqueCode = chartInfo.UniqueCode
  899. resp.ChartType = chartInfo.ChartType
  900. br.Ret = 200
  901. br.Success = true
  902. br.Msg = "保存成功"
  903. br.Data = resp
  904. br.IsAddLog = true
  905. }
  906. // CopyMultipleGraphConfigSaveChart 图表另存为
  907. func CopyMultipleGraphConfigSaveChart(req request.SaveMultipleGraphChartReq, this *ChartInfoController, br *models.BaseResponse, sysUser *system.Admin) {
  908. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  909. if err != nil {
  910. br.Msg = "获取失败"
  911. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  912. return
  913. }
  914. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  915. for _, v := range mappingList {
  916. if v.EdbInfoId == req.EdbInfoIdA {
  917. edbInfoMappingA = v
  918. }
  919. if v.EdbInfoId == req.EdbInfoIdB {
  920. edbInfoMappingB = v
  921. }
  922. }
  923. if edbInfoMappingA == nil {
  924. br.Msg = "指标A不存在"
  925. br.IsSendEmail = false
  926. return
  927. }
  928. if edbInfoMappingB == nil {
  929. br.Msg = "指标B不存在"
  930. br.IsSendEmail = false
  931. return
  932. }
  933. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  934. if err != nil {
  935. br.Msg = `保存失败`
  936. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  937. return
  938. }
  939. // 原图
  940. oldChartInfo, err := data_manage.GetChartInfoById(multipleGraphConfigChartMapping.ChartInfoId)
  941. if err != nil {
  942. if !utils.IsErrNoRow(err) {
  943. br.Msg = `保存失败`
  944. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  945. return
  946. }
  947. br.Msg = "原图不存在"
  948. br.IsSendEmail = false
  949. return
  950. }
  951. // 说明图还在,没有被删除
  952. if oldChartInfo == nil || oldChartInfo.ChartInfoId <= 0 {
  953. br.Msg = "原图不存在"
  954. br.IsSendEmail = false
  955. }
  956. var chartInfo *data_manage.ChartInfo
  957. var errMsg string
  958. isSendEmail := true
  959. switch req.Source {
  960. case 1: // 曲线图
  961. curveConf := req.Curve
  962. edbInfoType := 1 //1:标准指标,0:领先指标
  963. if !curveConf.EdbInfoType {
  964. edbInfoType = 0
  965. }
  966. chartSaveItemList := []*data_manage.ChartSaveItem{
  967. {
  968. EdbInfoId: edbInfoMappingA.EdbInfoId,
  969. MaxData: curveConf.LeftMax,
  970. MinData: curveConf.LeftMin,
  971. IsOrder: false,
  972. IsAxis: 1,
  973. EdbInfoType: 1,
  974. //LeadValue: curveConf.LeadValue,
  975. //LeadUnit: curveConf.LeadUnit,
  976. ChartStyle: "spline",
  977. ChartColor: `#00F`,
  978. PredictChartColor: "",
  979. ChartWidth: 3,
  980. Source: utils.CHART_SOURCE_DEFAULT,
  981. }, {
  982. EdbInfoId: edbInfoMappingB.EdbInfoId,
  983. MaxData: curveConf.RightMax,
  984. MinData: curveConf.RightMin,
  985. IsOrder: curveConf.IsOrder,
  986. IsAxis: 0,
  987. EdbInfoType: edbInfoType,
  988. LeadValue: curveConf.LeadValue,
  989. LeadUnit: curveConf.LeadUnit,
  990. ChartStyle: "spline",
  991. ChartColor: `#F00`,
  992. PredictChartColor: "",
  993. ChartWidth: 1,
  994. Source: utils.CHART_SOURCE_DEFAULT,
  995. },
  996. }
  997. addChartReq := data_manage.AddChartInfoReq{
  998. ChartEdbInfoList: chartSaveItemList,
  999. ChartClassifyId: req.ClassifyId,
  1000. ChartName: req.ChartName,
  1001. ChartType: utils.CHART_TYPE_CURVE,
  1002. DateType: curveConf.DateType,
  1003. StartDate: curveConf.StartDate,
  1004. EndDate: curveConf.EndDate,
  1005. StartYear: curveConf.StartYear,
  1006. Calendar: "公历",
  1007. LeftMin: fmt.Sprint(curveConf.LeftMin),
  1008. LeftMax: fmt.Sprint(curveConf.LeftMax),
  1009. RightMin: fmt.Sprint(curveConf.RightMin),
  1010. RightMax: fmt.Sprint(curveConf.RightMax),
  1011. }
  1012. chartInfo, err, errMsg, isSendEmail = data.AddChartInfo(addChartReq, sysUser.AdminId, sysUser.RealName, this.Lang)
  1013. case 2: // 相关性图
  1014. correlationConf := req.Correlation
  1015. // 配置了数据才有返回
  1016. if correlationConf.LeadValue > 0 {
  1017. leadValue := correlationConf.LeadValue
  1018. leadUnit := correlationConf.LeadUnit
  1019. calculateValue := correlationConf.CalculateValue
  1020. calculateUnit := correlationConf.CalculateUnit
  1021. moveUnitDays, ok := utils.FrequencyDaysMap[calculateUnit]
  1022. if !ok {
  1023. br.Msg = "错误的分析周期"
  1024. br.IsSendEmail = false
  1025. return
  1026. }
  1027. startDate := time.Now().AddDate(0, 0, -correlationConf.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  1028. endDate := time.Now().Format(utils.FormatDate)
  1029. correlationChartInfoReq := data_manage.CorrelationChartInfoReq{
  1030. LeadValue: leadValue,
  1031. LeadUnit: leadUnit,
  1032. CalculateValue: calculateValue,
  1033. CalculateUnit: calculateUnit,
  1034. StartDate: startDate,
  1035. EndDate: endDate,
  1036. EdbInfoIdList: []data_manage.CorrelationChartInfoEdbItemReq{
  1037. {
  1038. EdbInfoId: edbInfoMappingA.EdbInfoId,
  1039. Name: "",
  1040. NameEn: "",
  1041. }, {
  1042. EdbInfoId: edbInfoMappingB.EdbInfoId,
  1043. Name: "",
  1044. NameEn: "",
  1045. },
  1046. },
  1047. }
  1048. chartInfo, err, errMsg, isSendEmail = correlationServ.CopyChartInfo(multipleGraphConfigChartMapping.MultipleGraphConfigId, req.ClassifyId, req.ChartName, correlationChartInfoReq, oldChartInfo, sysUser, this.Lang)
  1049. }
  1050. default:
  1051. br.Msg = "错误的来源"
  1052. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  1053. br.IsSendEmail = false
  1054. return
  1055. }
  1056. if err != nil {
  1057. br.Msg = "保存失败"
  1058. if errMsg != `` {
  1059. br.Msg = errMsg
  1060. }
  1061. br.ErrMsg = err.Error()
  1062. br.IsSendEmail = isSendEmail
  1063. return
  1064. }
  1065. //新增操作日志
  1066. {
  1067. chartLog := new(data_manage.ChartInfoLog)
  1068. chartLog.ChartInfoId = chartInfo.ChartInfoId
  1069. chartLog.ChartName = req.ChartName
  1070. chartLog.ChartClassifyId = req.ClassifyId
  1071. chartLog.SysUserId = sysUser.AdminId
  1072. chartLog.SysUserRealName = sysUser.RealName
  1073. chartLog.UniqueCode = chartInfo.UniqueCode
  1074. chartLog.CreateTime = time.Now()
  1075. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1076. chartLog.Status = "多图配置另存为图表"
  1077. chartLog.Method = this.Ctx.Input.URI()
  1078. go data_manage.AddChartInfoLog(chartLog)
  1079. }
  1080. resp := new(data_manage.AddChartInfoResp)
  1081. resp.ChartInfoId = chartInfo.ChartInfoId
  1082. resp.UniqueCode = chartInfo.UniqueCode
  1083. resp.ChartType = chartInfo.ChartType
  1084. br.Ret = 200
  1085. br.Success = true
  1086. br.Msg = "保存成功"
  1087. br.Data = resp
  1088. br.IsAddLog = true
  1089. }
  1090. // MultipleGraphConfigSaveEdb
  1091. // @Title 保存指标接口
  1092. // @Description 保存指标接口
  1093. // @Param request body request.SaveMultipleGraphEdbReq true "type json string"
  1094. // @Success Ret=200 返回指标id
  1095. // @router /multiple_graph/edb/save [post]
  1096. func (this *ChartInfoController) MultipleGraphConfigSaveEdb() {
  1097. br := new(models.BaseResponse).Init()
  1098. defer func() {
  1099. this.Data["json"] = br
  1100. this.ServeJSON()
  1101. }()
  1102. sysUser := this.SysUser
  1103. if sysUser == nil {
  1104. br.Msg = "请登录"
  1105. br.ErrMsg = "请登录,SysUser Is Empty"
  1106. br.Ret = 408
  1107. return
  1108. }
  1109. var req request.SaveMultipleGraphEdbReq
  1110. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1111. if err != nil {
  1112. br.Msg = "参数解析异常!"
  1113. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1114. return
  1115. }
  1116. cacheKey := "CACHE_CHART_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1117. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1118. br.Msg = "系统处理中,请稍后重试!"
  1119. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1120. return
  1121. }
  1122. defer func() {
  1123. _ = utils.Rc.Delete(cacheKey)
  1124. }()
  1125. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  1126. if err != nil {
  1127. br.Msg = "获取失败"
  1128. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1129. return
  1130. }
  1131. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  1132. for _, v := range mappingList {
  1133. if v.EdbInfoId == req.EdbInfoIdA {
  1134. edbInfoMappingA = v
  1135. }
  1136. if v.EdbInfoId == req.EdbInfoIdB {
  1137. edbInfoMappingB = v
  1138. }
  1139. }
  1140. if edbInfoMappingA == nil {
  1141. br.Msg = "指标A不存在"
  1142. br.IsSendEmail = false
  1143. return
  1144. }
  1145. if edbInfoMappingB == nil {
  1146. br.Msg = "指标B不存在"
  1147. br.IsSendEmail = false
  1148. return
  1149. }
  1150. correlationConf := req.Correlation
  1151. // 配置了数据才有返回
  1152. if correlationConf.LeadValue <= 0 {
  1153. br.Msg = "相关性计算窗口必须大于0"
  1154. br.IsSendEmail = false
  1155. return
  1156. }
  1157. // 滚动相关性配置
  1158. var rollingCorrelation data_manage.RollingCorrelationConfig
  1159. switch req.Source {
  1160. case 3:
  1161. rollingCorrelation = req.RollingCorrelation[0]
  1162. case 4:
  1163. rollingCorrelation = req.RollingCorrelation[1]
  1164. default:
  1165. br.Msg = "错误的来源"
  1166. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  1167. br.IsSendEmail = false
  1168. return
  1169. }
  1170. // 配置了数据才有返回
  1171. if rollingCorrelation.CalculateValue <= 0 {
  1172. br.Msg = "滚动相关性计算窗口必须大于0"
  1173. br.IsSendEmail = false
  1174. return
  1175. }
  1176. multipleGraphConfigEdbMapping, err := data_manage.GetMultipleGraphConfigEdbMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  1177. if err != nil && !utils.IsErrNoRow(err) {
  1178. br.Msg = `保存失败`
  1179. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  1180. return
  1181. }
  1182. err = nil
  1183. var isAdd bool
  1184. var edbInfoId int
  1185. if multipleGraphConfigEdbMapping == nil || multipleGraphConfigEdbMapping.MultipleGraphConfigId <= 0 {
  1186. isAdd = true
  1187. } else {
  1188. edbInfo, err := data_manage.GetEdbInfoById(multipleGraphConfigEdbMapping.EdbInfoId)
  1189. if err != nil && !utils.IsErrNoRow(err) {
  1190. br.Msg = `保存失败`
  1191. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  1192. return
  1193. }
  1194. // 说明指标还在,没有被删除
  1195. if edbInfo != nil && edbInfo.EdbInfoId > 0 && !req.IsSaveAs {
  1196. edbInfoId = multipleGraphConfigEdbMapping.EdbInfoId
  1197. req.EdbName = edbInfo.EdbName
  1198. req.ClassifyId = edbInfo.ClassifyId
  1199. req.Frequency = edbInfo.Frequency
  1200. req.Unit = edbInfo.Unit
  1201. }
  1202. }
  1203. // 关联指标
  1204. edbInfoIdArr := []data_manage.EdbInfoFromTag{
  1205. {
  1206. EdbInfoId: edbInfoMappingA.EdbInfoId,
  1207. FromTag: "A",
  1208. MoveValue: 0,
  1209. }, {
  1210. EdbInfoId: edbInfoMappingB.EdbInfoId,
  1211. FromTag: "B",
  1212. MoveValue: 0,
  1213. },
  1214. }
  1215. calculateFormula := request.EdbCalculateFormula{
  1216. BaseCalculateValue: correlationConf.CalculateValue,
  1217. BaseCalculateUnit: correlationConf.CalculateUnit,
  1218. LeadValue: rollingCorrelation.LeadValue,
  1219. LeadUnit: rollingCorrelation.LeadUnit,
  1220. CalculateValue: rollingCorrelation.CalculateValue,
  1221. CalculateUnit: rollingCorrelation.CalculateUnit,
  1222. }
  1223. calculateFormulaByte, err := json.Marshal(calculateFormula)
  1224. if err != nil {
  1225. br.Msg = "参数解析异常!"
  1226. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1227. return
  1228. }
  1229. var respItem *data.AddPredictEdbDataResponse
  1230. if edbInfoId <= 0 || req.IsSaveAs {
  1231. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1232. AdminId: sysUser.AdminId,
  1233. AdminName: sysUser.RealName,
  1234. EdbName: req.EdbName,
  1235. Frequency: req.Frequency,
  1236. Unit: req.Unit,
  1237. ClassifyId: req.ClassifyId,
  1238. Formula: string(calculateFormulaByte),
  1239. Source: utils.DATA_SOURCE_CALCULATE_CORRELATION,
  1240. EdbInfoIdArr: edbInfoIdArr,
  1241. Calendar: `公历`,
  1242. }
  1243. // 调用指标库去添加
  1244. reqJson, err := json.Marshal(req2)
  1245. if err != nil {
  1246. br.Msg = "参数解析异常!"
  1247. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1248. return
  1249. }
  1250. respItem, err = data.BatchSaveEdbCalculateData(string(reqJson), this.Lang)
  1251. if err != nil {
  1252. br.Msg = "新增失败"
  1253. br.ErrMsg = "新增失败,Err:" + err.Error()
  1254. return
  1255. }
  1256. } else {
  1257. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  1258. EdbInfoId: edbInfoId,
  1259. EdbName: req.EdbName,
  1260. Frequency: req.Frequency,
  1261. Unit: req.Unit,
  1262. ClassifyId: req.ClassifyId,
  1263. Formula: string(calculateFormulaByte),
  1264. Source: utils.DATA_SOURCE_CALCULATE_CORRELATION,
  1265. EdbInfoIdArr: edbInfoIdArr,
  1266. Calendar: `公历`,
  1267. }
  1268. // 调用指标库去更新
  1269. reqJson, err := json.Marshal(req2)
  1270. if err != nil {
  1271. br.Msg = "参数解析异常!"
  1272. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1273. return
  1274. }
  1275. respItem, err = data.BatchEditEdbCalculateData(string(reqJson), this.Lang)
  1276. if err != nil {
  1277. br.Msg = "编辑失败"
  1278. br.ErrMsg = "编辑失败,Err:" + err.Error()
  1279. return
  1280. }
  1281. }
  1282. if respItem == nil {
  1283. br.Msg = "保存失败"
  1284. br.ErrMsg = "保存失败"
  1285. return
  1286. }
  1287. if respItem.Ret != 200 {
  1288. br.Msg = respItem.Msg
  1289. br.ErrMsg = respItem.ErrMsg
  1290. return
  1291. }
  1292. resp := respItem.Data
  1293. //添加es
  1294. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  1295. // 如果不是另存为的话,那么需要建立配置与指标的关系
  1296. if !req.IsSaveAs {
  1297. // 如果指标没有建立关联关系,那么就需要添加关系
  1298. if isAdd {
  1299. multipleGraphConfigEdbMapping = &data_manage.MultipleGraphConfigEdbMapping{
  1300. //Id: 0,
  1301. MultipleGraphConfigId: req.MultipleGraphConfigId,
  1302. EdbInfoId: resp.EdbInfoId,
  1303. Source: req.Source,
  1304. ModifyTime: time.Now(),
  1305. CreateTime: time.Now(),
  1306. }
  1307. err = data_manage.AddMultipleGraphConfigEdbMapping(multipleGraphConfigEdbMapping)
  1308. if err != nil {
  1309. br.Msg = "保存失败"
  1310. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1311. return
  1312. }
  1313. } else if multipleGraphConfigEdbMapping != nil {
  1314. multipleGraphConfigEdbMapping.EdbInfoId = resp.EdbInfoId
  1315. multipleGraphConfigEdbMapping.ModifyTime = time.Now()
  1316. err = multipleGraphConfigEdbMapping.Update([]string{"EdbInfoId", "ModifyTime"})
  1317. if err != nil {
  1318. br.Msg = "保存失败"
  1319. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1320. return
  1321. }
  1322. }
  1323. }
  1324. //新增操作日志
  1325. //{
  1326. // edbLog := new(data_manage.EdbInfoLog)
  1327. // edbLog.EdbInfoId = resp.EdbInfoId
  1328. // edbLog.SourceName = edbInfo.SourceName
  1329. // edbLog.Source = edbInfo.Source
  1330. // edbLog.EdbCode = edbInfo.EdbCode
  1331. // edbLog.EdbName = req2.EdbName
  1332. // edbLog.ClassifyId = req2.ClassifyId
  1333. // edbLog.SysUserId = sysUser.AdminId
  1334. // edbLog.SysUserRealName = sysUser.RealName
  1335. // edbLog.CreateTime = time.Now()
  1336. // edbLog.Content = string(this.Ctx.Input.RequestBody)
  1337. // edbLog.Status = "多图配置保存指标"
  1338. // edbLog.Method = this.Ctx.Input.URI()
  1339. // go data_manage.AddEdbInfoLog(edbLog)
  1340. //}
  1341. br.Ret = 200
  1342. br.Success = true
  1343. br.Msg = "保存成功"
  1344. br.Data = resp
  1345. br.IsAddLog = true
  1346. }
  1347. // GetMultipleGraphConfig
  1348. // @Title 保存配置接口
  1349. // @Description 保存配置接口
  1350. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  1351. // @Success Ret=200 返回图表id
  1352. // @router /multiple_graph/config/detail [get]
  1353. func (this *ChartInfoController) GetMultipleGraphConfig() {
  1354. br := new(models.BaseResponse).Init()
  1355. defer func() {
  1356. this.Data["json"] = br
  1357. this.ServeJSON()
  1358. }()
  1359. sysUser := this.SysUser
  1360. if sysUser == nil {
  1361. br.Msg = "请登录"
  1362. br.ErrMsg = "请登录,SysUser Is Empty"
  1363. br.Ret = 408
  1364. return
  1365. }
  1366. uniqueCode := this.GetString("UniqueCode")
  1367. if uniqueCode == "" {
  1368. br.Msg = "参数错误"
  1369. br.ErrMsg = "参数错误,uniqueCode is empty"
  1370. return
  1371. }
  1372. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  1373. if err != nil {
  1374. br.Msg = "获取失败"
  1375. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1376. return
  1377. }
  1378. if chartInfo == nil {
  1379. br.Msg = "获取失败"
  1380. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1381. return
  1382. }
  1383. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
  1384. if err != nil {
  1385. br.Msg = "获取失败"
  1386. br.ErrMsg = "获取滚动相关性图表的配置信息失败,Err:" + err.Error()
  1387. return
  1388. }
  1389. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1390. if err != nil {
  1391. br.Msg = `获取失败`
  1392. br.ErrMsg = `获取配置失败,ERR:` + err.Error()
  1393. return
  1394. }
  1395. // 生成的图表
  1396. multipleGraphConfigChartMappingList := make([]response.MultipleGraphConfigChartMapping, 0)
  1397. {
  1398. chartMappingList, err := data_manage.GetMultipleGraphConfigChartMappingListById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1399. if err != nil {
  1400. br.Msg = `获取失败`
  1401. br.ErrMsg = `获取关联图表配置失败,ERR:` + err.Error()
  1402. return
  1403. }
  1404. chartInfoIdList := make([]int, 0)
  1405. chartMappingMap := make(map[int]*data_manage.MultipleGraphConfigChartMapping)
  1406. for _, v := range chartMappingList {
  1407. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  1408. chartMappingMap[v.ChartInfoId] = v
  1409. }
  1410. chartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  1411. if err != nil {
  1412. br.Msg = `获取失败`
  1413. br.ErrMsg = `获取关联图表列表失败,ERR:` + err.Error()
  1414. return
  1415. }
  1416. for _, v := range chartList {
  1417. var mapping response.MultipleGraphConfigChartMapping
  1418. if tmpInfo, ok := chartMappingMap[v.ChartInfoId]; ok {
  1419. mapping.ChartInfoId = tmpInfo.ChartInfoId
  1420. mapping.Source = tmpInfo.Source
  1421. mapping.MultipleLocationSource = tmpInfo.Source
  1422. if v.ChartInfoId == chartInfo.ChartInfoId {
  1423. mapping.CorrelationExtraConfig = chartInfo.ExtraConfig
  1424. mapping.SourcesFrom = chartInfo.SourcesFrom
  1425. }
  1426. multipleGraphConfigChartMappingList = append(multipleGraphConfigChartMappingList, mapping)
  1427. }
  1428. }
  1429. }
  1430. // 生成的指标
  1431. multipleGraphConfigEdbMappingList := make([]response.MultipleGraphConfigEdbMapping, 0)
  1432. {
  1433. edbMappingList, err := data_manage.GetMultipleGraphConfigEdbMappingListById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1434. if err != nil {
  1435. br.Msg = `获取失败`
  1436. br.ErrMsg = `获取关联指标配置失败,ERR:` + err.Error()
  1437. return
  1438. }
  1439. edbInfoIdList := make([]int, 0)
  1440. edbMappingMap := make(map[int]*data_manage.MultipleGraphConfigEdbMapping)
  1441. for _, v := range edbMappingList {
  1442. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  1443. edbMappingMap[v.EdbInfoId] = v
  1444. }
  1445. edbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1446. if err != nil {
  1447. br.Msg = `获取失败`
  1448. br.ErrMsg = `获取关联图表列表失败,ERR:` + err.Error()
  1449. return
  1450. }
  1451. for _, v := range edbList {
  1452. if tmpInfo, ok := edbMappingMap[v.EdbInfoId]; ok {
  1453. multipleGraphConfigEdbMappingList = append(multipleGraphConfigEdbMappingList, response.MultipleGraphConfigEdbMapping{
  1454. EdbInfoId: tmpInfo.EdbInfoId,
  1455. Source: tmpInfo.Source,
  1456. MultipleLocationSource: tmpInfo.Source,
  1457. })
  1458. }
  1459. }
  1460. }
  1461. // 关联指标
  1462. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{multipleGraphConfig.EdbInfoIdA, multipleGraphConfig.EdbInfoIdB})
  1463. if err != nil {
  1464. br.Msg = "获取失败"
  1465. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1466. return
  1467. }
  1468. resp := response.MultipleGraphConfigDetailResp{
  1469. EdbInfoList: mappingList,
  1470. MultipleGraphConfig: *multipleGraphConfig,
  1471. ChartMappingList: multipleGraphConfigChartMappingList,
  1472. EdbMappingList: multipleGraphConfigEdbMappingList,
  1473. }
  1474. br.Ret = 200
  1475. br.Success = true
  1476. br.Msg = "保存成功"
  1477. br.Data = resp
  1478. br.IsAddLog = true
  1479. }
  1480. // func init() {
  1481. // //correlationChart := new(data_manage.ChartInfoCorrelation)
  1482. //
  1483. // condition := ` AND source = 3 `
  1484. // list, err := data_manage.GetChartListByCondition(condition, []interface{}{}, 0, 1000)
  1485. // if err != nil {
  1486. // fmt.Println("err:", err)
  1487. // return
  1488. // }
  1489. // timeNowFormat := time.Now().Format(utils.FormatDate)
  1490. // for _, v := range list {
  1491. // item, tmpErr := data_manage.GetMultipleGraphConfigChartMappingByChartIdAndSource(v.ChartInfoId)
  1492. // if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  1493. // fmt.Println("tmpErr:", tmpErr)
  1494. // continue
  1495. // }
  1496. // if item == nil {
  1497. // correlationChart := new(data_manage.ChartInfoCorrelation)
  1498. // e := correlationChart.GetItemById(v.ChartInfoId)
  1499. // if e != nil {
  1500. // fmt.Println("获取历史配置失败:", e)
  1501. // continue
  1502. // }
  1503. // day, _ := utils.GetDaysBetween2Date(utils.FormatDate, timeNowFormat, correlationChart.StartDate.Format(utils.FormatDate))
  1504. // req := request.SaveMultipleGraphConfigReq{
  1505. // MultipleGraphConfigId: 0,
  1506. // EdbInfoIdA: correlationChart.EdbInfoIdFirst,
  1507. // EdbInfoIdB: correlationChart.EdbInfoIdSecond,
  1508. // Curve: data_manage.CurveConfig{},
  1509. // Correlation: data_manage.CorrelationConfig{
  1510. // LeadValue: correlationChart.LeadValue,
  1511. // LeadUnit: correlationChart.LeadUnit,
  1512. // CalculateValue: day,
  1513. // CalculateUnit: "天",
  1514. // },
  1515. // RollingCorrelation: []data_manage.RollingCorrelationConfig{
  1516. // {}, {},
  1517. // },
  1518. // }
  1519. // curveStrByte, _ := json.Marshal(req.Curve)
  1520. // correlationStrByte, _ := json.Marshal(req.Correlation)
  1521. // rollingCorrelationStrByte, _ := json.Marshal(req.RollingCorrelation)
  1522. //
  1523. // multipleGraphConfig := &data_manage.MultipleGraphConfig{
  1524. // //MultipleGraphConfigId: 0,
  1525. // EdbInfoIdA: req.EdbInfoIdA,
  1526. // EdbInfoIdB: req.EdbInfoIdB,
  1527. // Curve: string(curveStrByte),
  1528. // Correlation: string(correlationStrByte),
  1529. // RollingCorrelation: string(rollingCorrelationStrByte),
  1530. // SysUserId: v.SysUserId,
  1531. // SysUserRealName: v.SysUserRealName,
  1532. // ModifyTime: v.ModifyTime,
  1533. // CreateTime: v.CreateTime,
  1534. // }
  1535. // err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1536. //
  1537. // if err != nil {
  1538. // fmt.Println(v.ChartInfoId, "创建配置失败:", err)
  1539. // continue
  1540. // }
  1541. //
  1542. // multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  1543. // //Id: 0,
  1544. // MultipleGraphConfigId: multipleGraphConfig.MultipleGraphConfigId,
  1545. // ChartInfoId: v.ChartInfoId,
  1546. // Source: 2,
  1547. // ModifyTime: time.Now(),
  1548. // CreateTime: time.Now(),
  1549. // }
  1550. // err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1551. // if err != nil {
  1552. // fmt.Println(v.ChartInfoId, "保存配置与图表的关联关系失败:", err)
  1553. // }
  1554. //
  1555. // correlationChart.CalculateUnit = "天"
  1556. // correlationChart.CalculateValue = day
  1557. // err = correlationChart.Update([]string{"CalculateUnit", "CalculateValue"})
  1558. // if err != nil {
  1559. // fmt.Println(v.ChartInfoId, "修复历史数据失败:", err)
  1560. // }
  1561. // }
  1562. // }
  1563. // }
  1564. // 修复相关性图表与滚动相关性图表的强关联关系
  1565. //func init() {
  1566. // condition := ` AND source = ? `
  1567. // pars := []interface{}{utils.CHART_SOURCE_ROLLING_CORRELATION}
  1568. // list, err := data_manage.GetChartListByCondition(condition, pars, 0, 1000)
  1569. // if err != nil {
  1570. // fmt.Println("err:", err)
  1571. // return
  1572. // }
  1573. // for _, chartInfo := range list {
  1574. // multipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
  1575. // if e != nil {
  1576. // fmt.Println(chartInfo.ChartInfoId, ";获取滚动相关性图表的配置信息失败, Err:"+e.Error())
  1577. // continue
  1578. // }
  1579. // baseChartMultipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(multipleGraphConfigChartMapping.MultipleGraphConfigId, 2)
  1580. // if e != nil {
  1581. // fmt.Println(chartInfo.ChartInfoId, ";获取相关性图表的配置信息失败, Err:"+e.Error())
  1582. // continue
  1583. // }
  1584. // baseChartCorrelationChart := new(data_manage.ChartInfoCorrelation)
  1585. // if e := baseChartCorrelationChart.GetItemById(baseChartMultipleGraphConfigChartMapping.ChartInfoId); e != nil {
  1586. // fmt.Println(chartInfo.ChartInfoId, ";获取基础相关性图表信息失败, Err:"+e.Error())
  1587. // continue
  1588. // }
  1589. //
  1590. // // 自己的配置
  1591. // selfChartCorrelationChart := new(data_manage.ChartInfoCorrelation)
  1592. // if e := selfChartCorrelationChart.GetItemById(chartInfo.ChartInfoId); e != nil {
  1593. // fmt.Println(chartInfo.ChartInfoId, ";获取自己的相关性图表信息失败, Err:"+e.Error())
  1594. // continue
  1595. // }
  1596. //
  1597. // selfChartCorrelationChart.BaseCalculateUnit = baseChartCorrelationChart.CalculateUnit
  1598. // selfChartCorrelationChart.BaseCalculateValue = baseChartCorrelationChart.CalculateValue
  1599. // err = selfChartCorrelationChart.Update([]string{"BaseCalculateUnit", "BaseCalculateValue"})
  1600. // if err != nil {
  1601. // fmt.Println(chartInfo.ChartInfoId, ";更新相关性图表信息失败, Err:"+err.Error())
  1602. // continue
  1603. // }
  1604. // }
  1605. //
  1606. //}
  1607. // 修复相关性图表与滚动相关性图表的强关联关系
  1608. //func init() {
  1609. // {
  1610. // condition := ` AND source = ? `
  1611. // pars := []interface{}{utils.CHART_SOURCE_ROLLING_CORRELATION}
  1612. // list, err := data_manage.GetChartListByCondition(condition, pars, 0, 1000)
  1613. // if err != nil {
  1614. // fmt.Println("err:", err)
  1615. // return
  1616. // }
  1617. // for _, chartInfo := range list {
  1618. // multipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
  1619. // if e != nil {
  1620. // fmt.Println(chartInfo.ChartInfoId, ";获取滚动相关性图表的配置信息失败, Err:"+e.Error())
  1621. // continue
  1622. // }
  1623. // multipleGraphConfig, e := data_manage.GetMultipleGraphConfigById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1624. // if e != nil {
  1625. // fmt.Println(chartInfo.ChartInfoId, ";获取相关性图表的配置信息失败, Err:"+e.Error())
  1626. // continue
  1627. // }
  1628. // var curveConf data_manage.CurveConfig
  1629. // e = json.Unmarshal([]byte(multipleGraphConfig.Curve), &curveConf)
  1630. // if e != nil {
  1631. // fmt.Println(chartInfo.ChartInfoId, ";格式化曲线图数据失败, Err:"+e.Error())
  1632. // continue
  1633. // }
  1634. //
  1635. // // 自己的配置
  1636. // selfChartCorrelationChart := new(data_manage.ChartInfoCorrelation)
  1637. // if e := selfChartCorrelationChart.GetItemById(chartInfo.ChartInfoId); e != nil {
  1638. // fmt.Println(chartInfo.ChartInfoId, ";获取自己的相关性图表信息失败, Err:"+e.Error())
  1639. // continue
  1640. // }
  1641. //
  1642. // selfChartCorrelationChart.DateType = curveConf.DateType
  1643. //
  1644. // startDate, endDate := utils.GetDateByDateType(curveConf.DateType, curveConf.StartDate, curveConf.EndDate)
  1645. // if startDate != `` {
  1646. // startDateTime, e := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  1647. // if e != nil {
  1648. // fmt.Println(chartInfo.ChartInfoId, ";开始日期格式化失败,开始日期:", startDate, ", Err:"+e.Error())
  1649. // continue
  1650. // }
  1651. // selfChartCorrelationChart.StartDate = startDateTime
  1652. // }
  1653. // if endDate != `` {
  1654. // endDateTime, e := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1655. // if e != nil {
  1656. // fmt.Println(chartInfo.ChartInfoId, ";开始日期格式化失败,开始日期:", startDate, ", Err:"+e.Error())
  1657. // continue
  1658. // }
  1659. // selfChartCorrelationChart.EndDate = endDateTime
  1660. // }
  1661. // err = selfChartCorrelationChart.Update([]string{"DateType", "StartDate", "EndDate"})
  1662. // if err != nil {
  1663. // fmt.Println(chartInfo.ChartInfoId, ";更新相关性图表信息失败, Err:"+err.Error())
  1664. // continue
  1665. // }
  1666. // }
  1667. //
  1668. // fmt.Println("图表更新完成")
  1669. // }
  1670. //
  1671. // {
  1672. // // 更新指标
  1673. // condition := ` AND source = ? `
  1674. // pars := []interface{}{utils.DATA_SOURCE_CALCULATE_CORRELATION}
  1675. // list, err := data_manage.GetEdbInfoListV2ByCondition(condition, pars, 0, 10000)
  1676. // if err != nil {
  1677. // fmt.Println("err:", err)
  1678. // return
  1679. // }
  1680. // for _, edbInfo := range list {
  1681. // multipleGraphConfigEdbMappingInfo, e := data_manage.GetMultipleGraphConfigEdbMappingByEdbInfoId(edbInfo.EdbInfoId)
  1682. // if e != nil {
  1683. // fmt.Println(edbInfo.EdbInfoId, ";获取滚动相关性指标的配置信息失败, Err:"+e.Error())
  1684. // continue
  1685. // }
  1686. // multipleGraphConfig, e := data_manage.GetMultipleGraphConfigById(multipleGraphConfigEdbMappingInfo.MultipleGraphConfigId)
  1687. // if e != nil {
  1688. // fmt.Println(edbInfo.EdbInfoId, ";获取相关性图表的配置信息失败, Err:"+e.Error())
  1689. // continue
  1690. // }
  1691. //
  1692. // // 曲线图配置
  1693. // var curveConf data_manage.CurveConfig
  1694. // e = json.Unmarshal([]byte(multipleGraphConfig.Curve), &curveConf)
  1695. // if e != nil {
  1696. // fmt.Println(edbInfo.EdbInfoId, ";格式化曲线图数据失败, Err:"+e.Error())
  1697. // continue
  1698. // }
  1699. //
  1700. // var formulaConf request.EdbCalculateFormula
  1701. // e = json.Unmarshal([]byte(edbInfo.CalculateFormula), &formulaConf)
  1702. // if e != nil {
  1703. // fmt.Println(edbInfo.EdbInfoId, ";格式化指标计算配置数据失败, Err:"+e.Error())
  1704. // continue
  1705. // }
  1706. // formulaConf.DateType = curveConf.DateType
  1707. // formulaConf.StartDate = curveConf.StartDate
  1708. // formulaConf.EndDate = curveConf.EndDate
  1709. // calculateFormulaByte, err := json.Marshal(formulaConf)
  1710. // edbInfo.CalculateFormula = string(calculateFormulaByte)
  1711. // err = edbInfo.Update([]string{"CalculateFormula"})
  1712. // if err != nil {
  1713. // fmt.Println(edbInfo.EdbInfoId, ";更新指标信息失败, Err:"+err.Error())
  1714. // continue
  1715. // }
  1716. // }
  1717. //
  1718. // fmt.Println("指标更新完成")
  1719. // }
  1720. //}