edb_info.go 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718
  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. if keyWord == `` {
  1621. keyWord = this.GetString("Keyword")
  1622. }
  1623. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  1624. filterSource, _ := this.GetInt("FilterSource")
  1625. if filterSource <= 0 {
  1626. filterSource = 1
  1627. }
  1628. //指标来源
  1629. source, _ := this.GetInt("Source")
  1630. frequency := this.GetString("Frequency") //频度
  1631. edbTypeStr := this.GetString("EdbType", "0") // 指标类型:0-基础和计算;1-基础指标;2-计算指标;3-预测指标
  1632. edbAuth, _ := this.GetInt("EdbAuth", 0) // 指标权限范围,0-全部;1-我的;2-公共
  1633. edbCollect, _ := this.GetInt("EdbCollect", 0) // 指标收藏状态:0-全部;1-已收藏
  1634. // 父级分类id
  1635. classifyId, _ := this.GetInt("ClassifyId")
  1636. var edbInfoList []*data_manage.EdbInfoList
  1637. var err error
  1638. // 获取es搜索参数
  1639. source, frequency, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, searchUserId, err, errMsg := data.GetGeneralEdbEsSearchParams(edbTypeStr, this.SysUser.AdminId, edbAuth, edbCollect, classifyId)
  1640. if err != nil {
  1641. br.Msg = "获取失败"
  1642. if errMsg != `` {
  1643. br.Msg = errMsg
  1644. }
  1645. br.ErrMsg = "获取失败:" + err.Error()
  1646. return
  1647. }
  1648. sortMap := make(map[string]string)
  1649. // 如果没有搜索关键词,则默认根据指标编码倒序排序
  1650. if keyWord == `` {
  1651. sortMap["EdbInfoId"] = `desc`
  1652. }
  1653. // 普通的搜索
  1654. total, edbInfoList, err = elastic.SearchEdbInfoData(keyWord, startSize, pageSize, filterSource, source, frequency, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, searchUserId, sortMap)
  1655. if err != nil {
  1656. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1657. }
  1658. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1659. edbInfoListLen := len(edbInfoList)
  1660. classifyIdList := make([]int, 0)
  1661. for _, v := range edbInfoList {
  1662. v.ConvertToResp()
  1663. v.EdbNameAlias = v.EdbName
  1664. classifyIdList = append(classifyIdList, v.ClassifyId)
  1665. }
  1666. // 当前列表中的分类map
  1667. classifyMap := make(map[int]*data_manage.EdbClassify)
  1668. if len(classifyIdList) > 0 {
  1669. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1670. if err != nil {
  1671. br.Msg = "获取失败"
  1672. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  1673. return
  1674. }
  1675. for _, v := range classifyList {
  1676. classifyMap[v.ClassifyId] = v
  1677. }
  1678. // 获取所有有权限的指标和分类
  1679. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1680. if err != nil {
  1681. br.Msg = "获取失败"
  1682. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1683. return
  1684. }
  1685. // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  1686. {
  1687. edbInfoIdList := make([]int, 0)
  1688. for _, tmpEdbInfo := range edbInfoList {
  1689. edbInfoIdList = append(edbInfoIdList, tmpEdbInfo.EdbInfoId)
  1690. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  1691. tmpEdbInfo.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1692. }
  1693. }
  1694. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1695. if err != nil {
  1696. br.Msg = "获取失败"
  1697. br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error()
  1698. return
  1699. }
  1700. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1701. for _, v := range tmpEdbList {
  1702. edbInfoMap[v.EdbInfoId] = v
  1703. }
  1704. // 查询收藏
  1705. collectEdbMap := make(map[int][]int)
  1706. collectNameEdbMap := make(map[int][]string)
  1707. {
  1708. obj := data_manage.EdbCollect{}
  1709. collectList, err := obj.GetItemsByUserIdAndEdbInfoIdList(this.SysUser.AdminId, edbInfoIdList)
  1710. if err != nil {
  1711. br.Msg = "获取指标信息失败"
  1712. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  1713. return
  1714. }
  1715. for _, v := range collectList {
  1716. collectIdList, ok := collectEdbMap[v.EdbInfoId]
  1717. if !ok {
  1718. collectIdList = make([]int, 0)
  1719. }
  1720. collectEdbMap[v.EdbInfoId] = append(collectIdList, v.EdbCollectClassifyId)
  1721. collectNameList, ok := collectNameEdbMap[v.EdbInfoId]
  1722. if !ok {
  1723. collectNameList = make([]string, 0)
  1724. }
  1725. collectNameEdbMap[v.EdbInfoId] = append(collectNameList, v.ClassifyName)
  1726. }
  1727. }
  1728. for _, v := range edbInfoList {
  1729. if collectIdList, ok := collectEdbMap[v.EdbInfoId]; ok {
  1730. v.CollectClassifyIdList = collectIdList
  1731. } else {
  1732. v.CollectClassifyIdList = []int{}
  1733. }
  1734. if collectNameList, ok := collectNameEdbMap[v.EdbInfoId]; ok {
  1735. v.CollectClassifyNameList = collectNameList
  1736. } else {
  1737. v.CollectClassifyNameList = []string{}
  1738. }
  1739. tmpEdbInfo, ok := edbInfoMap[v.EdbInfoId]
  1740. if !ok {
  1741. continue
  1742. }
  1743. v.IsJoinPermission = tmpEdbInfo.IsJoinPermission
  1744. }
  1745. }
  1746. // 权限校验
  1747. for _, tmpEdbInfoItem := range edbInfoList {
  1748. if currClassify, ok := classifyMap[tmpEdbInfoItem.ClassifyId]; ok {
  1749. tmpEdbInfoItem.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfoItem.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfoItem.EdbInfoId, tmpEdbInfoItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1750. }
  1751. }
  1752. }
  1753. for i := 0; i < edbInfoListLen; i++ {
  1754. for j := 0; j < edbInfoListLen; j++ {
  1755. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1756. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1757. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1758. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1759. }
  1760. }
  1761. }
  1762. // 不返回无权限的指标
  1763. respList := make([]*data_manage.EdbInfoList, 0)
  1764. for _, v := range edbInfoList {
  1765. if v.HaveOperaAuth {
  1766. respList = append(respList, v)
  1767. }
  1768. }
  1769. resp := data_manage.EdbInfoFilterDataResp{
  1770. Paging: page,
  1771. List: respList,
  1772. }
  1773. br.Ret = 200
  1774. br.Success = true
  1775. br.Msg = "获取成功"
  1776. br.Data = resp
  1777. }
  1778. func (this *EdbInfoController) EdbInfoFilter2() {
  1779. br := new(models.BaseResponse).Init()
  1780. defer func() {
  1781. this.Data["json"] = br
  1782. this.ServeJSON()
  1783. }()
  1784. keyWord := this.GetString("KeyWord")
  1785. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  1786. filterSource, _ := this.GetInt("FilterSource")
  1787. if filterSource <= 0 {
  1788. filterSource = 1
  1789. }
  1790. var pars []interface{}
  1791. //if keyWord != "" {
  1792. // condition += ` AND ( edb_code LIKE '%` + keyWord + `%' OR edb_name LIKE '%` + keyWord + `%' )`
  1793. //}
  1794. var keyWordArr []string
  1795. keyWordArr = append(keyWordArr, keyWord)
  1796. if keyWord != "" {
  1797. newKeyWord := strings.Split(keyWord, " ")
  1798. keyWordArr = append(keyWordArr, newKeyWord...)
  1799. }
  1800. keywordLen := len(keyWordArr)
  1801. ch := make(chan map[int][]*data_manage.EdbInfoList, keywordLen)
  1802. searchWait := sync.WaitGroup{}
  1803. searchWait.Add(keywordLen)
  1804. for index, v := range keyWordArr {
  1805. v = strings.Replace(v, " ", "%", -1)
  1806. var condition string
  1807. if filterSource == 2 {
  1808. condition += ` AND frequency='月度' `
  1809. }
  1810. if filterSource == 3 {
  1811. condition += ` AND frequency <> '日度' `
  1812. }
  1813. if filterSource == 4 {
  1814. condition += ` AND edb_type = 1 `
  1815. }
  1816. conditionWhere := `AND ( edb_code LIKE '%` + v + `%' OR edb_name LIKE '%` + v + `%' )`
  1817. condition += conditionWhere
  1818. go searchEdbInfoFilter(condition, pars, index, ch, &searchWait)
  1819. }
  1820. searchWait.Wait()
  1821. newEdbInfoListMap := make(map[int][]*data_manage.EdbInfoList)
  1822. i := 0
  1823. for tmpEdbInfoList := range ch {
  1824. i++
  1825. for k, v := range tmpEdbInfoList {
  1826. newEdbInfoListMap[k] = v
  1827. }
  1828. if i == keywordLen {
  1829. break
  1830. }
  1831. }
  1832. close(ch)
  1833. edbInfoList := make([]*data_manage.EdbInfoList, 0)
  1834. existMap := make(map[int]string)
  1835. for i := 0; i < keywordLen; i++ {
  1836. if newEdbInfoList, ok := newEdbInfoListMap[i]; ok {
  1837. for _, nv := range newEdbInfoList {
  1838. if _, ok := existMap[nv.EdbInfoId]; !ok {
  1839. edbInfoList = append(edbInfoList, nv)
  1840. }
  1841. existMap[nv.EdbInfoId] = nv.EdbCode
  1842. }
  1843. }
  1844. }
  1845. if edbInfoList == nil {
  1846. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1847. }
  1848. edbInfoListLen := len(edbInfoList)
  1849. for i := 0; i < edbInfoListLen; i++ {
  1850. edbInfoList[i].ConvertToResp()
  1851. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  1852. }
  1853. for i := 0; i < edbInfoListLen; i++ {
  1854. for j := 0; j < edbInfoListLen; j++ {
  1855. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1856. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1857. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1858. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1859. }
  1860. }
  1861. }
  1862. //新增搜索词记录
  1863. {
  1864. searchKeyword := new(data_manage.SearchKeyword)
  1865. searchKeyword.KeyWord = keyWord
  1866. searchKeyword.CreateTime = time.Now()
  1867. go data_manage.AddSearchKeyword(searchKeyword)
  1868. }
  1869. br.Ret = 200
  1870. br.Success = true
  1871. br.Msg = "获取成功"
  1872. br.Data = edbInfoList
  1873. }
  1874. func searchEdbInfoFilter(condition string, pars []interface{}, i int, ch chan map[int][]*data_manage.EdbInfoList, searchWait *sync.WaitGroup) (newEdbInfoListMap map[int][]*data_manage.EdbInfoList, err error) {
  1875. newEdbInfoListMap = make(map[int][]*data_manage.EdbInfoList)
  1876. defer func() {
  1877. if err != nil {
  1878. go alarm_msg.SendAlarmMsg("查询近6个月的客户增量数据异常:"+err.Error(), 3)
  1879. //go utils.SendEmail(utils.APPNAME+"查询近6个月的客户增量数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  1880. }
  1881. ch <- newEdbInfoListMap
  1882. searchWait.Done()
  1883. }()
  1884. newEdbInfoList, err := data_manage.GetEdbInfoFilter(condition, pars)
  1885. for _, v := range newEdbInfoList {
  1886. v.ConvertToResp()
  1887. }
  1888. newEdbInfoListMap[i] = newEdbInfoList
  1889. return
  1890. }
  1891. // EdbInfoAllRefresh
  1892. // @Title 指标全部刷新接口
  1893. // @Description 指标全部刷新接口
  1894. // @Param EdbInfoId query int true "指标id"
  1895. // @Success Ret=200 全部刷新成功
  1896. // @router /edb_info/refresh/all [get]
  1897. func (this *EdbInfoController) EdbInfoAllRefresh() {
  1898. br := new(models.BaseResponse).Init()
  1899. defer func() {
  1900. this.Data["json"] = br
  1901. this.ServeJSON()
  1902. }()
  1903. sysUser := this.SysUser
  1904. if sysUser == nil {
  1905. br.Msg = "请登录"
  1906. br.ErrMsg = "请登录,SysUser Is Empty"
  1907. br.Ret = 408
  1908. return
  1909. }
  1910. edbInfoId, _ := this.GetInt("EdbInfoId")
  1911. if edbInfoId <= 0 {
  1912. br.Msg = "参数错误"
  1913. br.ErrMsg = "参数错误"
  1914. return
  1915. }
  1916. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1917. if err != nil {
  1918. if utils.IsErrNoRow(err) {
  1919. br.Msg = "数据已被删除,请刷新页面"
  1920. br.ErrMsg = "数据已被删除,请刷新页面,edbInfoId:" + strconv.Itoa(edbInfoId)
  1921. return
  1922. }
  1923. br.Msg = "刷新失败"
  1924. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1925. return
  1926. }
  1927. if edbInfo == nil {
  1928. br.Msg = "数据已被删除,请刷新页面"
  1929. br.ErrMsg = "数据已被删除,请刷新页面,edbInfoId:" + strconv.Itoa(edbInfoId)
  1930. return
  1931. }
  1932. err, isAsync := data.EdbInfoRefreshAllFromBaseV2(edbInfoId, true, false)
  1933. if err != nil {
  1934. br.Msg = "刷新失败"
  1935. br.ErrMsg = "刷新指标失败,EdbInfoRefreshAllFromBase Err:" + err.Error()
  1936. return
  1937. }
  1938. br.Ret = 200
  1939. br.Success = true
  1940. br.Msg = "刷新成功"
  1941. if isAsync {
  1942. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  1943. }
  1944. }
  1945. // EdbInfoData
  1946. // @Title 获取指标数据
  1947. // @Description 获取指标数据
  1948. // @Param EdbInfoId query int true "指标ID"
  1949. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  1950. // @Param StartDate query string true "自定义开始日期"
  1951. // @Param EndDate query string true "自定义结束日期"
  1952. // @Success 200 {object} data_manage.EdbInfoDataResp
  1953. // @router /edb_info/data [get]
  1954. func (this *ChartInfoController) EdbInfoData() {
  1955. br := new(models.BaseResponse).Init()
  1956. defer func() {
  1957. this.Data["json"] = br
  1958. this.ServeJSON()
  1959. }()
  1960. sysUser := this.SysUser
  1961. if sysUser == nil {
  1962. br.Msg = "请登录"
  1963. br.ErrMsg = "请登录,SysUser Is Empty"
  1964. br.Ret = 408
  1965. return
  1966. }
  1967. fullEdb := new(data_manage.EdbInfoFullClassify)
  1968. resp := new(data_manage.EdbInfoDataFullClassifyResp)
  1969. dataList := make([]*data_manage.EdbDataList, 0)
  1970. edbInfoId, _ := this.GetInt("EdbInfoId")
  1971. if edbInfoId <= 0 {
  1972. br.Msg = "参数错误"
  1973. return
  1974. }
  1975. dateType, _ := this.GetInt("DateType")
  1976. if dateType <= 0 {
  1977. dateType = 3
  1978. }
  1979. edbInfoType, _ := this.GetInt("EdbInfoType")
  1980. if edbInfoType < 0 {
  1981. edbInfoType = 1
  1982. }
  1983. startDate := this.GetString("StartDate")
  1984. endDate := this.GetString("EndDate")
  1985. startYear, _ := this.GetInt("StartYear")
  1986. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1987. if err != nil {
  1988. if utils.IsErrNoRow(err) {
  1989. br.Ret = 200
  1990. return
  1991. }
  1992. br.Msg = "获取失败"
  1993. br.ErrMsg = "获取失败,Err:" + err.Error()
  1994. return
  1995. }
  1996. // 分类
  1997. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  1998. if err != nil {
  1999. br.Msg = err.Error()
  2000. br.ErrMsg = errMsg
  2001. return
  2002. }
  2003. edbInfoResp, err := edbInfo.ConvertToResp()
  2004. if err != nil {
  2005. br.Msg = err.Error()
  2006. br.ErrMsg = errMsg
  2007. return
  2008. }
  2009. fullEdb.EdbInfoResp = edbInfoResp
  2010. // 是否供应商停更
  2011. fullEdb.IsSupplierStop = data.GetIsSupplierStop(edbInfo.Source, edbInfo.EdbCode)
  2012. fullEdb.ClassifyList = classifyList
  2013. var currClassifyItem *data_manage.EdbClassifyIdItems
  2014. for _, v := range classifyList {
  2015. if v.ClassifyId == edbInfo.ClassifyId {
  2016. currClassifyItem = v
  2017. }
  2018. }
  2019. if currClassifyItem != nil {
  2020. haveOperaAuth, err := data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassifyItem.IsJoinPermission, sysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  2021. if err != nil {
  2022. br.Msg = err.Error()
  2023. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  2024. return
  2025. }
  2026. fullEdb.HaveOperaAuth = haveOperaAuth
  2027. }
  2028. resp.EdbInfo = fullEdb
  2029. resp.DataList = make([]*data_manage.EdbDataList, 0)
  2030. // 数据获取
  2031. if fullEdb.HaveOperaAuth { // 有权限才获取数据
  2032. maxYear := 0
  2033. if edbInfo.LatestDate != "" {
  2034. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  2035. maxYear = latestDateT.Year()
  2036. }
  2037. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  2038. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDate, endDate)
  2039. if err != nil {
  2040. br.Msg = "获取失败"
  2041. br.ErrMsg = "获取失败,Err:" + err.Error()
  2042. return
  2043. }
  2044. resp.DataList = dataList
  2045. }
  2046. br.Ret = 200
  2047. br.Success = true
  2048. br.Msg = "获取成功"
  2049. br.Data = resp
  2050. }
  2051. // EdbInfoReplaceCheck
  2052. // @Title 指标一键刷新接口
  2053. // @Description 指标一键刷新接口
  2054. // @Success Ret=200 更新成功
  2055. // @router /edb_info/replace/check [post]
  2056. func (this *EdbInfoController) EdbInfoReplaceCheck() {
  2057. br := new(models.BaseResponse).Init()
  2058. setNxKey := "EDB_INFO_REPLACE"
  2059. defer func() {
  2060. this.Data["json"] = br
  2061. this.ServeJSON()
  2062. }()
  2063. status := 0
  2064. if utils.Rc.IsExist(setNxKey) {
  2065. status = 1
  2066. } else {
  2067. status = 2
  2068. }
  2069. br.Ret = 200
  2070. br.Success = true
  2071. br.Msg = "检测成功"
  2072. br.Data = status
  2073. }
  2074. // EdbInfoReplace
  2075. // @Title 替换指标
  2076. // @Description 替换指标
  2077. // @Param request body data_manage.EdbInfoReplaceReq true "type json string"
  2078. // @Success 200 {object} data_manage.EdbInfoDataResp
  2079. // @router /edb_info/replace [post]
  2080. func (this *ChartInfoController) EdbInfoReplace() {
  2081. br := new(models.BaseResponse).Init()
  2082. setNxKey := "EDB_INFO_REPLACE"
  2083. deleteCache := true
  2084. defer func() {
  2085. if deleteCache {
  2086. utils.Rc.Delete(setNxKey)
  2087. }
  2088. this.Data["json"] = br
  2089. this.ServeJSON()
  2090. }()
  2091. sysUser := this.SysUser
  2092. if sysUser == nil {
  2093. br.Msg = "请登录"
  2094. br.ErrMsg = "请登录,SysUser Is Empty"
  2095. br.Ret = 408
  2096. return
  2097. }
  2098. if !utils.Rc.SetNX(setNxKey, 1, 30*time.Minute) {
  2099. deleteCache = false
  2100. br.Msg = "当前正在替换中,请耐心等待!"
  2101. br.IsSendEmail = false
  2102. return
  2103. }
  2104. var req data_manage.EdbInfoReplaceReq
  2105. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2106. if err != nil {
  2107. br.Msg = "参数解析异常!"
  2108. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2109. return
  2110. }
  2111. if req.OldEdbInfoId <= 0 || req.NewEdbInfoId <= 0 {
  2112. br.Msg = "参数错误!"
  2113. br.ErrMsg = "参数错误,指标id为0"
  2114. return
  2115. }
  2116. oldEdbInfo, err := data_manage.GetEdbInfoById(req.OldEdbInfoId)
  2117. if err != nil {
  2118. br.Msg = "替换失败!"
  2119. br.ErrMsg = "获取原指标失败,Err:" + err.Error()
  2120. return
  2121. }
  2122. newEdbInfo, err := data_manage.GetEdbInfoById(req.NewEdbInfoId)
  2123. if err != nil {
  2124. br.Msg = "替换失败!"
  2125. br.ErrMsg = "获取替换目标指标失败,Err:" + err.Error()
  2126. return
  2127. }
  2128. //if oldEdbInfo.EdbType != 1 {
  2129. // br.Msg = "原指标为计算指标,不可替换!"
  2130. // br.ErrMsg = "原指标为计算指标,不可替换!"
  2131. // return
  2132. //}
  2133. //
  2134. //if newEdbInfo.EdbType != 1 {
  2135. // br.Msg = "替换指标为计算指标,不可替换!"
  2136. // br.ErrMsg = "替换指标为计算指标,不可替换!"
  2137. // return
  2138. //}
  2139. if oldEdbInfo.Frequency != newEdbInfo.Frequency {
  2140. br.Msg = "原指标频度为:" + oldEdbInfo.Frequency + " 替换指标频度为:" + newEdbInfo.Frequency + ",频度不同,不可进行替换操作!"
  2141. br.ErrMsg = "原指标频度为:" + oldEdbInfo.Frequency + " 替换指标频度为:" + newEdbInfo.Frequency + ",频度不同,不可进行替换操作!"
  2142. return
  2143. }
  2144. sysAdminId := sysUser.AdminId
  2145. //replaceChartTotal, replaceCalculateTotal, err := data.EdbInfoReplace(oldEdbInfo, newEdbInfo, sysAdminId, sysUser.RealName)
  2146. _, _, err = data.EdbInfoReplace(oldEdbInfo, newEdbInfo, sysAdminId, sysUser.RealName)
  2147. //msgContent := ``
  2148. if err != nil {
  2149. br.Msg = "替换失败"
  2150. br.ErrMsg = "替换失败 replace err:" + err.Error()
  2151. return
  2152. }
  2153. br.Msg = "替换成功"
  2154. br.ErrMsg = "替换成功"
  2155. br.Ret = 200
  2156. br.IsAddLog = true
  2157. return
  2158. }
  2159. // RelationChartList
  2160. // @Title 获取ETA指标关联的图表列表接口
  2161. // @Description 获取ETA指标关联的图表列表接口
  2162. // @Param PageSize query int true "每页数据条数"
  2163. // @Param CurrentIndex query int true "当前页页码,从1开始"
  2164. // @Param EdbInfoId query int true "指标id"
  2165. // @Param IsShowMe query bool true "是否只看我的,true、false"
  2166. // @Success 200 {object} data_manage.ChartListResp
  2167. // @router /edb_info/relation/chart_list [get]
  2168. func (this *EdbInfoController) RelationChartList() {
  2169. br := new(models.BaseResponse).Init()
  2170. defer func() {
  2171. this.Data["json"] = br
  2172. this.ServeJSON()
  2173. }()
  2174. sysUser := this.SysUser
  2175. if sysUser == nil {
  2176. br.Msg = "请登录"
  2177. br.ErrMsg = "请登录,SysUser Is Empty"
  2178. br.Ret = 408
  2179. return
  2180. }
  2181. edbInfoId, _ := this.GetInt("EdbInfoId")
  2182. pageSize, _ := this.GetInt("PageSize")
  2183. currentIndex, _ := this.GetInt("CurrentIndex")
  2184. resp := data_manage.ChartListResp{}
  2185. var total int
  2186. page := paging.GetPaging(currentIndex, pageSize, total)
  2187. var startSize int
  2188. if pageSize <= 0 {
  2189. pageSize = utils.PageSize20
  2190. }
  2191. if currentIndex <= 0 {
  2192. currentIndex = 1
  2193. }
  2194. startSize = paging.StartIndex(currentIndex, pageSize)
  2195. var condition string
  2196. var pars []interface{}
  2197. if edbInfoId <= 0 {
  2198. br.Msg = "请选择指标"
  2199. br.ErrMsg = "请选择指标"
  2200. br.IsSendEmail = false
  2201. return
  2202. }
  2203. /*// 关联指标
  2204. condition += ` AND b.edb_info_id = ? `
  2205. pars = append(pars, edbInfoId)*/
  2206. //只看我的
  2207. isShowMe, _ := this.GetBool("IsShowMe")
  2208. if isShowMe {
  2209. condition += ` AND a.sys_user_id = ? `
  2210. pars = append(pars, sysUser.AdminId)
  2211. }
  2212. chartIds := make([]int, 0)
  2213. chartIdMap := make(map[int]bool)
  2214. // 查询指标绑定的图表
  2215. edbListTemp, err := data_manage.GetEdbMappingListByEdbInfoId(edbInfoId)
  2216. if err != nil && !utils.IsErrNoRow(err) {
  2217. br.Msg = "获取图表信息失败"
  2218. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2219. return
  2220. }
  2221. for _, v := range edbListTemp {
  2222. if _, ok := chartIdMap[v.ChartInfoId]; !ok {
  2223. chartIdMap[v.ChartInfoId] = true
  2224. }
  2225. }
  2226. // 查询跨品种的图表
  2227. tagXList, err := cross_variety.GetChartInfoCrossVarietyByXEdbInfoId(edbInfoId)
  2228. if err != nil && !utils.IsErrNoRow(err) {
  2229. br.Msg = "获取图表信息失败"
  2230. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2231. return
  2232. }
  2233. for _, v := range tagXList {
  2234. if _, ok := chartIdMap[v.ChartInfoId]; !ok {
  2235. chartIdMap[v.ChartInfoId] = true
  2236. }
  2237. }
  2238. tagYList, err := cross_variety.GetChartInfoCrossVarietyByYEdbInfoId(edbInfoId)
  2239. if err != nil && !utils.IsErrNoRow(err) {
  2240. br.Msg = "获取图表信息失败"
  2241. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2242. return
  2243. }
  2244. for _, v := range tagYList {
  2245. if _, ok := chartIdMap[v.ChartInfoId]; !ok {
  2246. chartIdMap[v.ChartInfoId] = true
  2247. }
  2248. }
  2249. // 获取当前账号的不可见图表
  2250. {
  2251. obj := data_manage.EdbInfoNoPermissionAdmin{}
  2252. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  2253. if err != nil && !utils.IsErrNoRow(err) {
  2254. br.Msg = "获取失败"
  2255. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2256. return
  2257. }
  2258. for _, v := range confList {
  2259. if _, ok := chartIdMap[v.ChartInfoId]; ok {
  2260. delete(chartIdMap, v.ChartInfoId)
  2261. }
  2262. }
  2263. }
  2264. for k, _ := range chartIdMap {
  2265. chartIds = append(chartIds, k)
  2266. }
  2267. // 关联指标
  2268. if len(chartIds) > 0 {
  2269. condition += ` AND a.chart_info_id in (` + utils.GetOrmInReplace(len(chartIds)) + `)`
  2270. pars = append(pars, chartIds)
  2271. } else {
  2272. items := make([]*data_manage.ChartInfoView, 0)
  2273. resp.Paging = page
  2274. resp.List = items
  2275. br.Ret = 200
  2276. br.Success = true
  2277. br.Msg = "获取成功"
  2278. return
  2279. }
  2280. // 得到图表ID
  2281. //获取关联图表列表
  2282. list, err := data_manage.GetRelationChartListByCondition(condition, pars, startSize, pageSize)
  2283. if err != nil && !utils.IsErrNoRow(err) {
  2284. br.Success = true
  2285. br.Msg = "获取图表信息失败"
  2286. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2287. return
  2288. }
  2289. if list == nil || len(list) <= 0 || (err != nil && utils.IsErrNoRow(err)) {
  2290. items := make([]*data_manage.ChartInfoView, 0)
  2291. resp.Paging = page
  2292. resp.List = items
  2293. br.Ret = 200
  2294. br.Success = true
  2295. br.Msg = "获取成功"
  2296. return
  2297. }
  2298. // 获取关联图表总数
  2299. dataCount, err := data_manage.GetRelationChartListCountByCondition(condition, pars)
  2300. if err != nil && !utils.IsErrNoRow(err) {
  2301. br.Msg = "获取指标信息失败"
  2302. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  2303. return
  2304. }
  2305. // 获取MY ETA 图表,用来判断是否加入我的图库
  2306. myChartList, err := data_manage.GetMyChartListByAdminId(sysUser.AdminId)
  2307. if err != nil && !utils.IsErrNoRow(err) {
  2308. br.Msg = "获取图表信息失败"
  2309. br.ErrMsg = "获取我的图表信息失败,Err:" + err.Error()
  2310. return
  2311. }
  2312. myChartMap := make(map[int]*data_manage.MyChartView)
  2313. for _, v := range myChartList {
  2314. myChartMap[v.ChartInfoId] = v
  2315. }
  2316. listLen := len(list)
  2317. if listLen > 0 {
  2318. // 分类id
  2319. classifyIdList := make([]int, 0)
  2320. // 当前列表中的分类map
  2321. classifyMap := make(map[int]*data_manage.ChartClassify)
  2322. for _, v := range list {
  2323. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  2324. }
  2325. classifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  2326. if err != nil {
  2327. br.Msg = "获取失败"
  2328. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2329. return
  2330. }
  2331. for _, v := range classifyList {
  2332. classifyMap[v.ChartClassifyId] = v
  2333. }
  2334. // 已授权分类id
  2335. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2336. if err != nil {
  2337. br.Msg = "获取失败"
  2338. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  2339. return
  2340. }
  2341. for _, v := range list {
  2342. if existItem, ok := myChartMap[v.ChartInfoId]; ok {
  2343. v.IsAdd = true
  2344. v.MyChartId = existItem.MyChartId
  2345. v.MyChartClassifyId = existItem.MyChartClassifyId
  2346. }
  2347. // 权限
  2348. if currClassify, ok := classifyMap[v.ChartClassifyId]; ok {
  2349. v.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.ChartInfoId, v.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2350. }
  2351. }
  2352. }
  2353. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  2354. resp.Paging = page
  2355. resp.List = list
  2356. br.Ret = 200
  2357. br.Success = true
  2358. br.Msg = "获取成功"
  2359. br.Data = resp
  2360. }
  2361. // RelationEdbList
  2362. // @Title 获取ETA指标关联的指标列表接口
  2363. // @Description 获取ETA指标关联的指标列表接口
  2364. // @Param PageSize query int true "每页数据条数"
  2365. // @Param CurrentIndex query int true "当前页页码,从1开始"
  2366. // @Param EdbInfoId query int true "指标id"
  2367. // @Param IsShowMe query bool true "是否只看我的,true、false"
  2368. // @Success 200 {object} data_manage.ChartListResp
  2369. // @router /edb_info/relation/edbinfo_list [get]
  2370. func (this *EdbInfoController) RelationEdbList() {
  2371. br := new(models.BaseResponse).Init()
  2372. defer func() {
  2373. this.Data["json"] = br
  2374. this.ServeJSON()
  2375. }()
  2376. sysUser := this.SysUser
  2377. if sysUser == nil {
  2378. br.Msg = "请登录"
  2379. br.ErrMsg = "请登录,SysUser Is Empty"
  2380. br.Ret = 408
  2381. return
  2382. }
  2383. edbInfoId, _ := this.GetInt("EdbInfoId")
  2384. pageSize, _ := this.GetInt("PageSize")
  2385. currentIndex, _ := this.GetInt("CurrentIndex")
  2386. resp := data_manage.RelationEdbInfoListResp{}
  2387. var total int
  2388. page := paging.GetPaging(currentIndex, pageSize, total)
  2389. var startSize int
  2390. if pageSize <= 0 {
  2391. pageSize = utils.PageSize20
  2392. }
  2393. if currentIndex <= 0 {
  2394. currentIndex = 1
  2395. }
  2396. startSize = paging.StartIndex(currentIndex, pageSize)
  2397. var condition string
  2398. var pars []interface{}
  2399. if edbInfoId <= 0 {
  2400. br.Msg = "请选择指标"
  2401. br.ErrMsg = "请选择指标"
  2402. br.IsSendEmail = false
  2403. return
  2404. }
  2405. // 关联指标
  2406. condition += ` AND b.from_edb_info_id = ? `
  2407. pars = append(pars, edbInfoId)
  2408. //只看我的
  2409. isShowMe, _ := this.GetBool("IsShowMe")
  2410. if isShowMe {
  2411. condition += ` AND a.sys_user_id = ? `
  2412. pars = append(pars, sysUser.AdminId)
  2413. }
  2414. // 获取当前账号的不可见图表
  2415. noPermissionEdbIdList := make([]int, 0)
  2416. {
  2417. obj := data_manage.EdbInfoNoPermissionAdmin{}
  2418. confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
  2419. if err != nil && !utils.IsErrNoRow(err) {
  2420. br.Msg = "获取失败"
  2421. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2422. return
  2423. }
  2424. for _, v := range confList {
  2425. noPermissionEdbIdList = append(noPermissionEdbIdList, v.EdbInfoId)
  2426. }
  2427. }
  2428. noPermissionEdbIdNum := len(noPermissionEdbIdList)
  2429. if noPermissionEdbIdNum > 0 {
  2430. condition += ` AND a.edb_info_id not in (` + utils.GetOrmInReplace(noPermissionEdbIdNum) + `) `
  2431. pars = append(pars, noPermissionEdbIdList)
  2432. }
  2433. //获取关联图表列表
  2434. list, err := data_manage.GetRelationEdbInfoListByCondition(condition, pars, startSize, pageSize)
  2435. if err != nil && !utils.IsErrNoRow(err) {
  2436. br.Success = true
  2437. br.Msg = "获取图表信息失败"
  2438. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2439. return
  2440. }
  2441. if list == nil || len(list) <= 0 || (err != nil && utils.IsErrNoRow(err)) {
  2442. items := make([]*data_manage.ChartEdbInfoMapping, 0)
  2443. resp.Paging = page
  2444. resp.List = items
  2445. br.Ret = 200
  2446. br.Success = true
  2447. br.Msg = "获取成功"
  2448. return
  2449. }
  2450. // 获取关联图表总数
  2451. dataCount, err := data_manage.GetRelationEdbInfoListCountByCondition(condition, pars)
  2452. if err != nil && !utils.IsErrNoRow(err) {
  2453. br.Msg = "获取指标信息失败"
  2454. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  2455. return
  2456. }
  2457. if err != nil {
  2458. br.Msg = "获取失败"
  2459. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2460. return
  2461. }
  2462. edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  2463. if len(list) > 0 {
  2464. // 当前的分类
  2465. classifyMap := make(map[int]*data_manage.EdbClassify)
  2466. {
  2467. classifyIdList := make([]int, 0)
  2468. for _, v := range list {
  2469. classifyIdList = append(classifyIdList, v.ClassifyId)
  2470. }
  2471. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2472. if err != nil {
  2473. if err != nil {
  2474. br.Msg = "获取失败"
  2475. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2476. return
  2477. }
  2478. }
  2479. for _, v := range classifyList {
  2480. classifyMap[v.ClassifyId] = v
  2481. }
  2482. }
  2483. // 获取所有有权限的指标和分类
  2484. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2485. if err != nil {
  2486. br.Msg = "获取失败"
  2487. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2488. return
  2489. }
  2490. for _, v := range list {
  2491. item := new(data_manage.ChartEdbInfoMapping)
  2492. item.EdbInfoId = v.EdbInfoId
  2493. item.SourceName = v.SourceName
  2494. item.Source = v.Source
  2495. item.EdbCode = v.EdbCode
  2496. item.EdbName = v.EdbName
  2497. item.EdbNameEn = v.EdbNameEn
  2498. item.Frequency = v.Frequency
  2499. item.FrequencyEn = data.GetFrequencyEn(v.Frequency)
  2500. if v.Unit != `无` {
  2501. item.Unit = v.Unit
  2502. }
  2503. item.UnitEn = v.UnitEn
  2504. item.StartDate = v.StartDate
  2505. item.EndDate = v.EndDate
  2506. item.ModifyTime = v.ModifyTime
  2507. item.IsAxis = 1
  2508. item.LeadValue = 0
  2509. item.LeadUnit = ""
  2510. item.ChartEdbMappingId = 0
  2511. item.ChartInfoId = 0
  2512. item.IsOrder = false
  2513. item.EdbInfoType = v.EdbInfoType
  2514. item.EdbInfoCategoryType = v.EdbInfoType
  2515. item.ChartStyle = ""
  2516. item.ChartColor = ""
  2517. item.ChartWidth = 0
  2518. item.MaxData = v.MaxValue
  2519. item.MinData = v.MinValue
  2520. item.LatestValue = v.LatestValue
  2521. item.LatestDate = v.LatestDate
  2522. item.UniqueCode = v.UniqueCode
  2523. item.ClassifyId = v.ClassifyId
  2524. dataList := make([]*data_manage.EdbDataList, 0)
  2525. // 指标权限
  2526. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  2527. item.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2528. }
  2529. // 有权限则获取数据
  2530. if item.HaveOperaAuth {
  2531. if v.EdbInfoType == 0 {
  2532. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, "", "")
  2533. } else {
  2534. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, "", "", false)
  2535. }
  2536. if err != nil {
  2537. br.Msg = "获取失败"
  2538. br.Msg = "获取失败,Err:" + err.Error()
  2539. return
  2540. }
  2541. }
  2542. item.DataList = dataList
  2543. item.ConvertToResp()
  2544. edbList = append(edbList, item)
  2545. }
  2546. }
  2547. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  2548. resp.Paging = page
  2549. resp.List = edbList
  2550. br.Ret = 200
  2551. br.Success = true
  2552. br.Msg = "获取成功"
  2553. br.Data = resp
  2554. }
  2555. // EdbInfoDataTb
  2556. // @Title 获取指标数据同比值
  2557. // @Description 获取指标数据
  2558. // @Param EdbInfoId query int true "指标ID"
  2559. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  2560. // @Param StartDate query string true "自定义开始日期"
  2561. // @Param EndDate query string true "自定义结束日期"
  2562. // @Success 200 {object} data_manage.EdbInfoDataResp
  2563. // @router /edb_info/data/tb [get]
  2564. func (this *ChartInfoController) EdbInfoDataTb() {
  2565. br := new(models.BaseResponse).Init()
  2566. defer func() {
  2567. this.Data["json"] = br
  2568. this.ServeJSON()
  2569. }()
  2570. sysUser := this.SysUser
  2571. if sysUser == nil {
  2572. br.Msg = "请登录"
  2573. br.ErrMsg = "请登录,SysUser Is Empty"
  2574. br.Ret = 408
  2575. return
  2576. }
  2577. edbInfoId, _ := this.GetInt("EdbInfoId")
  2578. if edbInfoId <= 0 {
  2579. br.Msg = "参数错误"
  2580. return
  2581. }
  2582. dateType, _ := this.GetInt("DateType")
  2583. if dateType <= 0 {
  2584. dateType = 3
  2585. }
  2586. edbInfoType, _ := this.GetInt("EdbInfoType")
  2587. if edbInfoType < 0 {
  2588. edbInfoType = 1
  2589. }
  2590. startDate := this.GetString("StartDate")
  2591. endDate := this.GetString("EndDate")
  2592. startYear, _ := this.GetInt("StartYear")
  2593. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  2594. if err != nil {
  2595. br.Msg = "获取失败"
  2596. br.Msg = "获取失败,Err:" + err.Error()
  2597. return
  2598. }
  2599. maxYear := 0
  2600. if edbInfo.LatestDate != "" {
  2601. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  2602. maxYear = latestDateT.Year()
  2603. }
  2604. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  2605. var startDateTime time.Time
  2606. if startDate != `` {
  2607. startDateTime, err = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  2608. if err != nil {
  2609. br.Msg = "开始日期异常"
  2610. br.Msg = "开始日期异常,Err:" + err.Error()
  2611. return
  2612. }
  2613. //同比值的计算方式,是需要与去年同期对比,所以如果用户需要某个时间段的数据的话,获取基础数据时,需要往前面推(1年+兼容的日期频度(目前是35天))
  2614. startDate = startDateTime.AddDate(-1, 0, -35).Format(utils.FormatDate)
  2615. }
  2616. tmpDataList, err := data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDate, endDate)
  2617. if err != nil {
  2618. br.Msg = "获取失败"
  2619. br.Msg = "获取失败,Err:" + err.Error()
  2620. return
  2621. }
  2622. // 计算同比值
  2623. dataList, minValue, maxValue, err := data.GetEdbDataTbz(edbInfo, tmpDataList, startDateTime)
  2624. if err != nil {
  2625. br.Msg = "获取失败"
  2626. br.Msg = "计算同比值失败,Err:" + err.Error()
  2627. return
  2628. }
  2629. edbInfo.MaxValue = maxValue
  2630. edbInfo.MinValue = minValue
  2631. resp := new(data_manage.EdbInfoDataResp)
  2632. resp.EdbInfo = edbInfo
  2633. resp.DataList = dataList
  2634. br.Ret = 200
  2635. br.Success = true
  2636. br.Msg = "获取成功"
  2637. br.Data = resp
  2638. }
  2639. // EdbInfoDataSeasonal
  2640. // @Title 获取指标数据(季节性)
  2641. // @Description 获取指标数据
  2642. // @Param EdbInfoId query int true "指标ID"
  2643. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  2644. // @Param StartDate query string true "自定义开始日期"
  2645. // @Param EndDate query string true "自定义结束日期"
  2646. // @Param Calendar query string true "公历/农历"
  2647. // @Success 200 {object} data_manage.EdbInfoDataResp
  2648. // @router /edb_info/data/seasonal [get]
  2649. func (this *ChartInfoController) EdbInfoDataSeasonal() {
  2650. br := new(models.BaseResponse).Init()
  2651. defer func() {
  2652. this.Data["json"] = br
  2653. this.ServeJSON()
  2654. }()
  2655. sysUser := this.SysUser
  2656. if sysUser == nil {
  2657. br.Msg = "请登录"
  2658. br.ErrMsg = "请登录,SysUser Is Empty"
  2659. br.Ret = 408
  2660. return
  2661. }
  2662. dateType, _ := this.GetInt("DateType")
  2663. startDate := this.GetString("StartDate")
  2664. endDate := this.GetString("EndDate")
  2665. startYear, _ := this.GetInt("StartYear")
  2666. edbInfoIdStr := this.GetString("EdbInfoId")
  2667. edbInfoId, err := strconv.Atoi(edbInfoIdStr)
  2668. if err != nil {
  2669. br.Msg = "指标id异常"
  2670. br.Msg = "指标id转换异常,Err:" + err.Error()
  2671. return
  2672. }
  2673. fmt.Println("startDate:", startDate)
  2674. fmt.Println("endDate:", endDate)
  2675. calendar := this.GetString("Calendar")
  2676. if calendar == "" {
  2677. calendar = "公历"
  2678. }
  2679. if dateType == 0 {
  2680. if startDate == "" {
  2681. dateType = utils.DateTypeNYears
  2682. } else {
  2683. dateType = 5
  2684. }
  2685. }
  2686. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  2687. if err != nil {
  2688. br.Msg = "获取失败"
  2689. br.Msg = "获取失败,Err:" + err.Error()
  2690. return
  2691. }
  2692. maxYear := 0
  2693. if edbInfo.LatestDate != "" {
  2694. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  2695. maxYear = latestDateT.Year()
  2696. }
  2697. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  2698. dataList, err := data.GetChartEdbSeasonalData(calendar, startDate, endDate, edbInfo)
  2699. if err != nil {
  2700. br.Msg = "获取失败"
  2701. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2702. return
  2703. }
  2704. resp := new(data_manage.EdbInfoDataSeasonalResp)
  2705. resp.EdbInfo = edbInfo
  2706. resp.DataList = dataList
  2707. br.Ret = 200
  2708. br.Success = true
  2709. br.Msg = "获取成功"
  2710. br.Data = resp
  2711. }
  2712. // SetEdbDataInsertConfig 设置插入值
  2713. // @Title 设置插入值接口
  2714. // @Description 设置插入值接口
  2715. // @Success 200 {object} data_manage.EditEdbEnInfoReq
  2716. // @router /edb_info/insert_config/set [post]
  2717. func (this *EdbInfoController) SetEdbDataInsertConfig() {
  2718. br := new(models.BaseResponse).Init()
  2719. defer func() {
  2720. this.Data["json"] = br
  2721. this.ServeJSON()
  2722. }()
  2723. sysUser := this.SysUser
  2724. if sysUser == nil {
  2725. br.Msg = "请登录"
  2726. br.ErrMsg = "请登录,SysUser Is Empty"
  2727. br.Ret = 408
  2728. return
  2729. }
  2730. var req data_manage.SetEdbDataInsertConfigReq
  2731. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2732. if err != nil {
  2733. br.Msg = "参数解析异常!"
  2734. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2735. return
  2736. }
  2737. dateStr := req.Date
  2738. if dateStr == `` {
  2739. br.Msg = "请选择日期"
  2740. br.IsSendEmail = false
  2741. return
  2742. }
  2743. date, err := time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  2744. if err != nil {
  2745. br.Msg = "日期异常"
  2746. br.ErrMsg = "日期异常,err:" + err.Error()
  2747. br.IsSendEmail = false
  2748. return
  2749. }
  2750. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  2751. if err != nil {
  2752. br.Msg = "获取指标信息失败"
  2753. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  2754. return
  2755. }
  2756. err, errMsg, isSendEmail := data_manage.CreateEdbDataInsertConfigAndData(edbInfo, date, req.Value)
  2757. if err != nil {
  2758. br.Msg = "设置失败"
  2759. if errMsg != `` {
  2760. br.Msg = errMsg
  2761. }
  2762. br.ErrMsg = "修改失败,Err:" + err.Error()
  2763. br.IsSendEmail = isSendEmail
  2764. return
  2765. }
  2766. //新增操作日志
  2767. {
  2768. edbLog := new(data_manage.EdbInfoLog)
  2769. edbLog.EdbInfoId = edbInfo.EdbInfoId
  2770. edbLog.SourceName = edbInfo.SourceName
  2771. edbLog.Source = edbInfo.Source
  2772. edbLog.EdbCode = edbInfo.EdbCode
  2773. edbLog.EdbName = edbInfo.EdbName
  2774. edbLog.ClassifyId = edbInfo.ClassifyId
  2775. edbLog.SysUserId = sysUser.AdminId
  2776. edbLog.SysUserRealName = sysUser.RealName
  2777. edbLog.CreateTime = time.Now()
  2778. edbLog.Content = string(this.Ctx.Input.RequestBody)
  2779. edbLog.Status = "设置插入值"
  2780. edbLog.Method = this.Ctx.Input.URI()
  2781. go data_manage.AddEdbInfoLog(edbLog)
  2782. }
  2783. br.Ret = 200
  2784. br.Success = true
  2785. br.Msg = "保存成功"
  2786. br.IsAddLog = true
  2787. }
  2788. // TraceEdbInfo
  2789. // @Title 指标溯源接口
  2790. // @Description 指标溯源接口
  2791. // @Param UniqueCode query int true "指标唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  2792. // @Success 200 {object} data_manage.ChartListResp
  2793. // @router /edb_info/trace [get]
  2794. func (this *EdbInfoController) TraceEdbInfo() {
  2795. br := new(models.BaseResponse).Init()
  2796. defer func() {
  2797. this.Data["json"] = br
  2798. this.ServeJSON()
  2799. }()
  2800. sysUser := this.SysUser
  2801. if sysUser == nil {
  2802. br.Msg = "请登录"
  2803. br.ErrMsg = "请登录,SysUser Is Empty"
  2804. br.Ret = 408
  2805. return
  2806. }
  2807. //edbInfoId, _ := this.GetInt("EdbInfoId")
  2808. //if edbInfoId <= 0 {
  2809. // br.Msg = "参数错误"
  2810. // br.ErrMsg = "参数错误"
  2811. // return
  2812. //}
  2813. uniqueCode := this.GetString("UniqueCode")
  2814. if uniqueCode == `` {
  2815. br.Msg = "参数错误"
  2816. br.ErrMsg = "参数错误"
  2817. return
  2818. }
  2819. // 根据UniqueCode获取指标信息
  2820. edbInfo, err := data_manage.GetEdbInfoByUniqueCode(uniqueCode)
  2821. if err != nil {
  2822. br.Msg = "获取失败"
  2823. br.Msg = "获取失败,Err:" + err.Error()
  2824. if utils.IsErrNoRow(err) {
  2825. br.Msg = "指标不存在"
  2826. }
  2827. return
  2828. }
  2829. resp, err := data.TraceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, this.SysUser.AdminId)
  2830. if err != nil {
  2831. br.Msg = "获取失败"
  2832. br.Msg = "获取失败,Err:" + err.Error()
  2833. return
  2834. }
  2835. br.Ret = 200
  2836. br.Data = resp
  2837. br.Success = true
  2838. br.Msg = "刷新成功"
  2839. }
  2840. // AllEdbInfoByEs
  2841. // @Title 所有指标筛选接口
  2842. // @Description 所有指标筛选接口
  2843. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  2844. // @Param FilterSource query int false "搜索来源:1:其他搜索,2:累计值转月值搜索,3:变频,4:基础指标,5:同比"
  2845. // @Param Frequency query string false "频度"
  2846. // @Param IsAddPredictEdb query bool false "是否查询添加预测指标"
  2847. // @Param PageSize query int true "每页数据条数"
  2848. // @Param CurrentIndex query int true "当前页页码,从1开始"
  2849. // @Success 200 {object} data_manage.EdbInfoList
  2850. // @router /edb_info/filter_by_es/all [get]
  2851. func (this *EdbInfoController) AllEdbInfoByEs() {
  2852. br := new(models.BaseResponse).Init()
  2853. defer func() {
  2854. this.Data["json"] = br
  2855. this.ServeJSON()
  2856. }()
  2857. pageSize, _ := this.GetInt("PageSize")
  2858. currentIndex, _ := this.GetInt("CurrentIndex")
  2859. var total int64
  2860. var startSize int
  2861. if pageSize <= 0 {
  2862. pageSize = utils.PageSize20
  2863. }
  2864. if currentIndex <= 0 {
  2865. currentIndex = 1
  2866. }
  2867. startSize = paging.StartIndex(currentIndex, pageSize)
  2868. keyWord := this.GetString("KeyWord")
  2869. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  2870. filterSource, _ := this.GetInt("FilterSource")
  2871. if filterSource <= 0 {
  2872. filterSource = 1
  2873. }
  2874. //指标来源
  2875. source, _ := this.GetInt("Source")
  2876. frequency := this.GetString("Frequency") //频度
  2877. isAddPredictEdb, _ := this.GetBool("IsAddPredictEdb") //是否查询添加预测指标
  2878. edbType, _ := this.GetInt("EdbType", 0) // 指标类型:0-基础和计算;1-基础指标;2-计算指标
  2879. var edbInfoList []*data_manage.EdbInfoList
  2880. var err error
  2881. // 无权限指标 和 无权限指标分类id
  2882. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(this.SysUser.AdminId, utils.EDB_INFO_TYPE, edbType)
  2883. if err != nil {
  2884. br.Msg = "获取失败"
  2885. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2886. return
  2887. }
  2888. // 是否走ES
  2889. isEs := false
  2890. if keyWord != "" {
  2891. var keyWordArr []string
  2892. keyWordArr = append(keyWordArr, keyWord)
  2893. newKeyWord := strings.Split(keyWord, " ")
  2894. keyWordArr = append(keyWordArr, newKeyWord...)
  2895. // 普通的搜索
  2896. 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{})
  2897. isEs = true
  2898. } else {
  2899. var condition string
  2900. var pars []interface{}
  2901. // 普通指标
  2902. condition += ` AND edb_info_type = ? `
  2903. pars = append(pars, 0)
  2904. // 无权限指标id
  2905. lenNoPermissionEdbInfoIdList := len(noPermissionEdbInfoIdList)
  2906. if lenNoPermissionEdbInfoIdList > 0 {
  2907. condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbInfoIdList) + `) `
  2908. pars = append(pars, noPermissionEdbInfoIdList)
  2909. }
  2910. // 无权限指标分类id
  2911. lenNoPermissionEdbClassifyIdList := len(noPermissionEdbClassifyIdList)
  2912. if lenNoPermissionEdbClassifyIdList > 0 {
  2913. condition += ` AND classify_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbClassifyIdList) + `) `
  2914. pars = append(pars, noPermissionEdbClassifyIdList)
  2915. }
  2916. switch filterSource {
  2917. case 2:
  2918. condition += ` AND frequency='月度' `
  2919. case 3:
  2920. condition += ` AND frequency <> '日度' `
  2921. case 4:
  2922. condition += ` AND edb_type = 1 `
  2923. case 5:
  2924. condition += ` AND source = 6 ` //来源(同比值)
  2925. case 6:
  2926. condition += ` AND frequency != ? `
  2927. pars = append(pars, "年度")
  2928. }
  2929. //频度
  2930. if frequency != "" {
  2931. condition += ` AND frequency = ? `
  2932. pars = append(pars, frequency)
  2933. }
  2934. // 查询只允许添加预测指标的搜索
  2935. if isAddPredictEdb {
  2936. condition += ` AND frequency in ("日度","周度","月度") `
  2937. }
  2938. total, edbInfoList, err = data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  2939. }
  2940. if err != nil {
  2941. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  2942. }
  2943. page := paging.GetPaging(currentIndex, pageSize, int(total))
  2944. edbInfoListLen := len(edbInfoList)
  2945. classifyIdList := make([]int, 0)
  2946. for i := 0; i < edbInfoListLen; i++ {
  2947. edbInfoList[i].ConvertToResp()
  2948. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  2949. classifyIdList = append(classifyIdList, edbInfoList[i].ClassifyId)
  2950. }
  2951. // 当前列表中的分类map
  2952. classifyMap := make(map[int]*data_manage.EdbClassify)
  2953. if edbInfoListLen > 0 {
  2954. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2955. if err != nil {
  2956. br.Msg = "获取失败"
  2957. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  2958. return
  2959. }
  2960. for _, v := range classifyList {
  2961. classifyMap[v.ClassifyId] = v
  2962. }
  2963. // 获取所有有权限的指标和分类
  2964. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  2965. if err != nil {
  2966. br.Msg = "获取失败"
  2967. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2968. return
  2969. }
  2970. // 如果是ES的话,需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  2971. if isEs {
  2972. edbInfoIdList := make([]int, 0)
  2973. for i := 0; i < edbInfoListLen; i++ {
  2974. edbInfoIdList = append(edbInfoIdList, edbInfoList[i].EdbInfoId)
  2975. tmpEdbInfo := edbInfoList[i]
  2976. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  2977. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2978. }
  2979. }
  2980. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  2981. if err != nil {
  2982. br.Msg = "获取失败"
  2983. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2984. return
  2985. }
  2986. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  2987. for _, v := range tmpEdbList {
  2988. edbInfoMap[v.EdbInfoId] = v
  2989. }
  2990. for i := 0; i < edbInfoListLen; i++ {
  2991. tmpEdbInfo, ok := edbInfoMap[edbInfoList[i].EdbInfoId]
  2992. if !ok {
  2993. continue
  2994. }
  2995. edbInfoList[i].IsJoinPermission = tmpEdbInfo.IsJoinPermission
  2996. }
  2997. }
  2998. // 权限校验
  2999. for i := 0; i < edbInfoListLen; i++ {
  3000. tmpEdbInfoItem := edbInfoList[i]
  3001. if currClassify, ok := classifyMap[tmpEdbInfoItem.ClassifyId]; ok {
  3002. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfoItem.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfoItem.EdbInfoId, tmpEdbInfoItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  3003. }
  3004. }
  3005. }
  3006. for i := 0; i < edbInfoListLen; i++ {
  3007. for j := 0; j < edbInfoListLen; j++ {
  3008. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  3009. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  3010. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  3011. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  3012. }
  3013. }
  3014. }
  3015. //新增搜索词记录
  3016. {
  3017. searchKeyword := new(data_manage.SearchKeyword)
  3018. searchKeyword.KeyWord = keyWord
  3019. searchKeyword.CreateTime = time.Now()
  3020. go data_manage.AddSearchKeyword(searchKeyword)
  3021. }
  3022. resp := data_manage.EdbInfoFilterDataResp{
  3023. Paging: page,
  3024. List: edbInfoList,
  3025. }
  3026. br.Ret = 200
  3027. br.Success = true
  3028. br.Msg = "获取成功"
  3029. br.Data = resp
  3030. }
  3031. // GetEdbDateData
  3032. // @Title 根据指标ID和具体日期获取对应日期的数据值
  3033. // @Description 根据指标ID和具体日期获取对应日期的数据值
  3034. // @Param EdbInfoId query int false "指标id"
  3035. // @Param Date query string false "日期"
  3036. // @Success 200 {object} data_manage.EdbInfoList
  3037. // @router /edb_info/date_data [get]
  3038. func (this *EdbInfoController) GetEdbDateData() {
  3039. br := new(models.BaseResponse).Init()
  3040. defer func() {
  3041. this.Data["json"] = br
  3042. this.ServeJSON()
  3043. }()
  3044. edbInfoId, _ := this.GetInt("EdbInfoId")
  3045. date := this.GetString("Date")
  3046. if edbInfoId <= 0 {
  3047. br.Msg = "请选择指标"
  3048. br.IsSendEmail = false
  3049. return
  3050. }
  3051. if date == `` {
  3052. br.Msg = "请选择日期"
  3053. br.IsSendEmail = false
  3054. return
  3055. }
  3056. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  3057. if err != nil {
  3058. br.Msg = "获取失败"
  3059. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  3060. return
  3061. }
  3062. startDate := date
  3063. endDate := date
  3064. dateType := "day"
  3065. //如果填写的是月度日期
  3066. if strings.Count(date, "-") == 1 {
  3067. startDate = date + "-01"
  3068. endDate = ``
  3069. dateType = "month"
  3070. }
  3071. dataItem, err := data.GetDateData(edbInfo, startDate, endDate)
  3072. if err != nil {
  3073. br.Msg = "获取失败"
  3074. br.ErrMsg = fmt.Sprint("获取指数据失败,Err:", err.Error())
  3075. return
  3076. }
  3077. val := ``
  3078. if dataItem != nil {
  3079. switch dateType {
  3080. case "month":
  3081. dateTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  3082. dateTimeStr := dateTime.Format(utils.FormatYearMonthDate)
  3083. if dateTimeStr != date {
  3084. val = ``
  3085. }
  3086. }
  3087. val = fmt.Sprint(dataItem.Value)
  3088. }
  3089. br.Ret = 200
  3090. br.Success = true
  3091. br.Msg = "获取成功"
  3092. br.Data = val
  3093. }
  3094. // GetEdbBeforeAndAfterDateData
  3095. // @Title 根据指标ID和具体日期获取对应日期的数据值+前后几期
  3096. // @Description 根据指标ID和具体日期获取对应日期的数据值+前后几期
  3097. // @Param EdbInfoId query int false "指标id"
  3098. // @Param Date query string false "日期"
  3099. // @Param Num query int false "前后几期数据"
  3100. // @Success 200 {object} data_manage.EdbInfoList
  3101. // @router /edb_info/date_data/before_after [post]
  3102. func (this *EdbInfoController) GetEdbBeforeAndAfterDateData() {
  3103. br := new(models.BaseResponse).Init()
  3104. defer func() {
  3105. this.Data["json"] = br
  3106. this.ServeJSON()
  3107. }()
  3108. requestBody := string(this.Ctx.Input.RequestBody)
  3109. var req request2.DateDataBeforeAfterReq
  3110. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3111. if err != nil {
  3112. br.Msg = "参数解析异常!"
  3113. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3114. return
  3115. }
  3116. edbInfoId := req.EdbInfoId
  3117. currDate := req.Date
  3118. num := req.Num
  3119. // 默认2期数据
  3120. if num <= 0 {
  3121. num = 2
  3122. }
  3123. if edbInfoId <= 0 {
  3124. br.Msg = "请选择指标"
  3125. br.IsSendEmail = false
  3126. return
  3127. }
  3128. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  3129. if err != nil {
  3130. br.Msg = "获取失败"
  3131. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  3132. return
  3133. }
  3134. dataList := make([]*data_manage.EdbDataList, 0)
  3135. switch edbInfo.EdbInfoType {
  3136. case 0:
  3137. dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  3138. case 1:
  3139. _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  3140. default:
  3141. br.Msg = "指标类型异常!"
  3142. br.ErrMsg = "指标类型异常,Err:" + strconv.Itoa(edbInfo.EdbInfoType)
  3143. return
  3144. }
  3145. if currDate == `` {
  3146. currDate, err = excel2.GetEdbDateByMoveForward(requestBody, dataList)
  3147. if err != nil {
  3148. br.Msg = "日期前移失败"
  3149. br.ErrMsg = "日期前移失败,Err:" + err.Error()
  3150. return
  3151. }
  3152. } else {
  3153. if strings.Count(currDate, "-") == 1 {
  3154. currDate = currDate + "-01"
  3155. // 查找这个月早的时间作为起始时间
  3156. for _, v := range dataList {
  3157. if v.DataTime >= currDate {
  3158. currDate = v.DataTime
  3159. break
  3160. }
  3161. }
  3162. }
  3163. }
  3164. currDate, err = excel2.HandleMixTableDateChange(currDate, requestBody)
  3165. if err != nil {
  3166. br.Msg = "日期变换失败"
  3167. br.ErrMsg = "日期变换失败,Err:" + err.Error()
  3168. return
  3169. }
  3170. list := make([]*data_manage.EdbDataList, 5)
  3171. list, err = data.GetEdbBeforeAndAfterDateData(currDate, dataList)
  3172. if err != nil {
  3173. br.Msg = "获取失败"
  3174. br.ErrMsg = fmt.Sprint("获取后面的指数据失败,Err:", err.Error())
  3175. return
  3176. }
  3177. // 这个日期不存在的话直接返回空数组
  3178. resp := data_manage.BeforeAndAfterDateDataResp{
  3179. List: list,
  3180. Date: currDate,
  3181. }
  3182. br.Ret = 200
  3183. br.Success = true
  3184. br.Msg = "获取成功"
  3185. br.Data = resp
  3186. }
  3187. // EdbChartAdminList
  3188. // @Title 获取创建人员分组
  3189. // @Description 获取创建人员分组
  3190. // @Param Source query int false "来源 :1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库"
  3191. // @Success 200 {object} company.DepartmentGroupSellersResp
  3192. // @router /edb_chart/adminList [get]
  3193. func (this *EdbInfoController) EdbChartAdminList() {
  3194. br := new(models.BaseResponse).Init()
  3195. defer func() {
  3196. this.Data["json"] = br
  3197. this.ServeJSON()
  3198. }()
  3199. sysUser := this.SysUser
  3200. if sysUser == nil {
  3201. br.Msg = "请登录"
  3202. br.ErrMsg = "请登录,SysUser Is Empty"
  3203. br.Ret = 408
  3204. return
  3205. }
  3206. source, _ := this.GetInt("Source", 0)
  3207. if source <= 0 || source > 5 {
  3208. br.Msg = "错误的来源"
  3209. br.IsSendEmail = false
  3210. return
  3211. }
  3212. idList, err := data.GetEdbChartAdminList(source)
  3213. if err != nil {
  3214. //br.Success = true
  3215. br.Msg = "获取失败"
  3216. br.ErrMsg = "获取失败,Err:" + err.Error()
  3217. return
  3218. }
  3219. fmt.Println("idlist:", idList)
  3220. systemAdminMap := make(map[int]*system.Admin)
  3221. departmentMap := make(map[string]string)
  3222. departmentAdminMap := make(map[int]string)
  3223. groupMap := make(map[string]string)
  3224. systemAdminList, err := system.GetAdminListByIdListWithoutEnable(idList)
  3225. if err != nil {
  3226. br.Msg = "获取用户列表失败"
  3227. br.ErrMsg = "获取用户列表失败,Err:" + err.Error()
  3228. return
  3229. }
  3230. for _, v := range systemAdminList {
  3231. systemAdminMap[v.AdminId] = v
  3232. departmentMap[v.DepartmentName] = v.DepartmentName
  3233. departmentAdminMap[v.AdminId] = v.DepartmentName
  3234. groupMap[v.GroupName+v.DepartmentName] = v.DepartmentName
  3235. }
  3236. depList := make([]company.DepartmentGroupAdmins, 0)
  3237. i := 1
  3238. for _, v := range departmentMap {
  3239. item := company.DepartmentGroupAdmins{
  3240. AdminId: "dep_" + strconv.Itoa(i),
  3241. RealName: v,
  3242. }
  3243. i++
  3244. depList = append(depList, item)
  3245. }
  3246. for k := 0; k < len(depList); k++ {
  3247. for k1, v1 := range groupMap {
  3248. if v1 == depList[k].RealName && v1 != "" {
  3249. item := &company.DepartmentGroupAdmins{
  3250. AdminId: "group_" + strconv.Itoa(k),
  3251. RealName: strings.Replace(k1, v1, "", -1),
  3252. }
  3253. depList[k].ChildrenList = append(depList[k].ChildrenList, item)
  3254. }
  3255. }
  3256. }
  3257. for _, admin := range systemAdminList {
  3258. for i, v := range depList {
  3259. for k, group := range v.ChildrenList {
  3260. if admin.GroupName == group.RealName && admin.DepartmentName == v.RealName {
  3261. item := &company.DepartmentGroupAdmins{
  3262. AdminId: strconv.Itoa(admin.AdminId),
  3263. RealName: admin.RealName,
  3264. RoleTypeCode: admin.RoleTypeCode,
  3265. Authority: admin.Authority,
  3266. }
  3267. if group.RealName != "" {
  3268. depList[i].ChildrenList[k].ChildrenList = append(depList[i].ChildrenList[k].ChildrenList, item)
  3269. } else {
  3270. depList[i].ChildrenList = append(depList[i].ChildrenList, item)
  3271. }
  3272. }
  3273. }
  3274. }
  3275. }
  3276. for i, groupList := range depList {
  3277. for k, v := range groupList.ChildrenList {
  3278. if v.RealName == "" {
  3279. newChildrenList := append(groupList.ChildrenList[:k], groupList.ChildrenList[k+1:]...)
  3280. depList[i].ChildrenList = newChildrenList
  3281. }
  3282. }
  3283. }
  3284. br.Ret = 200
  3285. br.Success = true
  3286. br.Msg = "获取成功"
  3287. br.Data = depList
  3288. }
  3289. // EdbSourceList
  3290. // @Title 指标来源列表
  3291. // @Description 指标来源列表
  3292. // @Param IsBase query int false "是否为基础指标: 1-基础指标,2:计算指标"
  3293. // @Param EdbInfoType query int false "指标类型,0:指标库;1:预测指标,默认:0"
  3294. // @Success 200 {object} data_manage.EdbInfoListResp
  3295. // @router /edb_source/list [get]
  3296. func (this *EdbInfoController) EdbSourceList() {
  3297. br := new(models.BaseResponse).Init()
  3298. defer func() {
  3299. if br.ErrMsg == "" {
  3300. br.IsSendEmail = false
  3301. }
  3302. this.Data["json"] = br
  3303. this.ServeJSON()
  3304. }()
  3305. isBase, _ := this.GetInt("IsBase", 0)
  3306. edbInfoType, _ := this.GetInt("EdbInfoType", 0)
  3307. pars := make([]interface{}, 0)
  3308. cond := ` AND edb_info_type = ? `
  3309. pars = append(pars, edbInfoType)
  3310. // 计算类型
  3311. switch isBase {
  3312. case 1:
  3313. cond += ` AND is_base = ? `
  3314. pars = append(pars, 1)
  3315. case 2:
  3316. cond += ` AND is_base = ? `
  3317. pars = append(pars, 2)
  3318. }
  3319. list, e := data_manage.GetEdbSourceItemsByCondition(cond, pars, []string{}, "")
  3320. if e != nil {
  3321. br.Msg = "获取失败"
  3322. br.ErrMsg = "获取指标来源列表失败, Err: " + e.Error()
  3323. return
  3324. }
  3325. br.Data = list
  3326. br.Ret = 200
  3327. br.Success = true
  3328. br.Msg = "获取成功"
  3329. }
  3330. // EdbSourceListByPython
  3331. // @Title 指标来源列表
  3332. // @Description 指标来源列表
  3333. // @Param IsBase query int false "是否为基础指标: 1-是"
  3334. // @Success 200 {object} data_manage.EdbInfoListResp
  3335. // @router /edb_source/list/python [get]
  3336. func (this *EdbInfoController) EdbSourceListByPython() {
  3337. br := new(models.BaseResponse).Init()
  3338. defer func() {
  3339. if br.ErrMsg == "" {
  3340. br.IsSendEmail = false
  3341. }
  3342. this.Data["json"] = br
  3343. this.ServeJSON()
  3344. }()
  3345. isBase, _ := this.GetInt("IsBase", 0)
  3346. cond := ``
  3347. if isBase > 0 {
  3348. cond = ` AND is_base = 1`
  3349. }
  3350. pars := make([]interface{}, 0)
  3351. list, e := data_manage.GetEdbSourceItemsByCondition(cond, pars, []string{}, "")
  3352. if e != nil {
  3353. br.Msg = "获取失败"
  3354. br.ErrMsg = "获取指标来源列表失败, Err: " + e.Error()
  3355. return
  3356. }
  3357. baseEdbList := make([]data_manage.EdbSourceChild, 0)
  3358. calEdbList := make([]data_manage.EdbSourceChild, 0)
  3359. for _, v := range list {
  3360. switch v.IsBase {
  3361. case 1:
  3362. baseEdbList = append(baseEdbList, data_manage.EdbSourceChild{
  3363. EdbSourceId: v.EdbSourceId,
  3364. SourceName: v.SourceName,
  3365. IsBase: v.IsBase,
  3366. TableName: v.TableName,
  3367. Child: []data_manage.EdbSourceChild{},
  3368. })
  3369. case 2:
  3370. calEdbList = append(calEdbList, data_manage.EdbSourceChild{
  3371. EdbSourceId: v.EdbSourceId,
  3372. SourceName: v.SourceName,
  3373. IsBase: v.IsBase,
  3374. TableName: v.TableName,
  3375. Child: []data_manage.EdbSourceChild{},
  3376. })
  3377. }
  3378. }
  3379. resp := []data_manage.EdbSourceChild{
  3380. {
  3381. EdbSourceId: 0,
  3382. SourceName: "基础指标",
  3383. IsBase: 1,
  3384. Child: baseEdbList,
  3385. },
  3386. {
  3387. EdbSourceId: 0,
  3388. SourceName: "计算指标",
  3389. IsBase: 1,
  3390. Child: calEdbList,
  3391. },
  3392. }
  3393. br.Data = resp
  3394. br.Ret = 200
  3395. br.Success = true
  3396. br.Msg = "获取成功"
  3397. }
  3398. // EdbChartList
  3399. // @Title 指标缩略图列表接口
  3400. // @Description 指标缩略图列表接口
  3401. // @Param PageSize query int true "每页数据条数"
  3402. // @Param CurrentIndex query int true "当前页页码,从1开始"
  3403. // @Param ClassifyId query int true "分类id"
  3404. // @Param AdminId query int false "创建人id"
  3405. // @Param IsOnlyMe query bool false "是否只看我的"
  3406. // @Success 200 {object} response.EdbInfoChartListResp
  3407. // @router /edb_info/chart/list [get]
  3408. func (this *EdbInfoController) EdbChartList() {
  3409. br := new(models.BaseResponse).Init()
  3410. defer func() {
  3411. if br.ErrMsg == "" {
  3412. br.IsSendEmail = false
  3413. }
  3414. this.Data["json"] = br
  3415. this.ServeJSON()
  3416. }()
  3417. sysUser := this.SysUser
  3418. if sysUser == nil {
  3419. br.Msg = "请登录"
  3420. br.ErrMsg = "请登录,SysUser Is Empty"
  3421. br.Ret = 408
  3422. return
  3423. }
  3424. // 分页
  3425. pageSize, _ := this.GetInt("PageSize")
  3426. currentIndex, _ := this.GetInt("CurrentIndex")
  3427. var total int
  3428. page := paging.GetPaging(currentIndex, pageSize, total)
  3429. var startSize int
  3430. if pageSize <= 0 {
  3431. pageSize = utils.PageSize20
  3432. }
  3433. if currentIndex <= 0 {
  3434. currentIndex = 1
  3435. }
  3436. startSize = paging.StartIndex(currentIndex, pageSize)
  3437. // 基础指标
  3438. var condition string
  3439. var pars []interface{}
  3440. condition += ` AND edb_info_type = ? `
  3441. pars = append(pars, 0)
  3442. // 指标类型
  3443. edbType, _ := this.GetInt("EdbType", 1)
  3444. if edbType > 0 {
  3445. condition += ` AND edb_type = ? `
  3446. pars = append(pars, edbType)
  3447. }
  3448. switch edbType {
  3449. case utils.EdbTypeBase:
  3450. condition += ` AND edb_type = ? `
  3451. pars = append(pars, edbType)
  3452. case utils.EdbTypeCalculate:
  3453. condition += ` AND edb_type = ? AND sys_user_id = ? `
  3454. pars = append(pars, edbType, sysUser.AdminId)
  3455. }
  3456. // 分类筛选
  3457. classifyId, _ := this.GetInt("ClassifyId")
  3458. if classifyId > 0 {
  3459. childClassify, e, _ := data.GetChildClassifyByClassifyId(classifyId)
  3460. if e != nil && !utils.IsErrNoRow(e) {
  3461. br.Msg = "获取失败"
  3462. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  3463. return
  3464. }
  3465. var classifyIds []int
  3466. for _, v := range childClassify {
  3467. classifyIds = append(classifyIds, v.ClassifyId)
  3468. }
  3469. condition += fmt.Sprintf(` AND classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  3470. pars = append(pars, classifyIds)
  3471. }
  3472. // 创建人
  3473. adminId, _ := this.GetInt("AdminId")
  3474. if adminId > 0 {
  3475. condition += ` AND sys_user_id = ? `
  3476. pars = append(pars, adminId)
  3477. }
  3478. // 无权限指标 和 无权限指标分类id
  3479. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(this.SysUser.AdminId, utils.EDB_INFO_TYPE, edbType)
  3480. if err != nil {
  3481. br.Msg = "获取失败"
  3482. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  3483. return
  3484. }
  3485. noPermissionEdbInfoIdNum := len(noPermissionEdbInfoIdList)
  3486. if noPermissionEdbInfoIdNum > 0 {
  3487. condition += ` AND edb_info_id NOT IN (` + utils.GetOrmInReplace(noPermissionEdbInfoIdNum) + `) `
  3488. pars = append(pars, noPermissionEdbInfoIdList)
  3489. }
  3490. noPermissionClassifyInfoIdNum := len(noPermissionEdbClassifyIdList)
  3491. if noPermissionClassifyInfoIdNum > 0 {
  3492. condition += ` AND classify_id NOT IN (` + utils.GetOrmInReplace(noPermissionClassifyInfoIdNum) + `) `
  3493. pars = append(pars, noPermissionEdbClassifyIdList)
  3494. }
  3495. // 获取指标信息
  3496. dataCount, list, e := data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  3497. if e != nil {
  3498. br.Msg = "获取失败"
  3499. br.ErrMsg = "获取普通指标列表失败, Err:" + e.Error()
  3500. return
  3501. }
  3502. respList := make([]*data_manage.EdbInfoList, 0)
  3503. if len(list) > 0 {
  3504. classifyIdList := make([]int, 0)
  3505. for _, v := range list {
  3506. v.ConvertToResp()
  3507. classifyIdList = append(classifyIdList, v.ClassifyId)
  3508. }
  3509. // 指标权限
  3510. {
  3511. // 当前的分类
  3512. classifyMap := make(map[int]*data_manage.EdbClassify)
  3513. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  3514. if err != nil {
  3515. if err != nil {
  3516. br.Msg = "获取失败"
  3517. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  3518. return
  3519. }
  3520. }
  3521. for _, v := range classifyList {
  3522. classifyMap[v.ClassifyId] = v
  3523. }
  3524. // 获取所有有权限的指标和分类
  3525. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  3526. if err != nil {
  3527. br.Msg = "获取失败"
  3528. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  3529. return
  3530. }
  3531. {
  3532. tmpEdbCodeList := make([]string, 0)
  3533. for _, v := range list {
  3534. if v.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  3535. tmpEdbCodeList = append(tmpEdbCodeList, v.EdbCode)
  3536. }
  3537. }
  3538. }
  3539. for _, v := range list {
  3540. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  3541. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  3542. // 不展示无权限的
  3543. if !v.HaveOperaAuth {
  3544. continue
  3545. }
  3546. v.Button = data.GetEdbOpButton(sysUser, v.SysUserId, v.EdbType, v.EdbInfoType, v.HaveOperaAuth)
  3547. }
  3548. respList = append(respList, v)
  3549. }
  3550. }
  3551. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  3552. }
  3553. resp := response.EdbInfoChartListResp{
  3554. Paging: page,
  3555. List: respList,
  3556. }
  3557. br.Ret = 200
  3558. br.Success = true
  3559. br.Msg = "获取成功"
  3560. br.Data = resp
  3561. }
  3562. // Modify
  3563. // @Title 修改指标信息接口
  3564. // @Description 编辑指标接口
  3565. // @Success 200 string "操作成功"
  3566. // @router /edb_info/modify [post]
  3567. func (this *EdbInfoController) Modify() {
  3568. br := new(models.BaseResponse).Init()
  3569. defer func() {
  3570. if br.ErrMsg == "" {
  3571. br.IsSendEmail = false
  3572. }
  3573. this.Data["json"] = br
  3574. this.ServeJSON()
  3575. }()
  3576. sysUser := this.SysUser
  3577. if sysUser == nil {
  3578. br.Msg = "请登录"
  3579. br.ErrMsg = "请登录,SysUser Is Empty"
  3580. br.Ret = 408
  3581. return
  3582. }
  3583. var req request.ModifyEdbInfoReq
  3584. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3585. if err != nil {
  3586. br.Msg = "参数解析异常!"
  3587. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3588. return
  3589. }
  3590. edbInfoId := req.EdbInfoId
  3591. if edbInfoId <= 0 {
  3592. br.Msg = "请选择指标"
  3593. br.IsSendEmail = false
  3594. return
  3595. }
  3596. edbInfo, e := data_manage.GetEdbInfoById(req.EdbInfoId)
  3597. if e != nil {
  3598. br.Msg = "操作失败"
  3599. br.ErrMsg = "获取指标信息失败, Err:" + err.Error()
  3600. return
  3601. }
  3602. var haveOperaAuth bool
  3603. // 权限校验
  3604. {
  3605. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, this.SysUser.AdminId)
  3606. if err != nil {
  3607. br.Msg = err.Error()
  3608. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  3609. return
  3610. }
  3611. }
  3612. // 编辑权限校验
  3613. button := data.GetEdbOpButton(this.SysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  3614. if !button.OpButton {
  3615. br.Msg = "无权限操作"
  3616. return
  3617. }
  3618. edbInfo.MaxValue = req.MaxValue
  3619. edbInfo.MinValue = req.MinValue
  3620. edbInfo.ManualSave = 1
  3621. edbInfo.ModifyTime = time.Now()
  3622. err = edbInfo.Update([]string{"MaxValue", "MinValue", "ManualSave", "ModifyTime"})
  3623. if err != nil {
  3624. br.Msg = "获取失败"
  3625. br.ErrMsg = "获取失败,Err:" + err.Error()
  3626. return
  3627. }
  3628. //修改es
  3629. data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  3630. br.Ret = 200
  3631. br.Success = true
  3632. br.Msg = "修改成功"
  3633. br.IsAddLog = true
  3634. }
  3635. // ChartImageSet
  3636. // @Title 设置指标的图表图片
  3637. // @Description 设置指标的图表图片接口
  3638. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  3639. // @Success Ret=200 保存成功
  3640. // @router /edb_info/image/set [post]
  3641. func (this *EdbInfoController) ChartImageSet() {
  3642. br := new(models.BaseResponse).Init()
  3643. defer func() {
  3644. if br.ErrMsg == "" {
  3645. br.IsSendEmail = false
  3646. }
  3647. this.Data["json"] = br
  3648. this.ServeJSON()
  3649. }()
  3650. sysUser := this.SysUser
  3651. if sysUser == nil {
  3652. br.Msg = "请登录"
  3653. br.ErrMsg = "请登录,SysUser Is Empty"
  3654. br.Ret = 408
  3655. return
  3656. }
  3657. var req data_manage.SetEdbInfoImageReq
  3658. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  3659. br.Msg = "参数解析异常!"
  3660. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  3661. return
  3662. }
  3663. if req.EdbInfoId <= 0 || req.ImageUrl == "" {
  3664. br.Ret = 200
  3665. br.Success = true
  3666. br.Msg = "保存成功"
  3667. return
  3668. }
  3669. edbInfo, e := data_manage.GetEdbInfoById(req.EdbInfoId)
  3670. if e != nil {
  3671. if utils.IsErrNoRow(e) {
  3672. br.Msg = "找不到该指标"
  3673. return
  3674. }
  3675. br.Msg = "保存失败"
  3676. br.ErrMsg = "获取指标失败, Err:" + e.Error()
  3677. return
  3678. }
  3679. edbInfo.ChartImage = req.ImageUrl
  3680. if e = edbInfo.Update([]string{"ChartImage"}); e != nil {
  3681. br.Msg = "保存失败"
  3682. br.ErrMsg = "更新指标缩略图失败, Err:" + e.Error()
  3683. return
  3684. }
  3685. // 修改es数据
  3686. go data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  3687. br.Ret = 200
  3688. br.Success = true
  3689. br.Msg = "保存成功"
  3690. }
  3691. // EdbInfoExistCheck
  3692. // @Title 新增指标-检验指标是否存在接口
  3693. // @Description 新增指标-检验指标是否存在接口
  3694. // @Param Source query int true "来源:1:同花顺,2:wind"
  3695. // @Param SubSource query int true "子数据来源:0:经济数据库,1:日期序列"
  3696. // @Param EdbCode query string false "指标编码/指标代码"
  3697. // @Param StockCode query string false "证券代码"
  3698. // @Success 200 {object} data_manage.EdbInfoExistCheckResp
  3699. // @router /edb_info/exist/check [get]
  3700. func (this *EdbInfoController) EdbInfoExistCheck() {
  3701. br := new(models.BaseResponse).Init()
  3702. defer func() {
  3703. this.Data["json"] = br
  3704. this.ServeJSON()
  3705. }()
  3706. source, _ := this.GetInt("Source")
  3707. edbCode := this.GetString("EdbCode")
  3708. edbCode = strings.Trim(edbCode, "\t")
  3709. edbCode = strings.Trim(edbCode, " ")
  3710. edbCode = strings.Replace(edbCode, "\t", "", -1)
  3711. subSource, _ := this.GetInt("SubSource")
  3712. stockCode := this.GetString("StockCode")
  3713. if source <= 0 {
  3714. br.Msg = "无效的数据来源"
  3715. return
  3716. }
  3717. if edbCode == "" {
  3718. br.Msg = "请输入指标ID"
  3719. return
  3720. }
  3721. if source != utils.DATA_SOURCE_WIND && source != utils.DATA_SOURCE_THS {
  3722. br.Msg = "无效的数据来源"
  3723. return
  3724. }
  3725. if subSource != utils.DATA_SUB_SOURCE_DATE {
  3726. br.Msg = "无效的数据库来源"
  3727. return
  3728. }
  3729. var indexCodeArr []string
  3730. edbCodeArr := strings.Split(edbCode, ",")
  3731. stockCodeArr := strings.Split(stockCode, ",")
  3732. var prefix string
  3733. if source == utils.DATA_SOURCE_WIND {
  3734. prefix = utils.WindDbWsd
  3735. } else if source == utils.DATA_SOURCE_THS {
  3736. prefix = utils.ThsDs
  3737. } else {
  3738. br.Msg = "来源错误"
  3739. br.ErrMsg = "来源错误"
  3740. return
  3741. }
  3742. for _, sv := range stockCodeArr {
  3743. for _, ev := range edbCodeArr {
  3744. indexCode := prefix + sv + ev
  3745. indexCodeArr = append(indexCodeArr, indexCode)
  3746. }
  3747. }
  3748. indexList, err := data_manage.GetEdbInfoByEdbCodeList(source, indexCodeArr)
  3749. if err != nil && !utils.IsErrNoRow(err) {
  3750. br.Msg = "校验指标是否存在失败"
  3751. br.ErrMsg = "校验指标是否存在失败,Err:" + err.Error()
  3752. return
  3753. }
  3754. var existStockCodeArr []string
  3755. var existEdbCodeArr []string
  3756. var existClassifyId []int
  3757. var existIndexId []int
  3758. for _, indexItem := range indexList {
  3759. for _, sv := range stockCodeArr {
  3760. for _, ev := range edbCodeArr {
  3761. if strings.Contains(indexItem.EdbCode, sv) {
  3762. existStockCodeArr = append(existStockCodeArr, sv)
  3763. }
  3764. if strings.Contains(indexItem.EdbCode, ev) {
  3765. existEdbCodeArr = append(existEdbCodeArr, ev)
  3766. existClassifyId = append(existClassifyId, indexItem.ClassifyId)
  3767. existIndexId = append(existIndexId, indexItem.EdbInfoId)
  3768. }
  3769. }
  3770. }
  3771. }
  3772. resp := new(data_manage.EdbInfoExistCheckResp)
  3773. if len(indexList) > 0 {
  3774. resp.IndexExist = true
  3775. }
  3776. resp.ExistEdbCodeArr = existEdbCodeArr
  3777. resp.ExistStockCodeArr = existStockCodeArr
  3778. resp.ExistClassifyId = existClassifyId
  3779. resp.ExistIndexId = existIndexId
  3780. resp.ExistEdbInfo = indexList
  3781. br.Ret = 200
  3782. br.Success = true
  3783. br.Msg = "获取成功"
  3784. br.Data = resp
  3785. }
  3786. // EdbInfoAdd
  3787. // @Title 指标批量保存接口
  3788. // @Description 指标批量保存接口
  3789. // @Param request body data_manage.BatchAddEdbInfoReq true "type json string"
  3790. // @Success Ret=200 保存成功
  3791. // @router /edb_info/batch/add [post]
  3792. func (this *EdbInfoController) EdbInfoBatchAdd() {
  3793. br := new(models.BaseResponse).Init()
  3794. defer func() {
  3795. this.Data["json"] = br
  3796. this.ServeJSON()
  3797. }()
  3798. sysUser := this.SysUser
  3799. if sysUser == nil {
  3800. br.Msg = "请登录"
  3801. br.ErrMsg = "请登录,SysUser Is Empty"
  3802. br.Ret = 408
  3803. return
  3804. }
  3805. deleteCache := true
  3806. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_" + strconv.Itoa(sysUser.AdminId)
  3807. defer func() {
  3808. if deleteCache {
  3809. utils.Rc.Delete(cacheKey)
  3810. }
  3811. }()
  3812. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  3813. deleteCache = false
  3814. br.Msg = "系统处理中,请稍后重试!"
  3815. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  3816. return
  3817. }
  3818. var req data_manage.BatchAddEdbInfoReq
  3819. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3820. if err != nil {
  3821. br.Msg = "参数解析异常!"
  3822. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3823. return
  3824. }
  3825. resp := new(data_manage.AddEdbInfoResp)
  3826. var edbNames string
  3827. for _, v := range req.BatchList {
  3828. edbNames += "'" + v.EdbName + "',"
  3829. }
  3830. edbNames = strings.Trim(edbNames, ",")
  3831. infoList, err := data_manage.GetEdbInfoByNames(edbNames)
  3832. if err != nil {
  3833. br.Msg = "查询指标名称重复错误!"
  3834. br.ErrMsg = "GetEdbInfoByNames,Err:" + err.Error()
  3835. return
  3836. }
  3837. if len(infoList) > 0 {
  3838. for _, v := range infoList {
  3839. resp.ExistEdbName = append(resp.ExistEdbName, v.EdbName)
  3840. }
  3841. br.Data = resp
  3842. br.Msg = "指标名称重复!"
  3843. br.ErrMsg = "指标名称重复"
  3844. return
  3845. }
  3846. for k, v := range req.BatchList {
  3847. v.EdbName = strings.Trim(v.EdbName, " ")
  3848. v.EdbCode = strings.Trim(v.EdbCode, " ")
  3849. v.StockCode = strings.Trim(v.StockCode, " ")
  3850. if v.Source <= 0 {
  3851. br.Msg = "无效的数据来源"
  3852. return
  3853. }
  3854. if v.EdbCode == "" {
  3855. br.Msg = "指标ID不能为空"
  3856. return
  3857. }
  3858. if v.StockCode == "" {
  3859. br.Msg = "证券代码不能为空"
  3860. return
  3861. }
  3862. if v.EdbName == "" {
  3863. br.Msg = "指标名称不能为空"
  3864. return
  3865. }
  3866. if v.Frequency == "" {
  3867. br.Msg = "频率不能为空"
  3868. return
  3869. }
  3870. if v.ClassifyId <= 0 {
  3871. br.Msg = "请选择分类"
  3872. return
  3873. }
  3874. terminalCode, serverUrl, _, e := data.GetEdbTerminalCodeBySource(v.Source, v.EdbCode, v.StockCode)
  3875. if e != nil {
  3876. br.Msg = "获取可以使用的终端地址失败"
  3877. br.ErrMsg = "获取可以使用的终端地址失败,Err:" + e.Error()
  3878. return
  3879. }
  3880. edbInfoItem := new(data_manage.EdbInfo)
  3881. edbInfoItem.Source = v.Source
  3882. switch v.Source {
  3883. case utils.DATA_SOURCE_WIND:
  3884. edbInfoItem.SourceName = "wind"
  3885. edbInfoItem.EdbCode = utils.WindDbWsd + v.StockCode + v.EdbCode
  3886. case utils.DATA_SOURCE_THS:
  3887. edbInfoItem.SourceName = "ths"
  3888. edbInfoItem.EdbCode = utils.ThsDs + v.StockCode + v.EdbCode
  3889. }
  3890. edbInfoItem.SubSource = utils.DATA_SUB_SOURCE_DATE
  3891. edbInfoItem.SubSourceName = "日期序列"
  3892. edbInfoItem.EdbName = v.EdbName
  3893. edbInfoItem.EdbNameSource = v.EdbName
  3894. edbInfoItem.EdbNameEn = v.EdbName
  3895. edbInfoItem.Frequency = v.Frequency
  3896. edbInfoItem.Unit = v.Unit
  3897. edbInfoItem.UnitEn = v.Unit
  3898. edbInfoItem.ClassifyId = v.ClassifyId
  3899. edbInfoItem.SysUserId = sysUser.AdminId
  3900. edbInfoItem.SysUserRealName = sysUser.RealName
  3901. edbInfoItem.IndicatorCode = v.EdbCode
  3902. edbInfoItem.StockCode = v.StockCode
  3903. edbInfoItem.TerminalCode = terminalCode
  3904. edbInfoItem.ServerUrl = serverUrl
  3905. var extra data_manage.EdbInfoExtra
  3906. if v.ApiExtraPars != "" {
  3907. extra.ApiExtraPars = v.ApiExtraPars
  3908. b, e := json.Marshal(extra)
  3909. if e != nil {
  3910. br.Msg = "保存失败"
  3911. br.ErrMsg = fmt.Sprintf("额外参数JSON格式化失败, %v", e)
  3912. return
  3913. }
  3914. edbInfoItem.Extra = string(b)
  3915. }
  3916. // 指标入库
  3917. edbInfo, err, errMsg, isSendEmail := data.EdbInfoWsdAdd(edbInfoItem)
  3918. if err != nil {
  3919. br.Msg = "保存失败"
  3920. if errMsg != `` {
  3921. br.Msg = errMsg
  3922. }
  3923. br.ErrMsg = err.Error()
  3924. br.IsSendEmail = isSendEmail
  3925. return
  3926. }
  3927. //新增操作日志
  3928. {
  3929. edbLog := new(data_manage.EdbInfoLog)
  3930. edbLog.EdbInfoId = edbInfo.EdbInfoId
  3931. edbLog.SourceName = edbInfo.SourceName
  3932. edbLog.Source = edbInfo.Source
  3933. edbLog.EdbCode = edbInfo.EdbCode
  3934. edbLog.EdbName = edbInfo.EdbName
  3935. edbLog.ClassifyId = edbInfo.ClassifyId
  3936. edbLog.SysUserId = sysUser.AdminId
  3937. edbLog.SysUserRealName = sysUser.RealName
  3938. edbLog.CreateTime = time.Now()
  3939. edbLog.Content = string(this.Ctx.Input.RequestBody)
  3940. edbLog.Status = "新增指标"
  3941. edbLog.Method = this.Ctx.Input.URI()
  3942. go data_manage.AddEdbInfoLog(edbLog)
  3943. }
  3944. if k <= 0 {
  3945. resp.EdbInfoId = edbInfo.EdbInfoId
  3946. resp.UniqueCode = edbInfo.UniqueCode
  3947. }
  3948. }
  3949. br.Ret = 200
  3950. br.Success = true
  3951. br.Msg = "保存成功"
  3952. br.Data = resp
  3953. br.IsAddLog = true
  3954. }
  3955. // SmmEdbInfoBatchAdd
  3956. // @Title 指标批量保存接口-smm
  3957. // @Description 指标批量保存接口-smm
  3958. // @Param request body data_manage.BatchAddEdbInfoReq true "type json string"
  3959. // @Success Ret=200 保存成功
  3960. // @router /edb_info/smm/batch/add [post]
  3961. func (this *EdbInfoController) SmmEdbInfoBatchAdd() {
  3962. br := new(models.BaseResponse).Init()
  3963. defer func() {
  3964. this.Data["json"] = br
  3965. this.ServeJSON()
  3966. }()
  3967. sysUser := this.SysUser
  3968. if sysUser == nil {
  3969. br.Msg = "请登录"
  3970. br.ErrMsg = "请登录,SysUser Is Empty"
  3971. br.Ret = 408
  3972. return
  3973. }
  3974. deleteCache := true
  3975. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_" + strconv.Itoa(sysUser.AdminId)
  3976. defer func() {
  3977. if deleteCache {
  3978. utils.Rc.Delete(cacheKey)
  3979. }
  3980. }()
  3981. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  3982. deleteCache = false
  3983. br.Msg = "系统处理中,请稍后重试!"
  3984. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  3985. return
  3986. }
  3987. var req data_manage.BatchAddEdbInfoReq
  3988. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3989. if err != nil {
  3990. br.Msg = "参数解析异常!"
  3991. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3992. return
  3993. }
  3994. resp := new(data_manage.AddEdbInfoResp)
  3995. var edbNames string
  3996. for _, v := range req.BatchList {
  3997. edbNames += "'" + v.EdbName + "',"
  3998. }
  3999. edbNames = strings.Trim(edbNames, ",")
  4000. infoList, err := data_manage.GetEdbInfoByNames(edbNames)
  4001. if err != nil {
  4002. br.Msg = "查询指标名称重复错误!"
  4003. br.ErrMsg = "GetEdbInfoByNames,Err:" + err.Error()
  4004. return
  4005. }
  4006. if len(infoList) > 0 {
  4007. for _, v := range infoList {
  4008. resp.ExistEdbName = append(resp.ExistEdbName, v.EdbName)
  4009. }
  4010. br.Data = resp
  4011. br.Msg = "指标名称重复!"
  4012. br.ErrMsg = "指标名称重复"
  4013. return
  4014. }
  4015. for k, v := range req.BatchList {
  4016. v.EdbName = strings.Trim(v.EdbName, " ")
  4017. v.EdbCode = strings.Trim(v.EdbCode, " ")
  4018. if v.Source <= 0 {
  4019. br.Msg = "无效的数据来源"
  4020. return
  4021. }
  4022. if v.EdbCode == "" {
  4023. br.Msg = "指标ID不能为空"
  4024. return
  4025. }
  4026. if v.EdbName == "" {
  4027. br.Msg = "指标名称不能为空"
  4028. return
  4029. }
  4030. if v.Frequency == "" {
  4031. br.Msg = "频率不能为空"
  4032. return
  4033. }
  4034. if v.ClassifyId <= 0 {
  4035. br.Msg = "请选择分类"
  4036. return
  4037. }
  4038. edbInfoItem := new(data_manage.EdbInfo)
  4039. edbInfoItem.Source = v.Source
  4040. edbInfoItem.SourceName = "SMM"
  4041. edbInfoItem.EdbCode = v.EdbCode
  4042. edbInfoItem.SubSource = 0
  4043. edbInfoItem.SubSourceName = ""
  4044. edbInfoItem.EdbName = v.EdbName
  4045. edbInfoItem.EdbNameSource = v.EdbName
  4046. edbInfoItem.EdbNameEn = v.EdbName
  4047. edbInfoItem.Frequency = v.Frequency
  4048. edbInfoItem.Unit = v.Unit
  4049. edbInfoItem.UnitEn = v.Unit
  4050. edbInfoItem.ClassifyId = v.ClassifyId
  4051. edbInfoItem.SysUserId = sysUser.AdminId
  4052. edbInfoItem.SysUserRealName = sysUser.RealName
  4053. edbInfoItem.IndicatorCode = ""
  4054. edbInfoItem.StockCode = ""
  4055. // 指标入库
  4056. edbInfo, err, errMsg, isSendEmail := data.EdbInfoWsdAdd(edbInfoItem)
  4057. if err != nil {
  4058. br.Msg = "保存失败"
  4059. if errMsg != `` {
  4060. br.Msg = errMsg
  4061. }
  4062. br.ErrMsg = err.Error()
  4063. br.IsSendEmail = isSendEmail
  4064. return
  4065. }
  4066. //新增操作日志
  4067. {
  4068. edbLog := new(data_manage.EdbInfoLog)
  4069. edbLog.EdbInfoId = edbInfo.EdbInfoId
  4070. edbLog.SourceName = edbInfo.SourceName
  4071. edbLog.Source = edbInfo.Source
  4072. edbLog.EdbCode = edbInfo.EdbCode
  4073. edbLog.EdbName = edbInfo.EdbName
  4074. edbLog.ClassifyId = edbInfo.ClassifyId
  4075. edbLog.SysUserId = sysUser.AdminId
  4076. edbLog.SysUserRealName = sysUser.RealName
  4077. edbLog.CreateTime = time.Now()
  4078. edbLog.Content = string(this.Ctx.Input.RequestBody)
  4079. edbLog.Status = "新增指标"
  4080. edbLog.Method = this.Ctx.Input.URI()
  4081. go data_manage.AddEdbInfoLog(edbLog)
  4082. }
  4083. if k <= 0 {
  4084. resp.EdbInfoId = edbInfo.EdbInfoId
  4085. resp.UniqueCode = edbInfo.UniqueCode
  4086. }
  4087. }
  4088. br.Ret = 200
  4089. br.Success = true
  4090. br.Msg = "保存成功"
  4091. br.Data = resp
  4092. br.IsAddLog = true
  4093. }
  4094. // ChartImageSetBySvg
  4095. // @Title 设置指标的图表图片
  4096. // @Description 设置指标的图表图片接口
  4097. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  4098. // @Success Ret=200 保存成功
  4099. // @router /edb_info/image/set_by_svg [post]
  4100. func (this *EdbInfoController) ChartImageSetBySvg() {
  4101. br := new(models.BaseResponse).Init()
  4102. defer func() {
  4103. if br.ErrMsg == "" {
  4104. br.IsSendEmail = false
  4105. }
  4106. this.Data["json"] = br
  4107. this.ServeJSON()
  4108. }()
  4109. sysUser := this.SysUser
  4110. if sysUser == nil {
  4111. br.Msg = "请登录"
  4112. br.ErrMsg = "请登录,SysUser Is Empty"
  4113. br.Ret = 408
  4114. return
  4115. }
  4116. imgData := this.GetString("Img")
  4117. if imgData == "" {
  4118. br.Msg = "图片参数错误"
  4119. br.ErrMsg = "图片参数错误,Img Is Empty"
  4120. return
  4121. }
  4122. edbInfoId, _ := this.GetInt("EdbInfoId", 0)
  4123. if edbInfoId <= 0 {
  4124. br.Msg = "指标参数错误"
  4125. br.ErrMsg = "指标参数错误,EdbInfoId Is Empty"
  4126. return
  4127. }
  4128. // 通过svg图片生成图片资源地址
  4129. resourceUrl, err, errMsg := services.GetResourceUrlBySvgImg(imgData)
  4130. if err != nil {
  4131. br.Msg = errMsg
  4132. br.ErrMsg = err.Error()
  4133. return
  4134. }
  4135. if edbInfoId <= 0 || resourceUrl == "" {
  4136. br.Ret = 200
  4137. br.Success = true
  4138. br.Msg = "保存成功"
  4139. return
  4140. }
  4141. edbInfo, e := data_manage.GetEdbInfoById(edbInfoId)
  4142. if e != nil {
  4143. if utils.IsErrNoRow(e) {
  4144. br.Msg = "找不到该指标"
  4145. return
  4146. }
  4147. br.Msg = "保存失败"
  4148. br.ErrMsg = "获取指标失败, Err:" + e.Error()
  4149. return
  4150. }
  4151. edbInfo.ChartImage = resourceUrl
  4152. if e = edbInfo.Update([]string{"ChartImage"}); e != nil {
  4153. br.Msg = "保存失败"
  4154. br.ErrMsg = "更新指标缩略图失败, Err:" + e.Error()
  4155. return
  4156. }
  4157. // 修改es数据
  4158. go data.AddOrEditEdbInfoToEs(edbInfoId)
  4159. br.Ret = 200
  4160. br.Success = true
  4161. br.Msg = "保存成功"
  4162. }
  4163. // BatchMoveEdb
  4164. // @Title 指标批量移动接口
  4165. // @Description 指标批量移动接口
  4166. // @Param request body request.MoveEdbClassifyReq true "type json string"
  4167. // @Success Ret=200 移动成功
  4168. // @router /edb_info/batch_move [post]
  4169. func (this *EdbInfoController) BatchMoveEdb() {
  4170. br := new(models.BaseResponse).Init()
  4171. defer func() {
  4172. this.Data["json"] = br
  4173. this.ServeJSON()
  4174. }()
  4175. var req request.MoveEdbClassifyReq
  4176. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  4177. if err != nil {
  4178. br.Msg = "参数解析异常!"
  4179. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  4180. return
  4181. }
  4182. if req.NewClassifyId <= 0 {
  4183. br.Msg = `请选择要转移的新分类`
  4184. br.IsSendEmail = false
  4185. return
  4186. }
  4187. classifyItem, err := data_manage.GetEdbClassifyById(req.NewClassifyId)
  4188. if err != nil {
  4189. br.Msg = "获取指标分类失败!"
  4190. br.ErrMsg = "获取指标分类失败!" + err.Error()
  4191. return
  4192. }
  4193. if classifyItem.SysUserId != this.SysUser.AdminId {
  4194. br.Msg = `您没有权限移动指标到该分类!`
  4195. br.IsSendEmail = false
  4196. return
  4197. }
  4198. edbInfoIdList := make([]int, 0)
  4199. if req.IsSelectAll {
  4200. // 获取指标
  4201. edbInfoList, err := data.GetAllEdbInfoListBySearchPublicEdbReq(req.SearchPublicEdbReq, this.SysUser.AdminId)
  4202. if err != nil {
  4203. br.Msg = "获取指标列表失败!"
  4204. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  4205. return
  4206. }
  4207. // 如果有过滤指标,那么就过滤吧
  4208. if len(req.NoEdbIdList) > 0 {
  4209. noEdbIdMap := make(map[int]bool)
  4210. for _, v := range req.NoEdbIdList {
  4211. noEdbIdMap[v] = true
  4212. }
  4213. for _, edbInfo := range edbInfoList {
  4214. if _, ok := noEdbIdMap[edbInfo.EdbInfoId]; !ok {
  4215. if edbInfo.SysUserId != this.SysUser.AdminId {
  4216. br.Msg = `您没有权限移动该指标!`
  4217. br.IsSendEmail = false
  4218. return
  4219. }
  4220. // 如果不在未选中的指标id列表中,那么就加入到选中的指标id列表
  4221. edbInfoIdList = append(edbInfoIdList, edbInfo.EdbInfoId)
  4222. }
  4223. }
  4224. } else {
  4225. for _, edbInfo := range edbInfoList {
  4226. edbInfoIdList = append(edbInfoIdList, edbInfo.EdbInfoId)
  4227. for _, v := range edbInfoList {
  4228. if v.SysUserId != this.SysUser.AdminId {
  4229. br.Msg = `您没有权限移动该指标!`
  4230. br.IsSendEmail = false
  4231. return
  4232. }
  4233. }
  4234. }
  4235. }
  4236. } else {
  4237. edbInfoList, err := data_manage.GetEdbInfoListByCond(` AND edb_info_id in (?) `, []interface{}{req.EdbIdList})
  4238. if err != nil {
  4239. br.Msg = "获取失败"
  4240. br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error()
  4241. return
  4242. }
  4243. for _, v := range edbInfoList {
  4244. if v.SysUserId != this.SysUser.AdminId {
  4245. br.Msg = `您没有权限移动该指标!`
  4246. br.IsSendEmail = false
  4247. return
  4248. }
  4249. }
  4250. edbInfoIdList = req.EdbIdList
  4251. }
  4252. num := len(edbInfoIdList)
  4253. if num <= 0 {
  4254. br.Msg = `请选择要移动的指标!`
  4255. br.IsSendEmail = false
  4256. return
  4257. }
  4258. if num > 100 {
  4259. br.Msg = `最多只能选择100条指标!`
  4260. br.IsSendEmail = false
  4261. return
  4262. }
  4263. // 开始批量修改指标分类
  4264. if len(edbInfoIdList) > 0 {
  4265. err = data_manage.UpdateClassifyIdByEdbInfoIdList(edbInfoIdList, req.NewClassifyId)
  4266. if err != nil {
  4267. br.Msg = `指标移动失败!`
  4268. br.ErrMsg = `指标移动失败,ERR:` + err.Error()
  4269. return
  4270. }
  4271. // 还得修改ES呢
  4272. for _, v := range edbInfoIdList {
  4273. data.AddOrEditEdbInfoToEs(v)
  4274. }
  4275. }
  4276. br.Ret = 200
  4277. br.Success = true
  4278. br.Msg = "获取成功"
  4279. }