edb_info_calculate.go 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/services/data"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 计算指标
  16. type EdbInfoCalculateController struct {
  17. controllers.BaseAuthController
  18. }
  19. // @Title 生成计算指标
  20. // @Description 生成计算指标接口
  21. // @Param request body data_manage.EdbInfoCalculateSaveReq true "type json string"
  22. // @Success Ret=200 返回图表id
  23. // @router /edb_info/calculate/save [post]
  24. func (this *ChartInfoController) CalculateSave() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. var req data_manage.EdbInfoCalculateSaveReq
  38. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  39. if err != nil {
  40. br.Msg = "参数解析异常!"
  41. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  42. return
  43. }
  44. req.EdbName = strings.Trim(req.EdbName, " ")
  45. if req.EdbName == "" {
  46. br.Msg = "指标名称不能为空"
  47. return
  48. }
  49. if req.Frequency == "" {
  50. br.Msg = "频率不能为空"
  51. return
  52. }
  53. if req.Unit == "" {
  54. br.Msg = "单位不能为空"
  55. return
  56. }
  57. if req.ClassifyId <= 0 {
  58. br.Msg = "请选择分类"
  59. return
  60. }
  61. if len(req.EdbInfoIdArr) <= 0 {
  62. br.Msg = "请选择指标"
  63. return
  64. }
  65. if req.CalculateFormula == "" {
  66. br.Msg = "请填写指标"
  67. return
  68. }
  69. calculateFormula := req.CalculateFormula
  70. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  71. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  72. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  73. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  74. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  75. req.CalculateFormula = calculateFormula
  76. //判断是否重复指标
  77. edbInfoMap := make(map[int]string)
  78. //移除研究员选择指标中的未使用的指标
  79. {
  80. //转大写的计算公式
  81. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  82. //用到的指标
  83. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  84. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  85. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  86. if ok {
  87. br.Msg = "选择指标失败,请勿选择重复指标!"
  88. return
  89. }
  90. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  91. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  92. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  93. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  94. }
  95. }
  96. req.EdbInfoIdArr = newEdbInfoIdArr
  97. }
  98. var condition string
  99. var pars []interface{}
  100. condition += " AND edb_name=? "
  101. pars = append(pars, req.EdbName)
  102. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  103. if err != nil {
  104. br.Msg = "判断指标名称是否存在失败"
  105. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  106. return
  107. }
  108. if count > 0 {
  109. br.Msg = "指标名称已存在,请重新填写"
  110. br.ErrMsg = "指标名称已存在,请重新填写"
  111. br.IsSendEmail = false
  112. return
  113. }
  114. //检验公式
  115. var formulaStr string
  116. var edbInfoIdBytes []string
  117. for _, v := range req.EdbInfoIdArr {
  118. formulaStr += v.FromTag + ","
  119. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  120. }
  121. formulaMap := data.CheckFormula(req.CalculateFormula)
  122. for _, v := range formulaMap {
  123. if !strings.Contains(formulaStr, v) {
  124. br.Msg = "公式错误,请重新填写"
  125. return
  126. }
  127. }
  128. // 调用指标库去新增
  129. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  130. AdminId: sysUser.AdminId,
  131. AdminName: sysUser.RealName,
  132. //EdbInfoId: req.EdbInfoId,
  133. EdbName: req.EdbName,
  134. Frequency: req.Frequency,
  135. Unit: req.Unit,
  136. ClassifyId: req.ClassifyId,
  137. CalculateFormula: req.CalculateFormula,
  138. EdbInfoIdArr: req.EdbInfoIdArr,
  139. }
  140. reqJson, err := json.Marshal(req2)
  141. if err != nil {
  142. br.Msg = "参数解析异常!"
  143. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  144. return
  145. }
  146. respItem, err := data.AddEdbCalculateData(string(reqJson))
  147. if err != nil {
  148. br.Msg = "新增失败"
  149. br.ErrMsg = "新增失败,Err:" + err.Error()
  150. return
  151. }
  152. if respItem.Ret != 200 {
  153. br.Msg = respItem.Msg
  154. br.ErrMsg = respItem.ErrMsg
  155. return
  156. }
  157. resp := respItem.Data
  158. //添加es
  159. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  160. br.Ret = 200
  161. br.Success = true
  162. br.Msg = "保存成功"
  163. br.Data = resp
  164. br.IsAddLog = true
  165. }
  166. // @Title 获取计算指标详情
  167. // @Description 获取计算指标详情接口
  168. // @Param EdbInfoId query int true "指标id"
  169. // @Success 200 {object} data_manage.CalculateDetailResp
  170. // @router /edb_info/calculate/detail [get]
  171. func (this *ChartInfoController) CalculateDetail() {
  172. br := new(models.BaseResponse).Init()
  173. defer func() {
  174. this.Data["json"] = br
  175. this.ServeJSON()
  176. }()
  177. sysUser := this.SysUser
  178. if sysUser == nil {
  179. br.Msg = "请登录"
  180. br.ErrMsg = "请登录,SysUser Is Empty"
  181. br.Ret = 408
  182. return
  183. }
  184. edbInfoId, _ := this.GetInt("EdbInfoId")
  185. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  186. if err != nil {
  187. br.Msg = "获取失败"
  188. br.ErrMsg = "获取失败,Err:" + err.Error()
  189. return
  190. }
  191. calculateList, err := data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
  192. if err != nil {
  193. br.Msg = "获取失败"
  194. br.ErrMsg = "获取失败,Err:" + err.Error()
  195. return
  196. }
  197. fullEdb := new(data_manage.EdbInfoFullClassify)
  198. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  199. if err != nil {
  200. br.Msg = err.Error()
  201. br.ErrMsg = errMsg
  202. return
  203. }
  204. fullEdb.EdbInfo = edbInfo
  205. fullEdb.ClassifyList = classifyList
  206. resp := new(data_manage.CalculateDetailResp)
  207. resp.EdbInfoDetail = fullEdb
  208. resp.CalculateList = calculateList
  209. br.Ret = 200
  210. br.Success = true
  211. br.Msg = "保存成功"
  212. br.Data = resp
  213. }
  214. // @Title 修改计算指标
  215. // @Description 修改计算指标接口
  216. // @Param request body data_manage.EdbInfoCalculateEditReq true "type json string"
  217. // @Success Ret=200 返回图表id
  218. // @router /edb_info/calculate/edit [post]
  219. func (this *ChartInfoController) CalculateEdit() {
  220. br := new(models.BaseResponse).Init()
  221. defer func() {
  222. this.Data["json"] = br
  223. this.ServeJSON()
  224. }()
  225. sysUser := this.SysUser
  226. if sysUser == nil {
  227. br.Msg = "请登录"
  228. br.ErrMsg = "请登录,SysUser Is Empty"
  229. br.Ret = 408
  230. return
  231. }
  232. var req data_manage.EdbInfoCalculateEditReq
  233. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  234. if err != nil {
  235. br.Msg = "参数解析异常!"
  236. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  237. return
  238. }
  239. if req.EdbInfoId <= 0 {
  240. br.Msg = "参数错误"
  241. br.ErrMsg = "指标ID:" + strconv.Itoa(req.EdbInfoId)
  242. return
  243. }
  244. req.EdbName = strings.Trim(req.EdbName, " ")
  245. if req.EdbName == "" {
  246. br.Msg = "指标名称不能为空"
  247. return
  248. }
  249. if req.Frequency == "" {
  250. br.Msg = "频率不能为空"
  251. return
  252. }
  253. if req.Unit == "" {
  254. br.Msg = "单位不能为空"
  255. return
  256. }
  257. if req.ClassifyId <= 0 {
  258. br.Msg = "请选择分类"
  259. return
  260. }
  261. if len(req.EdbInfoIdArr) <= 0 {
  262. br.Msg = "请选择指标"
  263. return
  264. }
  265. if req.CalculateFormula == "" {
  266. br.Msg = "请填写指标"
  267. return
  268. }
  269. calculateFormula := req.CalculateFormula
  270. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  271. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  272. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  273. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  274. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  275. req.CalculateFormula = calculateFormula
  276. fmt.Println("calculateFormula:" + calculateFormula)
  277. //判断是否重复指标
  278. edbInfoMap := make(map[int]string)
  279. //移除研究员选择指标中的未使用的指标
  280. {
  281. //转大写的计算公式
  282. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  283. //用到的指标
  284. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  285. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  286. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  287. if ok {
  288. br.Msg = "选择指标失败,请勿选择重复指标!"
  289. return
  290. }
  291. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  292. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  293. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  294. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  295. }
  296. }
  297. req.EdbInfoIdArr = newEdbInfoIdArr
  298. }
  299. var condition string
  300. var pars []interface{}
  301. condition += " AND edb_name=? "
  302. pars = append(pars, req.EdbName)
  303. condition += " AND edb_info_id<>? "
  304. pars = append(pars, req.EdbInfoId)
  305. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  306. if err != nil {
  307. br.Msg = "判断指标名称是否存在失败"
  308. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  309. return
  310. }
  311. if count > 0 {
  312. br.Msg = "指标名称已存在,请重新填写"
  313. br.ErrMsg = "指标名称已存在,请重新填写"
  314. br.IsSendEmail = false
  315. return
  316. }
  317. //判断公式,指标是否有改动
  318. edbInfoDetail, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  319. if err != nil {
  320. br.Msg = "修改失败"
  321. br.Msg = "获取指标信息失败,GetEdbInfoById Err:" + err.Error()
  322. return
  323. }
  324. if edbInfoDetail == nil {
  325. br.Msg = "修改失败"
  326. br.Msg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  327. return
  328. }
  329. // 调用指标库去更新
  330. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  331. AdminId: sysUser.AdminId,
  332. AdminName: sysUser.RealName,
  333. EdbInfoId: req.EdbInfoId,
  334. EdbName: req.EdbName,
  335. Frequency: req.Frequency,
  336. Unit: req.Unit,
  337. ClassifyId: req.ClassifyId,
  338. CalculateFormula: req.CalculateFormula,
  339. EdbInfoIdArr: req.EdbInfoIdArr,
  340. }
  341. reqJson, err := json.Marshal(req2)
  342. if err != nil {
  343. br.Msg = "参数解析异常!"
  344. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  345. return
  346. }
  347. respItem, err := data.EditEdbCalculateData(string(reqJson))
  348. if err != nil {
  349. br.Msg = "编辑失败"
  350. br.ErrMsg = "编辑失败,Err:" + err.Error()
  351. return
  352. }
  353. if respItem.Ret != 200 {
  354. br.Msg = respItem.Msg
  355. br.ErrMsg = respItem.ErrMsg
  356. return
  357. }
  358. resp := respItem.Data
  359. //添加es
  360. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  361. // 修改关联的预测指标基础信息
  362. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfoDetail)
  363. br.Ret = 200
  364. br.Success = true
  365. br.Msg = "保存成功"
  366. br.IsAddLog = true
  367. br.Data = resp
  368. }
  369. // CalculateBatchSave
  370. // @Title 累计值转月-同比值-同差等计算新增
  371. // @Description 累计值转月-同比值-同差等计算新增接口
  372. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  373. // @Success Ret=200 返回指标id
  374. // @router /edb_info/calculate/batch/save [post]
  375. func (this *ChartInfoController) CalculateBatchSave() {
  376. br := new(models.BaseResponse).Init()
  377. defer func() {
  378. if br.ErrMsg == "" {
  379. br.IsSendEmail = false
  380. }
  381. this.Data["json"] = br
  382. this.ServeJSON()
  383. }()
  384. sysUser := this.SysUser
  385. if sysUser == nil {
  386. br.Msg = "请登录"
  387. br.ErrMsg = "请登录,SysUser Is Empty"
  388. br.Ret = 408
  389. return
  390. }
  391. var req data_manage.EdbInfoCalculateBatchSaveReq
  392. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  393. if err != nil {
  394. br.Msg = "参数解析异常!"
  395. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  396. return
  397. }
  398. req.EdbName = strings.Trim(req.EdbName, " ")
  399. if req.EdbName == "" {
  400. br.Msg = "指标名称不能为空"
  401. return
  402. }
  403. if req.Frequency == "" {
  404. br.Msg = "频率不能为空"
  405. return
  406. }
  407. if req.Unit == "" {
  408. br.Msg = "单位不能为空"
  409. return
  410. }
  411. if req.ClassifyId <= 0 {
  412. br.Msg = "请选择分类"
  413. return
  414. }
  415. fromEdbInfoId := req.FromEdbInfoId
  416. var formulaInt int
  417. switch req.Source {
  418. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
  419. if req.Formula == "" {
  420. br.Msg = "请填写N值"
  421. return
  422. }
  423. formulaInt, _ = strconv.Atoi(req.Formula)
  424. if formulaInt <= 0 {
  425. br.Msg = "N值输入错误,请重新输入"
  426. return
  427. }
  428. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  429. //直接拼接指标
  430. //校验时间格式
  431. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  432. if err != nil {
  433. br.Msg = "拼接日期有误,请重新输入"
  434. return
  435. }
  436. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  437. //指标校验
  438. if len(req.EdbInfoIdArr) != 2 {
  439. br.Msg = "选择的指标异常,请重新选择"
  440. return
  441. }
  442. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  443. //校验时间格式
  444. //数据格式:2022-11-01,2022-11-10
  445. timeList := strings.Split(req.Formula, ",")
  446. if len(timeList) != 2 {
  447. br.Msg = "选择时间有误,请重新输入"
  448. return
  449. }
  450. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  451. if err != nil {
  452. br.Msg = "开始日期有误,请重新输入"
  453. return
  454. }
  455. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  456. if err != nil {
  457. br.Msg = "结束日期有误,请重新输入"
  458. return
  459. }
  460. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  461. br.Msg = "日期间隔不得少于两天"
  462. return
  463. }
  464. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  465. if req.Formula == "" {
  466. br.Msg = "请填写alpha值"
  467. return
  468. }
  469. alpha, e := strconv.ParseFloat(req.Formula, 64)
  470. if e != nil {
  471. br.Msg = "alpha值输入错误, 请重新输入"
  472. return
  473. }
  474. if alpha <= 0 || alpha >= 1 {
  475. br.Msg = "alpha值输入错误, 请重新输入"
  476. return
  477. }
  478. }
  479. notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_CALCULATE_KSZS} // 不需要传入来源指标id的 指标类型
  480. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  481. br.Msg = "请选择指标"
  482. return
  483. }
  484. //加入缓存机制,避免创建同一个名称的指标 start
  485. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  486. isExist := utils.Rc.IsExist(redisKey)
  487. if isExist {
  488. br.Msg = "指标正在处理,请勿重复提交"
  489. return
  490. } else {
  491. //设置3分钟缓存
  492. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  493. defer func() {
  494. utils.Rc.Delete(redisKey)
  495. }()
  496. }
  497. //加入缓存机制,避免创建同一个名称的指标 end
  498. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  499. AdminId: sysUser.AdminId,
  500. AdminName: sysUser.RealName,
  501. EdbInfoId: req.EdbInfoId,
  502. EdbName: req.EdbName,
  503. Frequency: req.Frequency,
  504. Unit: req.Unit,
  505. ClassifyId: req.ClassifyId,
  506. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  507. FromEdbInfoId: req.FromEdbInfoId,
  508. CalculateFormula: req.CalculateFormula,
  509. Source: req.Source,
  510. MoveType: req.MoveType,
  511. MoveFrequency: req.MoveFrequency,
  512. //CalculateFormula: edbInfo.CalculateFormula,
  513. EdbInfoIdArr: req.EdbInfoIdArr,
  514. Calendar: req.Calendar,
  515. }
  516. // 调用指标库去更新
  517. reqJson, err := json.Marshal(req2)
  518. if err != nil {
  519. br.Msg = "参数解析异常!"
  520. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  521. return
  522. }
  523. respItem, err := data.BatchSaveEdbCalculateData(string(reqJson))
  524. if err != nil {
  525. br.Msg = "新增失败"
  526. br.ErrMsg = "新增失败,Err:" + err.Error()
  527. return
  528. }
  529. if respItem.Ret != 200 {
  530. br.Msg = respItem.Msg
  531. br.ErrMsg = respItem.ErrMsg
  532. return
  533. }
  534. resp := respItem.Data
  535. //添加es
  536. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  537. br.Ret = 200
  538. br.Success = true
  539. br.Msg = "保存成功"
  540. br.Data = resp
  541. br.IsAddLog = true
  542. }
  543. // @Title 累计值转月-同比值-同差等计算编辑
  544. // @Description 累计值转月-同比值-同差等计算编辑接口
  545. // @Param request body data_manage.EdbInfoCalculateBatchEditReq true "type json string"
  546. // @Success Ret=200 返回指标id
  547. // @router /edb_info/calculate/batch/edit [post]
  548. func (this *ChartInfoController) CalculateBatchEdit() {
  549. br := new(models.BaseResponse).Init()
  550. defer func() {
  551. if br.ErrMsg == "" {
  552. br.IsSendEmail = false
  553. }
  554. this.Data["json"] = br
  555. this.ServeJSON()
  556. }()
  557. sysUser := this.SysUser
  558. if sysUser == nil {
  559. br.Msg = "请登录"
  560. br.ErrMsg = "请登录,SysUser Is Empty"
  561. br.Ret = 408
  562. return
  563. }
  564. var req data_manage.EdbInfoCalculateBatchEditReq
  565. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  566. if err != nil {
  567. br.Msg = "参数解析异常!"
  568. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  569. return
  570. }
  571. if req.EdbName == "" {
  572. br.Msg = "指标名称不能为空"
  573. return
  574. }
  575. if req.Frequency == "" {
  576. br.Msg = "频率不能为空"
  577. return
  578. }
  579. if req.Unit == "" {
  580. br.Msg = "单位不能为空"
  581. return
  582. }
  583. if req.ClassifyId <= 0 {
  584. br.Msg = "请选择分类"
  585. return
  586. }
  587. if req.EdbInfoId <= 0 {
  588. br.Msg = "请选择指标"
  589. return
  590. }
  591. var condition string
  592. var pars []interface{}
  593. condition += " AND edb_name=? "
  594. pars = append(pars, req.EdbName)
  595. condition += " AND edb_info_id<>? "
  596. pars = append(pars, req.EdbInfoId)
  597. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  598. if err != nil {
  599. br.Msg = "判断指标名称是否存在失败"
  600. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  601. return
  602. }
  603. if count > 0 {
  604. br.Msg = "指标名称已存在,请重新填写"
  605. br.ErrMsg = "指标名称已存在,请重新填写"
  606. br.IsSendEmail = false
  607. return
  608. }
  609. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  610. if err != nil {
  611. if err.Error() == utils.ErrNoRow() {
  612. br.Msg = "指标已被删除,请刷新页面"
  613. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  614. return
  615. }
  616. br.Msg = "获取指标信息失败"
  617. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  618. return
  619. }
  620. if req.Source <= 0 {
  621. req.Source = edbInfo.Source
  622. }
  623. var formulaInt int
  624. switch req.Source {
  625. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
  626. if req.Formula == "" {
  627. br.Msg = "请填写N值"
  628. return
  629. }
  630. formulaInt, _ = strconv.Atoi(req.Formula)
  631. if formulaInt <= 0 {
  632. br.Msg = "N值输入错误,请重新输入"
  633. return
  634. }
  635. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  636. //直接拼接指标
  637. //校验时间格式
  638. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  639. if err != nil {
  640. br.Msg = "拼接日期有误,请重新输入"
  641. return
  642. }
  643. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  644. //指标校验
  645. if len(req.EdbInfoIdArr) != 2 {
  646. br.Msg = "选择的指标异常,请重新选择"
  647. return
  648. }
  649. //校验时间格式
  650. //数据格式:2022-11-01,2022-11-10
  651. timeList := strings.Split(req.Formula, ",")
  652. if len(timeList) != 2 {
  653. br.Msg = "选择时间有误,请重新输入"
  654. return
  655. }
  656. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  657. if err != nil {
  658. br.Msg = "开始日期有误,请重新输入"
  659. return
  660. }
  661. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  662. if err != nil {
  663. br.Msg = "结束日期有误,请重新输入"
  664. return
  665. }
  666. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  667. br.Msg = "日期间隔不得少于两天"
  668. return
  669. }
  670. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  671. if req.Formula == "" {
  672. br.Msg = "请填写alpha值"
  673. return
  674. }
  675. alpha, e := strconv.ParseFloat(req.Formula, 64)
  676. if e != nil {
  677. br.Msg = "alpha值输入错误, 请重新输入"
  678. return
  679. }
  680. if alpha <= 0 || alpha >= 1 {
  681. br.Msg = "alpha值输入错误, 请重新输入"
  682. return
  683. }
  684. }
  685. // 构造请求
  686. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  687. EdbInfoId: req.EdbInfoId,
  688. EdbName: req.EdbName,
  689. Frequency: req.Frequency,
  690. Unit: req.Unit,
  691. ClassifyId: req.ClassifyId,
  692. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  693. FromEdbInfoId: req.FromEdbInfoId,
  694. Source: req.Source,
  695. MoveType: req.MoveType,
  696. MoveFrequency: req.MoveFrequency,
  697. EdbInfoIdArr: req.EdbInfoIdArr,
  698. Calendar: req.Calendar,
  699. }
  700. // 调用指标库去更新
  701. reqJson, err := json.Marshal(req2)
  702. if err != nil {
  703. br.Msg = "参数解析异常!"
  704. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  705. return
  706. }
  707. respItem, err := data.BatchEditEdbCalculateData(string(reqJson))
  708. if err != nil {
  709. br.Msg = "编辑失败"
  710. br.ErrMsg = "编辑失败,Err:" + err.Error()
  711. return
  712. }
  713. if respItem.Ret != 200 {
  714. br.Msg = respItem.Msg
  715. br.ErrMsg = respItem.ErrMsg
  716. return
  717. }
  718. resp := respItem.Data
  719. //添加es
  720. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  721. // 修改关联的预测指标基础信息
  722. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfo)
  723. br.Ret = 200
  724. br.Success = true
  725. br.Msg = "保存成功"
  726. br.Data = resp
  727. br.IsAddLog = true
  728. }
  729. // @Title 累计值转月-同比值-同差等重新计算
  730. // @Description 累计值转月-同比值-同差等重新计算接口
  731. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  732. // @Success Ret=200 返回指标id
  733. // @router /edb_info/calculate/batch/reset [post]
  734. func (this *ChartInfoController) CalculateBatchReset() {
  735. br := new(models.BaseResponse).Init()
  736. defer func() {
  737. this.Data["json"] = br
  738. this.ServeJSON()
  739. }()
  740. sysUser := this.SysUser
  741. if sysUser == nil {
  742. br.Msg = "请登录"
  743. br.ErrMsg = "请登录,SysUser Is Empty"
  744. br.Ret = 408
  745. return
  746. }
  747. var req data_manage.EdbInfoCalculateBatchSaveReq
  748. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  749. if err != nil {
  750. br.Msg = "参数解析异常!"
  751. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  752. return
  753. }
  754. if req.EdbInfoId <= 0 {
  755. br.Msg = "参数错误"
  756. br.ErrMsg = "参数错误:EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  757. return
  758. }
  759. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  760. if err != nil {
  761. if err.Error() == utils.ErrNoRow() {
  762. br.Msg = "指标已被删除,请刷新页面"
  763. br.ErrMsg = "指标已删除,请刷新页面:Err:" + err.Error()
  764. return
  765. }
  766. br.Msg = "重新计算失败"
  767. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  768. return
  769. }
  770. uniqueCode := edbInfo.UniqueCode
  771. sourName := edbInfo.SourceName
  772. edbInfoId := edbInfo.EdbInfoId
  773. //if source == utils.DATA_SOURCE_CALCULATE {
  774. // //检验公式
  775. // var formulaStr string
  776. // var edbInfoIdBytes []string
  777. // for _, v := range fromEdbInfoList {
  778. // formulaStr += v.FromTag + ","
  779. // edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  780. // }
  781. // formulaStr = strings.Trim(formulaStr, ",")
  782. // formulaMap := data.CheckFormula(req.CalculateFormula)
  783. // for _, v := range formulaMap {
  784. // if !strings.Contains(formulaStr, v) {
  785. // br.Msg = "公式错误,请重新填写"
  786. // return
  787. // }
  788. // }
  789. //
  790. // edbInfoList := make([]*data_manage.EdbInfo, 0)
  791. //
  792. // for _, v := range fromEdbInfoList {
  793. // edbInfo, err := data_manage.GetEdbInfoById(v.FromEdbInfoId)
  794. // if err != nil {
  795. // if err.Error() == utils.ErrNoRow() {
  796. // br.Msg = "重新计算失败"
  797. // br.Msg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  798. // return
  799. // }
  800. // br.Msg = "重新计算失败"
  801. // br.Msg = "获取指标失败:Err:" + err.Error()
  802. // return
  803. // }
  804. // edbInfoList = append(edbInfoList, edbInfo)
  805. // }
  806. // //清除历史数据
  807. // err = data_manage.DeleteCalculateData(edbInfoId)
  808. // if err != nil {
  809. // br.Msg = "重新计算失败"
  810. // br.Msg = "获取指标失败:Err:" + err.Error()
  811. // return
  812. // }
  813. // err = data.Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  814. // if err != nil {
  815. // br.Msg = "重新计算失败"
  816. // br.Msg = "生成计算指标失败,Calculate Err:" + err.Error()
  817. // return
  818. // }
  819. //} else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  820. // _, err = data_manage.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  821. //} else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  822. // sourName = "同比值"
  823. // edbInfoId, err = data_manage.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  824. //} else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  825. // sourName = "同差值"
  826. // edbInfoId, err = data_manage.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  827. //} else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  828. // sourName = "N数值移动平均计算"
  829. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  830. // edbInfoId, err = data_manage.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  831. //} else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  832. // sourName = "环比值"
  833. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  834. // edbInfoId, err = data_manage.AddCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  835. //} else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  836. // sourName = "环差值"
  837. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  838. // edbInfoId, err = data_manage.AddCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  839. //} else if source == utils.DATA_SOURCE_CALCULATE_BP {
  840. // sourName = "变频"
  841. // edbInfoId, err = data_manage.AddCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  842. //} else if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
  843. // sourName = "时间移位"
  844. // if req.MoveFrequency == "" {
  845. // req.MoveFrequency = edbInfo.MoveFrequency
  846. // }
  847. // if req.Formula == "" {
  848. // req.Formula = edbInfo.CalculateFormula
  849. // }
  850. // edbInfoId, err = data_manage.AddCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  851. //} else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ {
  852. // sourName = "直接拼接"
  853. // err = data_manage.RefreshAllCalculateZjpj(edbInfo)
  854. //} else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ {
  855. // sourName = "累计值同比拼接"
  856. // err = data_manage.RefreshAllCalculateLjztbpj(edbInfo)
  857. //} else {
  858. // br.Msg = "无效计算方式"
  859. // br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(source)
  860. // return
  861. //}
  862. // 重新计算
  863. err, isAsync := data.EdbInfoRefreshAllFromBaseV2(edbInfoId, true)
  864. if err != nil {
  865. fmt.Println(edbInfoId, "RefreshEdbCalculateData err", time.Now())
  866. br.Msg = "重新计算失败"
  867. br.ErrMsg = "生成" + sourName + "失败,RefreshEdbCalculateData Err:" + err.Error()
  868. return
  869. }
  870. resp := new(data_manage.AddEdbInfoResp)
  871. resp.EdbInfoId = edbInfoId
  872. resp.UniqueCode = uniqueCode
  873. br.Ret = 200
  874. br.Success = true
  875. br.Msg = "重新计算成功"
  876. if isAsync {
  877. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  878. }
  879. br.Data = resp
  880. br.IsAddLog = true
  881. }
  882. //累计值转月值
  883. //func init() {
  884. // fmt.Println("start CalculateBatchSave")
  885. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  886. // req.EdbInfoId = 100282
  887. //
  888. // randStr := utils.GetRandDigit(4)
  889. // edbCode := `C` + time.Now().Format("060102") + randStr
  890. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  891. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  892. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.EdbInfoId)
  893. // data_manage.AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck")
  894. // fmt.Println("end CalculateBatchSave")
  895. // return
  896. //}
  897. ////同比值
  898. //func init() {
  899. // fmt.Println("start AddCalculateTbz")
  900. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  901. // req.EdbInfoId = 100092
  902. //
  903. // randStr := utils.GetRandDigit(4)
  904. // edbCode := `C` + time.Now().Format("060102") + randStr
  905. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  906. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  907. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.EdbInfoId)
  908. //
  909. // data_manage.AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck")
  910. // fmt.Println("end AddCalculateTbz")
  911. // return
  912. //}
  913. //同差值
  914. //func init() {
  915. // fmt.Println("start AddCalculateTbz")
  916. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  917. // req.EdbInfoId = 100092
  918. //
  919. // randStr := utils.GetRandDigit(4)
  920. // edbCode := `C` + time.Now().Format("060102") + randStr
  921. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  922. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  923. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.EdbInfoId)
  924. //
  925. // data_manage.AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck")
  926. //
  927. // fmt.Println("end AddCalculateTbz")
  928. // return
  929. //}
  930. //N数值移动平均计算
  931. //func init() {
  932. // fmt.Println("start AddCalculateTbz")
  933. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  934. // req.FromEdbInfoId = 100081
  935. //
  936. // randStr := utils.GetRandDigit(4)
  937. // edbCode := `C` + time.Now().Format("060102") + randStr
  938. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  939. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  940. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.FromEdbInfoId)
  941. // formulaInt := 3
  942. // fmt.Println("edbCode:", edbCode)
  943. // data_manage.AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck", formulaInt)
  944. //
  945. // fmt.Println("end AddCalculateTbz")
  946. // return
  947. //}
  948. // ExecPythonCode
  949. // @Title 执行python代码
  950. // @Description 执行python代码接口
  951. // @Param request body data_manage.EdbInfoCalculateSaveReq true "type json string"
  952. // @Success Ret=200 返回图表id
  953. // @router /edb_info/python/exec [post]
  954. func (this *ChartInfoController) ExecPythonCode() {
  955. br := new(models.BaseResponse).Init()
  956. defer func() {
  957. this.Data["json"] = br
  958. this.ServeJSON()
  959. }()
  960. sysUser := this.SysUser
  961. if sysUser == nil {
  962. br.Msg = "请登录"
  963. br.ErrMsg = "请登录,SysUser Is Empty"
  964. br.Ret = 408
  965. return
  966. }
  967. var req data_manage.ExecPythonEdbReq
  968. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  969. if err != nil {
  970. br.Msg = "参数解析异常!"
  971. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  972. return
  973. }
  974. if req.PythonCode == "" {
  975. br.Msg = "python代码不能为空"
  976. return
  977. }
  978. resp, err := data.ExecPythonCode(req.PythonCode)
  979. if err != nil {
  980. br.Msg = "python代码执行失败,请检查代码"
  981. br.ErrMsg = "python代码执行失败,Err:" + err.Error()
  982. return
  983. }
  984. if resp.Ret != 200 {
  985. br.Msg = "python代码执行失败,请检查代码"
  986. br.ErrMsg = "python代码执行失败,Err:" + resp.ErrMsg
  987. return
  988. }
  989. br.Ret = 200
  990. br.Success = true
  991. br.Msg = "运行成功"
  992. br.Data = resp.Data
  993. }
  994. // AddPythonEdb
  995. // @Title 添加python指标
  996. // @Description 添加python指标接口
  997. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  998. // @Success 200 {object} data_manage.AddEdbInfoResp
  999. // @router /edb_info/python/add [post]
  1000. func (this *ChartInfoController) AddPythonEdb() {
  1001. br := new(models.BaseResponse).Init()
  1002. defer func() {
  1003. this.Data["json"] = br
  1004. this.ServeJSON()
  1005. }()
  1006. sysUser := this.SysUser
  1007. if sysUser == nil {
  1008. br.Msg = "请登录"
  1009. br.ErrMsg = "请登录,SysUser Is Empty"
  1010. br.Ret = 408
  1011. return
  1012. }
  1013. var req data_manage.PostAddPythonEdbReq
  1014. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1015. if err != nil {
  1016. br.Msg = "参数解析异常!"
  1017. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1018. return
  1019. }
  1020. //传入操作人
  1021. req.AdminId = sysUser.AdminId
  1022. req.AdminName = sysUser.RealName
  1023. reqJson, err := json.Marshal(req)
  1024. if err != nil {
  1025. br.Msg = "参数解析异常!"
  1026. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1027. return
  1028. }
  1029. // 判断指标名称是否重复
  1030. {
  1031. var condition string
  1032. var pars []interface{}
  1033. condition += " AND edb_name=? "
  1034. pars = append(pars, req.EdbName)
  1035. condition += " AND edb_info_id<>? "
  1036. pars = append(pars, req.EdbInfoId)
  1037. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1038. if err != nil {
  1039. br.Msg = "判断指标名称是否存在失败"
  1040. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1041. return
  1042. }
  1043. if count > 0 {
  1044. br.Msg = "指标名称已存在,请重新填写"
  1045. br.ErrMsg = "指标名称已存在,请重新填写"
  1046. br.IsSendEmail = false
  1047. return
  1048. }
  1049. }
  1050. // 添加python指标
  1051. respItem, err := data.AddPythonEdbData(string(reqJson))
  1052. if err != nil {
  1053. br.Msg = "添加失败"
  1054. br.ErrMsg = "获取失败,Err:" + err.Error()
  1055. return
  1056. }
  1057. if respItem.Ret != 200 {
  1058. br.Msg = "添加失败"
  1059. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1060. return
  1061. }
  1062. respData := respItem.Data.(map[string]interface{})
  1063. tmpEdbInfoId, ok := respData["EdbInfoId"]
  1064. if !ok {
  1065. br.Msg = `数据转换异常`
  1066. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  1067. return
  1068. }
  1069. //指标id
  1070. edbInfoId := int64(tmpEdbInfoId.(float64))
  1071. uniqueCode, ok := respData["UniqueCode"]
  1072. if !ok {
  1073. br.Msg = `数据转换异常`
  1074. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  1075. return
  1076. }
  1077. //添加es
  1078. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  1079. //
  1080. resp := data_manage.AddEdbInfoResp{
  1081. EdbInfoId: int(edbInfoId),
  1082. UniqueCode: uniqueCode.(string),
  1083. }
  1084. br.Ret = 200
  1085. br.Success = true
  1086. br.Msg = "保存成功"
  1087. br.Data = resp
  1088. br.IsAddLog = true
  1089. }
  1090. // EditPythonEdb
  1091. // @Title 修改python运算指标
  1092. // @Description 修改python运算指标接口
  1093. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  1094. // @Success Ret=200 返回成功
  1095. // @router /edb_info/python/edit [post]
  1096. func (this *ChartInfoController) EditPythonEdb() {
  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 data_manage.PostAddPythonEdbReq
  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. //传入操作人
  1117. req.AdminId = sysUser.AdminId
  1118. req.AdminName = sysUser.RealName
  1119. reqJson, err := json.Marshal(req)
  1120. if err != nil {
  1121. br.Msg = "参数解析异常!"
  1122. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1123. return
  1124. }
  1125. // 判断指标名称是否重复
  1126. {
  1127. var condition string
  1128. var pars []interface{}
  1129. condition += " AND edb_name=? "
  1130. pars = append(pars, req.EdbName)
  1131. condition += " AND edb_info_id<>? "
  1132. pars = append(pars, req.EdbInfoId)
  1133. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1134. if err != nil {
  1135. br.Msg = "判断指标名称是否存在失败"
  1136. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1137. return
  1138. }
  1139. if count > 0 {
  1140. br.Msg = "指标名称已存在,请重新填写"
  1141. br.ErrMsg = "指标名称已存在,请重新填写"
  1142. br.IsSendEmail = false
  1143. return
  1144. }
  1145. }
  1146. //获取指标信息
  1147. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1148. if err != nil {
  1149. if err.Error() == utils.ErrNoRow() {
  1150. br.Msg = "指标已被删除,请刷新页面"
  1151. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  1152. return
  1153. }
  1154. br.Msg = "获取指标信息失败"
  1155. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1156. return
  1157. }
  1158. // 修改python运算指标
  1159. respItem, err := data.EditPythonEdbData(string(reqJson))
  1160. if err != nil {
  1161. br.Msg = "保存失败"
  1162. br.ErrMsg = "保存失败,Err:" + err.Error()
  1163. return
  1164. }
  1165. if respItem.Ret != 200 {
  1166. br.Msg = "保存失败"
  1167. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1168. return
  1169. }
  1170. //添加es
  1171. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  1172. resp := new(data_manage.AddEdbInfoResp)
  1173. resp.EdbInfoId = req.EdbInfoId
  1174. resp.UniqueCode = edbInfo.UniqueCode
  1175. br.Ret = 200
  1176. br.Success = true
  1177. br.Data = resp
  1178. br.Msg = "保存成功"
  1179. br.IsAddLog = true
  1180. }
  1181. // GetPythonEdbCode
  1182. // @Title 获取python代码
  1183. // @Description 获取python代码接口
  1184. // @Param EdbInfoId query int true "指标id"
  1185. // @Success 200 {object} data_manage.PythonEdbDetailResp
  1186. // @router /edb_info/python/detail [get]
  1187. func (this *ChartInfoController) GetPythonEdbCode() {
  1188. br := new(models.BaseResponse).Init()
  1189. defer func() {
  1190. this.Data["json"] = br
  1191. this.ServeJSON()
  1192. }()
  1193. sysUser := this.SysUser
  1194. if sysUser == nil {
  1195. br.Msg = "请登录"
  1196. br.ErrMsg = "请登录,SysUser Is Empty"
  1197. br.Ret = 408
  1198. return
  1199. }
  1200. edbInfoId, _ := this.GetInt("EdbInfoId")
  1201. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1202. if err != nil {
  1203. br.Msg = "获取失败"
  1204. br.ErrMsg = "获取失败,Err:" + err.Error()
  1205. return
  1206. }
  1207. if edbInfo.Source != utils.DATA_SOURCE_PYTHON {
  1208. br.Msg = "该指标不是Python运算指标"
  1209. br.ErrMsg = "该指标不是Python运算指标"
  1210. return
  1211. }
  1212. pythonCode, err := data_manage.GetEdbPythonCodeById(edbInfoId)
  1213. if err != nil {
  1214. br.Msg = "获取失败"
  1215. br.ErrMsg = "获取失败,Err:" + err.Error()
  1216. return
  1217. }
  1218. resp := data_manage.PythonEdbDetailResp{
  1219. EdbInfoDetail: edbInfo,
  1220. PythonCode: pythonCode.PythonCode,
  1221. }
  1222. br.Ret = 200
  1223. br.Success = true
  1224. br.Msg = "保存成功"
  1225. br.Data = resp
  1226. }
  1227. // QueryEdbDataTable
  1228. // @Title 指标筛选接口
  1229. // @Description 指标筛选接口
  1230. // @Param EdbInfoId query int false "指标id"
  1231. // @Success 200 {object} data_manage.EdbInfoList
  1232. // @router /edb_data/table/detail [get]
  1233. func (this *EdbInfoController) QueryEdbDataTable() {
  1234. br := new(models.BaseResponse).Init()
  1235. defer func() {
  1236. this.Data["json"] = br
  1237. this.ServeJSON()
  1238. }()
  1239. edbInfoId, _ := this.GetInt("EdbInfoId")
  1240. if edbInfoId <= 0 {
  1241. br.Msg = "请选择指标"
  1242. br.ErrMsg = "请选择指标"
  1243. return
  1244. }
  1245. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1246. if err != nil {
  1247. br.Msg = "获取指标信息失败"
  1248. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1249. return
  1250. }
  1251. tableName := data_manage.GetEdbDataTableName(edbInfo.Source)
  1252. columnList := []map[string]string{
  1253. {
  1254. "edb_code": "指标code",
  1255. },
  1256. {
  1257. "data_time": "指标日期",
  1258. },
  1259. {
  1260. "value": "指标对应的值",
  1261. },
  1262. }
  1263. templateStr := fmt.Sprintf("sql1 = f\"\"\"SELECT data_time,`value` FROM %s WHERE edb_code = '%s' ORDER BY data_time DESC;\"\"\"\nraw = pandas_fetch_all(sql1, db)", tableName, edbInfo.EdbCode)
  1264. info := data_manage.TableInfoResp{
  1265. ColumnList: columnList,
  1266. TableName: tableName,
  1267. TemplateStr: url.PathEscape(templateStr),
  1268. }
  1269. br.Ret = 200
  1270. br.Success = true
  1271. br.Msg = "获取成功"
  1272. br.Data = info
  1273. }
  1274. // SaveAdjustEdbInfo
  1275. // @Title 保存数据调整接口
  1276. // @Description 保存数据调整接口
  1277. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  1278. // @Success 200 {object} data_manage.AddEdbInfoResp
  1279. // @router /edb_info/adjust/save [post]
  1280. func (this *ChartInfoController) SaveAdjustEdbInfo() {
  1281. br := new(models.BaseResponse).Init()
  1282. defer func() {
  1283. this.Data["json"] = br
  1284. this.ServeJSON()
  1285. }()
  1286. sysUser := this.SysUser
  1287. if sysUser == nil {
  1288. br.Msg = "请登录"
  1289. br.ErrMsg = "请登录,SysUser Is Empty"
  1290. br.Ret = 408
  1291. return
  1292. }
  1293. var req data_manage.SaveAdjustEdbInfoReq
  1294. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1295. if err != nil {
  1296. br.Msg = "参数解析异常!"
  1297. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1298. return
  1299. }
  1300. //传入操作人
  1301. req.AdminId = sysUser.AdminId
  1302. req.AdminName = sysUser.RealName
  1303. reqJson, err := json.Marshal(req)
  1304. if err != nil {
  1305. br.Msg = "参数解析异常!"
  1306. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1307. return
  1308. }
  1309. // 判断指标名称是否重复
  1310. {
  1311. var condition string
  1312. var pars []interface{}
  1313. condition += " AND edb_name=? "
  1314. pars = append(pars, req.EdbName)
  1315. condition += " AND edb_info_id<>? "
  1316. pars = append(pars, req.EdbInfoId)
  1317. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1318. if err != nil {
  1319. br.Msg = "判断指标名称是否存在失败"
  1320. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1321. return
  1322. }
  1323. if count > 0 {
  1324. br.Msg = "指标名称已存在,请重新填写"
  1325. br.ErrMsg = "指标名称已存在,请重新填写"
  1326. br.IsSendEmail = false
  1327. return
  1328. }
  1329. }
  1330. // 添加数据调整指标
  1331. respItem, err := data.SaveAdjustEdbInfo(string(reqJson))
  1332. if err != nil {
  1333. br.Msg = "添加失败"
  1334. br.ErrMsg = "获取失败,Err:" + err.Error()
  1335. return
  1336. }
  1337. if respItem.Ret != 200 {
  1338. br.Msg = "添加失败"
  1339. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1340. return
  1341. }
  1342. respData := respItem.Data.(map[string]interface{})
  1343. tmpEdbInfoId, ok := respData["EdbInfoId"]
  1344. if !ok {
  1345. br.Msg = `数据转换异常`
  1346. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  1347. return
  1348. }
  1349. //指标id
  1350. edbInfoId := int64(tmpEdbInfoId.(float64))
  1351. uniqueCode, ok := respData["UniqueCode"]
  1352. if !ok {
  1353. br.Msg = `数据转换异常`
  1354. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  1355. return
  1356. }
  1357. //添加es
  1358. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  1359. //
  1360. resp := data_manage.AddEdbInfoResp{
  1361. EdbInfoId: int(edbInfoId),
  1362. UniqueCode: uniqueCode.(string),
  1363. }
  1364. br.Ret = 200
  1365. br.Success = true
  1366. br.Msg = "保存成功"
  1367. br.Data = resp
  1368. br.IsAddLog = true
  1369. }
  1370. // BatchCalculateBatchSave
  1371. // @Title 批量添加 累计值转月-同比值-同差等计算新增
  1372. // @Description 批量添加 累计值转月-同比值-同差等计算新增接口
  1373. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  1374. // @Success Ret=200 返回指标id
  1375. // @router /edb_info/calculate/batch/save/batch [post]
  1376. func (this *ChartInfoController) BatchCalculateBatchSave() {
  1377. br := new(models.BaseResponse).Init()
  1378. defer func() {
  1379. if br.ErrMsg == "" {
  1380. br.IsSendEmail = false
  1381. }
  1382. this.Data["json"] = br
  1383. this.ServeJSON()
  1384. }()
  1385. sysUser := this.SysUser
  1386. if sysUser == nil {
  1387. br.Msg = "请登录"
  1388. br.ErrMsg = "请登录,SysUser Is Empty"
  1389. br.Ret = 408
  1390. return
  1391. }
  1392. var reqList []data_manage.BatchEdbInfoCalculateBatchSaveReq
  1393. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqList)
  1394. if err != nil {
  1395. br.Msg = "参数解析异常!"
  1396. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1397. return
  1398. }
  1399. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  1400. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  1401. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  1402. }
  1403. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  1404. defer func() {
  1405. for _, redisKey := range redisKeyList {
  1406. utils.Rc.Delete(redisKey)
  1407. }
  1408. }()
  1409. for _, v := range reqList {
  1410. req := v.CalculateInfo
  1411. req.EdbName = strings.Trim(req.EdbName, " ")
  1412. if req.EdbName == "" {
  1413. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1414. CalculateId: v.CalculateId,
  1415. Msg: "指标名称不能为空",
  1416. })
  1417. continue
  1418. }
  1419. if req.Frequency == "" {
  1420. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1421. CalculateId: v.CalculateId,
  1422. Msg: "频率不能为空",
  1423. })
  1424. continue
  1425. }
  1426. if req.Unit == "" {
  1427. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1428. CalculateId: v.CalculateId,
  1429. Msg: "单位不能为空",
  1430. })
  1431. continue
  1432. }
  1433. if req.ClassifyId <= 0 {
  1434. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1435. CalculateId: v.CalculateId,
  1436. Msg: "请选择分类",
  1437. })
  1438. continue
  1439. }
  1440. fromEdbInfoId := req.FromEdbInfoId
  1441. var formulaInt int
  1442. switch req.Source {
  1443. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
  1444. if req.Formula == "" {
  1445. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1446. CalculateId: v.CalculateId,
  1447. Msg: "请选择分类",
  1448. })
  1449. continue
  1450. }
  1451. formulaInt, _ = strconv.Atoi(req.Formula)
  1452. if formulaInt <= 0 {
  1453. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1454. CalculateId: v.CalculateId,
  1455. Msg: "N值输入错误,请重新输入",
  1456. })
  1457. continue
  1458. }
  1459. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  1460. //直接拼接指标
  1461. //校验时间格式
  1462. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  1463. if err != nil {
  1464. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1465. CalculateId: v.CalculateId,
  1466. Msg: "拼接日期有误,请重新输入",
  1467. })
  1468. continue
  1469. }
  1470. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  1471. //指标校验
  1472. if len(req.EdbInfoIdArr) != 2 {
  1473. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1474. CalculateId: v.CalculateId,
  1475. Msg: "选择的指标异常,请重新选择",
  1476. })
  1477. continue
  1478. }
  1479. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  1480. //校验时间格式
  1481. //数据格式:2022-11-01,2022-11-10
  1482. timeList := strings.Split(req.Formula, ",")
  1483. if len(timeList) != 2 {
  1484. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1485. CalculateId: v.CalculateId,
  1486. Msg: "选择时间有误,请重新输入",
  1487. })
  1488. continue
  1489. }
  1490. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1491. if err != nil {
  1492. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1493. CalculateId: v.CalculateId,
  1494. Msg: "开始日期有误,请重新输入",
  1495. })
  1496. continue
  1497. }
  1498. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1499. if err != nil {
  1500. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1501. CalculateId: v.CalculateId,
  1502. Msg: "结束日期有误,请重新输入",
  1503. })
  1504. continue
  1505. }
  1506. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1507. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1508. CalculateId: v.CalculateId,
  1509. Msg: "日期间隔不得少于两天",
  1510. })
  1511. continue
  1512. }
  1513. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  1514. if req.Formula == "" {
  1515. br.Msg = "请填写alpha值"
  1516. return
  1517. }
  1518. alpha, e := strconv.ParseFloat(req.Formula, 64)
  1519. if e != nil {
  1520. br.Msg = "alpha值输入错误, 请重新输入"
  1521. return
  1522. }
  1523. if alpha <= 0 || alpha >= 1 {
  1524. br.Msg = "alpha值输入错误, 请重新输入"
  1525. return
  1526. }
  1527. }
  1528. if fromEdbInfoId <= 0 {
  1529. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1530. CalculateId: v.CalculateId,
  1531. Msg: "请选择指标",
  1532. })
  1533. continue
  1534. }
  1535. //加入缓存机制,避免创建同一个名称的指标 start
  1536. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", sysUser.AdminId, ":", req.Source, ":", v.CalculateId)
  1537. isExist := utils.Rc.IsExist(redisKey)
  1538. if isExist {
  1539. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1540. CalculateId: v.CalculateId,
  1541. Msg: "指标正在处理,请勿重复提交",
  1542. })
  1543. continue
  1544. } else {
  1545. //设置3分钟缓存
  1546. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  1547. redisKeyList = append(redisKeyList, redisKey)
  1548. }
  1549. //加入缓存机制,避免创建同一个名称的指标 end
  1550. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1551. AdminId: sysUser.AdminId,
  1552. AdminName: sysUser.RealName,
  1553. EdbInfoId: req.EdbInfoId,
  1554. EdbName: req.EdbName,
  1555. Frequency: req.Frequency,
  1556. Unit: req.Unit,
  1557. ClassifyId: req.ClassifyId,
  1558. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  1559. FromEdbInfoId: req.FromEdbInfoId,
  1560. CalculateFormula: req.CalculateFormula,
  1561. Source: req.Source,
  1562. MoveType: req.MoveType,
  1563. MoveFrequency: req.MoveFrequency,
  1564. //CalculateFormula: edbInfo.CalculateFormula,
  1565. EdbInfoIdArr: req.EdbInfoIdArr,
  1566. Calendar: req.Calendar,
  1567. }
  1568. // 调用指标库去更新
  1569. reqJson, err := json.Marshal(req2)
  1570. if err != nil {
  1571. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1572. CalculateId: v.CalculateId,
  1573. Msg: "参数解析异常!",
  1574. ErrMsg: "参数解析失败,Err:" + err.Error(),
  1575. })
  1576. continue
  1577. }
  1578. respItem, err := data.BatchSaveEdbCalculateData(string(reqJson))
  1579. if err != nil {
  1580. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1581. CalculateId: v.CalculateId,
  1582. Msg: "新增失败!",
  1583. ErrMsg: "新增失败,Err:" + err.Error(),
  1584. })
  1585. continue
  1586. }
  1587. if respItem.Ret != 200 {
  1588. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1589. CalculateId: v.CalculateId,
  1590. Msg: respItem.Msg,
  1591. ErrMsg: respItem.ErrMsg,
  1592. })
  1593. continue
  1594. }
  1595. result := respItem.Data
  1596. //添加es
  1597. data.AddOrEditEdbInfoToEs(result.EdbInfoId)
  1598. resp.Success = append(resp.Success, data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp{
  1599. ClassifyId: req.ClassifyId,
  1600. CalculateId: v.CalculateId,
  1601. EdbInfoId: result.EdbInfoId,
  1602. UniqueCode: result.UniqueCode,
  1603. })
  1604. }
  1605. br.Ret = 200
  1606. br.Success = true
  1607. br.Msg = "批量保存成功"
  1608. br.Data = resp
  1609. br.IsAddLog = true
  1610. }
  1611. // CalculateMapping
  1612. // @Title 获取计算指标与基础指标关联
  1613. // @Description 获取计算指标与基础指标关联
  1614. // @Param EdbInfoId query int true "指标id"
  1615. // @Success 200 {object} data_manage.CalculateDetailResp
  1616. // @router /edb_info/calculate/mapping [get]
  1617. func (this *ChartInfoController) CalculateMapping() {
  1618. br := new(models.BaseResponse).Init()
  1619. defer func() {
  1620. this.Data["json"] = br
  1621. this.ServeJSON()
  1622. }()
  1623. sysUser := this.SysUser
  1624. if sysUser == nil {
  1625. br.Msg = "请登录"
  1626. br.ErrMsg = "请登录,SysUser Is Empty"
  1627. br.Ret = 408
  1628. return
  1629. }
  1630. edbInfoId, _ := this.GetInt("EdbInfoId")
  1631. if edbInfoId <= 0 {
  1632. br.Msg = "参数有误"
  1633. return
  1634. }
  1635. item, e := data_manage.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1636. if e != nil {
  1637. br.Msg = "获取失败"
  1638. br.ErrMsg = "获取失败,Err:" + e.Error()
  1639. return
  1640. }
  1641. br.Ret = 200
  1642. br.Success = true
  1643. br.Msg = "保存成功"
  1644. br.Data = item
  1645. }
  1646. // CalculateComputeCorrelation
  1647. // @Title 拟合残差计算相关性
  1648. // @Description 拟合残差计算相关性接口
  1649. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  1650. // @Success Ret=200 返回指标id
  1651. // @router /edb_info/calculate/compute_correlation [post]
  1652. func (this *ChartInfoController) CalculateComputeCorrelation() {
  1653. br := new(models.BaseResponse).Init()
  1654. defer func() {
  1655. this.Data["json"] = br
  1656. this.ServeJSON()
  1657. }()
  1658. sysUser := this.SysUser
  1659. if sysUser == nil {
  1660. br.Msg = "请登录"
  1661. br.ErrMsg = "请登录,SysUser Is Empty"
  1662. br.Ret = 408
  1663. return
  1664. }
  1665. var req data_manage.EdbInfoCalculateBatchSaveReq
  1666. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1667. if err != nil {
  1668. br.Msg = "参数解析异常!"
  1669. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1670. return
  1671. }
  1672. if len(req.EdbInfoIdArr) != 2 {
  1673. br.Msg = "选择的指标异常,请重新选择"
  1674. return
  1675. }
  1676. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1677. //校验时间格式
  1678. //数据格式:2022-11-01,2022-11-10
  1679. timeList := strings.Split(req.Formula, ",")
  1680. if len(timeList) != 2 {
  1681. br.Msg = "选择时间有误,请重新输入"
  1682. return
  1683. }
  1684. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1685. if err != nil {
  1686. br.Msg = "开始日期有误,请重新输入"
  1687. return
  1688. }
  1689. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1690. if err != nil {
  1691. br.Msg = "结束日期有误,请重新输入"
  1692. return
  1693. }
  1694. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1695. br.Msg = "日期间隔不得少于两天"
  1696. return
  1697. }
  1698. if fromEdbInfoId <= 0 {
  1699. br.Msg = "请选择指标"
  1700. return
  1701. }
  1702. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1703. AdminId: sysUser.AdminId,
  1704. AdminName: sysUser.RealName,
  1705. EdbInfoId: req.EdbInfoId,
  1706. EdbName: req.EdbName,
  1707. Frequency: req.Frequency,
  1708. Unit: req.Unit,
  1709. ClassifyId: req.ClassifyId,
  1710. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  1711. FromEdbInfoId: req.FromEdbInfoId,
  1712. CalculateFormula: req.CalculateFormula,
  1713. Source: req.Source,
  1714. MoveType: req.MoveType,
  1715. MoveFrequency: req.MoveFrequency,
  1716. //CalculateFormula: edbInfo.CalculateFormula,
  1717. EdbInfoIdArr: req.EdbInfoIdArr,
  1718. Calendar: req.Calendar,
  1719. }
  1720. // 调用指标库去更新
  1721. reqJson, err := json.Marshal(req2)
  1722. if err != nil {
  1723. br.Msg = "参数解析异常!"
  1724. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1725. return
  1726. }
  1727. respItem, err := data.CalculateComputeCorrelation(string(reqJson))
  1728. if err != nil {
  1729. br.Msg = "新增失败"
  1730. br.ErrMsg = "新增失败,Err:" + err.Error()
  1731. return
  1732. }
  1733. if respItem.Ret == 200 {
  1734. br.Data = respItem.Data
  1735. } else {
  1736. br.Data = ``
  1737. }
  1738. br.Ret = 200
  1739. br.Success = true
  1740. br.Msg = "计算成功"
  1741. br.IsAddLog = true
  1742. }