multiple_graph_config.go 59 KB

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