edb_info_calculate.go 75 KB

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