edb_info.go 136 KB

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