edb_info_calculate.go 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/services/data"
  8. "eta_gn/eta_api/services/data/data_manage_permission"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. )
  17. // 计算指标
  18. type EdbInfoCalculateController struct {
  19. controllers.BaseAuthController
  20. }
  21. // @Title 生成计算指标
  22. // @Description 生成计算指标接口
  23. // @Param request body data_manage.EdbInfoCalculateSaveReq true "type json string"
  24. // @Success Ret=200 返回图表id
  25. // @router /edb_info/calculate/save [post]
  26. func (this *ChartInfoController) CalculateSave() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. sysUser := this.SysUser
  33. if sysUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录,SysUser Is Empty"
  36. br.Ret = 408
  37. return
  38. }
  39. var req data_manage.EdbInfoCalculateSaveReq
  40. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  41. if err != nil {
  42. br.Msg = "参数解析异常!"
  43. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  44. return
  45. }
  46. req.EdbName = strings.Trim(req.EdbName, " ")
  47. if req.EdbName == "" {
  48. br.Msg = "指标名称不能为空"
  49. return
  50. }
  51. if req.Frequency == "" {
  52. br.Msg = "频率不能为空"
  53. return
  54. }
  55. if req.Unit == "" {
  56. br.Msg = "单位不能为空"
  57. return
  58. }
  59. if req.ClassifyId <= 0 {
  60. br.Msg = "请选择分类"
  61. return
  62. }
  63. if len(req.EdbInfoIdArr) <= 0 {
  64. br.Msg = "请选择指标"
  65. return
  66. }
  67. if req.CalculateFormula == "" {
  68. br.Msg = "请填写指标"
  69. return
  70. }
  71. calculateFormula := req.CalculateFormula
  72. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  73. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  74. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  75. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  76. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  77. req.CalculateFormula = calculateFormula
  78. //判断是否重复指标
  79. edbInfoMap := make(map[int]string)
  80. //移除研究员选择指标中的未使用的指标
  81. {
  82. //转大写的计算公式
  83. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  84. //用到的指标
  85. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  86. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  87. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  88. if ok {
  89. br.Msg = "选择指标失败,请勿选择重复指标!"
  90. return
  91. }
  92. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  93. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  94. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  95. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  96. }
  97. }
  98. req.EdbInfoIdArr = newEdbInfoIdArr
  99. }
  100. var condition string
  101. var pars []interface{}
  102. condition += " AND edb_name=? "
  103. pars = append(pars, req.EdbName)
  104. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  105. if err != nil {
  106. br.Msg = "判断指标名称是否存在失败"
  107. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  108. return
  109. }
  110. if count > 0 {
  111. br.Msg = "指标名称已存在,请重新填写"
  112. br.ErrMsg = "指标名称已存在,请重新填写"
  113. br.IsSendEmail = false
  114. return
  115. }
  116. //检验公式
  117. var formulaStr string
  118. var edbInfoIdBytes []string
  119. for _, v := range req.EdbInfoIdArr {
  120. formulaStr += v.FromTag + ","
  121. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  122. }
  123. formulaSlice, err := data.CheckFormulaJson(req.CalculateFormula)
  124. if err != nil {
  125. br.Msg = "公式格式错误,请重新填写"
  126. return
  127. }
  128. for _, formula := range formulaSlice {
  129. formulaMap := data.CheckFormula(formula)
  130. for _, v := range formulaMap {
  131. if !strings.Contains(formulaStr, v) {
  132. br.Msg = "公式错误,请重新填写"
  133. return
  134. }
  135. }
  136. }
  137. // 调用指标库去新增
  138. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  139. AdminId: sysUser.AdminId,
  140. AdminName: sysUser.RealName,
  141. //EdbInfoId: req.EdbInfoId,
  142. EdbName: req.EdbName,
  143. Frequency: req.Frequency,
  144. Unit: req.Unit,
  145. ClassifyId: req.ClassifyId,
  146. CalculateFormula: req.CalculateFormula,
  147. EdbInfoIdArr: req.EdbInfoIdArr,
  148. EmptyType: req.EmptyType,
  149. MaxEmptyType: req.MaxEmptyType,
  150. Extra: req.Extra,
  151. }
  152. reqJson, err := json.Marshal(req2)
  153. if err != nil {
  154. br.Msg = "参数解析异常!"
  155. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  156. return
  157. }
  158. respItem, err := data.AddEdbCalculateData(string(reqJson), this.Lang)
  159. if err != nil {
  160. br.Msg = "新增失败"
  161. br.ErrMsg = "新增失败,Err:" + err.Error()
  162. return
  163. }
  164. if respItem.Ret != 200 {
  165. br.Msg = respItem.Msg
  166. br.ErrMsg = respItem.ErrMsg
  167. return
  168. }
  169. resp := respItem.Data
  170. //添加es
  171. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  172. br.Ret = 200
  173. br.Success = true
  174. br.Msg = "保存成功"
  175. br.Data = resp
  176. br.IsAddLog = true
  177. }
  178. // CalculateDetail
  179. // @Title 获取计算指标详情
  180. // @Description 获取计算指标详情接口
  181. // @Param EdbInfoId query int true "指标id"
  182. // @Success 200 {object} data_manage.CalculateDetailResp
  183. // @router /edb_info/calculate/detail [get]
  184. func (this *ChartInfoController) CalculateDetail() {
  185. br := new(models.BaseResponse).Init()
  186. defer func() {
  187. this.Data["json"] = br
  188. this.ServeJSON()
  189. }()
  190. sysUser := this.SysUser
  191. if sysUser == nil {
  192. br.Msg = "请登录"
  193. br.ErrMsg = "请登录,SysUser Is Empty"
  194. br.Ret = 408
  195. return
  196. }
  197. edbInfoId, _ := this.GetInt("EdbInfoId")
  198. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  199. if err != nil {
  200. br.Msg = "获取失败"
  201. br.ErrMsg = "获取失败,Err:" + err.Error()
  202. return
  203. }
  204. calculateList, err := data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
  205. if err != nil {
  206. br.Msg = "获取失败"
  207. br.ErrMsg = "获取失败,Err:" + err.Error()
  208. return
  209. }
  210. // 日期转换
  211. for _, v := range calculateList {
  212. v.ConvertToResp()
  213. }
  214. fullEdb := new(data_manage.EdbInfoFullClassify)
  215. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  216. if err != nil {
  217. br.Msg = err.Error()
  218. br.ErrMsg = errMsg
  219. return
  220. }
  221. edbInfoResp, err := edbInfo.ConvertToResp()
  222. if err != nil {
  223. br.Msg = err.Error()
  224. br.ErrMsg = errMsg
  225. return
  226. }
  227. fullEdb.EdbInfoResp = edbInfoResp
  228. fullEdb.ClassifyList = classifyList
  229. var currClassifyItem *data_manage.EdbClassifyIdItems
  230. for _, v := range classifyList {
  231. if v.ClassifyId == edbInfo.ClassifyId {
  232. currClassifyItem = v
  233. }
  234. }
  235. if currClassifyItem != nil {
  236. haveOperaAuth, err := data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassifyItem.IsJoinPermission, sysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  237. if err != nil {
  238. br.Msg = err.Error()
  239. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  240. return
  241. }
  242. fullEdb.HaveOperaAuth = haveOperaAuth
  243. }
  244. // 拟合残差计算相关系数
  245. if fullEdb.HaveOperaAuth && edbInfo.Source == utils.DATA_SOURCE_CALCULATE_NHCC {
  246. var aEdbInfo, bEdbInfo *data_manage.EdbInfoCalculateDetail
  247. for _, v := range calculateList {
  248. if v.FromTag == "A" {
  249. aEdbInfo = v
  250. continue
  251. }
  252. if v.FromTag == "B" {
  253. bEdbInfo = v
  254. continue
  255. }
  256. }
  257. if aEdbInfo != nil && bEdbInfo != nil {
  258. edbInfoFromTagList := []data_manage.EdbInfoFromTag{
  259. {
  260. EdbInfoId: aEdbInfo.FromEdbInfoId,
  261. FromTag: aEdbInfo.FromTag,
  262. MoveValue: aEdbInfo.MoveValue,
  263. }, {
  264. EdbInfoId: bEdbInfo.FromEdbInfoId,
  265. FromTag: bEdbInfo.FromTag,
  266. MoveValue: bEdbInfo.MoveValue,
  267. },
  268. }
  269. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  270. AdminId: sysUser.AdminId,
  271. AdminName: sysUser.RealName,
  272. Formula: edbInfo.CalculateFormula,
  273. EdbInfoIdArr: edbInfoFromTagList,
  274. }
  275. // 计算
  276. val, err, _ := data.CallCalculateComputeCorrelation(req2, this.Lang)
  277. if err == nil {
  278. fullEdb.CorrelationStr = val
  279. }
  280. }
  281. }
  282. resp := new(data_manage.CalculateDetailResp)
  283. resp.EdbInfoDetail = fullEdb
  284. resp.CalculateList = calculateList
  285. br.Ret = 200
  286. br.Success = true
  287. br.Msg = "保存成功"
  288. br.Data = resp
  289. }
  290. // @Title 修改计算指标
  291. // @Description 修改计算指标接口
  292. // @Param request body data_manage.EdbInfoCalculateEditReq true "type json string"
  293. // @Success Ret=200 返回图表id
  294. // @router /edb_info/calculate/edit [post]
  295. func (this *ChartInfoController) CalculateEdit() {
  296. br := new(models.BaseResponse).Init()
  297. defer func() {
  298. this.Data["json"] = br
  299. this.ServeJSON()
  300. }()
  301. sysUser := this.SysUser
  302. if sysUser == nil {
  303. br.Msg = "请登录"
  304. br.ErrMsg = "请登录,SysUser Is Empty"
  305. br.Ret = 408
  306. return
  307. }
  308. var req data_manage.EdbInfoCalculateEditReq
  309. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  310. if err != nil {
  311. br.Msg = "参数解析异常!"
  312. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  313. return
  314. }
  315. if req.EdbInfoId <= 0 {
  316. br.Msg = "参数错误"
  317. br.ErrMsg = "指标ID:" + strconv.Itoa(req.EdbInfoId)
  318. return
  319. }
  320. req.EdbName = strings.Trim(req.EdbName, " ")
  321. if req.EdbName == "" {
  322. br.Msg = "指标名称不能为空"
  323. return
  324. }
  325. if req.Frequency == "" {
  326. br.Msg = "频率不能为空"
  327. return
  328. }
  329. if req.Unit == "" {
  330. br.Msg = "单位不能为空"
  331. return
  332. }
  333. if req.ClassifyId <= 0 {
  334. br.Msg = "请选择分类"
  335. return
  336. }
  337. if len(req.EdbInfoIdArr) <= 0 {
  338. br.Msg = "请选择指标"
  339. return
  340. }
  341. if req.CalculateFormula == "" {
  342. br.Msg = "请填写指标"
  343. return
  344. }
  345. calculateFormula := req.CalculateFormula
  346. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  347. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  348. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  349. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  350. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  351. req.CalculateFormula = calculateFormula
  352. fmt.Println("calculateFormula:" + calculateFormula)
  353. //判断是否重复指标
  354. edbInfoMap := make(map[int]string)
  355. //移除研究员选择指标中的未使用的指标
  356. {
  357. //转大写的计算公式
  358. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  359. //用到的指标
  360. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  361. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  362. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  363. if ok {
  364. br.Msg = "选择指标失败,请勿选择重复指标!"
  365. return
  366. }
  367. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  368. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  369. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  370. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  371. }
  372. }
  373. req.EdbInfoIdArr = newEdbInfoIdArr
  374. }
  375. var condition string
  376. var pars []interface{}
  377. condition += " AND edb_name=? "
  378. pars = append(pars, req.EdbName)
  379. condition += " AND edb_info_id<>? "
  380. pars = append(pars, req.EdbInfoId)
  381. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  382. if err != nil {
  383. br.Msg = "判断指标名称是否存在失败"
  384. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  385. return
  386. }
  387. if count > 0 {
  388. br.Msg = "指标名称已存在,请重新填写"
  389. br.ErrMsg = "指标名称已存在,请重新填写"
  390. br.IsSendEmail = false
  391. return
  392. }
  393. //判断公式,指标是否有改动
  394. edbInfoDetail, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  395. if err != nil {
  396. br.Msg = "修改失败"
  397. br.Msg = "获取指标信息失败,GetEdbInfoById Err:" + err.Error()
  398. return
  399. }
  400. if edbInfoDetail == nil {
  401. br.Msg = "修改失败"
  402. br.Msg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  403. return
  404. }
  405. // 调用指标库去更新
  406. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  407. AdminId: sysUser.AdminId,
  408. AdminName: sysUser.RealName,
  409. EdbInfoId: req.EdbInfoId,
  410. EdbName: req.EdbName,
  411. Frequency: req.Frequency,
  412. Unit: req.Unit,
  413. ClassifyId: req.ClassifyId,
  414. CalculateFormula: req.CalculateFormula,
  415. EdbInfoIdArr: req.EdbInfoIdArr,
  416. EmptyType: req.EmptyType,
  417. MaxEmptyType: req.MaxEmptyType,
  418. Extra: req.Extra,
  419. }
  420. reqJson, err := json.Marshal(req2)
  421. if err != nil {
  422. br.Msg = "参数解析异常!"
  423. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  424. return
  425. }
  426. respItem, err := data.EditEdbCalculateData(string(reqJson), this.Lang)
  427. if err != nil {
  428. br.Msg = "编辑失败"
  429. br.ErrMsg = "编辑失败,Err:" + err.Error()
  430. return
  431. }
  432. if respItem.Ret != 200 {
  433. br.Msg = respItem.Msg
  434. br.ErrMsg = respItem.ErrMsg
  435. return
  436. }
  437. resp := respItem.Data
  438. //添加es
  439. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  440. // 修改关联的预测指标基础信息
  441. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfoDetail, req.Frequency, req.Unit)
  442. br.Ret = 200
  443. br.Success = true
  444. br.Msg = "保存成功"
  445. br.IsAddLog = true
  446. br.Data = resp
  447. }
  448. // CalculateBatchSave
  449. // @Title 累计值转月-同比值-同差等计算新增
  450. // @Description 累计值转月-同比值-同差等计算新增接口
  451. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  452. // @Success Ret=200 返回指标id
  453. // @router /edb_info/calculate/batch/save [post]
  454. func (this *ChartInfoController) CalculateBatchSave() {
  455. br := new(models.BaseResponse).Init()
  456. defer func() {
  457. if br.ErrMsg == "" {
  458. br.IsSendEmail = false
  459. }
  460. this.Data["json"] = br
  461. this.ServeJSON()
  462. }()
  463. sysUser := this.SysUser
  464. if sysUser == nil {
  465. br.Msg = "请登录"
  466. br.ErrMsg = "请登录,SysUser Is Empty"
  467. br.Ret = 408
  468. return
  469. }
  470. var req data_manage.EdbInfoCalculateBatchSaveReq
  471. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  472. if err != nil {
  473. br.Msg = "参数解析异常!"
  474. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  475. return
  476. }
  477. req.EdbName = strings.Trim(req.EdbName, " ")
  478. if req.EdbName == "" {
  479. br.Msg = "指标名称不能为空"
  480. return
  481. }
  482. if req.Frequency == "" {
  483. br.Msg = "频率不能为空"
  484. return
  485. }
  486. if req.Unit == "" {
  487. br.Msg = "单位不能为空"
  488. return
  489. }
  490. if req.ClassifyId <= 0 {
  491. br.Msg = "请选择分类"
  492. return
  493. }
  494. fromEdbInfoId := req.FromEdbInfoId
  495. var formulaInt int
  496. switch req.Source {
  497. 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:
  498. if req.Formula == "" {
  499. br.Msg = "请填写N值"
  500. return
  501. }
  502. formulaInt, _ = strconv.Atoi(req.Formula)
  503. if formulaInt <= 0 {
  504. br.Msg = "N值输入错误,请重新输入"
  505. return
  506. }
  507. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  508. //直接拼接指标
  509. //校验时间格式
  510. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  511. if err != nil {
  512. br.Msg = "拼接日期有误,请重新输入"
  513. return
  514. }
  515. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  516. //指标校验
  517. if len(req.EdbInfoIdArr) != 2 {
  518. br.Msg = "选择的指标异常,请重新选择"
  519. return
  520. }
  521. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  522. //校验时间格式
  523. //数据格式:2022-11-01,2022-11-10
  524. timeList := strings.Split(req.Formula, ",")
  525. if len(timeList) != 2 {
  526. br.Msg = "选择时间有误,请重新输入"
  527. return
  528. }
  529. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  530. if err != nil {
  531. br.Msg = "开始日期有误,请重新输入"
  532. return
  533. }
  534. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  535. if err != nil {
  536. br.Msg = "结束日期有误,请重新输入"
  537. return
  538. }
  539. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  540. br.Msg = "日期间隔不得少于两天"
  541. return
  542. }
  543. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  544. if req.Formula == "" {
  545. br.Msg = "请填写alpha值"
  546. return
  547. }
  548. alpha, e := strconv.ParseFloat(req.Formula, 64)
  549. if e != nil {
  550. br.Msg = "alpha值输入错误, 请重新输入"
  551. return
  552. }
  553. if alpha <= 0 || alpha >= 1 {
  554. br.Msg = "alpha值输入错误, 请重新输入"
  555. return
  556. }
  557. }
  558. notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_CALCULATE_KSZS} // 不需要传入来源指标id的 指标类型
  559. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  560. br.Msg = "请选择指标"
  561. return
  562. }
  563. //加入缓存机制,避免创建同一个名称的指标 start
  564. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  565. isExist := utils.Rc.IsExist(redisKey)
  566. if isExist {
  567. br.Msg = "指标正在处理,请勿重复提交"
  568. return
  569. } else {
  570. //设置3分钟缓存
  571. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  572. defer func() {
  573. utils.Rc.Delete(redisKey)
  574. }()
  575. }
  576. //加入缓存机制,避免创建同一个名称的指标 end
  577. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  578. AdminId: sysUser.AdminId,
  579. AdminName: sysUser.RealName,
  580. EdbInfoId: req.EdbInfoId,
  581. EdbName: req.EdbName,
  582. Frequency: req.Frequency,
  583. Unit: req.Unit,
  584. ClassifyId: req.ClassifyId,
  585. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  586. FromEdbInfoId: req.FromEdbInfoId,
  587. CalculateFormula: req.CalculateFormula,
  588. Source: req.Source,
  589. MoveType: req.MoveType,
  590. MoveFrequency: req.MoveFrequency,
  591. //CalculateFormula: edbInfo.CalculateFormula,
  592. EdbInfoIdArr: req.EdbInfoIdArr,
  593. Calendar: req.Calendar,
  594. Extra: req.Extra,
  595. EmptyType: req.EmptyType,
  596. }
  597. // 调用指标库去更新
  598. reqJson, err := json.Marshal(req2)
  599. if err != nil {
  600. br.Msg = "参数解析异常!"
  601. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  602. return
  603. }
  604. respItem, err := data.BatchSaveEdbCalculateData(string(reqJson), this.Lang)
  605. if err != nil {
  606. br.Msg = "新增失败"
  607. br.ErrMsg = "新增失败,Err:" + err.Error()
  608. return
  609. }
  610. if respItem.Ret != 200 {
  611. br.Msg = respItem.Msg
  612. br.ErrMsg = respItem.ErrMsg
  613. return
  614. }
  615. resp := respItem.Data
  616. //添加es
  617. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  618. br.Ret = 200
  619. br.Success = true
  620. br.Msg = "保存成功"
  621. br.Data = resp
  622. br.IsAddLog = true
  623. }
  624. // @Title 累计值转月-同比值-同差等计算编辑
  625. // @Description 累计值转月-同比值-同差等计算编辑接口
  626. // @Param request body data_manage.EdbInfoCalculateBatchEditReq true "type json string"
  627. // @Success Ret=200 返回指标id
  628. // @router /edb_info/calculate/batch/edit [post]
  629. func (this *ChartInfoController) CalculateBatchEdit() {
  630. br := new(models.BaseResponse).Init()
  631. defer func() {
  632. if br.ErrMsg == "" {
  633. br.IsSendEmail = false
  634. }
  635. this.Data["json"] = br
  636. this.ServeJSON()
  637. }()
  638. sysUser := this.SysUser
  639. if sysUser == nil {
  640. br.Msg = "请登录"
  641. br.ErrMsg = "请登录,SysUser Is Empty"
  642. br.Ret = 408
  643. return
  644. }
  645. var req data_manage.EdbInfoCalculateBatchEditReq
  646. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  647. if err != nil {
  648. br.Msg = "参数解析异常!"
  649. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  650. return
  651. }
  652. if req.EdbName == "" {
  653. br.Msg = "指标名称不能为空"
  654. return
  655. }
  656. if req.Frequency == "" {
  657. br.Msg = "频率不能为空"
  658. return
  659. }
  660. if req.Unit == "" {
  661. br.Msg = "单位不能为空"
  662. return
  663. }
  664. if req.ClassifyId <= 0 {
  665. br.Msg = "请选择分类"
  666. return
  667. }
  668. if req.EdbInfoId <= 0 {
  669. br.Msg = "请选择指标"
  670. return
  671. }
  672. var condition string
  673. var pars []interface{}
  674. condition += " AND edb_name=? "
  675. pars = append(pars, req.EdbName)
  676. condition += " AND edb_info_id<>? "
  677. pars = append(pars, req.EdbInfoId)
  678. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  679. if err != nil {
  680. br.Msg = "判断指标名称是否存在失败"
  681. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  682. return
  683. }
  684. if count > 0 {
  685. br.Msg = "指标名称已存在,请重新填写"
  686. br.ErrMsg = "指标名称已存在,请重新填写"
  687. br.IsSendEmail = false
  688. return
  689. }
  690. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  691. if err != nil {
  692. if utils.IsErrNoRow(err) {
  693. br.Msg = "指标已被删除,请刷新页面"
  694. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  695. return
  696. }
  697. br.Msg = "获取指标信息失败"
  698. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  699. return
  700. }
  701. if req.Source <= 0 {
  702. req.Source = edbInfo.Source
  703. }
  704. var formulaInt int
  705. switch req.Source {
  706. 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:
  707. if req.Formula == "" {
  708. br.Msg = "请填写N值"
  709. return
  710. }
  711. formulaInt, _ = strconv.Atoi(req.Formula)
  712. if formulaInt <= 0 {
  713. br.Msg = "N值输入错误,请重新输入"
  714. return
  715. }
  716. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  717. //直接拼接指标
  718. //校验时间格式
  719. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  720. if err != nil {
  721. br.Msg = "拼接日期有误,请重新输入"
  722. return
  723. }
  724. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  725. //指标校验
  726. if len(req.EdbInfoIdArr) != 2 {
  727. br.Msg = "选择的指标异常,请重新选择"
  728. return
  729. }
  730. //校验时间格式
  731. //数据格式:2022-11-01,2022-11-10
  732. timeList := strings.Split(req.Formula, ",")
  733. if len(timeList) != 2 {
  734. br.Msg = "选择时间有误,请重新输入"
  735. return
  736. }
  737. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  738. if err != nil {
  739. br.Msg = "开始日期有误,请重新输入"
  740. return
  741. }
  742. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  743. if err != nil {
  744. br.Msg = "结束日期有误,请重新输入"
  745. return
  746. }
  747. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  748. br.Msg = "日期间隔不得少于两天"
  749. return
  750. }
  751. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  752. if req.Formula == "" {
  753. br.Msg = "请填写alpha值"
  754. return
  755. }
  756. alpha, e := strconv.ParseFloat(req.Formula, 64)
  757. if e != nil {
  758. br.Msg = "alpha值输入错误, 请重新输入"
  759. return
  760. }
  761. if alpha <= 0 || alpha >= 1 {
  762. br.Msg = "alpha值输入错误, 请重新输入"
  763. return
  764. }
  765. }
  766. // 构造请求
  767. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  768. EdbInfoId: req.EdbInfoId,
  769. EdbName: req.EdbName,
  770. Frequency: req.Frequency,
  771. Unit: req.Unit,
  772. ClassifyId: req.ClassifyId,
  773. AdminId: this.SysUser.AdminId,
  774. AdminName: this.SysUser.RealName,
  775. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  776. FromEdbInfoId: req.FromEdbInfoId,
  777. Source: req.Source,
  778. MoveType: req.MoveType,
  779. MoveFrequency: req.MoveFrequency,
  780. EdbInfoIdArr: req.EdbInfoIdArr,
  781. Calendar: req.Calendar,
  782. Extra: req.Extra,
  783. EmptyType: req.EmptyType,
  784. }
  785. // 调用指标库去更新
  786. reqJson, err := json.Marshal(req2)
  787. if err != nil {
  788. br.Msg = "参数解析异常!"
  789. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  790. return
  791. }
  792. respItem, err := data.BatchEditEdbCalculateData(string(reqJson), this.Lang)
  793. if err != nil {
  794. br.Msg = "编辑失败"
  795. br.ErrMsg = "编辑失败,Err:" + err.Error()
  796. return
  797. }
  798. if respItem.Ret != 200 {
  799. br.Msg = respItem.Msg
  800. br.ErrMsg = respItem.ErrMsg
  801. return
  802. }
  803. resp := respItem.Data
  804. //添加es
  805. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  806. // 修改关联的预测指标基础信息
  807. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfo, req.Frequency, req.Unit)
  808. br.Ret = 200
  809. br.Success = true
  810. br.Msg = "保存成功"
  811. br.Data = resp
  812. br.IsAddLog = true
  813. }
  814. // @Title 累计值转月-同比值-同差等重新计算
  815. // @Description 累计值转月-同比值-同差等重新计算接口
  816. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  817. // @Success Ret=200 返回指标id
  818. // @router /edb_info/calculate/batch/reset [post]
  819. func (this *ChartInfoController) CalculateBatchReset() {
  820. br := new(models.BaseResponse).Init()
  821. defer func() {
  822. this.Data["json"] = br
  823. this.ServeJSON()
  824. }()
  825. sysUser := this.SysUser
  826. if sysUser == nil {
  827. br.Msg = "请登录"
  828. br.ErrMsg = "请登录,SysUser Is Empty"
  829. br.Ret = 408
  830. return
  831. }
  832. var req data_manage.EdbInfoCalculateBatchSaveReq
  833. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  834. if err != nil {
  835. br.Msg = "参数解析异常!"
  836. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  837. return
  838. }
  839. if req.EdbInfoId <= 0 {
  840. br.Msg = "参数错误"
  841. br.ErrMsg = "参数错误:EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  842. return
  843. }
  844. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  845. if err != nil {
  846. if utils.IsErrNoRow(err) {
  847. br.Msg = "指标已被删除,请刷新页面"
  848. br.ErrMsg = "指标已删除,请刷新页面:Err:" + err.Error()
  849. return
  850. }
  851. br.Msg = "重新计算失败"
  852. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  853. return
  854. }
  855. uniqueCode := edbInfo.UniqueCode
  856. sourName := edbInfo.SourceName
  857. edbInfoId := edbInfo.EdbInfoId
  858. //if source == utils.DATA_SOURCE_CALCULATE {
  859. // //检验公式
  860. // var formulaStr string
  861. // var edbInfoIdBytes []string
  862. // for _, v := range fromEdbInfoList {
  863. // formulaStr += v.FromTag + ","
  864. // edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  865. // }
  866. // formulaStr = strings.Trim(formulaStr, ",")
  867. // formulaMap := data.CheckFormula(req.CalculateFormula)
  868. // for _, v := range formulaMap {
  869. // if !strings.Contains(formulaStr, v) {
  870. // br.Msg = "公式错误,请重新填写"
  871. // return
  872. // }
  873. // }
  874. //
  875. // edbInfoList := make([]*data_manage.EdbInfo, 0)
  876. //
  877. // for _, v := range fromEdbInfoList {
  878. // edbInfo, err := data_manage.GetEdbInfoById(v.FromEdbInfoId)
  879. // if err != nil {
  880. // if utils.IsErrNoRow(err) {
  881. // br.Msg = "重新计算失败"
  882. // br.Msg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  883. // return
  884. // }
  885. // br.Msg = "重新计算失败"
  886. // br.Msg = "获取指标失败:Err:" + err.Error()
  887. // return
  888. // }
  889. // edbInfoList = append(edbInfoList, edbInfo)
  890. // }
  891. // //清除历史数据
  892. // err = data_manage.DeleteCalculateData(edbInfoId)
  893. // if err != nil {
  894. // br.Msg = "重新计算失败"
  895. // br.Msg = "获取指标失败:Err:" + err.Error()
  896. // return
  897. // }
  898. // err = data.Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  899. // if err != nil {
  900. // br.Msg = "重新计算失败"
  901. // br.Msg = "生成计算指标失败,Calculate Err:" + err.Error()
  902. // return
  903. // }
  904. //} else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  905. // _, err = data_manage.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  906. //} else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  907. // sourName = "同比值"
  908. // edbInfoId, err = data_manage.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  909. //} else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  910. // sourName = "同差值"
  911. // edbInfoId, err = data_manage.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  912. //} else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  913. // sourName = "N数值移动平均计算"
  914. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  915. // edbInfoId, err = data_manage.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  916. //} else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  917. // sourName = "环比值"
  918. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  919. // edbInfoId, err = data_manage.AddCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  920. //} else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  921. // sourName = "环差值"
  922. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  923. // edbInfoId, err = data_manage.AddCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  924. //} else if source == utils.DATA_SOURCE_CALCULATE_BP {
  925. // sourName = "变频"
  926. // edbInfoId, err = data_manage.AddCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  927. //} else if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
  928. // sourName = "时间移位"
  929. // if req.MoveFrequency == "" {
  930. // req.MoveFrequency = edbInfo.MoveFrequency
  931. // }
  932. // if req.Formula == "" {
  933. // req.Formula = edbInfo.CalculateFormula
  934. // }
  935. // edbInfoId, err = data_manage.AddCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  936. //} else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ {
  937. // sourName = "直接拼接"
  938. // err = data_manage.RefreshAllCalculateZjpj(edbInfo)
  939. //} else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ {
  940. // sourName = "累计值同比拼接"
  941. // err = data_manage.RefreshAllCalculateLjztbpj(edbInfo)
  942. //} else {
  943. // br.Msg = "无效计算方式"
  944. // br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(source)
  945. // return
  946. //}
  947. // 重新计算
  948. err, isAsync := data.EdbInfoRefreshAllFromBaseV2(edbInfoId, true, false)
  949. if err != nil {
  950. fmt.Println(edbInfoId, "RefreshEdbCalculateData err", time.Now())
  951. br.Msg = "重新计算失败"
  952. br.ErrMsg = "生成" + sourName + "失败,RefreshEdbCalculateData Err:" + err.Error()
  953. return
  954. }
  955. resp := new(data_manage.AddEdbInfoResp)
  956. resp.EdbInfoId = edbInfoId
  957. resp.UniqueCode = uniqueCode
  958. br.Ret = 200
  959. br.Success = true
  960. br.Msg = "重新计算成功"
  961. if isAsync {
  962. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  963. }
  964. br.Data = resp
  965. br.IsAddLog = true
  966. }
  967. // ExecPythonCode
  968. // @Title 执行python代码
  969. // @Description 执行python代码接口
  970. // @Param request body data_manage.EdbInfoCalculateSaveReq true "type json string"
  971. // @Success Ret=200 返回图表id
  972. // @router /edb_info/python/exec [post]
  973. func (this *ChartInfoController) ExecPythonCode() {
  974. br := new(models.BaseResponse).Init()
  975. defer func() {
  976. this.Data["json"] = br
  977. this.ServeJSON()
  978. }()
  979. sysUser := this.SysUser
  980. if sysUser == nil {
  981. br.Msg = "请登录"
  982. br.ErrMsg = "请登录,SysUser Is Empty"
  983. br.Ret = 408
  984. return
  985. }
  986. var req data_manage.ExecPythonEdbReq
  987. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  988. if err != nil {
  989. br.Msg = "参数解析异常!"
  990. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  991. return
  992. }
  993. if req.PythonCode == "" {
  994. br.Msg = "python代码不能为空"
  995. return
  996. }
  997. resp, err := data.ExecPythonCode(req.PythonCode)
  998. if err != nil {
  999. br.Msg = "python代码执行失败,请检查代码"
  1000. br.ErrMsg = "python代码执行失败,Err:" + err.Error()
  1001. return
  1002. }
  1003. if resp.Ret != 200 {
  1004. br.Msg = "python代码执行失败,请检查代码:" + resp.Msg
  1005. br.ErrMsg = "python代码执行失败,Err:" + resp.ErrMsg
  1006. return
  1007. }
  1008. br.Ret = 200
  1009. br.Success = true
  1010. br.Msg = "运行成功"
  1011. br.Data = resp.Data
  1012. }
  1013. // AddPythonEdb
  1014. // @Title 添加python指标
  1015. // @Description 添加python指标接口
  1016. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  1017. // @Success 200 {object} data_manage.AddEdbInfoResp
  1018. // @router /edb_info/python/add [post]
  1019. func (this *ChartInfoController) AddPythonEdb() {
  1020. br := new(models.BaseResponse).Init()
  1021. defer func() {
  1022. this.Data["json"] = br
  1023. this.ServeJSON()
  1024. }()
  1025. sysUser := this.SysUser
  1026. if sysUser == nil {
  1027. br.Msg = "请登录"
  1028. br.ErrMsg = "请登录,SysUser Is Empty"
  1029. br.Ret = 408
  1030. return
  1031. }
  1032. var req data_manage.PostAddPythonEdbReq
  1033. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1034. if err != nil {
  1035. br.Msg = "参数解析异常!"
  1036. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1037. return
  1038. }
  1039. //传入操作人
  1040. req.AdminId = sysUser.AdminId
  1041. req.AdminName = sysUser.RealName
  1042. reqJson, err := json.Marshal(req)
  1043. if err != nil {
  1044. br.Msg = "参数解析异常!"
  1045. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1046. return
  1047. }
  1048. // 判断指标名称是否重复
  1049. {
  1050. var condition string
  1051. var pars []interface{}
  1052. condition += " AND edb_name=? "
  1053. pars = append(pars, req.EdbName)
  1054. condition += " AND edb_info_id<>? "
  1055. pars = append(pars, req.EdbInfoId)
  1056. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1057. if err != nil {
  1058. br.Msg = "判断指标名称是否存在失败"
  1059. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1060. return
  1061. }
  1062. if count > 0 {
  1063. br.Msg = "指标名称已存在,请重新填写"
  1064. br.ErrMsg = "指标名称已存在,请重新填写"
  1065. br.IsSendEmail = false
  1066. return
  1067. }
  1068. }
  1069. // 添加python指标
  1070. respItem, err := data.AddPythonEdbData(string(reqJson), this.Lang)
  1071. if err != nil {
  1072. br.Msg = "添加失败"
  1073. br.ErrMsg = "获取失败,Err:" + err.Error()
  1074. return
  1075. }
  1076. if respItem.Ret != 200 {
  1077. br.Msg = "添加失败"
  1078. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1079. return
  1080. }
  1081. respData := respItem.Data.(map[string]interface{})
  1082. tmpEdbInfoId, ok := respData["EdbInfoId"]
  1083. if !ok {
  1084. br.Msg = `数据转换异常`
  1085. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  1086. return
  1087. }
  1088. //指标id
  1089. edbInfoId := int64(tmpEdbInfoId.(float64))
  1090. uniqueCode, ok := respData["UniqueCode"]
  1091. if !ok {
  1092. br.Msg = `数据转换异常`
  1093. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  1094. return
  1095. }
  1096. //添加es
  1097. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  1098. //
  1099. resp := data_manage.AddEdbInfoResp{
  1100. EdbInfoId: int(edbInfoId),
  1101. UniqueCode: uniqueCode.(string),
  1102. }
  1103. br.Ret = 200
  1104. br.Success = true
  1105. br.Msg = "保存成功"
  1106. br.Data = resp
  1107. br.IsAddLog = true
  1108. }
  1109. // EditPythonEdb
  1110. // @Title 修改python运算指标
  1111. // @Description 修改python运算指标接口
  1112. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  1113. // @Success Ret=200 返回成功
  1114. // @router /edb_info/python/edit [post]
  1115. func (this *ChartInfoController) EditPythonEdb() {
  1116. br := new(models.BaseResponse).Init()
  1117. defer func() {
  1118. this.Data["json"] = br
  1119. this.ServeJSON()
  1120. }()
  1121. sysUser := this.SysUser
  1122. if sysUser == nil {
  1123. br.Msg = "请登录"
  1124. br.ErrMsg = "请登录,SysUser Is Empty"
  1125. br.Ret = 408
  1126. return
  1127. }
  1128. var req data_manage.PostAddPythonEdbReq
  1129. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1130. if err != nil {
  1131. br.Msg = "参数解析异常!"
  1132. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1133. return
  1134. }
  1135. //传入操作人
  1136. req.AdminId = sysUser.AdminId
  1137. req.AdminName = sysUser.RealName
  1138. reqJson, err := json.Marshal(req)
  1139. if err != nil {
  1140. br.Msg = "参数解析异常!"
  1141. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1142. return
  1143. }
  1144. // 判断指标名称是否重复
  1145. {
  1146. var condition string
  1147. var pars []interface{}
  1148. condition += " AND edb_name=? "
  1149. pars = append(pars, req.EdbName)
  1150. condition += " AND edb_info_id<>? "
  1151. pars = append(pars, req.EdbInfoId)
  1152. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1153. if err != nil {
  1154. br.Msg = "判断指标名称是否存在失败"
  1155. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1156. return
  1157. }
  1158. if count > 0 {
  1159. br.Msg = "指标名称已存在,请重新填写"
  1160. br.ErrMsg = "指标名称已存在,请重新填写"
  1161. br.IsSendEmail = false
  1162. return
  1163. }
  1164. }
  1165. //获取指标信息
  1166. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1167. if err != nil {
  1168. if utils.IsErrNoRow(err) {
  1169. br.Msg = "指标已被删除,请刷新页面"
  1170. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  1171. return
  1172. }
  1173. br.Msg = "获取指标信息失败"
  1174. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1175. return
  1176. }
  1177. // 修改python运算指标
  1178. respItem, err := data.EditPythonEdbData(string(reqJson), this.Lang)
  1179. if err != nil {
  1180. br.Msg = "保存失败"
  1181. br.ErrMsg = "保存失败,Err:" + err.Error()
  1182. return
  1183. }
  1184. if respItem.Ret != 200 {
  1185. br.Msg = "保存失败"
  1186. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1187. return
  1188. }
  1189. //添加es
  1190. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  1191. resp := new(data_manage.AddEdbInfoResp)
  1192. resp.EdbInfoId = req.EdbInfoId
  1193. resp.UniqueCode = edbInfo.UniqueCode
  1194. br.Ret = 200
  1195. br.Success = true
  1196. br.Data = resp
  1197. br.Msg = "保存成功"
  1198. br.IsAddLog = true
  1199. }
  1200. // GetPythonEdbCode
  1201. // @Title 获取python代码
  1202. // @Description 获取python代码接口
  1203. // @Param EdbInfoId query int true "指标id"
  1204. // @Success 200 {object} data_manage.PythonEdbDetailResp
  1205. // @router /edb_info/python/detail [get]
  1206. func (this *ChartInfoController) GetPythonEdbCode() {
  1207. br := new(models.BaseResponse).Init()
  1208. defer func() {
  1209. this.Data["json"] = br
  1210. this.ServeJSON()
  1211. }()
  1212. sysUser := this.SysUser
  1213. if sysUser == nil {
  1214. br.Msg = "请登录"
  1215. br.ErrMsg = "请登录,SysUser Is Empty"
  1216. br.Ret = 408
  1217. return
  1218. }
  1219. edbInfoId, _ := this.GetInt("EdbInfoId")
  1220. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1221. if err != nil {
  1222. br.Msg = "获取失败"
  1223. br.ErrMsg = "获取失败,Err:" + err.Error()
  1224. return
  1225. }
  1226. if edbInfo.Source != utils.DATA_SOURCE_PYTHON {
  1227. br.Msg = "该指标不是Python运算指标"
  1228. br.ErrMsg = "该指标不是Python运算指标"
  1229. return
  1230. }
  1231. pythonCode, err := data_manage.GetEdbPythonCodeById(edbInfoId)
  1232. if err != nil {
  1233. br.Msg = "获取失败"
  1234. br.ErrMsg = "获取失败,Err:" + err.Error()
  1235. return
  1236. }
  1237. resp := data_manage.PythonEdbDetailResp{
  1238. EdbInfoDetail: edbInfo,
  1239. PythonCode: pythonCode.PythonCode,
  1240. }
  1241. br.Ret = 200
  1242. br.Success = true
  1243. br.Msg = "保存成功"
  1244. br.Data = resp
  1245. }
  1246. // QueryEdbDataTable
  1247. // @Title 指标筛选接口
  1248. // @Description 指标筛选接口
  1249. // @Param EdbInfoId query int false "指标id"
  1250. // @Success 200 {object} data_manage.EdbInfoList
  1251. // @router /edb_data/table/detail [get]
  1252. func (this *EdbInfoController) QueryEdbDataTable() {
  1253. br := new(models.BaseResponse).Init()
  1254. defer func() {
  1255. this.Data["json"] = br
  1256. this.ServeJSON()
  1257. }()
  1258. edbInfoId, _ := this.GetInt("EdbInfoId")
  1259. if edbInfoId <= 0 {
  1260. br.Msg = "请选择指标"
  1261. br.ErrMsg = "请选择指标"
  1262. return
  1263. }
  1264. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1265. if err != nil {
  1266. br.Msg = "获取指标信息失败"
  1267. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1268. return
  1269. }
  1270. columnList := []map[string]string{
  1271. {
  1272. "edb_code": "指标code",
  1273. },
  1274. {
  1275. "data_time": "指标日期",
  1276. },
  1277. {
  1278. "value": "指标对应的值",
  1279. },
  1280. }
  1281. tableName := data_manage.GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  1282. var templateStr string
  1283. if edbInfo.Source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  1284. templateStr = fmt.Sprintf("# 查询条件\nquery = {\"edb_code\": \"%s\"}\n# 排序\nsort = [(\"data_time\", -1)] # -1 表示降序排列,对应 SQL 的 DESC\nprojection = {\"data_time\": 1, \"value\": 1, \"_id\": 0} # 只选择data_time和value字段,忽略_id字段\n# 使用 find() 方法获取数据,然后使用 aggregate() 转换为列表\nraw_data = list(collection.find(query, projection).sort(sort))\n# 将结果转换为 DataFrame\nraw = pd.DataFrame(raw_data)\n# 转换data_time字段为本地时区时间\nraw['data_time'] = raw['data_time'].apply(lambda x: x.replace(tzinfo=utc_tz)).dt.tz_convert(local_tz).dt.strftime('%s')", edbInfo.EdbCode, "%Y-%m-%d")
  1285. } else {
  1286. 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)
  1287. }
  1288. info := data_manage.TableInfoResp{
  1289. ColumnList: columnList,
  1290. TableName: tableName,
  1291. TemplateStr: url.PathEscape(templateStr),
  1292. }
  1293. br.Ret = 200
  1294. br.Success = true
  1295. br.Msg = "获取成功"
  1296. br.Data = info
  1297. }
  1298. // SaveAdjustEdbInfo
  1299. // @Title 保存数据调整接口
  1300. // @Description 保存数据调整接口
  1301. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  1302. // @Success 200 {object} data_manage.AddEdbInfoResp
  1303. // @router /edb_info/adjust/save [post]
  1304. func (this *ChartInfoController) SaveAdjustEdbInfo() {
  1305. br := new(models.BaseResponse).Init()
  1306. defer func() {
  1307. this.Data["json"] = br
  1308. this.ServeJSON()
  1309. }()
  1310. sysUser := this.SysUser
  1311. if sysUser == nil {
  1312. br.Msg = "请登录"
  1313. br.ErrMsg = "请登录,SysUser Is Empty"
  1314. br.Ret = 408
  1315. return
  1316. }
  1317. var req data_manage.SaveAdjustEdbInfoReq
  1318. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1319. if err != nil {
  1320. br.Msg = "参数解析异常!"
  1321. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1322. return
  1323. }
  1324. //传入操作人
  1325. req.AdminId = sysUser.AdminId
  1326. req.AdminName = sysUser.RealName
  1327. reqJson, err := json.Marshal(req)
  1328. if err != nil {
  1329. br.Msg = "参数解析异常!"
  1330. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1331. return
  1332. }
  1333. // 判断指标名称是否重复
  1334. {
  1335. var condition string
  1336. var pars []interface{}
  1337. condition += " AND edb_name=? "
  1338. pars = append(pars, req.EdbName)
  1339. condition += " AND edb_info_id<>? "
  1340. pars = append(pars, req.EdbInfoId)
  1341. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1342. if err != nil {
  1343. br.Msg = "判断指标名称是否存在失败"
  1344. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1345. return
  1346. }
  1347. if count > 0 {
  1348. br.Msg = "指标名称已存在,请重新填写"
  1349. br.ErrMsg = "指标名称已存在,请重新填写"
  1350. br.IsSendEmail = false
  1351. return
  1352. }
  1353. }
  1354. // 添加数据调整指标
  1355. respItem, err := data.SaveAdjustEdbInfo(string(reqJson), this.Lang)
  1356. if err != nil {
  1357. br.Msg = "添加失败"
  1358. br.ErrMsg = "获取失败,Err:" + err.Error()
  1359. return
  1360. }
  1361. if respItem.Ret != 200 {
  1362. br.Msg = "添加失败"
  1363. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1364. return
  1365. }
  1366. respData := respItem.Data.(map[string]interface{})
  1367. tmpEdbInfoId, ok := respData["EdbInfoId"]
  1368. if !ok {
  1369. br.Msg = `数据转换异常`
  1370. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  1371. return
  1372. }
  1373. //指标id
  1374. edbInfoId := int64(tmpEdbInfoId.(float64))
  1375. uniqueCode, ok := respData["UniqueCode"]
  1376. if !ok {
  1377. br.Msg = `数据转换异常`
  1378. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  1379. return
  1380. }
  1381. //添加es
  1382. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  1383. //
  1384. resp := data_manage.AddEdbInfoResp{
  1385. EdbInfoId: int(edbInfoId),
  1386. UniqueCode: uniqueCode.(string),
  1387. }
  1388. br.Ret = 200
  1389. br.Success = true
  1390. br.Msg = "保存成功"
  1391. br.Data = resp
  1392. br.IsAddLog = true
  1393. }
  1394. // BatchCalculateBatchSave
  1395. // @Title 批量添加 累计值转月-同比值-同差等计算新增
  1396. // @Description 批量添加 累计值转月-同比值-同差等计算新增接口
  1397. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  1398. // @Success Ret=200 返回指标id
  1399. // @router /edb_info/calculate/batch/save/batch [post]
  1400. func (this *ChartInfoController) BatchCalculateBatchSave() {
  1401. br := new(models.BaseResponse).Init()
  1402. defer func() {
  1403. if br.ErrMsg == "" {
  1404. br.IsSendEmail = false
  1405. }
  1406. this.Data["json"] = br
  1407. this.ServeJSON()
  1408. }()
  1409. sysUser := this.SysUser
  1410. if sysUser == nil {
  1411. br.Msg = "请登录"
  1412. br.ErrMsg = "请登录,SysUser Is Empty"
  1413. br.Ret = 408
  1414. return
  1415. }
  1416. var req data_manage.BatchEdbInfoCalculateBatchSaveReq
  1417. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1418. if err != nil {
  1419. br.Msg = "参数解析异常!"
  1420. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1421. return
  1422. }
  1423. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  1424. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  1425. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  1426. }
  1427. var formulaInt int
  1428. switch req.Source {
  1429. 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:
  1430. if req.Formula == "" {
  1431. br.Msg = "请输入N值"
  1432. return
  1433. }
  1434. formulaInt, _ = strconv.Atoi(req.Formula)
  1435. if formulaInt <= 0 {
  1436. br.Msg = "N值输入错误,请重新输入"
  1437. return
  1438. }
  1439. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  1440. //直接拼接指标
  1441. //校验时间格式
  1442. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  1443. if err != nil {
  1444. br.Msg = "拼接日期有误,请重新输入"
  1445. return
  1446. }
  1447. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  1448. //指标校验
  1449. if len(req.EdbInfoIdArr) != 2 {
  1450. br.Msg = "选择的指标异常,请重新选择"
  1451. return
  1452. }
  1453. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1454. if fromEdbInfoId <= 0 {
  1455. br.Msg = "请选择指标"
  1456. return
  1457. }
  1458. //校验时间格式
  1459. //数据格式:2022-11-01,2022-11-10
  1460. timeList := strings.Split(req.Formula, ",")
  1461. if len(timeList) != 2 {
  1462. br.Msg = "选择的指标异常,请重新选择"
  1463. return
  1464. }
  1465. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1466. if err != nil {
  1467. br.Msg = "开始日期有误,请重新输入"
  1468. return
  1469. }
  1470. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1471. if err != nil {
  1472. br.Msg = "结束日期有误,请重新输入"
  1473. return
  1474. }
  1475. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1476. br.Msg = "日期间隔不得少于两天"
  1477. return
  1478. }
  1479. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  1480. if req.Formula == "" {
  1481. br.Msg = "请填写alpha值"
  1482. return
  1483. }
  1484. alpha, e := strconv.ParseFloat(req.Formula, 64)
  1485. if e != nil {
  1486. br.Msg = "alpha值输入错误, 请重新输入"
  1487. return
  1488. }
  1489. if alpha <= 0 || alpha >= 1 {
  1490. br.Msg = "alpha值输入错误, 请重新输入"
  1491. return
  1492. }
  1493. }
  1494. if len(req.EdbList) > 100 {
  1495. br.Msg = "最多只能选择100个指标"
  1496. return
  1497. }
  1498. reqEdbList := make([]*data_manage.CalculateEdbInfoItem, 0)
  1499. for _, v := range req.EdbList {
  1500. v.EdbName = strings.Trim(v.EdbName, " ")
  1501. if v.EdbName == "" {
  1502. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1503. CalculateId: v.CalculateId,
  1504. Msg: "指标名称不能为空",
  1505. })
  1506. continue
  1507. }
  1508. if v.Frequency == "" {
  1509. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1510. CalculateId: v.CalculateId,
  1511. Msg: "频率不能为空",
  1512. })
  1513. continue
  1514. }
  1515. if v.Unit == "" {
  1516. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1517. CalculateId: v.CalculateId,
  1518. Msg: "单位不能为空",
  1519. })
  1520. continue
  1521. }
  1522. if v.ClassifyId <= 0 {
  1523. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1524. CalculateId: v.CalculateId,
  1525. Msg: "请选择分类",
  1526. })
  1527. continue
  1528. }
  1529. //加入缓存机制,避免创建同一个名称的指标 start
  1530. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.Source, ":", v.EdbName)
  1531. isExist := utils.Rc.IsExist(redisKey)
  1532. if isExist {
  1533. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1534. CalculateId: v.CalculateId,
  1535. Msg: "指标正在处理,请勿重复提交",
  1536. })
  1537. continue
  1538. } else {
  1539. //设置3分钟缓存
  1540. //utils.Rc.SetNX(redisKey, 1, time.Second*300)
  1541. //redisKeyList = append(redisKeyList, redisKey)
  1542. }
  1543. reqEdbList = append(reqEdbList, v)
  1544. }
  1545. if len(reqEdbList) <= 0 {
  1546. br.Msg = "新增失败!"
  1547. if len(resp.Fail) > 0 {
  1548. br.ErrMsg = resp.Fail[0].Msg
  1549. } else {
  1550. br.Msg = "请选择指标"
  1551. }
  1552. return
  1553. }
  1554. req.AdminId = sysUser.AdminId
  1555. req.AdminName = sysUser.RealName
  1556. req.EdbList = reqEdbList
  1557. // 调用指标库去更新
  1558. reqJson, err := json.Marshal(req)
  1559. if err != nil {
  1560. br.Msg = "参数解析异常!"
  1561. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1562. return
  1563. }
  1564. respItem, err := data.BatchSaveEdbCalculateMultiData(string(reqJson), this.Lang)
  1565. if err != nil {
  1566. br.Msg = "新增失败!"
  1567. br.ErrMsg = "新增失败,Err:" + err.Error()
  1568. return
  1569. }
  1570. if respItem.Ret != 200 {
  1571. br.Msg = respItem.Msg
  1572. br.ErrMsg = respItem.ErrMsg
  1573. return
  1574. }
  1575. br.Ret = 200
  1576. br.Success = true
  1577. br.Msg = "批量保存成功"
  1578. br.Data = respItem.Data
  1579. br.IsAddLog = true
  1580. }
  1581. // BatchCalculateBatchEdit
  1582. // @Title 多指标求和和多指标求平均编辑接口
  1583. // @Description 多指标求和和多指标求平均编辑接口
  1584. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  1585. // @Success Ret=200 返回指标id
  1586. // @router /edb_info/calculate/batch/edit/batch [post]
  1587. func (this *ChartInfoController) BatchCalculateBatchEdit() {
  1588. br := new(models.BaseResponse).Init()
  1589. defer func() {
  1590. if br.ErrMsg == "" {
  1591. br.IsSendEmail = false
  1592. }
  1593. this.Data["json"] = br
  1594. this.ServeJSON()
  1595. }()
  1596. sysUser := this.SysUser
  1597. if sysUser == nil {
  1598. br.Msg = "请登录"
  1599. br.ErrMsg = "请登录,SysUser Is Empty"
  1600. br.Ret = 408
  1601. return
  1602. }
  1603. var req data_manage.BatchEdbInfoCalculateBatchSaveReq
  1604. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1605. if err != nil {
  1606. br.Msg = "参数解析异常!"
  1607. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1608. return
  1609. }
  1610. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  1611. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  1612. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  1613. }
  1614. var formulaInt int
  1615. switch req.Source {
  1616. 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:
  1617. if req.Formula == "" {
  1618. br.Msg = "请输入N值"
  1619. return
  1620. }
  1621. formulaInt, _ = strconv.Atoi(req.Formula)
  1622. if formulaInt <= 0 {
  1623. br.Msg = "N值输入错误,请重新输入"
  1624. return
  1625. }
  1626. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  1627. //直接拼接指标
  1628. //校验时间格式
  1629. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  1630. if err != nil {
  1631. br.Msg = "拼接日期有误,请重新输入"
  1632. return
  1633. }
  1634. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  1635. //指标校验
  1636. if len(req.EdbInfoIdArr) != 2 {
  1637. br.Msg = "选择的指标异常,请重新选择"
  1638. return
  1639. }
  1640. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1641. if fromEdbInfoId <= 0 {
  1642. br.Msg = "请选择指标"
  1643. return
  1644. }
  1645. //校验时间格式
  1646. //数据格式:2022-11-01,2022-11-10
  1647. timeList := strings.Split(req.Formula, ",")
  1648. if len(timeList) != 2 {
  1649. br.Msg = "选择的指标异常,请重新选择"
  1650. return
  1651. }
  1652. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1653. if err != nil {
  1654. br.Msg = "开始日期有误,请重新输入"
  1655. return
  1656. }
  1657. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1658. if err != nil {
  1659. br.Msg = "结束日期有误,请重新输入"
  1660. return
  1661. }
  1662. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1663. br.Msg = "日期间隔不得少于两天"
  1664. return
  1665. }
  1666. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  1667. if req.Formula == "" {
  1668. br.Msg = "请填写alpha值"
  1669. return
  1670. }
  1671. alpha, e := strconv.ParseFloat(req.Formula, 64)
  1672. if e != nil {
  1673. br.Msg = "alpha值输入错误, 请重新输入"
  1674. return
  1675. }
  1676. if alpha <= 0 || alpha >= 1 {
  1677. br.Msg = "alpha值输入错误, 请重新输入"
  1678. return
  1679. }
  1680. }
  1681. if len(req.EdbList) > 50 {
  1682. br.Msg = "最多只能选择50个指标"
  1683. return
  1684. }
  1685. reqEdbList := make([]*data_manage.CalculateEdbInfoItem, 0)
  1686. for _, v := range req.EdbList {
  1687. if v.EdbInfoId <= 0 {
  1688. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1689. CalculateId: v.CalculateId,
  1690. Msg: "请选择要编辑的指标",
  1691. })
  1692. continue
  1693. }
  1694. v.EdbName = strings.Trim(v.EdbName, " ")
  1695. if v.EdbName == "" {
  1696. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1697. CalculateId: v.CalculateId,
  1698. Msg: "指标名称不能为空",
  1699. })
  1700. continue
  1701. }
  1702. if v.Frequency == "" {
  1703. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1704. CalculateId: v.CalculateId,
  1705. Msg: "频率不能为空",
  1706. })
  1707. continue
  1708. }
  1709. if v.Unit == "" {
  1710. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1711. CalculateId: v.CalculateId,
  1712. Msg: "单位不能为空",
  1713. })
  1714. continue
  1715. }
  1716. if v.ClassifyId <= 0 {
  1717. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1718. CalculateId: v.CalculateId,
  1719. Msg: "请选择分类",
  1720. })
  1721. continue
  1722. }
  1723. reqEdbList = append(reqEdbList, v)
  1724. }
  1725. if len(reqEdbList) <= 0 {
  1726. br.Msg = "编辑失败!"
  1727. if len(resp.Fail) > 0 {
  1728. br.ErrMsg = resp.Fail[0].Msg
  1729. } else {
  1730. br.Msg = "请选择指标"
  1731. }
  1732. return
  1733. }
  1734. req.AdminId = sysUser.AdminId
  1735. req.AdminName = sysUser.RealName
  1736. req.EdbList = reqEdbList
  1737. // 调用指标库去更新
  1738. reqJson, err := json.Marshal(req)
  1739. if err != nil {
  1740. br.Msg = "参数解析异常!"
  1741. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1742. return
  1743. }
  1744. respItem, err := data.BatchEditEdbCalculateMultiData(string(reqJson), this.Lang)
  1745. if err != nil {
  1746. br.Msg = "编辑失败!"
  1747. br.ErrMsg = "编辑失败,Err:" + err.Error()
  1748. return
  1749. }
  1750. if respItem.Ret != 200 {
  1751. br.Msg = respItem.Msg
  1752. br.ErrMsg = respItem.ErrMsg
  1753. return
  1754. }
  1755. br.Ret = 200
  1756. br.Success = true
  1757. br.Msg = "批量编辑成功"
  1758. br.Data = respItem.Data
  1759. br.IsAddLog = true
  1760. }
  1761. // CalculateMapping
  1762. // @Title 获取计算指标与基础指标关联
  1763. // @Description 获取计算指标与基础指标关联
  1764. // @Param EdbInfoId query int true "指标id"
  1765. // @Success 200 {object} data_manage.CalculateDetailResp
  1766. // @router /edb_info/calculate/mapping [get]
  1767. func (this *ChartInfoController) CalculateMapping() {
  1768. br := new(models.BaseResponse).Init()
  1769. defer func() {
  1770. this.Data["json"] = br
  1771. this.ServeJSON()
  1772. }()
  1773. sysUser := this.SysUser
  1774. if sysUser == nil {
  1775. br.Msg = "请登录"
  1776. br.ErrMsg = "请登录,SysUser Is Empty"
  1777. br.Ret = 408
  1778. return
  1779. }
  1780. edbInfoId, _ := this.GetInt("EdbInfoId")
  1781. if edbInfoId <= 0 {
  1782. br.Msg = "参数有误"
  1783. return
  1784. }
  1785. item, e := data_manage.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1786. if e != nil {
  1787. br.Msg = "获取失败"
  1788. br.ErrMsg = "获取失败,Err:" + e.Error()
  1789. return
  1790. }
  1791. // 英文来源,显示英文名称
  1792. if this.Lang == utils.EnLangVersion {
  1793. fromEdbInfo, e := data_manage.GetEdbInfoById(item.FromEdbInfoId)
  1794. if e != nil {
  1795. br.Msg = "获取失败"
  1796. br.ErrMsg = "获取失败,Err:" + e.Error()
  1797. return
  1798. }
  1799. item.FromEdbName = fromEdbInfo.EdbNameEn
  1800. }
  1801. br.Ret = 200
  1802. br.Success = true
  1803. br.Msg = "保存成功"
  1804. br.Data = item
  1805. }
  1806. // CalculateComputeCorrelation
  1807. // @Title 拟合残差计算相关性
  1808. // @Description 拟合残差计算相关性接口
  1809. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  1810. // @Success Ret=200 返回指标id
  1811. // @router /edb_info/calculate/compute_correlation [post]
  1812. func (this *ChartInfoController) CalculateComputeCorrelation() {
  1813. br := new(models.BaseResponse).Init()
  1814. defer func() {
  1815. this.Data["json"] = br
  1816. this.ServeJSON()
  1817. }()
  1818. sysUser := this.SysUser
  1819. if sysUser == nil {
  1820. br.Msg = "请登录"
  1821. br.ErrMsg = "请登录,SysUser Is Empty"
  1822. br.Ret = 408
  1823. return
  1824. }
  1825. var req data_manage.EdbInfoCalculateBatchSaveReq
  1826. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1827. if err != nil {
  1828. br.Msg = "参数解析异常!"
  1829. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1830. return
  1831. }
  1832. if len(req.EdbInfoIdArr) != 2 {
  1833. br.Msg = "选择的指标异常,请重新选择"
  1834. return
  1835. }
  1836. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1837. //校验时间格式
  1838. //数据格式:2022-11-01,2022-11-10
  1839. timeList := strings.Split(req.Formula, ",")
  1840. if len(timeList) != 2 {
  1841. br.Msg = "选择时间有误,请重新输入"
  1842. return
  1843. }
  1844. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1845. if err != nil {
  1846. br.Msg = "开始日期有误,请重新输入"
  1847. return
  1848. }
  1849. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1850. if err != nil {
  1851. br.Msg = "结束日期有误,请重新输入"
  1852. return
  1853. }
  1854. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1855. br.Msg = "日期间隔不得少于两天"
  1856. return
  1857. }
  1858. if fromEdbInfoId <= 0 {
  1859. br.Msg = "请选择指标"
  1860. return
  1861. }
  1862. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1863. AdminId: sysUser.AdminId,
  1864. AdminName: sysUser.RealName,
  1865. EdbInfoId: req.EdbInfoId,
  1866. EdbName: req.EdbName,
  1867. Frequency: req.Frequency,
  1868. Unit: req.Unit,
  1869. ClassifyId: req.ClassifyId,
  1870. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  1871. FromEdbInfoId: req.FromEdbInfoId,
  1872. CalculateFormula: req.CalculateFormula,
  1873. Source: req.Source,
  1874. MoveType: req.MoveType,
  1875. MoveFrequency: req.MoveFrequency,
  1876. //CalculateFormula: edbInfo.CalculateFormula,
  1877. EdbInfoIdArr: req.EdbInfoIdArr,
  1878. Calendar: req.Calendar,
  1879. }
  1880. val, err, errMsg := data.CallCalculateComputeCorrelation(req2, this.Lang)
  1881. if err != nil {
  1882. br.Msg = "计算失败"
  1883. if errMsg != `` {
  1884. br.Msg = errMsg
  1885. }
  1886. br.ErrMsg = err.Error()
  1887. return
  1888. }
  1889. br.Data = val
  1890. br.Ret = 200
  1891. br.Success = true
  1892. br.Msg = "计算成功"
  1893. br.IsAddLog = true
  1894. }
  1895. // CalculateMultiChoice
  1896. // @Title 批量计算-加入已选指标
  1897. // @Description 批量计算-加入已选指标
  1898. // @Param ClassifyIds query int true "指标库分类IDs"
  1899. // @Param SysUserIds query int true "创建人"
  1900. // @Param Keyword query string false "关键词搜索"
  1901. // @Param Frequency query string false "频度"
  1902. // @Param EdbInfoType query int false "指标类型: 0-普通指标; 1-预测指标"
  1903. // @Success 200 {object} data_manage.EdbInfoSearchResp
  1904. // @router /edb_info/calculate/multi/choice [get]
  1905. func (this *ChartInfoController) CalculateMultiChoice() {
  1906. br := new(models.BaseResponse).Init()
  1907. defer func() {
  1908. this.Data["json"] = br
  1909. this.ServeJSON()
  1910. }()
  1911. //来源类型,1:中文,2:英文
  1912. frequency := this.GetString("Frequency")
  1913. keyword := this.GetString("Keyword")
  1914. sysUserIds := this.GetString("SysUserIds")
  1915. classifyIds := this.GetString("ClassifyIds")
  1916. edbInfoIds := this.GetString("EdbInfoIds")
  1917. selectAll, _ := this.GetBool("SelectAll")
  1918. notFrequency := this.GetString("NotFrequency")
  1919. edbInfoType, _ := this.GetInt("EdbInfoType", 0)
  1920. var edbIdArr []int
  1921. var filterEdbIds []int
  1922. if edbInfoIds != "" {
  1923. arr := strings.Split(edbInfoIds, ",")
  1924. for _, v := range arr {
  1925. t, e := strconv.Atoi(v)
  1926. if e != nil {
  1927. br.Msg = "参数有误"
  1928. br.ErrMsg = fmt.Sprintf("指标ID有误, %s", v)
  1929. return
  1930. }
  1931. filterEdbIds = append(filterEdbIds, t)
  1932. }
  1933. }
  1934. if selectAll {
  1935. // 如果勾了列表全选,那么EdbCode传的就是排除的code
  1936. condition := ` AND edb_info_type = ? `
  1937. pars := make([]interface{}, 0)
  1938. pars = append(pars, edbInfoType)
  1939. if classifyIds != "" {
  1940. classifyIdsArr := strings.Split(classifyIds, ",")
  1941. condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyIdsArr)) + `) `
  1942. pars = append(pars, classifyIdsArr)
  1943. }
  1944. if frequency != "" {
  1945. frequencyArr := strings.Split(frequency, ",")
  1946. condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencyArr)) + `) `
  1947. pars = append(pars, frequencyArr)
  1948. }
  1949. if notFrequency != "" {
  1950. notFrequencyArr := strings.Split(notFrequency, ",")
  1951. condition += ` AND frequency NOT IN (` + utils.GetOrmInReplace(len(notFrequencyArr)) + `) `
  1952. pars = append(pars, notFrequencyArr)
  1953. }
  1954. if sysUserIds != "" {
  1955. sysUserIdSlice := strings.Split(sysUserIds, ",")
  1956. condition += ` AND sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  1957. pars = append(pars, sysUserIdSlice)
  1958. }
  1959. if keyword != "" {
  1960. keyWordArr := strings.Split(keyword, " ")
  1961. if len(keyWordArr) > 0 {
  1962. for _, v := range keyWordArr {
  1963. condition += ` AND CONCAT(edb_name,edb_code) LIKE ?`
  1964. pars = append(pars, utils.GetLikeKeyword(v))
  1965. }
  1966. }
  1967. }
  1968. edbList, e := data_manage.GetEdbInfoListByCond(condition, pars)
  1969. if e != nil {
  1970. br.Msg = "获取指标列表失败"
  1971. br.ErrMsg = "获取指标列表失败,Err:" + e.Error()
  1972. return
  1973. }
  1974. filterLen := len(filterEdbIds)
  1975. for _, v := range edbList {
  1976. // 全选-排除传入的指标
  1977. if filterLen > 0 && utils.InArrayByInt(filterEdbIds, v.EdbInfoId) {
  1978. continue
  1979. }
  1980. edbIdArr = append(edbIdArr, v.EdbInfoId)
  1981. }
  1982. } else {
  1983. //未勾选全选EdbCode就是需要的code
  1984. edbIdStrArr := strings.Split(edbInfoIds, ",")
  1985. for _, v := range edbIdStrArr {
  1986. id, e := strconv.Atoi(v)
  1987. if e != nil {
  1988. br.Msg = "获取指标列表失败"
  1989. br.ErrMsg = "获取指标列表失败,Err:" + e.Error()
  1990. return
  1991. }
  1992. edbIdArr = append(edbIdArr, id)
  1993. }
  1994. }
  1995. if len(edbIdArr) > 100 {
  1996. br.Msg = "最多只能选择100个指标"
  1997. return
  1998. }
  1999. if len(edbIdArr) <= 0 {
  2000. br.Msg = "无符合指标或指标代码错误"
  2001. return
  2002. }
  2003. list, err := data_manage.GetEdbInfoByIdList(edbIdArr)
  2004. if err != nil && !utils.IsErrNoRow(err) {
  2005. br.Msg = "获取指标列表失败"
  2006. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  2007. return
  2008. }
  2009. searchItemList := make([]data_manage.EdbInfoBase, 0)
  2010. resp := new(data_manage.CalculateMultiChoiceResp)
  2011. if len(list) > 0 {
  2012. // 当前列表中的分类map
  2013. classifyMap := make(map[int]*data_manage.EdbClassify)
  2014. {
  2015. classifyIdList := make([]int, 0)
  2016. for _, info := range list {
  2017. classifyIdList = append(classifyIdList, info.ClassifyId)
  2018. }
  2019. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2020. if err != nil {
  2021. if err != nil {
  2022. br.Msg = "获取失败"
  2023. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2024. return
  2025. }
  2026. }
  2027. for _, v := range classifyList {
  2028. classifyMap[v.ClassifyId] = v
  2029. }
  2030. }
  2031. // 获取所有有权限的指标和分类
  2032. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2033. if err != nil {
  2034. br.Msg = "获取失败"
  2035. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2036. return
  2037. }
  2038. for _, info := range list {
  2039. var haveOperaAuth bool
  2040. if currClassify, ok := classifyMap[info.ClassifyId]; ok {
  2041. haveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(info.IsJoinPermission, currClassify.IsJoinPermission, info.EdbInfoId, info.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2042. }
  2043. searchItem := data_manage.EdbInfoBase{
  2044. Frequency: info.Frequency,
  2045. Unit: info.Unit,
  2046. UnitEn: info.UnitEn,
  2047. EdbName: info.EdbName,
  2048. EdbNameEn: info.EdbNameEn,
  2049. EdbInfoId: info.EdbInfoId,
  2050. ClassifyId: info.ClassifyId,
  2051. HaveOperaAuth: haveOperaAuth,
  2052. EdbInfoType: info.EdbInfoType,
  2053. }
  2054. searchItemList = append(searchItemList, searchItem)
  2055. }
  2056. }
  2057. resp.SearchItem = searchItemList
  2058. br.Ret = 200
  2059. br.Success = true
  2060. br.Msg = "获取成功"
  2061. br.Data = resp
  2062. }
  2063. // CalculateMultiSearch
  2064. // @Title 批量计算-查询指标
  2065. // @Description 批量计算-查询指标
  2066. // @Param ClassifyIds query int true "指标库分类IDs"
  2067. // @Param SysUserIds query int true "创建人"
  2068. // @Param Keyword query string false "关键词搜索"
  2069. // @Param Frequency query string false "频度"
  2070. // @Param EdbInfoType query int false "指标类型: 0-普通指标; 1-预测指标"
  2071. // @Success 200 {object} data_manage.EdbInfoSearchResp
  2072. // @router /edb_info/calculate/multi/search [get]
  2073. func (this *ChartInfoController) CalculateMultiSearch() {
  2074. br := new(models.BaseResponse).Init()
  2075. defer func() {
  2076. this.Data["json"] = br
  2077. this.ServeJSON()
  2078. }()
  2079. //来源类型,1:中文,2:英文
  2080. frequency := this.GetString("Frequency")
  2081. keyword := this.GetString("Keyword")
  2082. sysUserIds := this.GetString("SysUserIds")
  2083. classifyIds := this.GetString("ClassifyIds")
  2084. pageSize, _ := this.GetInt("PageSize")
  2085. currentIndex, _ := this.GetInt("CurrentIndex")
  2086. notFrequency := this.GetString("NotFrequency")
  2087. edbInfoType, _ := this.GetInt("EdbInfoType", 0)
  2088. var startSize int
  2089. if pageSize <= 0 {
  2090. pageSize = utils.PageSize50
  2091. }
  2092. if currentIndex <= 0 {
  2093. currentIndex = 1
  2094. }
  2095. startSize = utils.StartIndex(currentIndex, pageSize)
  2096. var edbIdArr []int
  2097. condition := ` AND edb_info_type = ? `
  2098. pars := make([]interface{}, 0)
  2099. pars = append(pars, edbInfoType)
  2100. if classifyIds != "" {
  2101. classifyIdsArr := strings.Split(classifyIds, ",")
  2102. condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyIdsArr)) + `) `
  2103. pars = append(pars, classifyIdsArr)
  2104. }
  2105. if frequency != "" {
  2106. frequencyArr := strings.Split(frequency, ",")
  2107. condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencyArr)) + `) `
  2108. pars = append(pars, frequencyArr)
  2109. }
  2110. if notFrequency != "" {
  2111. notFrequencyArr := strings.Split(notFrequency, ",")
  2112. condition += ` AND frequency NOT IN (` + utils.GetOrmInReplace(len(notFrequencyArr)) + `) `
  2113. pars = append(pars, notFrequencyArr)
  2114. }
  2115. if sysUserIds != "" {
  2116. sysUserIdSlice := strings.Split(sysUserIds, ",")
  2117. condition += ` AND sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  2118. pars = append(pars, sysUserIdSlice)
  2119. }
  2120. if keyword != "" {
  2121. keyWordArr := strings.Split(keyword, " ")
  2122. if len(keyWordArr) > 0 {
  2123. for _, v := range keyWordArr {
  2124. condition += ` AND CONCAT(edb_name,edb_code) LIKE ?`
  2125. pars = append(pars, utils.GetLikeKeyword(v))
  2126. }
  2127. }
  2128. }
  2129. total, e := data_manage.GetEdbInfoByConditionCount(condition, pars)
  2130. if e != nil {
  2131. br.Msg = "获取指标列表总数失败"
  2132. br.ErrMsg = "获取指标列表总数失败,Err:" + e.Error()
  2133. return
  2134. }
  2135. searchItemList := make([]data_manage.CalculateMultiEdbSearchItem, 0)
  2136. page := paging.GetPaging(currentIndex, pageSize, total)
  2137. resp := new(data_manage.CalculateMultiEdbSearchResp)
  2138. edbList, e := data_manage.GetEdbInfoListByCondition(condition, pars, startSize, pageSize, "")
  2139. if e != nil {
  2140. br.Msg = "获取指标列表失败"
  2141. br.ErrMsg = "获取指标列表失败,Err:" + e.Error()
  2142. return
  2143. }
  2144. if len(edbList) > 0 {
  2145. classifyIdList := make([]int, 0)
  2146. for _, v := range edbList {
  2147. edbIdArr = append(edbIdArr, v.EdbInfoId)
  2148. classifyIdList = append(classifyIdList, v.ClassifyId)
  2149. }
  2150. // 当前列表中的分类map
  2151. classifyMap := make(map[int]*data_manage.EdbClassify)
  2152. {
  2153. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2154. if err != nil {
  2155. if err != nil {
  2156. br.Msg = "获取失败"
  2157. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2158. return
  2159. }
  2160. }
  2161. for _, v := range classifyList {
  2162. classifyMap[v.ClassifyId] = v
  2163. }
  2164. }
  2165. // 获取所有有权限的指标和分类
  2166. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2167. if err != nil {
  2168. br.Msg = "获取失败"
  2169. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2170. return
  2171. }
  2172. list, err := data_manage.GetEdbInfoByIdList(edbIdArr)
  2173. if err != nil && !utils.IsErrNoRow(err) {
  2174. br.Msg = "获取指标列表失败"
  2175. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  2176. return
  2177. }
  2178. for _, info := range list {
  2179. var haveOperaAuth bool
  2180. if currClassify, ok := classifyMap[info.ClassifyId]; ok {
  2181. haveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(info.IsJoinPermission, currClassify.IsJoinPermission, info.EdbInfoId, info.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2182. }
  2183. searchItem := data_manage.CalculateMultiEdbSearchItem{
  2184. Frequency: info.Frequency,
  2185. Unit: info.Unit,
  2186. UnitEn: info.UnitEn,
  2187. EdbName: info.EdbName,
  2188. EdbNameEn: info.EdbNameEn,
  2189. EdbInfoId: info.EdbInfoId,
  2190. ClassifyId: info.ClassifyId,
  2191. SysUserRealName: info.SysUserRealName,
  2192. SysUserId: info.SysUserId,
  2193. EndDate: utils.TimeToFormatDate(info.EndDate),
  2194. EndValue: info.EndValue,
  2195. HaveOperaAuth: haveOperaAuth,
  2196. EdbInfoType: info.EdbInfoType,
  2197. }
  2198. searchItemList = append(searchItemList, searchItem)
  2199. }
  2200. }
  2201. resp.SearchItem = searchItemList
  2202. resp.Paging = page
  2203. br.Ret = 200
  2204. br.Success = true
  2205. br.Msg = "获取成功"
  2206. br.Data = resp
  2207. }