edb_info_calculate.go 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482
  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. }