edb_info.go 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723
  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/company"
  7. "eta_gn/eta_api/models/data_manage"
  8. "eta_gn/eta_api/models/data_manage/cross_variety"
  9. request2 "eta_gn/eta_api/models/data_manage/excel/request"
  10. "eta_gn/eta_api/models/data_manage/request"
  11. "eta_gn/eta_api/models/data_manage/response"
  12. "eta_gn/eta_api/models/system"
  13. "eta_gn/eta_api/services"
  14. "eta_gn/eta_api/services/alarm_msg"
  15. "eta_gn/eta_api/services/data"
  16. "eta_gn/eta_api/services/data/data_manage_permission"
  17. excel2 "eta_gn/eta_api/services/data/excel"
  18. "eta_gn/eta_api/services/elastic"
  19. "eta_gn/eta_api/utils"
  20. "fmt"
  21. "strconv"
  22. "strings"
  23. "sync"
  24. "time"
  25. "github.com/rdlucklib/rdluck_tools/paging"
  26. )
  27. // EdbInfoController 数据管理
  28. type EdbInfoController struct {
  29. controllers.BaseAuthController
  30. }
  31. // EdbInfoSearch
  32. // @Title 新增指标-查询指标信息接口
  33. // @Description 新增指标-查询指标信息接口
  34. // @Param Source query int true "来源:1:同花顺,2:wind,3:彭博"
  35. // @Param SubSource query int true "子数据来源:0:经济数据库,1:日期序列"
  36. // @Param EdbCode query string false "指标编码/指标代码"
  37. // @Param StockCode query string false "证券代码"
  38. // @Param Frequency query string false "频度"
  39. // @Success 200 {object} data_manage.EdbInfoSearchResp
  40. // @router /edb_info/search [get]
  41. func (this *EdbInfoController) EdbInfoSearch() {
  42. br := new(models.BaseResponse).Init()
  43. defer func() {
  44. this.Data["json"] = br
  45. this.ServeJSON()
  46. }()
  47. source, _ := this.GetInt("Source")
  48. edbCode := this.GetString("EdbCode")
  49. edbCode = strings.Trim(edbCode, "\t")
  50. edbCode = strings.Trim(edbCode, " ")
  51. edbCode = strings.Replace(edbCode, "\t", "", -1)
  52. subSource, _ := this.GetInt("SubSource")
  53. stockCode := this.GetString("StockCode")
  54. frequency := this.GetString("Frequency")
  55. extraPars := this.GetString("ExtraPars")
  56. extraPars = strings.TrimSpace(extraPars)
  57. if source <= 0 {
  58. br.Msg = "无效的数据来源"
  59. return
  60. }
  61. if edbCode == "" {
  62. br.Msg = "请输入指标ID"
  63. return
  64. }
  65. //如果是彭博财务指标,那么需要传入客户code
  66. if source == utils.DATA_SOURCE_PB_FINANCE {
  67. companyCode := this.GetString("CompanyCode")
  68. companyCode = strings.Trim(companyCode, "\t")
  69. companyCode = strings.Trim(companyCode, " ")
  70. companyCode = strings.Replace(companyCode, "\t", "", -1)
  71. // 如果财务数据,那么需要拼接上客户code
  72. edbCode = companyCode + "&&" + edbCode
  73. }
  74. resp := new(data_manage.EdbInfoSearchResp)
  75. //判断指标是否存在
  76. var item *data_manage.EdbInfo
  77. var err error
  78. item, err = data_manage.GetEdbInfoByEdbCode(source, edbCode)
  79. if err != nil {
  80. if !utils.IsErrNoRow(err) {
  81. br.Msg = "获取失败"
  82. br.ErrMsg = "获取失败,Err:" + err.Error()
  83. return
  84. }
  85. }
  86. var isAdd bool
  87. var isInEdb bool //是否在指标库中
  88. if item != nil && item.EdbInfoId > 0 {
  89. isInEdb = true //在指标库中
  90. resp.Status = 1
  91. searchItem := new(data_manage.EdbInfoSearch)
  92. searchItem.EdbCode = item.EdbCode
  93. searchItem.StartDate = utils.TimeToFormatDate(item.StartDate)
  94. searchItem.EndDate = utils.TimeToFormatDate(item.EndDate)
  95. searchItem.EdbName = item.EdbName
  96. resp.SearchItem = searchItem
  97. //查询目录
  98. resultList, _ := data_manage.GetClassifyAllByClassifyId(item.ClassifyId)
  99. resp.ClassifyList = resultList
  100. // 查询该指标是否有权限
  101. obj := data_manage.EdbInfoNoPermissionAdmin{}
  102. conf, err := obj.GetByEdbInfoIdAndAdminId(this.SysUser.AdminId, item.EdbInfoId)
  103. if err != nil && !utils.IsErrNoRow(err) {
  104. br.Msg = "获取失败"
  105. br.ErrMsg = "获取当前账号的不可见指标配置失败,err:" + err.Error()
  106. return
  107. }
  108. if conf != nil && conf.Id > 0 {
  109. resp.Status = 3
  110. }
  111. } else {
  112. //dateNow := time.Now()
  113. //startDate := dateNow.AddDate(-30, 0, 0).Format(utils.FormatDate)
  114. searchItem := new(data_manage.EdbInfoSearch)
  115. if source == utils.DATA_SOURCE_MANUAL { //手工数据指标
  116. dataItems, err := data_manage.GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_MANUAL, subSource, utils.EDB_DATA_LIMIT)
  117. if err != nil && !utils.IsErrNoRow(err) {
  118. br.Msg = "获取失败"
  119. br.ErrMsg = "获取Ths已存在信息失败,Err:" + err.Error()
  120. return
  121. }
  122. fmt.Println("ths len:", len(dataItems))
  123. if len(dataItems) > 0 {
  124. fmt.Println("ths data exist")
  125. searchItem.EdbCode = edbCode
  126. minDate, maxDate, err := data_manage.GetEdbDataManualMaxOrMinDate(edbCode)
  127. if err != nil {
  128. br.Msg = "获取失败"
  129. br.ErrMsg = "获取Ths日期信息失败,Err:" + err.Error()
  130. return
  131. }
  132. searchItem.DataList = dataItems
  133. searchItem.StartDate = minDate
  134. searchItem.EndDate = maxDate
  135. } else {
  136. //searchItem, err = data_manage.GetEdbDataByManual(edbCode, startDate, endDate)
  137. //if err != nil {
  138. // br.Msg = "获取失败"
  139. // br.ErrMsg = "获取失败,Err:" + err.Error()
  140. // return
  141. //}
  142. respItem, err := data.AddEdbData(source, edbCode, frequency)
  143. if err != nil {
  144. br.Msg = "获取失败"
  145. br.ErrMsg = "获取失败,Err:" + err.Error()
  146. return
  147. }
  148. if respItem.Ret != 200 {
  149. br.Msg = "未搜索到该指标"
  150. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + edbCode
  151. return
  152. }
  153. isAdd = true
  154. }
  155. //获取指标信息
  156. manualInfo, err := models.GetEdbinfoByTradeCode(edbCode)
  157. if err != nil && !utils.IsErrNoRow(err) {
  158. br.Msg = "获取失败"
  159. br.ErrMsg = "获取手工指标详情失败,Err:" + err.Error()
  160. return
  161. }
  162. if manualInfo != nil {
  163. searchItem.Frequency = manualInfo.Frequency
  164. searchItem.Unit = manualInfo.Unit
  165. searchItem.EdbName = manualInfo.SecName
  166. }
  167. } else if source == utils.DATA_SOURCE_LT {
  168. fmt.Println("lt data exist")
  169. if strings.Contains(edbCode, "=") {
  170. br.Msg = "暂不支持编码中含有=的指标"
  171. br.ErrMsg = "暂不支持编码中含有=的指标"
  172. return
  173. }
  174. dataItems, err := data_manage.GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_LT, subSource, utils.EDB_DATA_LIMIT)
  175. if err != nil && !utils.IsErrNoRow(err) {
  176. br.Msg = "获取失败"
  177. br.ErrMsg = "获取 lt 已存在信息失败,Err:" + err.Error()
  178. return
  179. }
  180. fmt.Println("lt dataItems:", len(dataItems))
  181. if len(dataItems) > 0 {
  182. searchItem.EdbCode = edbCode
  183. edbInfoMaxAndMinInfo, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  184. if err != nil {
  185. br.Msg = "获取失败"
  186. br.ErrMsg = "获取lt日期信息失败,Err:" + err.Error()
  187. return
  188. }
  189. searchItem.DataList = dataItems
  190. searchItem.StartDate = edbInfoMaxAndMinInfo.MinDate
  191. searchItem.EndDate = edbInfoMaxAndMinInfo.MaxDate
  192. } else {
  193. //endDate := dateNow.Format(utils.FormatDate)
  194. //searchItem, err = data.GetEdbDataByLt(edbCode, startDate, endDate)
  195. //if err != nil {
  196. // br.Msg = "获取失败"
  197. // br.ErrMsg = "lt 获取失败,Err:" + err.Error()
  198. // return
  199. //}
  200. respItem, err := data.AddEdbData(source, edbCode, frequency)
  201. if err != nil {
  202. br.Msg = "获取失败"
  203. br.ErrMsg = "获取失败,Err:" + err.Error()
  204. return
  205. }
  206. if respItem.Ret != 200 {
  207. br.Msg = respItem.Msg
  208. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + edbCode
  209. return
  210. }
  211. isAdd = true
  212. }
  213. } else if source == utils.DATA_SOURCE_GOOGLE_TRAVEL { //谷歌出行数据
  214. dataItems, err := data_manage.GetEdbDataAllByEdbCode(edbCode, source, subSource, utils.EDB_DATA_LIMIT)
  215. if err != nil && !utils.IsErrNoRow(err) {
  216. br.Msg = "获取失败"
  217. br.ErrMsg = "获取煤炭网已存在信息失败,Err:" + err.Error()
  218. return
  219. }
  220. if len(dataItems) > 0 {
  221. searchItem.EdbCode = edbCode
  222. item, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  223. if err != nil {
  224. br.Msg = "获取失败"
  225. br.ErrMsg = "获取煤炭网日期信息失败,Err:" + err.Error()
  226. return
  227. }
  228. if item != nil {
  229. searchItem.DataList = dataItems
  230. searchItem.StartDate = item.MinDate
  231. searchItem.EndDate = item.MaxDate
  232. }
  233. } else {
  234. respItem, err := data.AddEdbData(source, edbCode, frequency)
  235. if err != nil {
  236. br.Msg = "获取失败"
  237. br.ErrMsg = "获取失败,Err:" + err.Error()
  238. return
  239. }
  240. if respItem.Ret != 200 {
  241. br.Msg = "未搜索到该指标"
  242. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + edbCode
  243. return
  244. }
  245. isAdd = true
  246. }
  247. } else if source == utils.DATA_SOURCE_BUSINESS { //自有数据
  248. indexObj := data_manage.BaseFromBusinessIndex{}
  249. indexInfo, err := indexObj.GetDetailByEdbCode(edbCode)
  250. if err != nil && !utils.IsErrNoRow(err) {
  251. br.Msg = "获取失败"
  252. br.ErrMsg = "获取自有数据指标信息失败,Err:" + err.Error()
  253. return
  254. }
  255. _, tmpDataList, err := data.GetPageBaseBusinessIndexData(edbCode, 1, utils.EDB_DATA_LIMIT)
  256. if err != nil && !utils.IsErrNoRow(err) {
  257. br.Msg = "获取失败"
  258. br.ErrMsg = "获取自有数据已存在信息失败,Err:" + err.Error()
  259. return
  260. }
  261. dataItems := make([]*data_manage.EdbInfoSearchData, 0)
  262. for _, v := range tmpDataList {
  263. dataItems = append(dataItems, &data_manage.EdbInfoSearchData{
  264. DataTime: v.DataTime,
  265. Value: v.Value,
  266. })
  267. }
  268. searchItem.EdbCode = edbCode
  269. searchItem.DataList = dataItems
  270. searchItem.StartDate = indexInfo.StartDate.Format(utils.FormatDate)
  271. searchItem.EndDate = indexInfo.EndDate.Format(utils.FormatDate)
  272. searchItem.Frequency = indexInfo.Frequency
  273. searchItem.Unit = indexInfo.Unit
  274. searchItem.EdbName = indexInfo.IndexName
  275. } else {
  276. // 代码中没有的来源那么从edb_source中找是否有对应的
  277. sourceItem := data_manage.GetEdbSourceBySourceId(source)
  278. if sourceItem == nil {
  279. br.Msg = "无效指标来源"
  280. return
  281. }
  282. // 获取指标数据
  283. dataList, e := data_manage.GetEdbDataAllByEdbCode(edbCode, source, subSource, utils.EDB_DATA_LIMIT)
  284. if e != nil {
  285. br.Msg = "获取失败"
  286. br.ErrMsg = "获取指标数据失败, Err: " + e.Error()
  287. return
  288. }
  289. if len(dataList) > 0 {
  290. searchItem.EdbCode = edbCode
  291. searchItem.DataList = dataList
  292. // 获取最大最小值
  293. edbInfoMaxAndMinInfo, e := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  294. if e != nil && !utils.IsErrNoRow(e) {
  295. br.Msg = "获取失败"
  296. br.ErrMsg = "获取数据失败, GetEdbInfoMaxAndMinInfo Err:" + e.Error()
  297. return
  298. }
  299. if edbInfoMaxAndMinInfo != nil {
  300. searchItem.StartDate = edbInfoMaxAndMinInfo.MinDate
  301. searchItem.EndDate = edbInfoMaxAndMinInfo.MaxDate
  302. }
  303. } else {
  304. // 新增指标数据
  305. addRes, e := data.AddEdbData(source, edbCode, frequency)
  306. if e != nil {
  307. br.Msg = "获取失败"
  308. br.ErrMsg = "获取失败,Err:" + e.Error()
  309. return
  310. }
  311. if addRes == nil {
  312. br.Msg = "获取失败"
  313. br.ErrMsg = "新增指标数据失败, addRes nil"
  314. return
  315. }
  316. if addRes.Ret != 200 {
  317. br.Msg = "未搜索到该指标, " + addRes.Msg
  318. //br.ErrMsg = addRes.ErrMsg + ";EdbCode:" + edbCode
  319. br.Success = true
  320. return
  321. }
  322. isAdd = true
  323. }
  324. // 查询基本信息
  325. baseIndexInfo, tErr := data_manage.GetBaseIndexInfoByEdbCode(edbCode, source)
  326. if tErr == nil {
  327. searchItem.EdbName = baseIndexInfo.IndexName
  328. searchItem.Frequency = baseIndexInfo.Frequency
  329. searchItem.Unit = baseIndexInfo.Unit
  330. }
  331. }
  332. if isAdd {
  333. if subSource == utils.DATA_SUB_SOURCE_DATE {
  334. if source == utils.DATA_SOURCE_WIND {
  335. //wsdData := windWsdRespItem.Data
  336. //var buf bytes.Buffer
  337. //// 创建一个encoder并指定目标为buf
  338. //enc := gob.NewEncoder(&buf)
  339. //
  340. //// 将data编码到buf中
  341. //err = enc.Encode(wsdData)
  342. //if err != nil {
  343. // br.Msg = "获取失败"
  344. // br.ErrMsg = "获取数据失败,wind wsd encoding GOB Err:" + err.Error()
  345. // return
  346. //}
  347. //
  348. //wsdDataMap := make(map[string]map[string]interface{})
  349. //err = json.Unmarshal(buf.Bytes(), &wsdDataMap)
  350. //if err != nil {
  351. // br.Msg = "获取失败"
  352. // br.ErrMsg = "获取数据失败,wind wsd json.Unmarshal Err:" + err.Error()
  353. // return
  354. //}
  355. //
  356. //stockList := make([]*data_manage.StockInfo, 0)
  357. //
  358. //dataList := make([]*data_manage.EdbInfoSearchData, 0)
  359. //for wk, wv := range wsdDataMap {
  360. // for vk, vv := range wv {
  361. // wkInt, err := strconv.ParseInt(wk, 10, 64)
  362. // if err != nil {
  363. // br.Msg = "获取失败"
  364. // br.ErrMsg = "获取数据失败,转换日期类型失败 Err:" + err.Error()
  365. // return
  366. // }
  367. //
  368. // vk = strings.ToLower(vk)
  369. // wkInt = wkInt / 1000
  370. // t := time.Unix(wkInt, 0)
  371. // dateTime := t.Format(utils.FormatDate)
  372. // //saveVal := utils.SubFloatToString(val, 20)
  373. // if vv == nil {
  374. // continue
  375. // }
  376. // var saveVal string
  377. // switch vt := vv.(type) {
  378. // case int:
  379. // saveVal = strconv.Itoa(vt)
  380. // case float64:
  381. // saveVal = utils.SubFloatToFloatStr(vt, 20)
  382. // case string:
  383. // saveVal = vt
  384. // }
  385. //
  386. // dataItem := new(data_manage.EdbInfoSearchData)
  387. // dataItem.Value = saveVal
  388. // dataItem.DataTime = dateTime
  389. // dataList = append(dataList, dataItem)
  390. // if len(dataList) >= 10 {
  391. // break
  392. // }
  393. // }
  394. //}
  395. //searchItem.StockList = stockList
  396. stockList := make([]*data_manage.StockInfo, 0)
  397. edbCodeArr := strings.Split(edbCode, ",")
  398. for _, v := range edbCodeArr {
  399. indexCode := utils.WindDbWsd + stockCode + v
  400. dataList, err := data_manage.GetEdbDataAllByEdbCodeAndSubSource(indexCode, source, utils.DATA_SUB_SOURCE_DATE, utils.EDB_DATA_LIMIT)
  401. if err != nil && !utils.IsErrNoRow(err) {
  402. br.Msg = "获取失败"
  403. br.ErrMsg = "获取数据失败,GetEdbDataAllByEdbCode Err:" + err.Error()
  404. return
  405. }
  406. stockInfo := new(data_manage.StockInfo)
  407. stockInfo.StockCode = stockCode
  408. stockInfo.EdbCode = v
  409. stockInfo.DataList = dataList
  410. stockList = append(stockList, stockInfo)
  411. }
  412. searchItem.StockList = stockList
  413. } else if source == utils.DATA_SOURCE_THS {
  414. stockList := make([]*data_manage.StockInfo, 0)
  415. edbCodeArr := strings.Split(edbCode, ",")
  416. for _, v := range edbCodeArr {
  417. indexCode := utils.ThsDs + stockCode + v
  418. dataList, err := data_manage.GetEdbDataAllByEdbCodeAndSubSource(indexCode, source, utils.DATA_SUB_SOURCE_DATE, utils.EDB_DATA_LIMIT)
  419. if err != nil && !utils.IsErrNoRow(err) {
  420. br.Msg = "获取失败"
  421. br.ErrMsg = "获取数据失败,GetEdbDataAllByEdbCode Err:" + err.Error()
  422. return
  423. }
  424. stockInfo := new(data_manage.StockInfo)
  425. stockInfo.StockCode = stockCode
  426. stockInfo.EdbCode = v
  427. stockInfo.DataList = dataList
  428. stockList = append(stockList, stockInfo)
  429. }
  430. searchItem.StockList = stockList
  431. } else {
  432. br.Msg = "来源错误"
  433. br.ErrMsg = "来源错误"
  434. return
  435. }
  436. } else {
  437. dataList, err := data_manage.GetEdbDataAllByEdbCode(edbCode, source, subSource, utils.EDB_DATA_LIMIT)
  438. if err != nil && !utils.IsErrNoRow(err) {
  439. br.Msg = "获取失败"
  440. br.ErrMsg = "获取数据失败,GetEdbDataAllByEdbCode Err:" + err.Error()
  441. return
  442. }
  443. searchItem.DataList = dataList
  444. edbInfoMaxAndMinInfo, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  445. if err != nil && !utils.IsErrNoRow(err) {
  446. br.Msg = "获取失败"
  447. br.ErrMsg = "获取数据失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  448. return
  449. }
  450. searchItem.EdbCode = edbCode
  451. searchItem.DataList = dataList
  452. if edbInfoMaxAndMinInfo != nil {
  453. searchItem.StartDate = edbInfoMaxAndMinInfo.MinDate
  454. searchItem.EndDate = edbInfoMaxAndMinInfo.MaxDate
  455. }
  456. //edb指标信息
  457. edbInfoItem, err := data_manage.GetEdbInfoByEdbCode(source, edbCode)
  458. if edbInfoItem != nil {
  459. searchItem.EdbName = edbInfoItem.EdbName
  460. }
  461. }
  462. }
  463. resp.Status = 2
  464. resp.SearchItem = searchItem
  465. }
  466. //如果不在指标库中,且不是新增
  467. if !isInEdb && !isAdd {
  468. if resp.SearchItem.DataList == nil || len(resp.SearchItem.DataList) <= 0 {
  469. br.Msg = "目标指标无有效值,请重新搜索"
  470. br.Data = resp
  471. return
  472. }
  473. }
  474. br.Ret = 200
  475. br.Success = true
  476. br.Msg = "获取成功"
  477. br.Data = resp
  478. }
  479. // EdbInfoList
  480. // @Title 指标列表接口
  481. // @Description 指标列表接口
  482. // @Param PageSize query int true "每页数据条数"
  483. // @Param CurrentIndex query int true "当前页页码,从1开始"
  484. // @Param EdbInfoId query int true "指标id"
  485. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  486. // @Success 200 {object} data_manage.EdbInfoListResp
  487. // @router /edb_info/list [get]
  488. func (this *EdbInfoController) EdbInfoList() {
  489. br := new(models.BaseResponse).Init()
  490. defer func() {
  491. this.Data["json"] = br
  492. this.ServeJSON()
  493. }()
  494. edbInfoId, _ := this.GetInt("EdbInfoId")
  495. keyWord := this.GetString("KeyWord")
  496. pageSize, _ := this.GetInt("PageSize")
  497. currentIndex, _ := this.GetInt("CurrentIndex")
  498. var total int
  499. page := paging.GetPaging(currentIndex, pageSize, total)
  500. var startSize int
  501. if pageSize <= 0 {
  502. pageSize = utils.PageSize20
  503. }
  504. if currentIndex <= 0 {
  505. currentIndex = 1
  506. }
  507. startSize = paging.StartIndex(currentIndex, pageSize)
  508. var condition string
  509. var pars []interface{}
  510. //if edbInfoId <= 0 && keyWord == "" {
  511. // br.Msg = "参数错误"
  512. // return
  513. //}
  514. // 指标类型,0:普通指标,1:预测指标
  515. condition += " AND edb_info_type=? "
  516. pars = append(pars, 0)
  517. if edbInfoId > 0 {
  518. condition += " AND edb_info_id=? "
  519. pars = append(pars, edbInfoId)
  520. // 获取当前账号的不可见指标配置
  521. obj := data_manage.EdbInfoNoPermissionAdmin{}
  522. conf, err := obj.GetByEdbInfoIdAndAdminId(this.SysUser.AdminId, edbInfoId)
  523. if err != nil && !utils.IsErrNoRow(err) {
  524. br.Msg = "获取失败"
  525. br.ErrMsg = "获取当前账号的不可见指标配置失败,err:" + err.Error()
  526. return
  527. }
  528. if conf != nil && conf.Id > 0 {
  529. br.Msg = "无权限,请联系管理员"
  530. br.IsSendEmail = false
  531. return
  532. }
  533. }
  534. if keyWord != "" {
  535. condition += ` AND ( edb_code LIKE '%` + keyWord + `%' OR edb_name LIKE '%` + keyWord + `%' )`
  536. //pars = append(pars, keyWord)
  537. //pars = append(pars, keyWord)
  538. }
  539. resp := new(data_manage.EdbInfoListResp)
  540. if condition == "" {
  541. item := new(data_manage.EdbInfoList)
  542. resp.Paging = page
  543. resp.Item = item
  544. br.Ret = 200
  545. br.Success = true
  546. br.Msg = "获取成功"
  547. return
  548. }
  549. //获取指标信息
  550. edbInfoItem, err := data_manage.GetEdbInfoByCondition(condition, pars)
  551. if err != nil && !utils.IsErrNoRow(err) {
  552. br.Success = true
  553. br.Msg = "获取指标信息失败"
  554. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  555. return
  556. }
  557. if edbInfoItem == nil || (err != nil && utils.IsErrNoRow(err)) {
  558. item := new(data_manage.EdbInfoList)
  559. resp.Paging = page
  560. resp.Item = item
  561. br.Ret = 200
  562. br.Success = true
  563. br.Msg = "获取成功"
  564. return
  565. }
  566. // 时间转格式
  567. edbInfoItem.ConvertToResp()
  568. //获取英文频度
  569. edbInfoItem.FrequencyEn = data.GetFrequencyEn(edbInfoItem.Frequency)
  570. // 获取是否供应商停更
  571. edbInfoItem.IsSupplierStop = data.GetIsSupplierStop(edbInfoItem.Source, edbInfoItem.EdbCode)
  572. //查询目录
  573. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfoItem.ClassifyId)
  574. if err != nil {
  575. br.Msg = err.Error()
  576. br.ErrMsg = errMsg
  577. return
  578. }
  579. // 权限校验
  580. {
  581. var currClassify *data_manage.EdbClassifyIdItems
  582. for _, v := range classifyList {
  583. if edbInfoItem.ClassifyId == v.ClassifyId {
  584. currClassify = v
  585. break
  586. }
  587. }
  588. if currClassify != nil {
  589. edbInfoItem.HaveOperaAuth, err = data_manage_permission.CheckEdbPermission(edbInfoItem.IsJoinPermission, currClassify.IsJoinPermission, this.SysUser.AdminId, edbInfoItem.EdbInfoId, edbInfoItem.ClassifyId)
  590. if err != nil {
  591. br.Msg = err.Error()
  592. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  593. return
  594. }
  595. }
  596. }
  597. // 按钮校验
  598. button := data.GetEdbOpButton(this.SysUser, edbInfoItem.SysUserId, edbInfoItem.EdbType, edbInfoItem.EdbInfoType, edbInfoItem.HaveOperaAuth)
  599. button.AddButton = false
  600. edbInfoItem.Button = button
  601. edbInfoItem.DataList = make([]*data_manage.EdbData, 0)
  602. // 如果有数据权限,那么就去获取指标数据
  603. if edbInfoItem.HaveOperaAuth {
  604. dataCount, dataList, err := data.GetPageData(edbInfoItem.EdbInfoId, edbInfoItem.Source, edbInfoItem.SubSource, "", startSize, pageSize)
  605. if err != nil && !utils.IsErrNoRow(err) {
  606. br.Msg = "获取指标信息失败"
  607. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  608. return
  609. }
  610. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  611. edbInfoItem.DataList = dataList
  612. }
  613. // 获取指标插入配置
  614. {
  615. edbDataInsertConfig, err := data_manage.GetEdbDataInsertConfigByEdbId(edbInfoId)
  616. if err != nil && !utils.IsErrNoRow(err) {
  617. br.Msg = "获取指标信息失败"
  618. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  619. return
  620. }
  621. if edbDataInsertConfig != nil {
  622. edbInfoItem.DataInsertConfig.Date = edbDataInsertConfig.Date.Format(utils.FormatDate)
  623. edbInfoItem.DataInsertConfig.RealDate = edbDataInsertConfig.RealDate.Format(utils.FormatDate)
  624. edbInfoItem.DataInsertConfig.Value = edbDataInsertConfig.Value
  625. }
  626. }
  627. //是否展示英文标识
  628. edbInfoItem.IsEnEdb = data.CheckIsEnEdb(edbInfoItem.EdbNameEn, edbInfoItem.Unit, edbInfoItem.UnitEn)
  629. // 所属收藏分类id列表
  630. {
  631. obj := data_manage.EdbCollect{}
  632. tmpCondition := ` AND sys_user_id = ? AND edb_info_id = ? `
  633. tmpPars := []interface{}{this.SysUser.AdminId, edbInfoId}
  634. collectList, err := obj.GetItemsByCondition(tmpCondition, tmpPars, []string{"edb_collect_classify_id"}, "")
  635. if err != nil {
  636. br.Msg = "获取指标信息失败"
  637. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  638. return
  639. }
  640. collectIdList := make([]int, 0)
  641. for _, v := range collectList {
  642. collectIdList = append(collectIdList, v.EdbCollectClassifyId)
  643. }
  644. edbInfoItem.CollectClassifyIdList = collectIdList
  645. }
  646. //CollectClassifyIdList
  647. resp.Paging = page
  648. resp.Item = edbInfoItem
  649. resp.ClassifyList = classifyList
  650. br.Ret = 200
  651. br.Success = true
  652. br.Msg = "获取成功"
  653. br.Data = resp
  654. }
  655. // EdbInfoAdd
  656. // @Title 新增指标接口
  657. // @Description 新增指标接口
  658. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  659. // @Success Ret=200 保存成功
  660. // @router /edb_info/add [post]
  661. func (this *EdbInfoController) EdbInfoAdd() {
  662. br := new(models.BaseResponse).Init()
  663. defer func() {
  664. this.Data["json"] = br
  665. this.ServeJSON()
  666. }()
  667. sysUser := this.SysUser
  668. if sysUser == nil {
  669. br.Msg = "请登录"
  670. br.ErrMsg = "请登录,SysUser Is Empty"
  671. br.Ret = 408
  672. return
  673. }
  674. deleteCache := true
  675. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  676. defer func() {
  677. if deleteCache {
  678. utils.Rc.Delete(cacheKey)
  679. }
  680. }()
  681. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  682. deleteCache = false
  683. br.Msg = "系统处理中,请稍后重试!"
  684. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  685. return
  686. }
  687. var req data_manage.AddEdbInfoReq
  688. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  689. if err != nil {
  690. br.Msg = "参数解析异常!"
  691. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  692. return
  693. }
  694. source := req.Source
  695. if source <= 0 {
  696. br.Msg = "无效的数据来源"
  697. return
  698. }
  699. req.EdbName = strings.Trim(req.EdbName, " ")
  700. req.EdbCode = strings.Trim(req.EdbCode, " ")
  701. if req.EdbCode == "" {
  702. br.Msg = "指标ID不能为空"
  703. return
  704. }
  705. if req.EdbName == "" {
  706. br.Msg = "指标名称不能为空"
  707. return
  708. }
  709. if req.Frequency == "" {
  710. br.Msg = "频率不能为空"
  711. return
  712. }
  713. if req.Unit == "" {
  714. br.Msg = "单位不能为空"
  715. return
  716. }
  717. if req.ClassifyId <= 0 {
  718. br.Msg = "请选择分类"
  719. return
  720. }
  721. // 指标入库
  722. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, req.StartDate, req.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
  723. if err != nil {
  724. br.Msg = "保存失败"
  725. if errMsg != `` {
  726. br.Msg = errMsg
  727. }
  728. br.ErrMsg = err.Error()
  729. br.IsSendEmail = isSendEmail
  730. return
  731. }
  732. //新增操作日志
  733. {
  734. edbLog := new(data_manage.EdbInfoLog)
  735. edbLog.EdbInfoId = edbInfo.EdbInfoId
  736. edbLog.SourceName = edbInfo.SourceName
  737. edbLog.Source = edbInfo.Source
  738. edbLog.EdbCode = edbInfo.EdbCode
  739. edbLog.EdbName = edbInfo.EdbName
  740. edbLog.ClassifyId = edbInfo.ClassifyId
  741. edbLog.SysUserId = sysUser.AdminId
  742. edbLog.SysUserRealName = sysUser.RealName
  743. edbLog.CreateTime = time.Now()
  744. edbLog.Content = string(this.Ctx.Input.RequestBody)
  745. edbLog.Status = "新增指标"
  746. edbLog.Method = this.Ctx.Input.URI()
  747. go data_manage.AddEdbInfoLog(edbLog)
  748. }
  749. // 更新es
  750. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  751. resp := new(data_manage.AddEdbInfoResp)
  752. resp.EdbInfoId = edbInfo.EdbInfoId
  753. resp.UniqueCode = edbInfo.UniqueCode
  754. br.Ret = 200
  755. br.Success = true
  756. br.Msg = "保存成功"
  757. br.Data = resp
  758. br.IsAddLog = true
  759. }
  760. // EdbInfoEdit
  761. // @Title 编辑指标接口
  762. // @Description 编辑指标接口
  763. // @Success 200 {object} data_manage.EditEdbInfoReq
  764. // @router /edb_info/edit [post]
  765. func (this *EdbInfoController) EdbInfoEdit() {
  766. br := new(models.BaseResponse).Init()
  767. defer func() {
  768. this.Data["json"] = br
  769. this.ServeJSON()
  770. }()
  771. sysUser := this.SysUser
  772. if sysUser == nil {
  773. br.Msg = "请登录"
  774. br.ErrMsg = "请登录,SysUser Is Empty"
  775. br.Ret = 408
  776. return
  777. }
  778. var req data_manage.EditEdbInfoReq
  779. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  780. if err != nil {
  781. br.Msg = "参数解析异常!"
  782. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  783. return
  784. }
  785. req.EdbName = strings.Trim(req.EdbName, " ")
  786. if req.EdbInfoId <= 0 {
  787. br.Msg = "参数错误"
  788. br.ErrMsg = "指标id小于等于0"
  789. return
  790. }
  791. if req.EdbName == "" {
  792. br.Msg = "指标名称不能为空"
  793. return
  794. }
  795. if req.Frequency == "" {
  796. br.Msg = "频率不能为空"
  797. return
  798. }
  799. if req.Unit == "" {
  800. br.Msg = "单位不能为空"
  801. return
  802. }
  803. if req.ClassifyId <= 0 {
  804. br.Msg = "请选择分类"
  805. return
  806. }
  807. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  808. if err != nil {
  809. br.Msg = "修改失败"
  810. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  811. return
  812. }
  813. // 记录旧的指标基本信息
  814. oldEdbName := edbInfo.EdbName
  815. oldFrequency := edbInfo.Frequency
  816. oldUnit := edbInfo.Unit
  817. var haveOperaAuth bool
  818. // 权限校验
  819. {
  820. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, this.SysUser.AdminId)
  821. if err != nil {
  822. br.Msg = err.Error()
  823. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  824. return
  825. }
  826. }
  827. // 编辑权限校验
  828. button := data.GetEdbOpButton(this.SysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  829. if !button.OpButton {
  830. br.Msg = "无权限操作"
  831. br.IsSendEmail = false
  832. return
  833. }
  834. //判断指标名称是否存在
  835. var condition string
  836. var pars []interface{}
  837. condition += " AND edb_info_id<>? "
  838. pars = append(pars, req.EdbInfoId)
  839. //condition += " AND source=? "
  840. //pars = append(pars, edbInfo.Source)
  841. updateCols := make([]string, 0)
  842. switch this.Lang {
  843. case utils.EnLangVersion:
  844. condition += " AND edb_name_en = ? "
  845. edbInfo.EdbNameEn = req.EdbName
  846. edbInfo.Frequency = req.Frequency
  847. edbInfo.UnitEn = req.Unit
  848. edbInfo.ClassifyId = req.ClassifyId
  849. updateCols = append(updateCols, "EdbNameEn", "Frequency", "UnitEn", "ClassifyId")
  850. default:
  851. condition += " AND edb_name=? "
  852. edbInfo.EdbName = req.EdbName
  853. edbInfo.EdbNameSource = req.EdbName
  854. edbInfo.Frequency = req.Frequency
  855. edbInfo.Unit = req.Unit
  856. edbInfo.ClassifyId = req.ClassifyId
  857. updateCols = append(updateCols, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId")
  858. }
  859. pars = append(pars, req.EdbName)
  860. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  861. if err != nil {
  862. br.Msg = "判断指标名称是否存在失败"
  863. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  864. return
  865. }
  866. if count > 0 {
  867. br.Msg = "指标名称已存在,请重新填写"
  868. br.ErrMsg = "指标名称已存在,请重新填写"
  869. br.IsSendEmail = false
  870. return
  871. }
  872. if len(updateCols) > 0 {
  873. err = edbInfo.Update(updateCols)
  874. if err != nil {
  875. br.Msg = "保存失败"
  876. br.ErrMsg = "修改失败,Err:" + err.Error()
  877. return
  878. }
  879. }
  880. //处理同名指标
  881. //{
  882. // edbNameList, err := data_manage.GetEdbInfoByName(req.EdbName)
  883. // if err != nil {
  884. // br.Msg = "保存失败"
  885. // br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  886. // return
  887. // }
  888. // if len(edbNameList) >= 2 {
  889. // for _, v := range edbNameList {
  890. // edbName := v.EdbName + "(" + v.SourceName + ")"
  891. // err = data_manage.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  892. // if err != nil {
  893. // br.Msg = "保存失败"
  894. // br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  895. // return
  896. // }
  897. // }
  898. // }
  899. //}
  900. // 新增保存记录日志
  901. oldEdbInfo := new(data_manage.EdbInfo)
  902. oldEdbInfo.EdbInfoId = edbInfo.EdbInfoId
  903. oldEdbInfo.EdbName = oldEdbName
  904. oldEdbInfo.Frequency = oldFrequency
  905. oldEdbInfo.Unit = oldUnit
  906. newEdbInfoRecord := new(data_manage.EdbInfoEditRecord)
  907. newEdbInfoRecord.EdbName = req.EdbName
  908. newEdbInfoRecord.Frequency = req.Frequency
  909. newEdbInfoRecord.Unit = req.Unit
  910. newEdbInfoRecord.OperateUserId = sysUser.AdminId
  911. newEdbInfoRecord.OperateUserRealName = sysUser.RealName
  912. go data.AddEditEdbInfoRcord(oldEdbInfo, newEdbInfoRecord)
  913. //新增操作日志
  914. {
  915. edbLog := new(data_manage.EdbInfoLog)
  916. edbLog.EdbInfoId = edbInfo.EdbInfoId
  917. edbLog.SourceName = edbInfo.SourceName
  918. edbLog.Source = edbInfo.Source
  919. edbLog.EdbCode = edbInfo.EdbCode
  920. edbLog.EdbName = edbInfo.EdbName
  921. edbLog.ClassifyId = edbInfo.ClassifyId
  922. edbLog.SysUserId = sysUser.AdminId
  923. edbLog.SysUserRealName = sysUser.RealName
  924. edbLog.CreateTime = time.Now()
  925. edbLog.Content = string(this.Ctx.Input.RequestBody)
  926. edbLog.Status = "编辑指标"
  927. edbLog.Method = this.Ctx.Input.URI()
  928. go data_manage.AddEdbInfoLog(edbLog)
  929. }
  930. //添加es
  931. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  932. switch this.Lang {
  933. case utils.EnLangVersion:
  934. // 修改关联的预测指标基础信息
  935. go data.ModifyPredictEdbEnBaseInfoBySourceEdb(edbInfo, req.Unit)
  936. default:
  937. // 修改关联的预测指标基础信息
  938. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfo, req.Frequency, req.Unit)
  939. }
  940. br.Ret = 200
  941. br.Success = true
  942. br.Msg = "保存成功"
  943. br.IsAddLog = true
  944. }
  945. // EdbEnInfoEdit
  946. // @Title 编辑指标英文信息接口
  947. // @Description 编辑指标英文信息接口
  948. // @Success 200 {object} data_manage.EditEdbEnInfoReq
  949. // @router /edb_info/en/edit [post]
  950. func (this *EdbInfoController) EdbEnInfoEdit() {
  951. br := new(models.BaseResponse).Init()
  952. defer func() {
  953. this.Data["json"] = br
  954. this.ServeJSON()
  955. }()
  956. sysUser := this.SysUser
  957. if sysUser == nil {
  958. br.Msg = "请登录"
  959. br.ErrMsg = "请登录,SysUser Is Empty"
  960. br.Ret = 408
  961. return
  962. }
  963. var req data_manage.EditEdbEnInfoReq
  964. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  965. if err != nil {
  966. br.Msg = "参数解析异常!"
  967. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  968. return
  969. }
  970. req.EdbNameEn = strings.Trim(req.EdbNameEn, " ")
  971. req.UnitEn = strings.Trim(req.UnitEn, " ")
  972. if req.EdbInfoId <= 0 {
  973. br.Msg = "参数错误"
  974. br.ErrMsg = "指标id小于等于0"
  975. return
  976. }
  977. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  978. if err != nil {
  979. br.Msg = "获取指标信息失败"
  980. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  981. return
  982. }
  983. if req.EdbNameEn != "" {
  984. //判断指标名称是否存在
  985. var condition string
  986. var pars []interface{}
  987. condition += " AND edb_info_id<>? "
  988. pars = append(pars, req.EdbInfoId)
  989. /*condition += " AND source=? "
  990. pars = append(pars, edbInfo.Source)*/
  991. condition += " AND edb_name_en=? "
  992. pars = append(pars, req.EdbNameEn)
  993. edbExist, err := data_manage.GetEdbInfoByCondition(condition, pars)
  994. if err != nil {
  995. if !utils.IsErrNoRow(err) {
  996. br.Msg = "判断英文指标名称是否存在失败"
  997. br.ErrMsg = "判断英文指标名称是否存在失败,Err:" + err.Error()
  998. return
  999. }
  1000. }
  1001. if err == nil && edbExist.EdbInfoId > 0 {
  1002. br.Msg = edbExist.EdbName + ":" + req.EdbNameEn + "指标名称已存在"
  1003. br.ErrMsg = "英文指标名称已存在,请重新填写"
  1004. br.IsSendEmail = false
  1005. return
  1006. }
  1007. }
  1008. err = data_manage.ModifyEdbEnInfo(&req)
  1009. if err != nil {
  1010. br.Msg = "保存失败"
  1011. br.ErrMsg = "修改失败,Err:" + err.Error()
  1012. return
  1013. }
  1014. //新增操作日志
  1015. {
  1016. edbLog := new(data_manage.EdbInfoLog)
  1017. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1018. edbLog.SourceName = edbInfo.SourceName
  1019. edbLog.Source = edbInfo.Source
  1020. edbLog.EdbCode = edbInfo.EdbCode
  1021. edbLog.EdbName = edbInfo.EdbName
  1022. edbLog.ClassifyId = edbInfo.ClassifyId
  1023. edbLog.SysUserId = sysUser.AdminId
  1024. edbLog.SysUserRealName = sysUser.RealName
  1025. edbLog.CreateTime = time.Now()
  1026. edbLog.Content = string(this.Ctx.Input.RequestBody)
  1027. edbLog.Status = "编辑指标英文信息"
  1028. edbLog.Method = this.Ctx.Input.URI()
  1029. go data_manage.AddEdbInfoLog(edbLog)
  1030. }
  1031. //添加es
  1032. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  1033. // 修改关联的预测指标基础信息
  1034. if edbInfo.EdbInfoType == 0 {
  1035. go data.ModifyPredictEdbEnBaseInfoBySourceEdb(edbInfo, req.UnitEn)
  1036. }
  1037. br.Ret = 200
  1038. br.Success = true
  1039. br.Msg = "保存成功"
  1040. br.IsAddLog = true
  1041. }
  1042. // EdbInfoBaseEdit
  1043. // @Title 编辑指标基础信息接口
  1044. // @Description 编辑指标基础信息接口
  1045. // @Success 200 {object} data_manage.EditEdbBaseInfoReq
  1046. // @router /edb_info/base/edit [post]
  1047. func (this *EdbInfoController) EdbInfoBaseEdit() {
  1048. br := new(models.BaseResponse).Init()
  1049. defer func() {
  1050. this.Data["json"] = br
  1051. this.ServeJSON()
  1052. }()
  1053. sysUser := this.SysUser
  1054. if sysUser == nil {
  1055. br.Msg = "请登录"
  1056. br.ErrMsg = "请登录,SysUser Is Empty"
  1057. br.Ret = 408
  1058. return
  1059. }
  1060. var req data_manage.EditEdbBaseInfoReq
  1061. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1062. if err != nil {
  1063. br.Msg = "参数解析异常!"
  1064. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1065. return
  1066. }
  1067. req.EdbName = strings.Trim(req.EdbName, " ")
  1068. req.Unit = strings.Trim(req.Unit, " ")
  1069. if req.EdbInfoId <= 0 {
  1070. br.Msg = "参数错误"
  1071. br.ErrMsg = "指标id小于等于0"
  1072. return
  1073. }
  1074. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1075. if err != nil {
  1076. br.Msg = "获取指标信息失败"
  1077. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1078. return
  1079. }
  1080. updateCols := make([]string, 0)
  1081. if req.EdbName != "" {
  1082. //判断指标名称是否存在
  1083. var condition string
  1084. var pars []interface{}
  1085. condition += " AND edb_info_id<>? "
  1086. pars = append(pars, req.EdbInfoId)
  1087. /*condition += " AND source=? "
  1088. pars = append(pars, edbInfo.Source)*/
  1089. switch this.Lang {
  1090. case utils.EnLangVersion:
  1091. condition += " AND edb_name_en = ? "
  1092. edbInfo.EdbNameEn = req.EdbName
  1093. edbInfo.UnitEn = req.Unit
  1094. edbInfo.ModifyTime = time.Now()
  1095. updateCols = append(updateCols, "EdbNameEn", "UnitEn", "ModifyTime")
  1096. default:
  1097. condition += " AND edb_name = ? "
  1098. edbInfo.EdbName = req.EdbName
  1099. edbInfo.EdbNameSource = req.EdbName
  1100. edbInfo.Unit = req.Unit
  1101. edbInfo.ModifyTime = time.Now()
  1102. updateCols = append(updateCols, "EdbName", "EdbNameSource", "Unit", "ModifyTime")
  1103. }
  1104. pars = append(pars, req.EdbName)
  1105. edbExist, err := data_manage.GetEdbInfoByCondition(condition, pars)
  1106. if err != nil {
  1107. if !utils.IsErrNoRow(err) {
  1108. br.Msg = "判断英文指标名称是否存在失败"
  1109. br.ErrMsg = "判断英文指标名称是否存在失败,Err:" + err.Error()
  1110. return
  1111. }
  1112. }
  1113. if err == nil && edbExist.EdbInfoId > 0 {
  1114. br.Msg = edbExist.EdbName + ":" + req.EdbName + "指标名称已存在"
  1115. br.ErrMsg = "英文指标名称已存在,请重新填写"
  1116. br.IsSendEmail = false
  1117. return
  1118. }
  1119. }
  1120. if len(updateCols) > 0 {
  1121. err = edbInfo.Update(updateCols)
  1122. if err != nil {
  1123. br.Msg = "保存失败"
  1124. br.ErrMsg = "修改失败,Err:" + err.Error()
  1125. return
  1126. }
  1127. }
  1128. //新增操作日志
  1129. {
  1130. edbLog := new(data_manage.EdbInfoLog)
  1131. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1132. edbLog.SourceName = edbInfo.SourceName
  1133. edbLog.Source = edbInfo.Source
  1134. edbLog.EdbCode = edbInfo.EdbCode
  1135. edbLog.EdbName = edbInfo.EdbName
  1136. edbLog.ClassifyId = edbInfo.ClassifyId
  1137. edbLog.SysUserId = sysUser.AdminId
  1138. edbLog.SysUserRealName = sysUser.RealName
  1139. edbLog.CreateTime = time.Now()
  1140. edbLog.Content = string(this.Ctx.Input.RequestBody)
  1141. edbLog.Status = "编辑指标英文信息"
  1142. edbLog.Method = this.Ctx.Input.URI()
  1143. go data_manage.AddEdbInfoLog(edbLog)
  1144. }
  1145. //添加es
  1146. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  1147. // 修改关联的预测指标基础信息
  1148. if edbInfo.EdbInfoType == 0 {
  1149. switch this.Lang {
  1150. case utils.EnLangVersion:
  1151. go data.ModifyPredictEdbEnBaseInfoBySourceEdb(edbInfo, req.Unit)
  1152. default:
  1153. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfo, edbInfo.Frequency, req.Unit)
  1154. }
  1155. }
  1156. br.Ret = 200
  1157. br.Success = true
  1158. br.Msg = "保存成功"
  1159. br.IsAddLog = true
  1160. }
  1161. // EdbInfoNewest
  1162. // @Title 获取最新指标接口
  1163. // @Description 获取最新指标接口
  1164. // @Success 200 {object} data_manage.EdbInfoList
  1165. // @router /edb_info/newest [get]
  1166. func (this *EdbInfoController) EdbInfoNewest() {
  1167. br := new(models.BaseResponse).Init()
  1168. defer func() {
  1169. this.Data["json"] = br
  1170. this.ServeJSON()
  1171. }()
  1172. item, err := data_manage.GetEdbInfoByNewest()
  1173. if err != nil && !utils.IsErrNoRow(err) {
  1174. br.Msg = "获取最新指标失败"
  1175. br.ErrMsg = "获取最新指标失败,Err:" + err.Error()
  1176. return
  1177. }
  1178. br.Ret = 200
  1179. br.Success = true
  1180. br.Msg = "获取成功"
  1181. br.Data = item
  1182. }
  1183. // EdbInfoRefresh
  1184. // @Title 指标刷新接口
  1185. // @Description 指标刷新接口
  1186. // @Param EdbInfoId query int true "指标id"
  1187. // @Success Ret=200 刷新成功
  1188. // @router /edb_info/refresh [get]
  1189. func (this *EdbInfoController) EdbInfoRefresh() {
  1190. br := new(models.BaseResponse).Init()
  1191. defer func() {
  1192. this.Data["json"] = br
  1193. this.ServeJSON()
  1194. }()
  1195. sysUser := this.SysUser
  1196. if sysUser == nil {
  1197. br.Msg = "请登录"
  1198. br.ErrMsg = "请登录,SysUser Is Empty"
  1199. br.Ret = 408
  1200. return
  1201. }
  1202. edbInfoId, _ := this.GetInt("EdbInfoId")
  1203. if edbInfoId <= 0 {
  1204. br.Msg = "参数错误"
  1205. br.ErrMsg = "参数错误"
  1206. return
  1207. }
  1208. _, err := data_manage.GetEdbInfoById(edbInfoId)
  1209. if err != nil {
  1210. if utils.IsErrNoRow(err) {
  1211. br.Msg = "数据已被删除,请刷新页面"
  1212. br.ErrMsg = "数据已被删除,请刷新页面,edbInfoId:" + strconv.Itoa(edbInfoId)
  1213. return
  1214. }
  1215. br.Msg = "刷新失败"
  1216. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1217. return
  1218. }
  1219. err, isAsync := data.EdbInfoRefreshAllFromBaseV2(edbInfoId, false, false)
  1220. if err != nil {
  1221. br.Msg = "刷新失败"
  1222. br.ErrMsg = "刷新指标失败,EdbInfoRefresh Err:" + err.Error()
  1223. return
  1224. }
  1225. br.Ret = 200
  1226. br.Msg = "刷新成功"
  1227. if isAsync {
  1228. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  1229. }
  1230. br.Success = true
  1231. br.IsAddLog = true
  1232. }
  1233. // EdbInfoMove
  1234. // @Title 移动指标接口
  1235. // @Description 移动指标接口
  1236. // @Success 200 {object} data_manage.MoveEdbInfoReq
  1237. // @router /edb_info/move [post]
  1238. func (this *EdbInfoController) EdbInfoMove() {
  1239. br := new(models.BaseResponse).Init()
  1240. defer func() {
  1241. this.Data["json"] = br
  1242. this.ServeJSON()
  1243. }()
  1244. sysUser := this.SysUser
  1245. if sysUser == nil {
  1246. br.Msg = "请登录"
  1247. br.ErrMsg = "请登录,SysUser Is Empty"
  1248. br.Ret = 408
  1249. return
  1250. }
  1251. var req data_manage.MoveEdbInfoReq
  1252. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1253. if err != nil {
  1254. br.Msg = "参数解析异常!"
  1255. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1256. return
  1257. }
  1258. if req.EdbInfoId <= 0 {
  1259. br.Msg = "参数错误"
  1260. br.ErrMsg = "指标id小于等于0"
  1261. return
  1262. }
  1263. if req.ClassifyId <= 0 {
  1264. br.Msg = "请选择分类"
  1265. return
  1266. }
  1267. //判断分类是否存在
  1268. count, _ := data_manage.GetEdbClassifyCountById(req.ClassifyId)
  1269. if count <= 0 {
  1270. br.Msg = "分类已被删除,不可移动,请刷新页面"
  1271. return
  1272. }
  1273. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1274. if err != nil {
  1275. br.Msg = "移动失败"
  1276. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1277. return
  1278. }
  1279. var haveOperaAuth bool
  1280. // 权限校验
  1281. {
  1282. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.IsJoinPermission, edbInfo.EdbInfoId, edbInfo.ClassifyId, this.SysUser.AdminId)
  1283. if err != nil {
  1284. br.Msg = err.Error()
  1285. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  1286. return
  1287. }
  1288. }
  1289. // 移动权限校验
  1290. button := data.GetEdbOpButton(this.SysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  1291. if !button.MoveButton {
  1292. br.Msg = "无权限操作"
  1293. br.IsSendEmail = false
  1294. return
  1295. }
  1296. //如果改变了分类,那么移动该指标数据
  1297. if edbInfo.ClassifyId != req.ClassifyId {
  1298. err = data_manage.MoveEdbInfo(req.EdbInfoId, req.ClassifyId)
  1299. if err != nil {
  1300. br.Msg = "移动失败"
  1301. br.ErrMsg = "移动指标失败,Err:" + err.Error()
  1302. return
  1303. }
  1304. }
  1305. updateCol := make([]string, 0)
  1306. //如果有传入 上一个兄弟节点分类id
  1307. if req.PrevEdbInfoId > 0 {
  1308. prevEdbInfo, err := data_manage.GetEdbInfoById(req.PrevEdbInfoId)
  1309. if err != nil {
  1310. br.Msg = "移动失败"
  1311. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  1312. return
  1313. }
  1314. //如果是移动在两个兄弟节点之间
  1315. if req.NextEdbInfoId > 0 {
  1316. //下一个兄弟节点
  1317. nextEdbInfo, err := data_manage.GetEdbInfoById(req.NextEdbInfoId)
  1318. if err != nil {
  1319. br.Msg = "移动失败"
  1320. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  1321. return
  1322. }
  1323. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1324. if prevEdbInfo.Sort == nextEdbInfo.Sort || prevEdbInfo.Sort == edbInfo.Sort {
  1325. //变更兄弟节点的排序
  1326. updateSortStr := `sort + 2`
  1327. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  1328. } else {
  1329. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1330. if nextEdbInfo.Sort-prevEdbInfo.Sort == 1 {
  1331. //变更兄弟节点的排序
  1332. updateSortStr := `sort + 1`
  1333. _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
  1334. }
  1335. }
  1336. }
  1337. edbInfo.Sort = prevEdbInfo.Sort + 1
  1338. edbInfo.ModifyTime = time.Now()
  1339. updateCol = append(updateCol, "Sort", "ModifyTime")
  1340. } else {
  1341. firstClassify, err := data_manage.GetFirstEdbInfoByClassifyId(req.ClassifyId)
  1342. if err != nil && !utils.IsErrNoRow(err) {
  1343. br.Msg = "移动失败"
  1344. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  1345. return
  1346. }
  1347. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1348. if firstClassify != nil && firstClassify.ClassifyId > 0 && firstClassify.Sort == 0 {
  1349. updateSortStr := ` sort + 1 `
  1350. _ = data_manage.UpdateEdbInfoSortByClassifyId(firstClassify.ClassifyId, 0, firstClassify.EdbInfoId-1, updateSortStr)
  1351. }
  1352. edbInfo.Sort = 0 //那就是排在第一位
  1353. edbInfo.ModifyTime = time.Now()
  1354. updateCol = append(updateCol, "Sort", "ModifyTime")
  1355. }
  1356. //更新
  1357. if len(updateCol) > 0 {
  1358. err = edbInfo.Update(updateCol)
  1359. if err != nil {
  1360. br.Msg = "移动失败"
  1361. br.ErrMsg = "修改失败,Err:" + err.Error()
  1362. return
  1363. }
  1364. }
  1365. if err != nil {
  1366. br.Msg = "移动失败"
  1367. br.ErrMsg = "修改失败,Err:" + err.Error()
  1368. return
  1369. }
  1370. //新增操作日志
  1371. {
  1372. edbLog := new(data_manage.EdbInfoLog)
  1373. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1374. edbLog.SourceName = edbInfo.SourceName
  1375. edbLog.Source = edbInfo.Source
  1376. edbLog.EdbCode = edbInfo.EdbCode
  1377. edbLog.EdbName = edbInfo.EdbName
  1378. edbLog.ClassifyId = edbInfo.ClassifyId
  1379. edbLog.SysUserId = sysUser.AdminId
  1380. edbLog.SysUserRealName = sysUser.RealName
  1381. edbLog.CreateTime = time.Now()
  1382. edbLog.Content = string(this.Ctx.Input.RequestBody)
  1383. edbLog.Status = "移动指标"
  1384. edbLog.Method = this.Ctx.Input.URI()
  1385. go data_manage.AddEdbInfoLog(edbLog)
  1386. }
  1387. br.Ret = 200
  1388. br.Success = true
  1389. br.Msg = "移动成功"
  1390. }
  1391. // EdbInfoDetail
  1392. // @Title 获取指标详情
  1393. // @Description 获取指标详情接口
  1394. // @Param EdbInfoId query int true "指标id"
  1395. // @Success 200 {object} data_manage.EdbInfo
  1396. // @router /edb_info/detail [get]
  1397. func (this *EdbInfoController) EdbInfoDetail() {
  1398. br := new(models.BaseResponse).Init()
  1399. defer func() {
  1400. this.Data["json"] = br
  1401. this.ServeJSON()
  1402. }()
  1403. sysUser := this.SysUser
  1404. if sysUser == nil {
  1405. br.Msg = "请登录"
  1406. br.ErrMsg = "请登录,SysUser Is Empty"
  1407. br.Ret = 408
  1408. return
  1409. }
  1410. edbInfoId, _ := this.GetInt("EdbInfoId")
  1411. if edbInfoId <= 0 {
  1412. br.Msg = "参数错误"
  1413. br.ErrMsg = "参数错误"
  1414. return
  1415. }
  1416. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1417. if err != nil {
  1418. br.Msg = "获取失败"
  1419. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1420. return
  1421. }
  1422. var resp data_manage.EdbInfoFullClassify
  1423. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  1424. if err != nil {
  1425. br.Msg = err.Error()
  1426. br.ErrMsg = errMsg
  1427. return
  1428. }
  1429. // 权限校验
  1430. {
  1431. classifyMap := make(map[int]*data_manage.EdbClassifyIdItems)
  1432. for _, v := range classifyList {
  1433. classifyMap[v.ClassifyId] = v
  1434. }
  1435. if currClassify, ok := classifyMap[edbInfo.ClassifyId]; ok {
  1436. haveOperaAuth, err := data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassify.IsJoinPermission, this.SysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  1437. if err != nil {
  1438. br.Msg = err.Error()
  1439. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  1440. return
  1441. }
  1442. resp.HaveOperaAuth = haveOperaAuth
  1443. }
  1444. }
  1445. edbInfoResp, err := edbInfo.ConvertToResp()
  1446. if err != nil {
  1447. br.Msg = err.Error()
  1448. br.ErrMsg = errMsg
  1449. return
  1450. }
  1451. resp.EdbInfoResp = edbInfoResp
  1452. resp.ClassifyList = classifyList
  1453. br.Ret = 200
  1454. br.Success = true
  1455. br.Msg = "获取成功"
  1456. br.Data = resp
  1457. }
  1458. // EdbInfoFilter
  1459. // @Title 指标筛选接口
  1460. // @Description 指标筛选接口
  1461. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  1462. // @Param FilterSource query int false "搜索来源:1:其他搜索,2:累计值转月值搜索,3:变频,4:基础指标"
  1463. // @Param PageSize query int true "每页数据条数"
  1464. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1465. // @Success 200 {object} data_manage.EdbInfoList
  1466. // @router /edb_info/filter [get]
  1467. func (this *EdbInfoController) EdbInfoFilter() {
  1468. br := new(models.BaseResponse).Init()
  1469. defer func() {
  1470. this.Data["json"] = br
  1471. this.ServeJSON()
  1472. }()
  1473. keyWord := this.GetString("KeyWord")
  1474. filterSource, _ := this.GetInt("FilterSource")
  1475. if filterSource <= 0 {
  1476. filterSource = 1
  1477. }
  1478. var pars []interface{}
  1479. noPermissionEdbInfoIdList := make([]int, 0) //无权限指标
  1480. // 获取当前账号的不可见指标
  1481. {
  1482. obj := data_manage.EdbInfoNoPermissionAdmin{}
  1483. confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
  1484. if err != nil && !utils.IsErrNoRow(err) {
  1485. br.Msg = "获取失败"
  1486. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1487. return
  1488. }
  1489. for _, v := range confList {
  1490. noPermissionEdbInfoIdList = append(noPermissionEdbInfoIdList, v.EdbInfoId)
  1491. }
  1492. }
  1493. var keyWordArr []string
  1494. keyWordArr = append(keyWordArr, keyWord)
  1495. if keyWord != "" {
  1496. newKeyWord := strings.Split(keyWord, " ")
  1497. keyWordArr = append(keyWordArr, newKeyWord...)
  1498. }
  1499. //var err error
  1500. edbInfoList := make([]*data_manage.EdbInfoList, 0)
  1501. //if keyWord == "" {
  1502. // edbInfoList, err = data_manage.GetEdbInfoFilter(condition, pars)
  1503. // if err != nil && !utils.IsErrNoRow(err) {
  1504. // br.Success = true
  1505. // br.Msg = "获取指标信息失败"
  1506. // br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1507. // return
  1508. // }
  1509. //} else {
  1510. // //获取指标信息
  1511. // indexName := utils.DATA_INDEX_NAME
  1512. // edbInfoList, err = data.EsSearchEdbInfo(indexName, keyWord,filterSource)
  1513. // if err != nil && !utils.IsErrNoRow(err) {
  1514. // br.Success = true
  1515. // br.Msg = "获取指标信息失败"
  1516. // br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1517. // return
  1518. // }
  1519. //}
  1520. var baseCondition string
  1521. // 无权限指标id
  1522. lenNoPermissionEdbInfoIdList := len(noPermissionEdbInfoIdList)
  1523. if lenNoPermissionEdbInfoIdList > 0 {
  1524. baseCondition = ` AND edb_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbInfoIdList) + `) `
  1525. pars = append(pars, noPermissionEdbInfoIdList)
  1526. }
  1527. existMap := make(map[int]string)
  1528. for _, v := range keyWordArr {
  1529. v = strings.Replace(v, " ", "%", -1)
  1530. var condition string
  1531. if filterSource == 2 {
  1532. condition += ` AND frequency='月度' `
  1533. }
  1534. if filterSource == 3 {
  1535. condition += ` AND frequency <> '日度' `
  1536. }
  1537. if filterSource == 4 {
  1538. condition += ` AND edb_type = 1 `
  1539. }
  1540. conditionWhere := `AND ( edb_code LIKE '%` + v + `%' OR edb_name LIKE '%` + v + `%' )`
  1541. condition += conditionWhere + baseCondition
  1542. newEdbInfoList, err := data_manage.GetEdbInfoFilter(condition, pars)
  1543. if err != nil && !utils.IsErrNoRow(err) {
  1544. br.Success = true
  1545. br.Msg = "获取指标信息失败"
  1546. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1547. return
  1548. }
  1549. for _, nv := range newEdbInfoList {
  1550. nv.ConvertToResp()
  1551. if _, ok := existMap[nv.EdbInfoId]; !ok {
  1552. edbInfoList = append(edbInfoList, nv)
  1553. }
  1554. existMap[nv.EdbInfoId] = nv.EdbCode
  1555. }
  1556. }
  1557. if edbInfoList == nil {
  1558. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1559. }
  1560. edbInfoListLen := len(edbInfoList)
  1561. for i := 0; i < edbInfoListLen; i++ {
  1562. // 时间转格式
  1563. edbInfoList[i].ConvertToResp()
  1564. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  1565. }
  1566. for i := 0; i < edbInfoListLen; i++ {
  1567. for j := 0; j < edbInfoListLen; j++ {
  1568. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1569. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1570. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1571. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1572. }
  1573. }
  1574. }
  1575. //新增搜索词记录
  1576. {
  1577. searchKeyword := new(data_manage.SearchKeyword)
  1578. searchKeyword.KeyWord = keyWord
  1579. searchKeyword.CreateTime = time.Now()
  1580. go data_manage.AddSearchKeyword(searchKeyword)
  1581. }
  1582. br.Ret = 200
  1583. br.Success = true
  1584. br.Msg = "获取成功"
  1585. br.Data = edbInfoList
  1586. }
  1587. // EdbInfoFilterByEs
  1588. // @Title 指标筛选接口
  1589. // @Description 指标筛选接口
  1590. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1591. // @Param PageSize query int true "每页数据条数"
  1592. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  1593. // @Param FilterSource query int false "搜索来源:1:其他搜索,2:累计值转月值搜索,3:变频,4:基础指标,5:同比"
  1594. // @Param Frequency query string false "频度"
  1595. // @Param Source query int true "来源"
  1596. // @Param EdbType query string false "指标类型:0-基础和计算;1-基础指标;2-计算指标;3-衍生指标"
  1597. // @Param EdbAuth query int false "指标权限:0-全部;1-我的;2-公共"
  1598. // @Param EdbCollect query int false "指标收藏状态:0-全部;1-已收藏"
  1599. // @Param ClassifyId query int false "父级分类id"
  1600. // @Success 200 {object} data_manage.EdbInfoList
  1601. // @router /edb_info/filter_by_es [get]
  1602. func (this *EdbInfoController) EdbInfoFilterByEs() {
  1603. br := new(models.BaseResponse).Init()
  1604. defer func() {
  1605. this.Data["json"] = br
  1606. this.ServeJSON()
  1607. }()
  1608. pageSize, _ := this.GetInt("PageSize")
  1609. currentIndex, _ := this.GetInt("CurrentIndex")
  1610. var total int64
  1611. var startSize int
  1612. if pageSize <= 0 {
  1613. pageSize = utils.PageSize20
  1614. }
  1615. if currentIndex <= 0 {
  1616. currentIndex = 1
  1617. }
  1618. startSize = paging.StartIndex(currentIndex, pageSize)
  1619. keyWord := this.GetString("KeyWord")
  1620. if keyWord == `` {
  1621. keyWord = this.GetString("Keyword")
  1622. }
  1623. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  1624. filterSource, _ := this.GetInt("FilterSource")
  1625. if filterSource <= 0 {
  1626. filterSource = 1
  1627. }
  1628. //指标来源
  1629. source, _ := this.GetInt("Source")
  1630. frequency := this.GetString("Frequency") //频度
  1631. edbTypeStr := this.GetString("EdbType", "0") // 指标类型:0-基础和计算;1-基础指标;2-计算指标;3-预测指标
  1632. edbAuth, _ := this.GetInt("EdbAuth", 0) // 指标权限范围,0-全部;1-我的;2-公共
  1633. edbCollect, _ := this.GetInt("EdbCollect", 0) // 指标收藏状态:0-全部;1-已收藏
  1634. // 父级分类id
  1635. classifyId, _ := this.GetInt("ClassifyId")
  1636. var edbInfoList []*data_manage.EdbInfoList
  1637. var err error
  1638. // 获取es搜索参数
  1639. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, searchUserId, frequencyList, err, errMsg := data.GetGeneralEdbEsSearchParams(edbTypeStr, frequency, this.SysUser.AdminId, edbAuth, edbCollect, classifyId)
  1640. if err != nil {
  1641. br.Msg = "获取失败"
  1642. if errMsg != `` {
  1643. br.Msg = errMsg
  1644. }
  1645. br.ErrMsg = "获取失败:" + err.Error()
  1646. return
  1647. }
  1648. sortMap := make(map[string]string)
  1649. // 如果没有搜索关键词,则默认根据指标编码倒序排序
  1650. if keyWord == `` {
  1651. sortMap["EdbInfoId"] = `desc`
  1652. }
  1653. // 普通的搜索
  1654. total, edbInfoList, err = elastic.SearchEdbInfoData(keyWord, startSize, pageSize, filterSource, source, frequencyList, []string{}, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, searchUserId, sortMap)
  1655. if err != nil {
  1656. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1657. }
  1658. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1659. edbInfoListLen := len(edbInfoList)
  1660. classifyIdList := make([]int, 0)
  1661. for _, v := range edbInfoList {
  1662. v.ConvertToResp()
  1663. v.EdbNameAlias = v.EdbName
  1664. classifyIdList = append(classifyIdList, v.ClassifyId)
  1665. }
  1666. // 当前列表中的分类map
  1667. classifyMap := make(map[int]*data_manage.EdbClassify)
  1668. if len(classifyIdList) > 0 {
  1669. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1670. if err != nil {
  1671. br.Msg = "获取失败"
  1672. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  1673. return
  1674. }
  1675. for _, v := range classifyList {
  1676. classifyMap[v.ClassifyId] = v
  1677. }
  1678. // 获取所有有权限的指标和分类
  1679. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1680. if err != nil {
  1681. br.Msg = "获取失败"
  1682. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1683. return
  1684. }
  1685. // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  1686. {
  1687. edbInfoIdList := make([]int, 0)
  1688. for _, tmpEdbInfo := range edbInfoList {
  1689. edbInfoIdList = append(edbInfoIdList, tmpEdbInfo.EdbInfoId)
  1690. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  1691. tmpEdbInfo.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1692. }
  1693. }
  1694. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1695. if err != nil {
  1696. br.Msg = "获取失败"
  1697. br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error()
  1698. return
  1699. }
  1700. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1701. for _, v := range tmpEdbList {
  1702. edbInfoMap[v.EdbInfoId] = v
  1703. }
  1704. // 查询收藏
  1705. collectEdbMap := make(map[int][]int)
  1706. collectNameEdbMap := make(map[int][]string)
  1707. {
  1708. obj := data_manage.EdbCollect{}
  1709. collectList, err := obj.GetItemsByUserIdAndEdbInfoIdList(this.SysUser.AdminId, edbInfoIdList)
  1710. if err != nil {
  1711. br.Msg = "获取指标信息失败"
  1712. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  1713. return
  1714. }
  1715. for _, v := range collectList {
  1716. collectIdList, ok := collectEdbMap[v.EdbInfoId]
  1717. if !ok {
  1718. collectIdList = make([]int, 0)
  1719. }
  1720. collectEdbMap[v.EdbInfoId] = append(collectIdList, v.EdbCollectClassifyId)
  1721. collectNameList, ok := collectNameEdbMap[v.EdbInfoId]
  1722. if !ok {
  1723. collectNameList = make([]string, 0)
  1724. }
  1725. collectNameEdbMap[v.EdbInfoId] = append(collectNameList, v.ClassifyName)
  1726. }
  1727. }
  1728. for _, v := range edbInfoList {
  1729. if collectIdList, ok := collectEdbMap[v.EdbInfoId]; ok {
  1730. v.CollectClassifyIdList = collectIdList
  1731. } else {
  1732. v.CollectClassifyIdList = []int{}
  1733. }
  1734. if collectNameList, ok := collectNameEdbMap[v.EdbInfoId]; ok {
  1735. v.CollectClassifyNameList = collectNameList
  1736. } else {
  1737. v.CollectClassifyNameList = []string{}
  1738. }
  1739. tmpEdbInfo, ok := edbInfoMap[v.EdbInfoId]
  1740. if !ok {
  1741. continue
  1742. }
  1743. v.IsJoinPermission = tmpEdbInfo.IsJoinPermission
  1744. }
  1745. }
  1746. // 权限校验
  1747. for _, tmpEdbInfoItem := range edbInfoList {
  1748. if currClassify, ok := classifyMap[tmpEdbInfoItem.ClassifyId]; ok {
  1749. tmpEdbInfoItem.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfoItem.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfoItem.EdbInfoId, tmpEdbInfoItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1750. }
  1751. }
  1752. }
  1753. for i := 0; i < edbInfoListLen; i++ {
  1754. for j := 0; j < edbInfoListLen; j++ {
  1755. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1756. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1757. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1758. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1759. }
  1760. }
  1761. }
  1762. // 不返回无权限的指标
  1763. respList := make([]*data_manage.EdbInfoList, 0)
  1764. for _, v := range edbInfoList {
  1765. if v.HaveOperaAuth {
  1766. respList = append(respList, v)
  1767. }
  1768. }
  1769. resp := data_manage.EdbInfoFilterDataResp{
  1770. Paging: page,
  1771. List: respList,
  1772. }
  1773. br.Ret = 200
  1774. br.Success = true
  1775. br.Msg = "获取成功"
  1776. br.Data = resp
  1777. }
  1778. func (this *EdbInfoController) EdbInfoFilter2() {
  1779. br := new(models.BaseResponse).Init()
  1780. defer func() {
  1781. this.Data["json"] = br
  1782. this.ServeJSON()
  1783. }()
  1784. keyWord := this.GetString("KeyWord")
  1785. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  1786. filterSource, _ := this.GetInt("FilterSource")
  1787. if filterSource <= 0 {
  1788. filterSource = 1
  1789. }
  1790. var pars []interface{}
  1791. //if keyWord != "" {
  1792. // condition += ` AND ( edb_code LIKE '%` + keyWord + `%' OR edb_name LIKE '%` + keyWord + `%' )`
  1793. //}
  1794. var keyWordArr []string
  1795. keyWordArr = append(keyWordArr, keyWord)
  1796. if keyWord != "" {
  1797. newKeyWord := strings.Split(keyWord, " ")
  1798. keyWordArr = append(keyWordArr, newKeyWord...)
  1799. }
  1800. keywordLen := len(keyWordArr)
  1801. ch := make(chan map[int][]*data_manage.EdbInfoList, keywordLen)
  1802. searchWait := sync.WaitGroup{}
  1803. searchWait.Add(keywordLen)
  1804. for index, v := range keyWordArr {
  1805. v = strings.Replace(v, " ", "%", -1)
  1806. var condition string
  1807. if filterSource == 2 {
  1808. condition += ` AND frequency='月度' `
  1809. }
  1810. if filterSource == 3 {
  1811. condition += ` AND frequency <> '日度' `
  1812. }
  1813. if filterSource == 4 {
  1814. condition += ` AND edb_type = 1 `
  1815. }
  1816. conditionWhere := `AND ( edb_code LIKE '%` + v + `%' OR edb_name LIKE '%` + v + `%' )`
  1817. condition += conditionWhere
  1818. go searchEdbInfoFilter(condition, pars, index, ch, &searchWait)
  1819. }
  1820. searchWait.Wait()
  1821. newEdbInfoListMap := make(map[int][]*data_manage.EdbInfoList)
  1822. i := 0
  1823. for tmpEdbInfoList := range ch {
  1824. i++
  1825. for k, v := range tmpEdbInfoList {
  1826. newEdbInfoListMap[k] = v
  1827. }
  1828. if i == keywordLen {
  1829. break
  1830. }
  1831. }
  1832. close(ch)
  1833. edbInfoList := make([]*data_manage.EdbInfoList, 0)
  1834. existMap := make(map[int]string)
  1835. for i := 0; i < keywordLen; i++ {
  1836. if newEdbInfoList, ok := newEdbInfoListMap[i]; ok {
  1837. for _, nv := range newEdbInfoList {
  1838. if _, ok := existMap[nv.EdbInfoId]; !ok {
  1839. edbInfoList = append(edbInfoList, nv)
  1840. }
  1841. existMap[nv.EdbInfoId] = nv.EdbCode
  1842. }
  1843. }
  1844. }
  1845. if edbInfoList == nil {
  1846. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1847. }
  1848. edbInfoListLen := len(edbInfoList)
  1849. for i := 0; i < edbInfoListLen; i++ {
  1850. edbInfoList[i].ConvertToResp()
  1851. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  1852. }
  1853. for i := 0; i < edbInfoListLen; i++ {
  1854. for j := 0; j < edbInfoListLen; j++ {
  1855. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1856. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1857. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1858. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1859. }
  1860. }
  1861. }
  1862. //新增搜索词记录
  1863. {
  1864. searchKeyword := new(data_manage.SearchKeyword)
  1865. searchKeyword.KeyWord = keyWord
  1866. searchKeyword.CreateTime = time.Now()
  1867. go data_manage.AddSearchKeyword(searchKeyword)
  1868. }
  1869. br.Ret = 200
  1870. br.Success = true
  1871. br.Msg = "获取成功"
  1872. br.Data = edbInfoList
  1873. }
  1874. func searchEdbInfoFilter(condition string, pars []interface{}, i int, ch chan map[int][]*data_manage.EdbInfoList, searchWait *sync.WaitGroup) (newEdbInfoListMap map[int][]*data_manage.EdbInfoList, err error) {
  1875. newEdbInfoListMap = make(map[int][]*data_manage.EdbInfoList)
  1876. defer func() {
  1877. if err != nil {
  1878. go alarm_msg.SendAlarmMsg("查询近6个月的客户增量数据异常:"+err.Error(), 3)
  1879. //go utils.SendEmail(utils.APPNAME+"查询近6个月的客户增量数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  1880. }
  1881. ch <- newEdbInfoListMap
  1882. searchWait.Done()
  1883. }()
  1884. newEdbInfoList, err := data_manage.GetEdbInfoFilter(condition, pars)
  1885. for _, v := range newEdbInfoList {
  1886. v.ConvertToResp()
  1887. }
  1888. newEdbInfoListMap[i] = newEdbInfoList
  1889. return
  1890. }
  1891. // EdbInfoAllRefresh
  1892. // @Title 指标全部刷新接口
  1893. // @Description 指标全部刷新接口
  1894. // @Param EdbInfoId query int true "指标id"
  1895. // @Success Ret=200 全部刷新成功
  1896. // @router /edb_info/refresh/all [get]
  1897. func (this *EdbInfoController) EdbInfoAllRefresh() {
  1898. br := new(models.BaseResponse).Init()
  1899. defer func() {
  1900. this.Data["json"] = br
  1901. this.ServeJSON()
  1902. }()
  1903. sysUser := this.SysUser
  1904. if sysUser == nil {
  1905. br.Msg = "请登录"
  1906. br.ErrMsg = "请登录,SysUser Is Empty"
  1907. br.Ret = 408
  1908. return
  1909. }
  1910. edbInfoId, _ := this.GetInt("EdbInfoId")
  1911. if edbInfoId <= 0 {
  1912. br.Msg = "参数错误"
  1913. br.ErrMsg = "参数错误"
  1914. return
  1915. }
  1916. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1917. if err != nil {
  1918. if utils.IsErrNoRow(err) {
  1919. br.Msg = "数据已被删除,请刷新页面"
  1920. br.ErrMsg = "数据已被删除,请刷新页面,edbInfoId:" + strconv.Itoa(edbInfoId)
  1921. return
  1922. }
  1923. br.Msg = "刷新失败"
  1924. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1925. return
  1926. }
  1927. if edbInfo == nil {
  1928. br.Msg = "数据已被删除,请刷新页面"
  1929. br.ErrMsg = "数据已被删除,请刷新页面,edbInfoId:" + strconv.Itoa(edbInfoId)
  1930. return
  1931. }
  1932. err, isAsync := data.EdbInfoRefreshAllFromBaseV2(edbInfoId, true, false)
  1933. if err != nil {
  1934. br.Msg = "刷新失败"
  1935. br.ErrMsg = "刷新指标失败,EdbInfoRefreshAllFromBase Err:" + err.Error()
  1936. return
  1937. }
  1938. br.Ret = 200
  1939. br.Success = true
  1940. br.Msg = "刷新成功"
  1941. if isAsync {
  1942. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  1943. }
  1944. }
  1945. // EdbInfoData
  1946. // @Title 获取指标数据
  1947. // @Description 获取指标数据
  1948. // @Param EdbInfoId query int true "指标ID"
  1949. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  1950. // @Param StartDate query string true "自定义开始日期"
  1951. // @Param EndDate query string true "自定义结束日期"
  1952. // @Success 200 {object} data_manage.EdbInfoDataResp
  1953. // @router /edb_info/data [get]
  1954. func (this *ChartInfoController) EdbInfoData() {
  1955. br := new(models.BaseResponse).Init()
  1956. defer func() {
  1957. this.Data["json"] = br
  1958. this.ServeJSON()
  1959. }()
  1960. sysUser := this.SysUser
  1961. if sysUser == nil {
  1962. br.Msg = "请登录"
  1963. br.ErrMsg = "请登录,SysUser Is Empty"
  1964. br.Ret = 408
  1965. return
  1966. }
  1967. fullEdb := new(data_manage.EdbInfoFullClassify)
  1968. resp := new(data_manage.EdbInfoDataFullClassifyResp)
  1969. dataList := make([]*data_manage.EdbDataList, 0)
  1970. edbInfoId, _ := this.GetInt("EdbInfoId")
  1971. if edbInfoId <= 0 {
  1972. br.Msg = "参数错误"
  1973. return
  1974. }
  1975. dateType, _ := this.GetInt("DateType")
  1976. if dateType <= 0 {
  1977. dateType = 3
  1978. }
  1979. edbInfoType, _ := this.GetInt("EdbInfoType")
  1980. if edbInfoType < 0 {
  1981. edbInfoType = 1
  1982. }
  1983. startDate := this.GetString("StartDate")
  1984. endDate := this.GetString("EndDate")
  1985. startYear, _ := this.GetInt("StartYear")
  1986. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1987. if err != nil {
  1988. if utils.IsErrNoRow(err) {
  1989. br.Ret = 200
  1990. return
  1991. }
  1992. br.Msg = "获取失败"
  1993. br.ErrMsg = "获取失败,Err:" + err.Error()
  1994. return
  1995. }
  1996. // 分类
  1997. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  1998. if err != nil {
  1999. br.Msg = err.Error()
  2000. br.ErrMsg = errMsg
  2001. return
  2002. }
  2003. edbInfoResp, err := edbInfo.ConvertToResp()
  2004. if err != nil {
  2005. br.Msg = err.Error()
  2006. br.ErrMsg = errMsg
  2007. return
  2008. }
  2009. fullEdb.EdbInfoResp = edbInfoResp
  2010. // 是否供应商停更
  2011. fullEdb.IsSupplierStop = data.GetIsSupplierStop(edbInfo.Source, edbInfo.EdbCode)
  2012. fullEdb.ClassifyList = classifyList
  2013. var currClassifyItem *data_manage.EdbClassifyIdItems
  2014. for _, v := range classifyList {
  2015. if v.ClassifyId == edbInfo.ClassifyId {
  2016. currClassifyItem = v
  2017. }
  2018. }
  2019. if currClassifyItem != nil {
  2020. haveOperaAuth, err := data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassifyItem.IsJoinPermission, sysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  2021. if err != nil {
  2022. br.Msg = err.Error()
  2023. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  2024. return
  2025. }
  2026. fullEdb.HaveOperaAuth = haveOperaAuth
  2027. }
  2028. resp.EdbInfo = fullEdb
  2029. resp.DataList = make([]*data_manage.EdbDataList, 0)
  2030. // 数据获取
  2031. if fullEdb.HaveOperaAuth { // 有权限才获取数据
  2032. maxYear := 0
  2033. if edbInfo.LatestDate != "" {
  2034. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  2035. maxYear = latestDateT.Year()
  2036. }
  2037. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  2038. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDate, endDate)
  2039. if err != nil {
  2040. br.Msg = "获取失败"
  2041. br.ErrMsg = "获取失败,Err:" + err.Error()
  2042. return
  2043. }
  2044. resp.DataList = dataList
  2045. }
  2046. br.Ret = 200
  2047. br.Success = true
  2048. br.Msg = "获取成功"
  2049. br.Data = resp
  2050. }
  2051. // EdbInfoReplaceCheck
  2052. // @Title 指标一键刷新接口
  2053. // @Description 指标一键刷新接口
  2054. // @Success Ret=200 更新成功
  2055. // @router /edb_info/replace/check [post]
  2056. func (this *EdbInfoController) EdbInfoReplaceCheck() {
  2057. br := new(models.BaseResponse).Init()
  2058. setNxKey := "EDB_INFO_REPLACE"
  2059. defer func() {
  2060. this.Data["json"] = br
  2061. this.ServeJSON()
  2062. }()
  2063. status := 0
  2064. if utils.Rc.IsExist(setNxKey) {
  2065. status = 1
  2066. } else {
  2067. status = 2
  2068. }
  2069. br.Ret = 200
  2070. br.Success = true
  2071. br.Msg = "检测成功"
  2072. br.Data = status
  2073. }
  2074. // EdbInfoReplace
  2075. // @Title 替换指标
  2076. // @Description 替换指标
  2077. // @Param request body data_manage.EdbInfoReplaceReq true "type json string"
  2078. // @Success 200 {object} data_manage.EdbInfoDataResp
  2079. // @router /edb_info/replace [post]
  2080. func (this *ChartInfoController) EdbInfoReplace() {
  2081. br := new(models.BaseResponse).Init()
  2082. setNxKey := "EDB_INFO_REPLACE"
  2083. deleteCache := true
  2084. defer func() {
  2085. if deleteCache {
  2086. utils.Rc.Delete(setNxKey)
  2087. }
  2088. this.Data["json"] = br
  2089. this.ServeJSON()
  2090. }()
  2091. sysUser := this.SysUser
  2092. if sysUser == nil {
  2093. br.Msg = "请登录"
  2094. br.ErrMsg = "请登录,SysUser Is Empty"
  2095. br.Ret = 408
  2096. return
  2097. }
  2098. if !utils.Rc.SetNX(setNxKey, 1, 30*time.Minute) {
  2099. deleteCache = false
  2100. br.Msg = "当前正在替换中,请耐心等待!"
  2101. br.IsSendEmail = false
  2102. return
  2103. }
  2104. var req data_manage.EdbInfoReplaceReq
  2105. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2106. if err != nil {
  2107. br.Msg = "参数解析异常!"
  2108. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2109. return
  2110. }
  2111. if req.OldEdbInfoId <= 0 || req.NewEdbInfoId <= 0 {
  2112. br.Msg = "参数错误!"
  2113. br.ErrMsg = "参数错误,指标id为0"
  2114. return
  2115. }
  2116. oldEdbInfo, err := data_manage.GetEdbInfoById(req.OldEdbInfoId)
  2117. if err != nil {
  2118. br.Msg = "替换失败!"
  2119. br.ErrMsg = "获取原指标失败,Err:" + err.Error()
  2120. return
  2121. }
  2122. newEdbInfo, err := data_manage.GetEdbInfoById(req.NewEdbInfoId)
  2123. if err != nil {
  2124. br.Msg = "替换失败!"
  2125. br.ErrMsg = "获取替换目标指标失败,Err:" + err.Error()
  2126. return
  2127. }
  2128. //if oldEdbInfo.EdbType != 1 {
  2129. // br.Msg = "原指标为计算指标,不可替换!"
  2130. // br.ErrMsg = "原指标为计算指标,不可替换!"
  2131. // return
  2132. //}
  2133. //
  2134. //if newEdbInfo.EdbType != 1 {
  2135. // br.Msg = "替换指标为计算指标,不可替换!"
  2136. // br.ErrMsg = "替换指标为计算指标,不可替换!"
  2137. // return
  2138. //}
  2139. if oldEdbInfo.Frequency != newEdbInfo.Frequency {
  2140. br.Msg = "原指标频度为:" + oldEdbInfo.Frequency + " 替换指标频度为:" + newEdbInfo.Frequency + ",频度不同,不可进行替换操作!"
  2141. br.ErrMsg = "原指标频度为:" + oldEdbInfo.Frequency + " 替换指标频度为:" + newEdbInfo.Frequency + ",频度不同,不可进行替换操作!"
  2142. return
  2143. }
  2144. sysAdminId := sysUser.AdminId
  2145. //replaceChartTotal, replaceCalculateTotal, err := data.EdbInfoReplace(oldEdbInfo, newEdbInfo, sysAdminId, sysUser.RealName)
  2146. _, _, err = data.EdbInfoReplace(oldEdbInfo, newEdbInfo, sysAdminId, sysUser.RealName)
  2147. //msgContent := ``
  2148. if err != nil {
  2149. br.Msg = "替换失败"
  2150. br.ErrMsg = "替换失败 replace err:" + err.Error()
  2151. return
  2152. }
  2153. br.Msg = "替换成功"
  2154. br.ErrMsg = "替换成功"
  2155. br.Ret = 200
  2156. br.IsAddLog = true
  2157. return
  2158. }
  2159. // RelationChartList
  2160. // @Title 获取ETA指标关联的图表列表接口
  2161. // @Description 获取ETA指标关联的图表列表接口
  2162. // @Param PageSize query int true "每页数据条数"
  2163. // @Param CurrentIndex query int true "当前页页码,从1开始"
  2164. // @Param EdbInfoId query int true "指标id"
  2165. // @Param IsShowMe query bool true "是否只看我的,true、false"
  2166. // @Success 200 {object} data_manage.ChartListResp
  2167. // @router /edb_info/relation/chart_list [get]
  2168. func (this *EdbInfoController) RelationChartList() {
  2169. br := new(models.BaseResponse).Init()
  2170. defer func() {
  2171. this.Data["json"] = br
  2172. this.ServeJSON()
  2173. }()
  2174. sysUser := this.SysUser
  2175. if sysUser == nil {
  2176. br.Msg = "请登录"
  2177. br.ErrMsg = "请登录,SysUser Is Empty"
  2178. br.Ret = 408
  2179. return
  2180. }
  2181. edbInfoId, _ := this.GetInt("EdbInfoId")
  2182. pageSize, _ := this.GetInt("PageSize")
  2183. currentIndex, _ := this.GetInt("CurrentIndex")
  2184. resp := data_manage.ChartListResp{}
  2185. var total int
  2186. page := paging.GetPaging(currentIndex, pageSize, total)
  2187. var startSize int
  2188. if pageSize <= 0 {
  2189. pageSize = utils.PageSize20
  2190. }
  2191. if currentIndex <= 0 {
  2192. currentIndex = 1
  2193. }
  2194. startSize = paging.StartIndex(currentIndex, pageSize)
  2195. var condition string
  2196. var pars []interface{}
  2197. if edbInfoId <= 0 {
  2198. br.Msg = "请选择指标"
  2199. br.ErrMsg = "请选择指标"
  2200. br.IsSendEmail = false
  2201. return
  2202. }
  2203. /*// 关联指标
  2204. condition += ` AND b.edb_info_id = ? `
  2205. pars = append(pars, edbInfoId)*/
  2206. //只看我的
  2207. isShowMe, _ := this.GetBool("IsShowMe")
  2208. if isShowMe {
  2209. condition += ` AND a.sys_user_id = ? `
  2210. pars = append(pars, sysUser.AdminId)
  2211. }
  2212. chartIds := make([]int, 0)
  2213. chartIdMap := make(map[int]bool)
  2214. // 查询指标绑定的图表
  2215. edbListTemp, err := data_manage.GetEdbMappingListByEdbInfoId(edbInfoId)
  2216. if err != nil && !utils.IsErrNoRow(err) {
  2217. br.Msg = "获取图表信息失败"
  2218. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2219. return
  2220. }
  2221. for _, v := range edbListTemp {
  2222. if _, ok := chartIdMap[v.ChartInfoId]; !ok {
  2223. chartIdMap[v.ChartInfoId] = true
  2224. }
  2225. }
  2226. // 查询跨品种的图表
  2227. tagXList, err := cross_variety.GetChartInfoCrossVarietyByXEdbInfoId(edbInfoId)
  2228. if err != nil && !utils.IsErrNoRow(err) {
  2229. br.Msg = "获取图表信息失败"
  2230. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2231. return
  2232. }
  2233. for _, v := range tagXList {
  2234. if _, ok := chartIdMap[v.ChartInfoId]; !ok {
  2235. chartIdMap[v.ChartInfoId] = true
  2236. }
  2237. }
  2238. tagYList, err := cross_variety.GetChartInfoCrossVarietyByYEdbInfoId(edbInfoId)
  2239. if err != nil && !utils.IsErrNoRow(err) {
  2240. br.Msg = "获取图表信息失败"
  2241. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2242. return
  2243. }
  2244. for _, v := range tagYList {
  2245. if _, ok := chartIdMap[v.ChartInfoId]; !ok {
  2246. chartIdMap[v.ChartInfoId] = true
  2247. }
  2248. }
  2249. // 获取当前账号的不可见图表
  2250. {
  2251. obj := data_manage.EdbInfoNoPermissionAdmin{}
  2252. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  2253. if err != nil && !utils.IsErrNoRow(err) {
  2254. br.Msg = "获取失败"
  2255. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2256. return
  2257. }
  2258. for _, v := range confList {
  2259. if _, ok := chartIdMap[v.ChartInfoId]; ok {
  2260. delete(chartIdMap, v.ChartInfoId)
  2261. }
  2262. }
  2263. }
  2264. for k, _ := range chartIdMap {
  2265. chartIds = append(chartIds, k)
  2266. }
  2267. // 关联指标
  2268. if len(chartIds) > 0 {
  2269. condition += ` AND a.chart_info_id in (` + utils.GetOrmInReplace(len(chartIds)) + `)`
  2270. pars = append(pars, chartIds)
  2271. } else {
  2272. items := make([]*data_manage.ChartInfoView, 0)
  2273. resp.Paging = page
  2274. resp.List = items
  2275. br.Ret = 200
  2276. br.Success = true
  2277. br.Msg = "获取成功"
  2278. return
  2279. }
  2280. // 得到图表ID
  2281. //获取关联图表列表
  2282. list, err := data_manage.GetRelationChartListByCondition(condition, pars, startSize, pageSize)
  2283. if err != nil && !utils.IsErrNoRow(err) {
  2284. br.Success = true
  2285. br.Msg = "获取图表信息失败"
  2286. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2287. return
  2288. }
  2289. if list == nil || len(list) <= 0 || (err != nil && utils.IsErrNoRow(err)) {
  2290. items := make([]*data_manage.ChartInfoView, 0)
  2291. resp.Paging = page
  2292. resp.List = items
  2293. br.Ret = 200
  2294. br.Success = true
  2295. br.Msg = "获取成功"
  2296. return
  2297. }
  2298. // 获取关联图表总数
  2299. dataCount, err := data_manage.GetRelationChartListCountByCondition(condition, pars)
  2300. if err != nil && !utils.IsErrNoRow(err) {
  2301. br.Msg = "获取指标信息失败"
  2302. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  2303. return
  2304. }
  2305. // 获取MY ETA 图表,用来判断是否加入我的图库
  2306. myChartList, err := data_manage.GetMyChartListByAdminId(sysUser.AdminId)
  2307. if err != nil && !utils.IsErrNoRow(err) {
  2308. br.Msg = "获取图表信息失败"
  2309. br.ErrMsg = "获取我的图表信息失败,Err:" + err.Error()
  2310. return
  2311. }
  2312. myChartMap := make(map[int]*data_manage.MyChartView)
  2313. for _, v := range myChartList {
  2314. myChartMap[v.ChartInfoId] = v
  2315. }
  2316. listLen := len(list)
  2317. if listLen > 0 {
  2318. // 分类id
  2319. classifyIdList := make([]int, 0)
  2320. // 当前列表中的分类map
  2321. classifyMap := make(map[int]*data_manage.ChartClassify)
  2322. for _, v := range list {
  2323. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  2324. }
  2325. classifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  2326. if err != nil {
  2327. br.Msg = "获取失败"
  2328. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2329. return
  2330. }
  2331. for _, v := range classifyList {
  2332. classifyMap[v.ChartClassifyId] = v
  2333. }
  2334. // 已授权分类id
  2335. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2336. if err != nil {
  2337. br.Msg = "获取失败"
  2338. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  2339. return
  2340. }
  2341. for _, v := range list {
  2342. if existItem, ok := myChartMap[v.ChartInfoId]; ok {
  2343. v.IsAdd = true
  2344. v.MyChartId = existItem.MyChartId
  2345. v.MyChartClassifyId = existItem.MyChartClassifyId
  2346. }
  2347. // 权限
  2348. if currClassify, ok := classifyMap[v.ChartClassifyId]; ok {
  2349. v.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.ChartInfoId, v.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2350. }
  2351. }
  2352. }
  2353. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  2354. resp.Paging = page
  2355. resp.List = list
  2356. br.Ret = 200
  2357. br.Success = true
  2358. br.Msg = "获取成功"
  2359. br.Data = resp
  2360. }
  2361. // RelationEdbList
  2362. // @Title 获取ETA指标关联的指标列表接口
  2363. // @Description 获取ETA指标关联的指标列表接口
  2364. // @Param PageSize query int true "每页数据条数"
  2365. // @Param CurrentIndex query int true "当前页页码,从1开始"
  2366. // @Param EdbInfoId query int true "指标id"
  2367. // @Param IsShowMe query bool true "是否只看我的,true、false"
  2368. // @Success 200 {object} data_manage.ChartListResp
  2369. // @router /edb_info/relation/edbinfo_list [get]
  2370. func (this *EdbInfoController) RelationEdbList() {
  2371. br := new(models.BaseResponse).Init()
  2372. defer func() {
  2373. this.Data["json"] = br
  2374. this.ServeJSON()
  2375. }()
  2376. sysUser := this.SysUser
  2377. if sysUser == nil {
  2378. br.Msg = "请登录"
  2379. br.ErrMsg = "请登录,SysUser Is Empty"
  2380. br.Ret = 408
  2381. return
  2382. }
  2383. edbInfoId, _ := this.GetInt("EdbInfoId")
  2384. pageSize, _ := this.GetInt("PageSize")
  2385. currentIndex, _ := this.GetInt("CurrentIndex")
  2386. resp := data_manage.RelationEdbInfoListResp{}
  2387. var total int
  2388. page := paging.GetPaging(currentIndex, pageSize, total)
  2389. var startSize int
  2390. if pageSize <= 0 {
  2391. pageSize = utils.PageSize20
  2392. }
  2393. if currentIndex <= 0 {
  2394. currentIndex = 1
  2395. }
  2396. startSize = paging.StartIndex(currentIndex, pageSize)
  2397. var condition string
  2398. var pars []interface{}
  2399. if edbInfoId <= 0 {
  2400. br.Msg = "请选择指标"
  2401. br.ErrMsg = "请选择指标"
  2402. br.IsSendEmail = false
  2403. return
  2404. }
  2405. // 关联指标
  2406. condition += ` AND b.from_edb_info_id = ? `
  2407. pars = append(pars, edbInfoId)
  2408. //只看我的
  2409. isShowMe, _ := this.GetBool("IsShowMe")
  2410. if isShowMe {
  2411. condition += ` AND a.sys_user_id = ? `
  2412. pars = append(pars, sysUser.AdminId)
  2413. }
  2414. // 获取当前账号的不可见图表
  2415. noPermissionEdbIdList := make([]int, 0)
  2416. {
  2417. obj := data_manage.EdbInfoNoPermissionAdmin{}
  2418. confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
  2419. if err != nil && !utils.IsErrNoRow(err) {
  2420. br.Msg = "获取失败"
  2421. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2422. return
  2423. }
  2424. for _, v := range confList {
  2425. noPermissionEdbIdList = append(noPermissionEdbIdList, v.EdbInfoId)
  2426. }
  2427. }
  2428. noPermissionEdbIdNum := len(noPermissionEdbIdList)
  2429. if noPermissionEdbIdNum > 0 {
  2430. condition += ` AND a.edb_info_id not in (` + utils.GetOrmInReplace(noPermissionEdbIdNum) + `) `
  2431. pars = append(pars, noPermissionEdbIdList)
  2432. }
  2433. //获取关联图表列表
  2434. list, err := data_manage.GetRelationEdbInfoListByCondition(condition, pars, startSize, pageSize)
  2435. if err != nil && !utils.IsErrNoRow(err) {
  2436. br.Success = true
  2437. br.Msg = "获取图表信息失败"
  2438. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2439. return
  2440. }
  2441. if list == nil || len(list) <= 0 || (err != nil && utils.IsErrNoRow(err)) {
  2442. items := make([]*data_manage.ChartEdbInfoMapping, 0)
  2443. resp.Paging = page
  2444. resp.List = items
  2445. br.Ret = 200
  2446. br.Success = true
  2447. br.Msg = "获取成功"
  2448. return
  2449. }
  2450. // 获取关联图表总数
  2451. dataCount, err := data_manage.GetRelationEdbInfoListCountByCondition(condition, pars)
  2452. if err != nil && !utils.IsErrNoRow(err) {
  2453. br.Msg = "获取指标信息失败"
  2454. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  2455. return
  2456. }
  2457. if err != nil {
  2458. br.Msg = "获取失败"
  2459. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2460. return
  2461. }
  2462. edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  2463. if len(list) > 0 {
  2464. // 当前的分类
  2465. classifyMap := make(map[int]*data_manage.EdbClassify)
  2466. {
  2467. classifyIdList := make([]int, 0)
  2468. for _, v := range list {
  2469. classifyIdList = append(classifyIdList, v.ClassifyId)
  2470. }
  2471. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2472. if err != nil {
  2473. if err != nil {
  2474. br.Msg = "获取失败"
  2475. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2476. return
  2477. }
  2478. }
  2479. for _, v := range classifyList {
  2480. classifyMap[v.ClassifyId] = v
  2481. }
  2482. }
  2483. // 获取所有有权限的指标和分类
  2484. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2485. if err != nil {
  2486. br.Msg = "获取失败"
  2487. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2488. return
  2489. }
  2490. for _, v := range list {
  2491. item := new(data_manage.ChartEdbInfoMapping)
  2492. item.EdbInfoId = v.EdbInfoId
  2493. item.SourceName = v.SourceName
  2494. item.Source = v.Source
  2495. item.EdbCode = v.EdbCode
  2496. item.EdbName = v.EdbName
  2497. item.EdbNameEn = v.EdbNameEn
  2498. item.Frequency = v.Frequency
  2499. item.FrequencyEn = data.GetFrequencyEn(v.Frequency)
  2500. if v.Unit != `无` {
  2501. item.Unit = v.Unit
  2502. }
  2503. item.UnitEn = v.UnitEn
  2504. item.StartDate = v.StartDate
  2505. item.EndDate = v.EndDate
  2506. item.ModifyTime = v.ModifyTime
  2507. item.IsAxis = 1
  2508. item.LeadValue = 0
  2509. item.LeadUnit = ""
  2510. item.ChartEdbMappingId = 0
  2511. item.ChartInfoId = 0
  2512. item.IsOrder = false
  2513. item.EdbInfoType = v.EdbInfoType
  2514. item.EdbInfoCategoryType = v.EdbInfoType
  2515. item.ChartStyle = ""
  2516. item.ChartColor = ""
  2517. item.ChartWidth = 0
  2518. item.MaxData = v.MaxValue
  2519. item.MinData = v.MinValue
  2520. item.LatestValue = v.LatestValue
  2521. item.LatestDate = v.LatestDate
  2522. item.UniqueCode = v.UniqueCode
  2523. item.ClassifyId = v.ClassifyId
  2524. dataList := make([]*data_manage.EdbDataList, 0)
  2525. // 指标权限
  2526. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  2527. item.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2528. }
  2529. // 有权限则获取数据
  2530. if item.HaveOperaAuth {
  2531. if v.EdbInfoType == 0 {
  2532. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, "", "")
  2533. } else {
  2534. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, "", "", false)
  2535. }
  2536. if err != nil {
  2537. br.Msg = "获取失败"
  2538. br.Msg = "获取失败,Err:" + err.Error()
  2539. return
  2540. }
  2541. }
  2542. item.DataList = dataList
  2543. item.ConvertToResp()
  2544. edbList = append(edbList, item)
  2545. }
  2546. }
  2547. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  2548. resp.Paging = page
  2549. resp.List = edbList
  2550. br.Ret = 200
  2551. br.Success = true
  2552. br.Msg = "获取成功"
  2553. br.Data = resp
  2554. }
  2555. // EdbInfoDataTb
  2556. // @Title 获取指标数据同比值
  2557. // @Description 获取指标数据
  2558. // @Param EdbInfoId query int true "指标ID"
  2559. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  2560. // @Param StartDate query string true "自定义开始日期"
  2561. // @Param EndDate query string true "自定义结束日期"
  2562. // @Success 200 {object} data_manage.EdbInfoDataResp
  2563. // @router /edb_info/data/tb [get]
  2564. func (this *ChartInfoController) EdbInfoDataTb() {
  2565. br := new(models.BaseResponse).Init()
  2566. defer func() {
  2567. this.Data["json"] = br
  2568. this.ServeJSON()
  2569. }()
  2570. sysUser := this.SysUser
  2571. if sysUser == nil {
  2572. br.Msg = "请登录"
  2573. br.ErrMsg = "请登录,SysUser Is Empty"
  2574. br.Ret = 408
  2575. return
  2576. }
  2577. edbInfoId, _ := this.GetInt("EdbInfoId")
  2578. if edbInfoId <= 0 {
  2579. br.Msg = "参数错误"
  2580. return
  2581. }
  2582. dateType, _ := this.GetInt("DateType")
  2583. if dateType <= 0 {
  2584. dateType = 3
  2585. }
  2586. edbInfoType, _ := this.GetInt("EdbInfoType")
  2587. if edbInfoType < 0 {
  2588. edbInfoType = 1
  2589. }
  2590. startDate := this.GetString("StartDate")
  2591. endDate := this.GetString("EndDate")
  2592. startYear, _ := this.GetInt("StartYear")
  2593. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  2594. if err != nil {
  2595. br.Msg = "获取失败"
  2596. br.Msg = "获取失败,Err:" + err.Error()
  2597. return
  2598. }
  2599. maxYear := 0
  2600. if edbInfo.LatestDate != "" {
  2601. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  2602. maxYear = latestDateT.Year()
  2603. }
  2604. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  2605. var startDateTime time.Time
  2606. if startDate != `` {
  2607. startDateTime, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  2608. if err != nil {
  2609. br.Msg = "开始日期异常"
  2610. br.Msg = "开始日期异常,Err:" + err.Error()
  2611. return
  2612. }
  2613. //同比值的计算方式,是需要与去年同期对比,所以如果用户需要某个时间段的数据的话,获取基础数据时,需要往前面推(1年+兼容的日期频度(目前是35天))
  2614. startDate = startDateTime.AddDate(-1, 0, -35).Format(utils.FormatDate)
  2615. }
  2616. tmpDataList, err := data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDate, endDate)
  2617. if err != nil {
  2618. br.Msg = "获取失败"
  2619. br.Msg = "获取失败,Err:" + err.Error()
  2620. return
  2621. }
  2622. // 计算同比值
  2623. dataList, minValue, maxValue, err := data.GetEdbDataTbz(edbInfo, tmpDataList, startDateTime)
  2624. if err != nil {
  2625. br.Msg = "获取失败"
  2626. br.Msg = "计算同比值失败,Err:" + err.Error()
  2627. return
  2628. }
  2629. edbInfo.MaxValue = maxValue
  2630. edbInfo.MinValue = minValue
  2631. resp := new(data_manage.EdbInfoDataResp)
  2632. resp.EdbInfo = edbInfo
  2633. resp.DataList = dataList
  2634. br.Ret = 200
  2635. br.Success = true
  2636. br.Msg = "获取成功"
  2637. br.Data = resp
  2638. }
  2639. // EdbInfoDataSeasonal
  2640. // @Title 获取指标数据(季节性)
  2641. // @Description 获取指标数据
  2642. // @Param EdbInfoId query int true "指标ID"
  2643. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  2644. // @Param StartDate query string true "自定义开始日期"
  2645. // @Param EndDate query string true "自定义结束日期"
  2646. // @Param Calendar query string true "公历/农历"
  2647. // @Success 200 {object} data_manage.EdbInfoDataResp
  2648. // @router /edb_info/data/seasonal [get]
  2649. func (this *ChartInfoController) EdbInfoDataSeasonal() {
  2650. br := new(models.BaseResponse).Init()
  2651. defer func() {
  2652. this.Data["json"] = br
  2653. this.ServeJSON()
  2654. }()
  2655. sysUser := this.SysUser
  2656. if sysUser == nil {
  2657. br.Msg = "请登录"
  2658. br.ErrMsg = "请登录,SysUser Is Empty"
  2659. br.Ret = 408
  2660. return
  2661. }
  2662. dateType, _ := this.GetInt("DateType")
  2663. startDate := this.GetString("StartDate")
  2664. endDate := this.GetString("EndDate")
  2665. startYear, _ := this.GetInt("StartYear")
  2666. edbInfoIdStr := this.GetString("EdbInfoId")
  2667. edbInfoId, err := strconv.Atoi(edbInfoIdStr)
  2668. if err != nil {
  2669. br.Msg = "指标id异常"
  2670. br.Msg = "指标id转换异常,Err:" + err.Error()
  2671. return
  2672. }
  2673. fmt.Println("startDate:", startDate)
  2674. fmt.Println("endDate:", endDate)
  2675. calendar := this.GetString("Calendar")
  2676. if calendar == "" {
  2677. calendar = "公历"
  2678. }
  2679. if dateType == 0 {
  2680. if startDate == "" {
  2681. dateType = utils.DateTypeNYears
  2682. } else {
  2683. dateType = 5
  2684. }
  2685. }
  2686. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  2687. if err != nil {
  2688. br.Msg = "获取失败"
  2689. br.Msg = "获取失败,Err:" + err.Error()
  2690. return
  2691. }
  2692. maxYear := 0
  2693. if edbInfo.LatestDate != "" {
  2694. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  2695. maxYear = latestDateT.Year()
  2696. }
  2697. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  2698. dataList, err := data.GetChartEdbSeasonalData(calendar, startDate, endDate, edbInfo)
  2699. if err != nil {
  2700. br.Msg = "获取失败"
  2701. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2702. return
  2703. }
  2704. resp := new(data_manage.EdbInfoDataSeasonalResp)
  2705. resp.EdbInfo = edbInfo
  2706. resp.DataList = dataList
  2707. br.Ret = 200
  2708. br.Success = true
  2709. br.Msg = "获取成功"
  2710. br.Data = resp
  2711. }
  2712. // SetEdbDataInsertConfig 设置插入值
  2713. // @Title 设置插入值接口
  2714. // @Description 设置插入值接口
  2715. // @Success 200 {object} data_manage.EditEdbEnInfoReq
  2716. // @router /edb_info/insert_config/set [post]
  2717. func (this *EdbInfoController) SetEdbDataInsertConfig() {
  2718. br := new(models.BaseResponse).Init()
  2719. defer func() {
  2720. this.Data["json"] = br
  2721. this.ServeJSON()
  2722. }()
  2723. sysUser := this.SysUser
  2724. if sysUser == nil {
  2725. br.Msg = "请登录"
  2726. br.ErrMsg = "请登录,SysUser Is Empty"
  2727. br.Ret = 408
  2728. return
  2729. }
  2730. var req data_manage.SetEdbDataInsertConfigReq
  2731. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2732. if err != nil {
  2733. br.Msg = "参数解析异常!"
  2734. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2735. return
  2736. }
  2737. dateStr := req.Date
  2738. if dateStr == `` {
  2739. br.Msg = "请选择日期"
  2740. br.IsSendEmail = false
  2741. return
  2742. }
  2743. date, err := time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  2744. if err != nil {
  2745. br.Msg = "日期异常"
  2746. br.ErrMsg = "日期异常,err:" + err.Error()
  2747. br.IsSendEmail = false
  2748. return
  2749. }
  2750. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  2751. if err != nil {
  2752. br.Msg = "获取指标信息失败"
  2753. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  2754. return
  2755. }
  2756. err, errMsg, isSendEmail := data_manage.CreateEdbDataInsertConfigAndData(edbInfo, date, req.Value)
  2757. if err != nil {
  2758. br.Msg = "设置失败"
  2759. if errMsg != `` {
  2760. br.Msg = errMsg
  2761. }
  2762. br.ErrMsg = "修改失败,Err:" + err.Error()
  2763. br.IsSendEmail = isSendEmail
  2764. return
  2765. }
  2766. //新增操作日志
  2767. {
  2768. edbLog := new(data_manage.EdbInfoLog)
  2769. edbLog.EdbInfoId = edbInfo.EdbInfoId
  2770. edbLog.SourceName = edbInfo.SourceName
  2771. edbLog.Source = edbInfo.Source
  2772. edbLog.EdbCode = edbInfo.EdbCode
  2773. edbLog.EdbName = edbInfo.EdbName
  2774. edbLog.ClassifyId = edbInfo.ClassifyId
  2775. edbLog.SysUserId = sysUser.AdminId
  2776. edbLog.SysUserRealName = sysUser.RealName
  2777. edbLog.CreateTime = time.Now()
  2778. edbLog.Content = string(this.Ctx.Input.RequestBody)
  2779. edbLog.Status = "设置插入值"
  2780. edbLog.Method = this.Ctx.Input.URI()
  2781. go data_manage.AddEdbInfoLog(edbLog)
  2782. }
  2783. br.Ret = 200
  2784. br.Success = true
  2785. br.Msg = "保存成功"
  2786. br.IsAddLog = true
  2787. }
  2788. // TraceEdbInfo
  2789. // @Title 指标溯源接口
  2790. // @Description 指标溯源接口
  2791. // @Param UniqueCode query int true "指标唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  2792. // @Success 200 {object} data_manage.ChartListResp
  2793. // @router /edb_info/trace [get]
  2794. func (this *EdbInfoController) TraceEdbInfo() {
  2795. br := new(models.BaseResponse).Init()
  2796. defer func() {
  2797. this.Data["json"] = br
  2798. this.ServeJSON()
  2799. }()
  2800. sysUser := this.SysUser
  2801. if sysUser == nil {
  2802. br.Msg = "请登录"
  2803. br.ErrMsg = "请登录,SysUser Is Empty"
  2804. br.Ret = 408
  2805. return
  2806. }
  2807. //edbInfoId, _ := this.GetInt("EdbInfoId")
  2808. //if edbInfoId <= 0 {
  2809. // br.Msg = "参数错误"
  2810. // br.ErrMsg = "参数错误"
  2811. // return
  2812. //}
  2813. uniqueCode := this.GetString("UniqueCode")
  2814. if uniqueCode == `` {
  2815. br.Msg = "参数错误"
  2816. br.ErrMsg = "参数错误"
  2817. return
  2818. }
  2819. // 根据UniqueCode获取指标信息
  2820. edbInfo, err := data_manage.GetEdbInfoByUniqueCode(uniqueCode)
  2821. if err != nil {
  2822. br.Msg = "获取失败"
  2823. br.Msg = "获取失败,Err:" + err.Error()
  2824. if utils.IsErrNoRow(err) {
  2825. br.Msg = "指标不存在"
  2826. }
  2827. return
  2828. }
  2829. resp, err := data.TraceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, this.SysUser.AdminId)
  2830. if err != nil {
  2831. br.Msg = "获取失败"
  2832. br.Msg = "获取失败,Err:" + err.Error()
  2833. return
  2834. }
  2835. br.Ret = 200
  2836. br.Data = resp
  2837. br.Success = true
  2838. br.Msg = "刷新成功"
  2839. }
  2840. // AllEdbInfoByEs
  2841. // @Title 所有指标筛选接口
  2842. // @Description 所有指标筛选接口
  2843. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  2844. // @Param FilterSource query int false "搜索来源:1:其他搜索,2:累计值转月值搜索,3:变频,4:基础指标,5:同比"
  2845. // @Param Frequency query string false "频度"
  2846. // @Param IsAddPredictEdb query bool false "是否查询添加预测指标"
  2847. // @Param PageSize query int true "每页数据条数"
  2848. // @Param CurrentIndex query int true "当前页页码,从1开始"
  2849. // @Success 200 {object} data_manage.EdbInfoList
  2850. // @router /edb_info/filter_by_es/all [get]
  2851. func (this *EdbInfoController) AllEdbInfoByEs() {
  2852. br := new(models.BaseResponse).Init()
  2853. defer func() {
  2854. this.Data["json"] = br
  2855. this.ServeJSON()
  2856. }()
  2857. pageSize, _ := this.GetInt("PageSize")
  2858. currentIndex, _ := this.GetInt("CurrentIndex")
  2859. var total int64
  2860. var startSize int
  2861. if pageSize <= 0 {
  2862. pageSize = utils.PageSize20
  2863. }
  2864. if currentIndex <= 0 {
  2865. currentIndex = 1
  2866. }
  2867. startSize = paging.StartIndex(currentIndex, pageSize)
  2868. keyWord := this.GetString("KeyWord")
  2869. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  2870. filterSource, _ := this.GetInt("FilterSource")
  2871. if filterSource <= 0 {
  2872. filterSource = 1
  2873. }
  2874. //指标来源
  2875. source, _ := this.GetInt("Source")
  2876. frequency := this.GetString("Frequency") //频度
  2877. isAddPredictEdb, _ := this.GetBool("IsAddPredictEdb") //是否查询添加预测指标
  2878. edbType, _ := this.GetInt("EdbType", 0) // 指标类型:0-基础和计算;1-基础指标;2-计算指标
  2879. var edbInfoList []*data_manage.EdbInfoList
  2880. var err error
  2881. // 无权限指标 和 无权限指标分类id
  2882. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(this.SysUser.AdminId, utils.EDB_INFO_TYPE, edbType)
  2883. if err != nil {
  2884. br.Msg = "获取失败"
  2885. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2886. return
  2887. }
  2888. // 是否走ES
  2889. isEs := false
  2890. if keyWord != "" {
  2891. var keyWordArr []string
  2892. keyWordArr = append(keyWordArr, keyWord)
  2893. newKeyWord := strings.Split(keyWord, " ")
  2894. keyWordArr = append(keyWordArr, newKeyWord...)
  2895. frequencyList := make([]string, 0)
  2896. if frequency != `` {
  2897. frequencyList = []string{frequency}
  2898. }
  2899. // 普通的搜索
  2900. total, edbInfoList, err = elastic.SearchEdbInfoData(keyWord, startSize, pageSize, filterSource, source, frequencyList, []string{}, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, []int{}, []int{}, []int{}, []int{1, 2}, -1, 0, this.SysUser.AdminId, map[string]string{})
  2901. isEs = true
  2902. } else {
  2903. var condition string
  2904. var pars []interface{}
  2905. // 普通指标
  2906. condition += ` AND edb_info_type = ? `
  2907. pars = append(pars, 0)
  2908. // 无权限指标id
  2909. lenNoPermissionEdbInfoIdList := len(noPermissionEdbInfoIdList)
  2910. if lenNoPermissionEdbInfoIdList > 0 {
  2911. condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbInfoIdList) + `) `
  2912. pars = append(pars, noPermissionEdbInfoIdList)
  2913. }
  2914. // 无权限指标分类id
  2915. lenNoPermissionEdbClassifyIdList := len(noPermissionEdbClassifyIdList)
  2916. if lenNoPermissionEdbClassifyIdList > 0 {
  2917. condition += ` AND classify_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbClassifyIdList) + `) `
  2918. pars = append(pars, noPermissionEdbClassifyIdList)
  2919. }
  2920. switch filterSource {
  2921. case 2:
  2922. condition += ` AND frequency='月度' `
  2923. case 3:
  2924. condition += ` AND frequency <> '日度' `
  2925. case 4:
  2926. condition += ` AND edb_type = 1 `
  2927. case 5:
  2928. condition += ` AND source = 6 ` //来源(同比值)
  2929. case 6:
  2930. condition += ` AND frequency != ? `
  2931. pars = append(pars, "年度")
  2932. }
  2933. //频度
  2934. if frequency != "" {
  2935. condition += ` AND frequency = ? `
  2936. pars = append(pars, frequency)
  2937. }
  2938. // 查询只允许添加预测指标的搜索
  2939. if isAddPredictEdb {
  2940. condition += ` AND frequency in ("日度","周度","月度") `
  2941. }
  2942. total, edbInfoList, err = data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  2943. }
  2944. if err != nil {
  2945. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  2946. }
  2947. page := paging.GetPaging(currentIndex, pageSize, int(total))
  2948. edbInfoListLen := len(edbInfoList)
  2949. classifyIdList := make([]int, 0)
  2950. for i := 0; i < edbInfoListLen; i++ {
  2951. edbInfoList[i].ConvertToResp()
  2952. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  2953. classifyIdList = append(classifyIdList, edbInfoList[i].ClassifyId)
  2954. }
  2955. // 当前列表中的分类map
  2956. classifyMap := make(map[int]*data_manage.EdbClassify)
  2957. if edbInfoListLen > 0 {
  2958. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2959. if err != nil {
  2960. br.Msg = "获取失败"
  2961. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2962. return
  2963. }
  2964. for _, v := range classifyList {
  2965. classifyMap[v.ClassifyId] = v
  2966. }
  2967. // 获取所有有权限的指标和分类
  2968. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2969. if err != nil {
  2970. br.Msg = "获取失败"
  2971. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2972. return
  2973. }
  2974. // 如果是ES的话,需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  2975. if isEs {
  2976. edbInfoIdList := make([]int, 0)
  2977. for i := 0; i < edbInfoListLen; i++ {
  2978. edbInfoIdList = append(edbInfoIdList, edbInfoList[i].EdbInfoId)
  2979. tmpEdbInfo := edbInfoList[i]
  2980. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  2981. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2982. }
  2983. }
  2984. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  2985. if err != nil {
  2986. br.Msg = "获取失败"
  2987. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2988. return
  2989. }
  2990. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  2991. for _, v := range tmpEdbList {
  2992. edbInfoMap[v.EdbInfoId] = v
  2993. }
  2994. for i := 0; i < edbInfoListLen; i++ {
  2995. tmpEdbInfo, ok := edbInfoMap[edbInfoList[i].EdbInfoId]
  2996. if !ok {
  2997. continue
  2998. }
  2999. edbInfoList[i].IsJoinPermission = tmpEdbInfo.IsJoinPermission
  3000. }
  3001. }
  3002. // 权限校验
  3003. for i := 0; i < edbInfoListLen; i++ {
  3004. tmpEdbInfoItem := edbInfoList[i]
  3005. if currClassify, ok := classifyMap[tmpEdbInfoItem.ClassifyId]; ok {
  3006. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfoItem.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfoItem.EdbInfoId, tmpEdbInfoItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  3007. }
  3008. }
  3009. }
  3010. for i := 0; i < edbInfoListLen; i++ {
  3011. for j := 0; j < edbInfoListLen; j++ {
  3012. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  3013. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  3014. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  3015. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  3016. }
  3017. }
  3018. }
  3019. //新增搜索词记录
  3020. {
  3021. searchKeyword := new(data_manage.SearchKeyword)
  3022. searchKeyword.KeyWord = keyWord
  3023. searchKeyword.CreateTime = time.Now()
  3024. go data_manage.AddSearchKeyword(searchKeyword)
  3025. }
  3026. resp := data_manage.EdbInfoFilterDataResp{
  3027. Paging: page,
  3028. List: edbInfoList,
  3029. }
  3030. br.Ret = 200
  3031. br.Success = true
  3032. br.Msg = "获取成功"
  3033. br.Data = resp
  3034. }
  3035. // GetEdbDateData
  3036. // @Title 根据指标ID和具体日期获取对应日期的数据值
  3037. // @Description 根据指标ID和具体日期获取对应日期的数据值
  3038. // @Param EdbInfoId query int false "指标id"
  3039. // @Param Date query string false "日期"
  3040. // @Success 200 {object} data_manage.EdbInfoList
  3041. // @router /edb_info/date_data [get]
  3042. func (this *EdbInfoController) GetEdbDateData() {
  3043. br := new(models.BaseResponse).Init()
  3044. defer func() {
  3045. this.Data["json"] = br
  3046. this.ServeJSON()
  3047. }()
  3048. edbInfoId, _ := this.GetInt("EdbInfoId")
  3049. date := this.GetString("Date")
  3050. if edbInfoId <= 0 {
  3051. br.Msg = "请选择指标"
  3052. br.IsSendEmail = false
  3053. return
  3054. }
  3055. if date == `` {
  3056. br.Msg = "请选择日期"
  3057. br.IsSendEmail = false
  3058. return
  3059. }
  3060. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  3061. if err != nil {
  3062. br.Msg = "获取失败"
  3063. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  3064. return
  3065. }
  3066. startDate := date
  3067. endDate := date
  3068. dateType := "day"
  3069. //如果填写的是月度日期
  3070. if strings.Count(date, "-") == 1 {
  3071. startDate = date + "-01"
  3072. endDate = ``
  3073. dateType = "month"
  3074. }
  3075. dataItem, err := data.GetDateData(edbInfo, startDate, endDate)
  3076. if err != nil {
  3077. br.Msg = "获取失败"
  3078. br.ErrMsg = fmt.Sprint("获取指数据失败,Err:", err.Error())
  3079. return
  3080. }
  3081. val := ``
  3082. if dataItem != nil {
  3083. switch dateType {
  3084. case "month":
  3085. dateTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  3086. dateTimeStr := dateTime.Format(utils.FormatYearMonthDate)
  3087. if dateTimeStr != date {
  3088. val = ``
  3089. }
  3090. }
  3091. val = fmt.Sprint(dataItem.Value)
  3092. }
  3093. br.Ret = 200
  3094. br.Success = true
  3095. br.Msg = "获取成功"
  3096. br.Data = val
  3097. }
  3098. // GetEdbBeforeAndAfterDateData
  3099. // @Title 根据指标ID和具体日期获取对应日期的数据值+前后几期
  3100. // @Description 根据指标ID和具体日期获取对应日期的数据值+前后几期
  3101. // @Param EdbInfoId query int false "指标id"
  3102. // @Param Date query string false "日期"
  3103. // @Param Num query int false "前后几期数据"
  3104. // @Success 200 {object} data_manage.EdbInfoList
  3105. // @router /edb_info/date_data/before_after [post]
  3106. func (this *EdbInfoController) GetEdbBeforeAndAfterDateData() {
  3107. br := new(models.BaseResponse).Init()
  3108. defer func() {
  3109. this.Data["json"] = br
  3110. this.ServeJSON()
  3111. }()
  3112. requestBody := string(this.Ctx.Input.RequestBody)
  3113. var req request2.DateDataBeforeAfterReq
  3114. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3115. if err != nil {
  3116. br.Msg = "参数解析异常!"
  3117. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3118. return
  3119. }
  3120. edbInfoId := req.EdbInfoId
  3121. currDate := req.Date
  3122. num := req.Num
  3123. // 默认2期数据
  3124. if num <= 0 {
  3125. num = 2
  3126. }
  3127. if edbInfoId <= 0 {
  3128. br.Msg = "请选择指标"
  3129. br.IsSendEmail = false
  3130. return
  3131. }
  3132. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  3133. if err != nil {
  3134. br.Msg = "获取失败"
  3135. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  3136. return
  3137. }
  3138. dataList := make([]*data_manage.EdbDataList, 0)
  3139. switch edbInfo.EdbInfoType {
  3140. case 0:
  3141. dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  3142. case 1:
  3143. _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  3144. default:
  3145. br.Msg = "指标类型异常!"
  3146. br.ErrMsg = "指标类型异常,Err:" + strconv.Itoa(edbInfo.EdbInfoType)
  3147. return
  3148. }
  3149. if currDate == `` {
  3150. currDate, err = excel2.GetEdbDateByMoveForward(requestBody, dataList)
  3151. if err != nil {
  3152. br.Msg = "日期前移失败"
  3153. br.ErrMsg = "日期前移失败,Err:" + err.Error()
  3154. return
  3155. }
  3156. } else {
  3157. if strings.Count(currDate, "-") == 1 {
  3158. currDate = currDate + "-01"
  3159. // 查找这个月早的时间作为起始时间
  3160. for _, v := range dataList {
  3161. if v.DataTime >= currDate {
  3162. currDate = v.DataTime
  3163. break
  3164. }
  3165. }
  3166. }
  3167. }
  3168. currDate, err = excel2.HandleMixTableDateChange(currDate, requestBody)
  3169. if err != nil {
  3170. br.Msg = "日期变换失败"
  3171. br.ErrMsg = "日期变换失败,Err:" + err.Error()
  3172. return
  3173. }
  3174. list := make([]*data_manage.EdbDataList, 5)
  3175. list, err = data.GetEdbBeforeAndAfterDateData(currDate, dataList)
  3176. if err != nil {
  3177. br.Msg = "获取失败"
  3178. br.ErrMsg = fmt.Sprint("获取后面的指数据失败,Err:", err.Error())
  3179. return
  3180. }
  3181. // 这个日期不存在的话直接返回空数组
  3182. resp := data_manage.BeforeAndAfterDateDataResp{
  3183. List: list,
  3184. Date: currDate,
  3185. }
  3186. br.Ret = 200
  3187. br.Success = true
  3188. br.Msg = "获取成功"
  3189. br.Data = resp
  3190. }
  3191. // EdbChartAdminList
  3192. // @Title 获取创建人员分组
  3193. // @Description 获取创建人员分组
  3194. // @Param Source query int false "来源 :1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库"
  3195. // @Success 200 {object} company.DepartmentGroupSellersResp
  3196. // @router /edb_chart/adminList [get]
  3197. func (this *EdbInfoController) EdbChartAdminList() {
  3198. br := new(models.BaseResponse).Init()
  3199. defer func() {
  3200. this.Data["json"] = br
  3201. this.ServeJSON()
  3202. }()
  3203. sysUser := this.SysUser
  3204. if sysUser == nil {
  3205. br.Msg = "请登录"
  3206. br.ErrMsg = "请登录,SysUser Is Empty"
  3207. br.Ret = 408
  3208. return
  3209. }
  3210. source, _ := this.GetInt("Source", 0)
  3211. if source <= 0 || source > 5 {
  3212. br.Msg = "错误的来源"
  3213. br.IsSendEmail = false
  3214. return
  3215. }
  3216. idList, err := data.GetEdbChartAdminList(source)
  3217. if err != nil {
  3218. //br.Success = true
  3219. br.Msg = "获取失败"
  3220. br.ErrMsg = "获取失败,Err:" + err.Error()
  3221. return
  3222. }
  3223. fmt.Println("idlist:", idList)
  3224. systemAdminMap := make(map[int]*system.Admin)
  3225. departmentMap := make(map[string]string)
  3226. departmentAdminMap := make(map[int]string)
  3227. groupMap := make(map[string]string)
  3228. systemAdminList, err := system.GetAdminListByIdListWithoutEnable(idList)
  3229. if err != nil {
  3230. br.Msg = "获取用户列表失败"
  3231. br.ErrMsg = "获取用户列表失败,Err:" + err.Error()
  3232. return
  3233. }
  3234. for _, v := range systemAdminList {
  3235. systemAdminMap[v.AdminId] = v
  3236. departmentMap[v.DepartmentName] = v.DepartmentName
  3237. departmentAdminMap[v.AdminId] = v.DepartmentName
  3238. groupMap[v.GroupName+v.DepartmentName] = v.DepartmentName
  3239. }
  3240. depList := make([]company.DepartmentGroupAdmins, 0)
  3241. i := 1
  3242. for _, v := range departmentMap {
  3243. item := company.DepartmentGroupAdmins{
  3244. AdminId: "dep_" + strconv.Itoa(i),
  3245. RealName: v,
  3246. }
  3247. i++
  3248. depList = append(depList, item)
  3249. }
  3250. for k := 0; k < len(depList); k++ {
  3251. for k1, v1 := range groupMap {
  3252. if v1 == depList[k].RealName && v1 != "" {
  3253. item := &company.DepartmentGroupAdmins{
  3254. AdminId: "group_" + strconv.Itoa(k),
  3255. RealName: strings.Replace(k1, v1, "", -1),
  3256. }
  3257. depList[k].ChildrenList = append(depList[k].ChildrenList, item)
  3258. }
  3259. }
  3260. }
  3261. for _, admin := range systemAdminList {
  3262. for i, v := range depList {
  3263. for k, group := range v.ChildrenList {
  3264. if admin.GroupName == group.RealName && admin.DepartmentName == v.RealName {
  3265. item := &company.DepartmentGroupAdmins{
  3266. AdminId: strconv.Itoa(admin.AdminId),
  3267. RealName: admin.RealName,
  3268. RoleTypeCode: admin.RoleTypeCode,
  3269. Authority: admin.Authority,
  3270. }
  3271. if group.RealName != "" {
  3272. depList[i].ChildrenList[k].ChildrenList = append(depList[i].ChildrenList[k].ChildrenList, item)
  3273. } else {
  3274. depList[i].ChildrenList = append(depList[i].ChildrenList, item)
  3275. }
  3276. }
  3277. }
  3278. }
  3279. }
  3280. for i, groupList := range depList {
  3281. for k, v := range groupList.ChildrenList {
  3282. if v.RealName == "" {
  3283. newChildrenList := append(groupList.ChildrenList[:k], groupList.ChildrenList[k+1:]...)
  3284. depList[i].ChildrenList = newChildrenList
  3285. }
  3286. }
  3287. }
  3288. br.Ret = 200
  3289. br.Success = true
  3290. br.Msg = "获取成功"
  3291. br.Data = depList
  3292. }
  3293. // EdbSourceList
  3294. // @Title 指标来源列表
  3295. // @Description 指标来源列表
  3296. // @Param IsBase query int false "是否为基础指标: 1-基础指标,2:计算指标"
  3297. // @Param EdbInfoType query int false "指标类型,0:指标库;1:预测指标,默认:0"
  3298. // @Success 200 {object} data_manage.EdbInfoListResp
  3299. // @router /edb_source/list [get]
  3300. func (this *EdbInfoController) EdbSourceList() {
  3301. br := new(models.BaseResponse).Init()
  3302. defer func() {
  3303. if br.ErrMsg == "" {
  3304. br.IsSendEmail = false
  3305. }
  3306. this.Data["json"] = br
  3307. this.ServeJSON()
  3308. }()
  3309. isBase, _ := this.GetInt("IsBase", 0)
  3310. edbInfoType, _ := this.GetInt("EdbInfoType", 0)
  3311. pars := make([]interface{}, 0)
  3312. cond := ` AND edb_info_type = ? `
  3313. pars = append(pars, edbInfoType)
  3314. // 计算类型
  3315. switch isBase {
  3316. case 1:
  3317. cond += ` AND is_base = ? `
  3318. pars = append(pars, 1)
  3319. case 2:
  3320. cond += ` AND is_base = ? `
  3321. pars = append(pars, 2)
  3322. }
  3323. list, e := data_manage.GetEdbSourceItemsByCondition(cond, pars, []string{}, "")
  3324. if e != nil {
  3325. br.Msg = "获取失败"
  3326. br.ErrMsg = "获取指标来源列表失败, Err: " + e.Error()
  3327. return
  3328. }
  3329. br.Data = list
  3330. br.Ret = 200
  3331. br.Success = true
  3332. br.Msg = "获取成功"
  3333. }
  3334. // EdbSourceListByPython
  3335. // @Title 指标来源列表
  3336. // @Description 指标来源列表
  3337. // @Param IsBase query int false "是否为基础指标: 1-是"
  3338. // @Success 200 {object} data_manage.EdbInfoListResp
  3339. // @router /edb_source/list/python [get]
  3340. func (this *EdbInfoController) EdbSourceListByPython() {
  3341. br := new(models.BaseResponse).Init()
  3342. defer func() {
  3343. if br.ErrMsg == "" {
  3344. br.IsSendEmail = false
  3345. }
  3346. this.Data["json"] = br
  3347. this.ServeJSON()
  3348. }()
  3349. isBase, _ := this.GetInt("IsBase", 0)
  3350. cond := ``
  3351. if isBase > 0 {
  3352. cond = ` AND is_base = 1`
  3353. }
  3354. pars := make([]interface{}, 0)
  3355. list, e := data_manage.GetEdbSourceItemsByCondition(cond, pars, []string{}, "")
  3356. if e != nil {
  3357. br.Msg = "获取失败"
  3358. br.ErrMsg = "获取指标来源列表失败, Err: " + e.Error()
  3359. return
  3360. }
  3361. baseEdbList := make([]data_manage.EdbSourceChild, 0)
  3362. calEdbList := make([]data_manage.EdbSourceChild, 0)
  3363. for _, v := range list {
  3364. switch v.IsBase {
  3365. case 1:
  3366. baseEdbList = append(baseEdbList, data_manage.EdbSourceChild{
  3367. EdbSourceId: v.EdbSourceId,
  3368. SourceName: v.SourceName,
  3369. IsBase: v.IsBase,
  3370. TableName: v.TableName,
  3371. Child: []data_manage.EdbSourceChild{},
  3372. })
  3373. case 2:
  3374. calEdbList = append(calEdbList, data_manage.EdbSourceChild{
  3375. EdbSourceId: v.EdbSourceId,
  3376. SourceName: v.SourceName,
  3377. IsBase: v.IsBase,
  3378. TableName: v.TableName,
  3379. Child: []data_manage.EdbSourceChild{},
  3380. })
  3381. }
  3382. }
  3383. resp := []data_manage.EdbSourceChild{
  3384. {
  3385. EdbSourceId: 0,
  3386. SourceName: "基础指标",
  3387. IsBase: 1,
  3388. Child: baseEdbList,
  3389. },
  3390. {
  3391. EdbSourceId: 0,
  3392. SourceName: "计算指标",
  3393. IsBase: 1,
  3394. Child: calEdbList,
  3395. },
  3396. }
  3397. br.Data = resp
  3398. br.Ret = 200
  3399. br.Success = true
  3400. br.Msg = "获取成功"
  3401. }
  3402. // EdbChartList
  3403. // @Title 指标缩略图列表接口
  3404. // @Description 指标缩略图列表接口
  3405. // @Param PageSize query int true "每页数据条数"
  3406. // @Param CurrentIndex query int true "当前页页码,从1开始"
  3407. // @Param ClassifyId query int true "分类id"
  3408. // @Param AdminId query int false "创建人id"
  3409. // @Param IsOnlyMe query bool false "是否只看我的"
  3410. // @Success 200 {object} response.EdbInfoChartListResp
  3411. // @router /edb_info/chart/list [get]
  3412. func (this *EdbInfoController) EdbChartList() {
  3413. br := new(models.BaseResponse).Init()
  3414. defer func() {
  3415. if br.ErrMsg == "" {
  3416. br.IsSendEmail = false
  3417. }
  3418. this.Data["json"] = br
  3419. this.ServeJSON()
  3420. }()
  3421. sysUser := this.SysUser
  3422. if sysUser == nil {
  3423. br.Msg = "请登录"
  3424. br.ErrMsg = "请登录,SysUser Is Empty"
  3425. br.Ret = 408
  3426. return
  3427. }
  3428. // 分页
  3429. pageSize, _ := this.GetInt("PageSize")
  3430. currentIndex, _ := this.GetInt("CurrentIndex")
  3431. var total int
  3432. page := paging.GetPaging(currentIndex, pageSize, total)
  3433. var startSize int
  3434. if pageSize <= 0 {
  3435. pageSize = utils.PageSize20
  3436. }
  3437. if currentIndex <= 0 {
  3438. currentIndex = 1
  3439. }
  3440. startSize = paging.StartIndex(currentIndex, pageSize)
  3441. // 基础指标
  3442. var condition string
  3443. var pars []interface{}
  3444. condition += ` AND edb_info_type = ? `
  3445. pars = append(pars, 0)
  3446. // 指标类型
  3447. edbType, _ := this.GetInt("EdbType", 1)
  3448. if edbType > 0 {
  3449. condition += ` AND edb_type = ? `
  3450. pars = append(pars, edbType)
  3451. }
  3452. switch edbType {
  3453. case utils.EdbTypeBase:
  3454. condition += ` AND edb_type = ? `
  3455. pars = append(pars, edbType)
  3456. case utils.EdbTypeCalculate:
  3457. condition += ` AND edb_type = ? AND sys_user_id = ? `
  3458. pars = append(pars, edbType, sysUser.AdminId)
  3459. }
  3460. // 分类筛选
  3461. classifyId, _ := this.GetInt("ClassifyId")
  3462. if classifyId > 0 {
  3463. childClassify, e, _ := data.GetChildClassifyByClassifyId(classifyId)
  3464. if e != nil && !utils.IsErrNoRow(e) {
  3465. br.Msg = "获取失败"
  3466. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  3467. return
  3468. }
  3469. var classifyIds []int
  3470. for _, v := range childClassify {
  3471. classifyIds = append(classifyIds, v.ClassifyId)
  3472. }
  3473. condition += fmt.Sprintf(` AND classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  3474. pars = append(pars, classifyIds)
  3475. }
  3476. // 创建人
  3477. adminId, _ := this.GetInt("AdminId")
  3478. if adminId > 0 {
  3479. condition += ` AND sys_user_id = ? `
  3480. pars = append(pars, adminId)
  3481. }
  3482. // 无权限指标 和 无权限指标分类id
  3483. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(this.SysUser.AdminId, utils.EDB_INFO_TYPE, edbType)
  3484. if err != nil {
  3485. br.Msg = "获取失败"
  3486. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  3487. return
  3488. }
  3489. noPermissionEdbInfoIdNum := len(noPermissionEdbInfoIdList)
  3490. if noPermissionEdbInfoIdNum > 0 {
  3491. condition += ` AND edb_info_id NOT IN (` + utils.GetOrmInReplace(noPermissionEdbInfoIdNum) + `) `
  3492. pars = append(pars, noPermissionEdbInfoIdList)
  3493. }
  3494. noPermissionClassifyInfoIdNum := len(noPermissionEdbClassifyIdList)
  3495. if noPermissionClassifyInfoIdNum > 0 {
  3496. condition += ` AND classify_id NOT IN (` + utils.GetOrmInReplace(noPermissionClassifyInfoIdNum) + `) `
  3497. pars = append(pars, noPermissionEdbClassifyIdList)
  3498. }
  3499. // 获取指标信息
  3500. dataCount, list, e := data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  3501. if e != nil {
  3502. br.Msg = "获取失败"
  3503. br.ErrMsg = "获取普通指标列表失败, Err:" + e.Error()
  3504. return
  3505. }
  3506. respList := make([]*data_manage.EdbInfoList, 0)
  3507. if len(list) > 0 {
  3508. classifyIdList := make([]int, 0)
  3509. for _, v := range list {
  3510. v.ConvertToResp()
  3511. classifyIdList = append(classifyIdList, v.ClassifyId)
  3512. }
  3513. // 指标权限
  3514. {
  3515. // 当前的分类
  3516. classifyMap := make(map[int]*data_manage.EdbClassify)
  3517. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  3518. if err != nil {
  3519. if err != nil {
  3520. br.Msg = "获取失败"
  3521. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  3522. return
  3523. }
  3524. }
  3525. for _, v := range classifyList {
  3526. classifyMap[v.ClassifyId] = v
  3527. }
  3528. // 获取所有有权限的指标和分类
  3529. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  3530. if err != nil {
  3531. br.Msg = "获取失败"
  3532. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  3533. return
  3534. }
  3535. {
  3536. tmpEdbCodeList := make([]string, 0)
  3537. for _, v := range list {
  3538. if v.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  3539. tmpEdbCodeList = append(tmpEdbCodeList, v.EdbCode)
  3540. }
  3541. }
  3542. }
  3543. for _, v := range list {
  3544. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  3545. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  3546. // 不展示无权限的
  3547. if !v.HaveOperaAuth {
  3548. continue
  3549. }
  3550. v.Button = data.GetEdbOpButton(sysUser, v.SysUserId, v.EdbType, v.EdbInfoType, v.HaveOperaAuth)
  3551. }
  3552. respList = append(respList, v)
  3553. }
  3554. }
  3555. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  3556. }
  3557. resp := response.EdbInfoChartListResp{
  3558. Paging: page,
  3559. List: respList,
  3560. }
  3561. br.Ret = 200
  3562. br.Success = true
  3563. br.Msg = "获取成功"
  3564. br.Data = resp
  3565. }
  3566. // Modify
  3567. // @Title 修改指标信息接口
  3568. // @Description 编辑指标接口
  3569. // @Success 200 string "操作成功"
  3570. // @router /edb_info/modify [post]
  3571. func (this *EdbInfoController) Modify() {
  3572. br := new(models.BaseResponse).Init()
  3573. defer func() {
  3574. if br.ErrMsg == "" {
  3575. br.IsSendEmail = false
  3576. }
  3577. this.Data["json"] = br
  3578. this.ServeJSON()
  3579. }()
  3580. sysUser := this.SysUser
  3581. if sysUser == nil {
  3582. br.Msg = "请登录"
  3583. br.ErrMsg = "请登录,SysUser Is Empty"
  3584. br.Ret = 408
  3585. return
  3586. }
  3587. var req request.ModifyEdbInfoReq
  3588. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3589. if err != nil {
  3590. br.Msg = "参数解析异常!"
  3591. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3592. return
  3593. }
  3594. edbInfoId := req.EdbInfoId
  3595. if edbInfoId <= 0 {
  3596. br.Msg = "请选择指标"
  3597. br.IsSendEmail = false
  3598. return
  3599. }
  3600. edbInfo, e := data_manage.GetEdbInfoById(req.EdbInfoId)
  3601. if e != nil {
  3602. br.Msg = "操作失败"
  3603. br.ErrMsg = "获取指标信息失败, Err:" + err.Error()
  3604. return
  3605. }
  3606. var haveOperaAuth bool
  3607. // 权限校验
  3608. {
  3609. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, this.SysUser.AdminId)
  3610. if err != nil {
  3611. br.Msg = err.Error()
  3612. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  3613. return
  3614. }
  3615. }
  3616. // 编辑权限校验
  3617. button := data.GetEdbOpButton(this.SysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  3618. if !button.OpButton {
  3619. br.Msg = "无权限操作"
  3620. return
  3621. }
  3622. edbInfo.MaxValue = req.MaxValue
  3623. edbInfo.MinValue = req.MinValue
  3624. edbInfo.ManualSave = 1
  3625. edbInfo.ModifyTime = time.Now()
  3626. err = edbInfo.Update([]string{"MaxValue", "MinValue", "ManualSave", "ModifyTime"})
  3627. if err != nil {
  3628. br.Msg = "获取失败"
  3629. br.ErrMsg = "获取失败,Err:" + err.Error()
  3630. return
  3631. }
  3632. //修改es
  3633. data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  3634. br.Ret = 200
  3635. br.Success = true
  3636. br.Msg = "修改成功"
  3637. br.IsAddLog = true
  3638. }
  3639. // ChartImageSet
  3640. // @Title 设置指标的图表图片
  3641. // @Description 设置指标的图表图片接口
  3642. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  3643. // @Success Ret=200 保存成功
  3644. // @router /edb_info/image/set [post]
  3645. func (this *EdbInfoController) ChartImageSet() {
  3646. br := new(models.BaseResponse).Init()
  3647. defer func() {
  3648. if br.ErrMsg == "" {
  3649. br.IsSendEmail = false
  3650. }
  3651. this.Data["json"] = br
  3652. this.ServeJSON()
  3653. }()
  3654. sysUser := this.SysUser
  3655. if sysUser == nil {
  3656. br.Msg = "请登录"
  3657. br.ErrMsg = "请登录,SysUser Is Empty"
  3658. br.Ret = 408
  3659. return
  3660. }
  3661. var req data_manage.SetEdbInfoImageReq
  3662. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  3663. br.Msg = "参数解析异常!"
  3664. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  3665. return
  3666. }
  3667. if req.EdbInfoId <= 0 || req.ImageUrl == "" {
  3668. br.Ret = 200
  3669. br.Success = true
  3670. br.Msg = "保存成功"
  3671. return
  3672. }
  3673. edbInfo, e := data_manage.GetEdbInfoById(req.EdbInfoId)
  3674. if e != nil {
  3675. if utils.IsErrNoRow(e) {
  3676. br.Msg = "找不到该指标"
  3677. return
  3678. }
  3679. br.Msg = "保存失败"
  3680. br.ErrMsg = "获取指标失败, Err:" + e.Error()
  3681. return
  3682. }
  3683. edbInfo.ChartImage = req.ImageUrl
  3684. if e = edbInfo.Update([]string{"ChartImage"}); e != nil {
  3685. br.Msg = "保存失败"
  3686. br.ErrMsg = "更新指标缩略图失败, Err:" + e.Error()
  3687. return
  3688. }
  3689. // 修改es数据
  3690. go data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  3691. br.Ret = 200
  3692. br.Success = true
  3693. br.Msg = "保存成功"
  3694. }
  3695. // EdbInfoExistCheck
  3696. // @Title 新增指标-检验指标是否存在接口
  3697. // @Description 新增指标-检验指标是否存在接口
  3698. // @Param Source query int true "来源:1:同花顺,2:wind"
  3699. // @Param SubSource query int true "子数据来源:0:经济数据库,1:日期序列"
  3700. // @Param EdbCode query string false "指标编码/指标代码"
  3701. // @Param StockCode query string false "证券代码"
  3702. // @Success 200 {object} data_manage.EdbInfoExistCheckResp
  3703. // @router /edb_info/exist/check [get]
  3704. func (this *EdbInfoController) EdbInfoExistCheck() {
  3705. br := new(models.BaseResponse).Init()
  3706. defer func() {
  3707. this.Data["json"] = br
  3708. this.ServeJSON()
  3709. }()
  3710. source, _ := this.GetInt("Source")
  3711. edbCode := this.GetString("EdbCode")
  3712. edbCode = strings.Trim(edbCode, "\t")
  3713. edbCode = strings.Trim(edbCode, " ")
  3714. edbCode = strings.Replace(edbCode, "\t", "", -1)
  3715. subSource, _ := this.GetInt("SubSource")
  3716. stockCode := this.GetString("StockCode")
  3717. if source <= 0 {
  3718. br.Msg = "无效的数据来源"
  3719. return
  3720. }
  3721. if edbCode == "" {
  3722. br.Msg = "请输入指标ID"
  3723. return
  3724. }
  3725. if source != utils.DATA_SOURCE_WIND && source != utils.DATA_SOURCE_THS {
  3726. br.Msg = "无效的数据来源"
  3727. return
  3728. }
  3729. if subSource != utils.DATA_SUB_SOURCE_DATE {
  3730. br.Msg = "无效的数据库来源"
  3731. return
  3732. }
  3733. var indexCodeArr []string
  3734. edbCodeArr := strings.Split(edbCode, ",")
  3735. stockCodeArr := strings.Split(stockCode, ",")
  3736. var prefix string
  3737. if source == utils.DATA_SOURCE_WIND {
  3738. prefix = utils.WindDbWsd
  3739. } else if source == utils.DATA_SOURCE_THS {
  3740. prefix = utils.ThsDs
  3741. } else {
  3742. br.Msg = "来源错误"
  3743. br.ErrMsg = "来源错误"
  3744. return
  3745. }
  3746. for _, sv := range stockCodeArr {
  3747. for _, ev := range edbCodeArr {
  3748. indexCode := prefix + sv + ev
  3749. indexCodeArr = append(indexCodeArr, indexCode)
  3750. }
  3751. }
  3752. indexList, err := data_manage.GetEdbInfoByEdbCodeList(source, indexCodeArr)
  3753. if err != nil && !utils.IsErrNoRow(err) {
  3754. br.Msg = "校验指标是否存在失败"
  3755. br.ErrMsg = "校验指标是否存在失败,Err:" + err.Error()
  3756. return
  3757. }
  3758. var existStockCodeArr []string
  3759. var existEdbCodeArr []string
  3760. var existClassifyId []int
  3761. var existIndexId []int
  3762. for _, indexItem := range indexList {
  3763. for _, sv := range stockCodeArr {
  3764. for _, ev := range edbCodeArr {
  3765. if strings.Contains(indexItem.EdbCode, sv) {
  3766. existStockCodeArr = append(existStockCodeArr, sv)
  3767. }
  3768. if strings.Contains(indexItem.EdbCode, ev) {
  3769. existEdbCodeArr = append(existEdbCodeArr, ev)
  3770. existClassifyId = append(existClassifyId, indexItem.ClassifyId)
  3771. existIndexId = append(existIndexId, indexItem.EdbInfoId)
  3772. }
  3773. }
  3774. }
  3775. }
  3776. resp := new(data_manage.EdbInfoExistCheckResp)
  3777. if len(indexList) > 0 {
  3778. resp.IndexExist = true
  3779. }
  3780. resp.ExistEdbCodeArr = existEdbCodeArr
  3781. resp.ExistStockCodeArr = existStockCodeArr
  3782. resp.ExistClassifyId = existClassifyId
  3783. resp.ExistIndexId = existIndexId
  3784. resp.ExistEdbInfo = indexList
  3785. br.Ret = 200
  3786. br.Success = true
  3787. br.Msg = "获取成功"
  3788. br.Data = resp
  3789. }
  3790. // EdbInfoAdd
  3791. // @Title 指标批量保存接口
  3792. // @Description 指标批量保存接口
  3793. // @Param request body data_manage.BatchAddEdbInfoReq true "type json string"
  3794. // @Success Ret=200 保存成功
  3795. // @router /edb_info/batch/add [post]
  3796. func (this *EdbInfoController) EdbInfoBatchAdd() {
  3797. br := new(models.BaseResponse).Init()
  3798. defer func() {
  3799. this.Data["json"] = br
  3800. this.ServeJSON()
  3801. }()
  3802. sysUser := this.SysUser
  3803. if sysUser == nil {
  3804. br.Msg = "请登录"
  3805. br.ErrMsg = "请登录,SysUser Is Empty"
  3806. br.Ret = 408
  3807. return
  3808. }
  3809. deleteCache := true
  3810. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_" + strconv.Itoa(sysUser.AdminId)
  3811. defer func() {
  3812. if deleteCache {
  3813. utils.Rc.Delete(cacheKey)
  3814. }
  3815. }()
  3816. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  3817. deleteCache = false
  3818. br.Msg = "系统处理中,请稍后重试!"
  3819. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  3820. return
  3821. }
  3822. var req data_manage.BatchAddEdbInfoReq
  3823. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3824. if err != nil {
  3825. br.Msg = "参数解析异常!"
  3826. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3827. return
  3828. }
  3829. resp := new(data_manage.AddEdbInfoResp)
  3830. var edbNames string
  3831. for _, v := range req.BatchList {
  3832. edbNames += "'" + v.EdbName + "',"
  3833. }
  3834. edbNames = strings.Trim(edbNames, ",")
  3835. infoList, err := data_manage.GetEdbInfoByNames(edbNames)
  3836. if err != nil {
  3837. br.Msg = "查询指标名称重复错误!"
  3838. br.ErrMsg = "GetEdbInfoByNames,Err:" + err.Error()
  3839. return
  3840. }
  3841. if len(infoList) > 0 {
  3842. for _, v := range infoList {
  3843. resp.ExistEdbName = append(resp.ExistEdbName, v.EdbName)
  3844. }
  3845. br.Data = resp
  3846. br.Msg = "指标名称重复!"
  3847. br.ErrMsg = "指标名称重复"
  3848. return
  3849. }
  3850. for k, v := range req.BatchList {
  3851. v.EdbName = strings.Trim(v.EdbName, " ")
  3852. v.EdbCode = strings.Trim(v.EdbCode, " ")
  3853. v.StockCode = strings.Trim(v.StockCode, " ")
  3854. if v.Source <= 0 {
  3855. br.Msg = "无效的数据来源"
  3856. return
  3857. }
  3858. if v.EdbCode == "" {
  3859. br.Msg = "指标ID不能为空"
  3860. return
  3861. }
  3862. if v.StockCode == "" {
  3863. br.Msg = "证券代码不能为空"
  3864. return
  3865. }
  3866. if v.EdbName == "" {
  3867. br.Msg = "指标名称不能为空"
  3868. return
  3869. }
  3870. if v.Frequency == "" {
  3871. br.Msg = "频率不能为空"
  3872. return
  3873. }
  3874. if v.ClassifyId <= 0 {
  3875. br.Msg = "请选择分类"
  3876. return
  3877. }
  3878. terminalCode, serverUrl, _, e := data.GetEdbTerminalCodeBySource(v.Source, v.EdbCode, v.StockCode)
  3879. if e != nil {
  3880. br.Msg = "获取可以使用的终端地址失败"
  3881. br.ErrMsg = "获取可以使用的终端地址失败,Err:" + e.Error()
  3882. return
  3883. }
  3884. edbInfoItem := new(data_manage.EdbInfo)
  3885. edbInfoItem.Source = v.Source
  3886. switch v.Source {
  3887. case utils.DATA_SOURCE_WIND:
  3888. edbInfoItem.SourceName = "wind"
  3889. edbInfoItem.EdbCode = utils.WindDbWsd + v.StockCode + v.EdbCode
  3890. case utils.DATA_SOURCE_THS:
  3891. edbInfoItem.SourceName = "ths"
  3892. edbInfoItem.EdbCode = utils.ThsDs + v.StockCode + v.EdbCode
  3893. }
  3894. edbInfoItem.SubSource = utils.DATA_SUB_SOURCE_DATE
  3895. edbInfoItem.SubSourceName = "日期序列"
  3896. edbInfoItem.EdbName = v.EdbName
  3897. edbInfoItem.EdbNameSource = v.EdbName
  3898. edbInfoItem.EdbNameEn = v.EdbName
  3899. edbInfoItem.Frequency = v.Frequency
  3900. edbInfoItem.Unit = v.Unit
  3901. edbInfoItem.UnitEn = v.Unit
  3902. edbInfoItem.ClassifyId = v.ClassifyId
  3903. edbInfoItem.SysUserId = sysUser.AdminId
  3904. edbInfoItem.SysUserRealName = sysUser.RealName
  3905. edbInfoItem.IndicatorCode = v.EdbCode
  3906. edbInfoItem.StockCode = v.StockCode
  3907. edbInfoItem.TerminalCode = terminalCode
  3908. edbInfoItem.ServerUrl = serverUrl
  3909. var extra data_manage.EdbInfoExtra
  3910. if v.ApiExtraPars != "" {
  3911. extra.ApiExtraPars = v.ApiExtraPars
  3912. b, e := json.Marshal(extra)
  3913. if e != nil {
  3914. br.Msg = "保存失败"
  3915. br.ErrMsg = fmt.Sprintf("额外参数JSON格式化失败, %v", e)
  3916. return
  3917. }
  3918. edbInfoItem.Extra = string(b)
  3919. }
  3920. // 指标入库
  3921. edbInfo, err, errMsg, isSendEmail := data.EdbInfoWsdAdd(edbInfoItem)
  3922. if err != nil {
  3923. br.Msg = "保存失败"
  3924. if errMsg != `` {
  3925. br.Msg = errMsg
  3926. }
  3927. br.ErrMsg = err.Error()
  3928. br.IsSendEmail = isSendEmail
  3929. return
  3930. }
  3931. //新增操作日志
  3932. {
  3933. edbLog := new(data_manage.EdbInfoLog)
  3934. edbLog.EdbInfoId = edbInfo.EdbInfoId
  3935. edbLog.SourceName = edbInfo.SourceName
  3936. edbLog.Source = edbInfo.Source
  3937. edbLog.EdbCode = edbInfo.EdbCode
  3938. edbLog.EdbName = edbInfo.EdbName
  3939. edbLog.ClassifyId = edbInfo.ClassifyId
  3940. edbLog.SysUserId = sysUser.AdminId
  3941. edbLog.SysUserRealName = sysUser.RealName
  3942. edbLog.CreateTime = time.Now()
  3943. edbLog.Content = string(this.Ctx.Input.RequestBody)
  3944. edbLog.Status = "新增指标"
  3945. edbLog.Method = this.Ctx.Input.URI()
  3946. go data_manage.AddEdbInfoLog(edbLog)
  3947. }
  3948. if k <= 0 {
  3949. resp.EdbInfoId = edbInfo.EdbInfoId
  3950. resp.UniqueCode = edbInfo.UniqueCode
  3951. }
  3952. }
  3953. br.Ret = 200
  3954. br.Success = true
  3955. br.Msg = "保存成功"
  3956. br.Data = resp
  3957. br.IsAddLog = true
  3958. }
  3959. // SmmEdbInfoBatchAdd
  3960. // @Title 指标批量保存接口-smm
  3961. // @Description 指标批量保存接口-smm
  3962. // @Param request body data_manage.BatchAddEdbInfoReq true "type json string"
  3963. // @Success Ret=200 保存成功
  3964. // @router /edb_info/smm/batch/add [post]
  3965. func (this *EdbInfoController) SmmEdbInfoBatchAdd() {
  3966. br := new(models.BaseResponse).Init()
  3967. defer func() {
  3968. this.Data["json"] = br
  3969. this.ServeJSON()
  3970. }()
  3971. sysUser := this.SysUser
  3972. if sysUser == nil {
  3973. br.Msg = "请登录"
  3974. br.ErrMsg = "请登录,SysUser Is Empty"
  3975. br.Ret = 408
  3976. return
  3977. }
  3978. deleteCache := true
  3979. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_" + strconv.Itoa(sysUser.AdminId)
  3980. defer func() {
  3981. if deleteCache {
  3982. utils.Rc.Delete(cacheKey)
  3983. }
  3984. }()
  3985. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  3986. deleteCache = false
  3987. br.Msg = "系统处理中,请稍后重试!"
  3988. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  3989. return
  3990. }
  3991. var req data_manage.BatchAddEdbInfoReq
  3992. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3993. if err != nil {
  3994. br.Msg = "参数解析异常!"
  3995. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3996. return
  3997. }
  3998. resp := new(data_manage.AddEdbInfoResp)
  3999. var edbNames string
  4000. for _, v := range req.BatchList {
  4001. edbNames += "'" + v.EdbName + "',"
  4002. }
  4003. edbNames = strings.Trim(edbNames, ",")
  4004. infoList, err := data_manage.GetEdbInfoByNames(edbNames)
  4005. if err != nil {
  4006. br.Msg = "查询指标名称重复错误!"
  4007. br.ErrMsg = "GetEdbInfoByNames,Err:" + err.Error()
  4008. return
  4009. }
  4010. if len(infoList) > 0 {
  4011. for _, v := range infoList {
  4012. resp.ExistEdbName = append(resp.ExistEdbName, v.EdbName)
  4013. }
  4014. br.Data = resp
  4015. br.Msg = "指标名称重复!"
  4016. br.ErrMsg = "指标名称重复"
  4017. return
  4018. }
  4019. for k, v := range req.BatchList {
  4020. v.EdbName = strings.Trim(v.EdbName, " ")
  4021. v.EdbCode = strings.Trim(v.EdbCode, " ")
  4022. if v.Source <= 0 {
  4023. br.Msg = "无效的数据来源"
  4024. return
  4025. }
  4026. if v.EdbCode == "" {
  4027. br.Msg = "指标ID不能为空"
  4028. return
  4029. }
  4030. if v.EdbName == "" {
  4031. br.Msg = "指标名称不能为空"
  4032. return
  4033. }
  4034. if v.Frequency == "" {
  4035. br.Msg = "频率不能为空"
  4036. return
  4037. }
  4038. if v.ClassifyId <= 0 {
  4039. br.Msg = "请选择分类"
  4040. return
  4041. }
  4042. edbInfoItem := new(data_manage.EdbInfo)
  4043. edbInfoItem.Source = v.Source
  4044. edbInfoItem.SourceName = "SMM"
  4045. edbInfoItem.EdbCode = v.EdbCode
  4046. edbInfoItem.SubSource = 0
  4047. edbInfoItem.SubSourceName = ""
  4048. edbInfoItem.EdbName = v.EdbName
  4049. edbInfoItem.EdbNameSource = v.EdbName
  4050. edbInfoItem.EdbNameEn = v.EdbName
  4051. edbInfoItem.Frequency = v.Frequency
  4052. edbInfoItem.Unit = v.Unit
  4053. edbInfoItem.UnitEn = v.Unit
  4054. edbInfoItem.ClassifyId = v.ClassifyId
  4055. edbInfoItem.SysUserId = sysUser.AdminId
  4056. edbInfoItem.SysUserRealName = sysUser.RealName
  4057. edbInfoItem.IndicatorCode = ""
  4058. edbInfoItem.StockCode = ""
  4059. // 指标入库
  4060. edbInfo, err, errMsg, isSendEmail := data.EdbInfoWsdAdd(edbInfoItem)
  4061. if err != nil {
  4062. br.Msg = "保存失败"
  4063. if errMsg != `` {
  4064. br.Msg = errMsg
  4065. }
  4066. br.ErrMsg = err.Error()
  4067. br.IsSendEmail = isSendEmail
  4068. return
  4069. }
  4070. //新增操作日志
  4071. {
  4072. edbLog := new(data_manage.EdbInfoLog)
  4073. edbLog.EdbInfoId = edbInfo.EdbInfoId
  4074. edbLog.SourceName = edbInfo.SourceName
  4075. edbLog.Source = edbInfo.Source
  4076. edbLog.EdbCode = edbInfo.EdbCode
  4077. edbLog.EdbName = edbInfo.EdbName
  4078. edbLog.ClassifyId = edbInfo.ClassifyId
  4079. edbLog.SysUserId = sysUser.AdminId
  4080. edbLog.SysUserRealName = sysUser.RealName
  4081. edbLog.CreateTime = time.Now()
  4082. edbLog.Content = string(this.Ctx.Input.RequestBody)
  4083. edbLog.Status = "新增指标"
  4084. edbLog.Method = this.Ctx.Input.URI()
  4085. go data_manage.AddEdbInfoLog(edbLog)
  4086. }
  4087. if k <= 0 {
  4088. resp.EdbInfoId = edbInfo.EdbInfoId
  4089. resp.UniqueCode = edbInfo.UniqueCode
  4090. }
  4091. }
  4092. br.Ret = 200
  4093. br.Success = true
  4094. br.Msg = "保存成功"
  4095. br.Data = resp
  4096. br.IsAddLog = true
  4097. }
  4098. // ChartImageSetBySvg
  4099. // @Title 设置指标的图表图片
  4100. // @Description 设置指标的图表图片接口
  4101. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  4102. // @Success Ret=200 保存成功
  4103. // @router /edb_info/image/set_by_svg [post]
  4104. func (this *EdbInfoController) ChartImageSetBySvg() {
  4105. br := new(models.BaseResponse).Init()
  4106. defer func() {
  4107. if br.ErrMsg == "" {
  4108. br.IsSendEmail = false
  4109. }
  4110. this.Data["json"] = br
  4111. this.ServeJSON()
  4112. }()
  4113. sysUser := this.SysUser
  4114. if sysUser == nil {
  4115. br.Msg = "请登录"
  4116. br.ErrMsg = "请登录,SysUser Is Empty"
  4117. br.Ret = 408
  4118. return
  4119. }
  4120. imgData := this.GetString("Img")
  4121. if imgData == "" {
  4122. br.Msg = "图片参数错误"
  4123. br.ErrMsg = "图片参数错误,Img Is Empty"
  4124. return
  4125. }
  4126. edbInfoId, _ := this.GetInt("EdbInfoId", 0)
  4127. if edbInfoId <= 0 {
  4128. br.Msg = "指标参数错误"
  4129. br.ErrMsg = "指标参数错误,EdbInfoId Is Empty"
  4130. return
  4131. }
  4132. // 通过svg图片生成图片资源地址
  4133. resourceUrl, err, errMsg := services.GetResourceUrlBySvgImg(imgData)
  4134. if err != nil {
  4135. br.Msg = errMsg
  4136. br.ErrMsg = err.Error()
  4137. return
  4138. }
  4139. if edbInfoId <= 0 || resourceUrl == "" {
  4140. br.Ret = 200
  4141. br.Success = true
  4142. br.Msg = "保存成功"
  4143. return
  4144. }
  4145. edbInfo, e := data_manage.GetEdbInfoById(edbInfoId)
  4146. if e != nil {
  4147. if utils.IsErrNoRow(e) {
  4148. br.Msg = "找不到该指标"
  4149. return
  4150. }
  4151. br.Msg = "保存失败"
  4152. br.ErrMsg = "获取指标失败, Err:" + e.Error()
  4153. return
  4154. }
  4155. edbInfo.ChartImage = resourceUrl
  4156. if e = edbInfo.Update([]string{"ChartImage"}); e != nil {
  4157. br.Msg = "保存失败"
  4158. br.ErrMsg = "更新指标缩略图失败, Err:" + e.Error()
  4159. return
  4160. }
  4161. // 修改es数据
  4162. go data.AddOrEditEdbInfoToEs(edbInfoId)
  4163. br.Ret = 200
  4164. br.Success = true
  4165. br.Msg = "保存成功"
  4166. }
  4167. // BatchMoveEdb
  4168. // @Title 指标批量移动接口
  4169. // @Description 指标批量移动接口
  4170. // @Param request body request.MoveEdbClassifyReq true "type json string"
  4171. // @Success Ret=200 移动成功
  4172. // @router /edb_info/batch_move [post]
  4173. func (this *EdbInfoController) BatchMoveEdb() {
  4174. br := new(models.BaseResponse).Init()
  4175. defer func() {
  4176. this.Data["json"] = br
  4177. this.ServeJSON()
  4178. }()
  4179. var req request.MoveEdbClassifyReq
  4180. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  4181. if err != nil {
  4182. br.Msg = "参数解析异常!"
  4183. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  4184. return
  4185. }
  4186. if req.NewClassifyId <= 0 {
  4187. br.Msg = `请选择要转移的新分类`
  4188. br.IsSendEmail = false
  4189. return
  4190. }
  4191. classifyItem, err := data_manage.GetEdbClassifyById(req.NewClassifyId)
  4192. if err != nil {
  4193. br.Msg = "获取指标分类失败!"
  4194. br.ErrMsg = "获取指标分类失败!" + err.Error()
  4195. return
  4196. }
  4197. if classifyItem.SysUserId != this.SysUser.AdminId {
  4198. br.Msg = `您没有权限移动指标到该分类!`
  4199. br.IsSendEmail = false
  4200. return
  4201. }
  4202. edbInfoIdList := make([]int, 0)
  4203. if req.IsSelectAll {
  4204. // 获取指标
  4205. edbInfoList, err := data.GetAllEdbInfoListBySearchPublicEdbReq(req.SearchPublicEdbReq, this.SysUser.AdminId)
  4206. if err != nil {
  4207. br.Msg = "获取指标列表失败!"
  4208. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  4209. return
  4210. }
  4211. // 如果有过滤指标,那么就过滤吧
  4212. if len(req.NoEdbIdList) > 0 {
  4213. noEdbIdMap := make(map[int]bool)
  4214. for _, v := range req.NoEdbIdList {
  4215. noEdbIdMap[v] = true
  4216. }
  4217. for _, edbInfo := range edbInfoList {
  4218. if _, ok := noEdbIdMap[edbInfo.EdbInfoId]; !ok {
  4219. if edbInfo.SysUserId != this.SysUser.AdminId {
  4220. br.Msg = `您没有权限移动该指标!`
  4221. br.IsSendEmail = false
  4222. return
  4223. }
  4224. // 如果不在未选中的指标id列表中,那么就加入到选中的指标id列表
  4225. edbInfoIdList = append(edbInfoIdList, edbInfo.EdbInfoId)
  4226. }
  4227. }
  4228. } else {
  4229. for _, edbInfo := range edbInfoList {
  4230. edbInfoIdList = append(edbInfoIdList, edbInfo.EdbInfoId)
  4231. for _, v := range edbInfoList {
  4232. if v.SysUserId != this.SysUser.AdminId {
  4233. br.Msg = `您没有权限移动该指标!`
  4234. br.IsSendEmail = false
  4235. return
  4236. }
  4237. }
  4238. }
  4239. }
  4240. } else {
  4241. edbInfoList, err := data_manage.GetEdbInfoListByCond(` AND edb_info_id in (?) `, []interface{}{req.EdbIdList})
  4242. if err != nil {
  4243. br.Msg = "获取失败"
  4244. br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error()
  4245. return
  4246. }
  4247. for _, v := range edbInfoList {
  4248. if v.SysUserId != this.SysUser.AdminId {
  4249. br.Msg = `您没有权限移动该指标!`
  4250. br.IsSendEmail = false
  4251. return
  4252. }
  4253. }
  4254. edbInfoIdList = req.EdbIdList
  4255. }
  4256. num := len(edbInfoIdList)
  4257. if num <= 0 {
  4258. br.Msg = `请选择要移动的指标!`
  4259. br.IsSendEmail = false
  4260. return
  4261. }
  4262. if num > 100 {
  4263. br.Msg = `最多只能选择100条指标!`
  4264. br.IsSendEmail = false
  4265. return
  4266. }
  4267. // 开始批量修改指标分类
  4268. if len(edbInfoIdList) > 0 {
  4269. err = data_manage.UpdateClassifyIdByEdbInfoIdList(edbInfoIdList, req.NewClassifyId)
  4270. if err != nil {
  4271. br.Msg = `指标移动失败!`
  4272. br.ErrMsg = `指标移动失败,ERR:` + err.Error()
  4273. return
  4274. }
  4275. // 还得修改ES呢
  4276. for _, v := range edbInfoIdList {
  4277. data.AddOrEditEdbInfoToEs(v)
  4278. }
  4279. }
  4280. br.Ret = 200
  4281. br.Success = true
  4282. br.Msg = "获取成功"
  4283. }