edb_info_calculate.go 69 KB

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