edb_info.go 136 KB

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