edb_info_calculate.go 53 KB

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