chart_info.go 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/controllers"
  6. "eta/eta_mobile/models"
  7. "eta/eta_mobile/models/data_manage"
  8. "eta/eta_mobile/models/data_manage/response"
  9. "eta/eta_mobile/models/system"
  10. "eta/eta_mobile/services"
  11. "eta/eta_mobile/services/data"
  12. "eta/eta_mobile/services/data/area_graph"
  13. "eta/eta_mobile/services/data/data_manage_permission"
  14. "eta/eta_mobile/services/data/excel"
  15. etaTrialService "eta/eta_mobile/services/eta_trial"
  16. "eta/eta_mobile/utils"
  17. "fmt"
  18. "github.com/rdlucklib/rdluck_tools/paging"
  19. "os"
  20. "os/exec"
  21. "sort"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. // 图表管理
  27. type ChartInfoController struct {
  28. controllers.BaseAuthController
  29. }
  30. // @Title 保存图表接口
  31. // @Description 保存图表接口
  32. // @Param request body data_manage.SaveChartInfoReq true "type json string"
  33. // @Success Ret=200 返回图表id
  34. // @router /chart_info/save [post]
  35. func (this *ChartInfoController) ChartInfoSave() {
  36. br := new(models.BaseResponse).Init()
  37. defer func() {
  38. this.Data["json"] = br
  39. this.ServeJSON()
  40. }()
  41. sysUser := this.SysUser
  42. if sysUser == nil {
  43. br.Msg = "请登录"
  44. br.ErrMsg = "请登录,SysUser Is Empty"
  45. br.Ret = 408
  46. return
  47. }
  48. var req data_manage.SaveChartInfoReq
  49. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  50. if err != nil {
  51. br.Msg = "参数解析异常!"
  52. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  53. return
  54. }
  55. if req.ChartInfoId > 0 && len(req.ChartEdbInfoList) <= 0 {
  56. br.Msg = "参数错误!"
  57. return
  58. }
  59. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  60. if err != nil {
  61. if err.Error() == utils.ErrNoRow() {
  62. br.Msg = "图表已被删除,请刷新页面!"
  63. br.ErrMsg = "图表已被删除,请刷新页面,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  64. return
  65. }
  66. br.Msg = "获取图表信息失败!"
  67. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  68. return
  69. }
  70. //图表操作权限
  71. //ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId)
  72. //if !ok {
  73. // br.Msg = "没有该图表的操作权限"
  74. // br.ErrMsg = "没有该图表的操作权限"
  75. // return
  76. //}
  77. if chartItem.ChartType == 2 && len(req.ChartEdbInfoList) > 1 {
  78. br.Msg = "您选择的图表样式为季节性图表,只支持单指标画图!"
  79. br.ErrMsg = "您选择的图表样式为季节性图表,只支持单指标画图,Data:" + string(this.Ctx.Input.RequestBody)
  80. return
  81. }
  82. if req.Calendar == "" {
  83. req.Calendar = "公历"
  84. }
  85. var edbInfoIdArr []int
  86. edbList := make([]*data_manage.EdbInfo, 0)
  87. for _, v := range req.ChartEdbInfoList {
  88. edbInfo, err := data_manage.GetEdbInfoById(v.EdbInfoId)
  89. if err != nil {
  90. if err.Error() == utils.ErrNoRow() {
  91. br.Msg = "图表不存在!"
  92. br.ErrMsg = "图表不存在,ChartInfoId:" + strconv.Itoa(v.EdbInfoId)
  93. return
  94. } else {
  95. br.Msg = "获取图表信息失败!"
  96. br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
  97. return
  98. }
  99. }
  100. if edbInfo == nil {
  101. br.Msg = "指标不存在!"
  102. br.ErrMsg = "指标不存在,ChartInfoId:" + strconv.Itoa(v.EdbInfoId)
  103. return
  104. }
  105. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  106. edbInfo.EdbNameSource = edbInfo.EdbName
  107. edbList = append(edbList, edbInfo)
  108. }
  109. sort.Ints(edbInfoIdArr)
  110. var edbInfoIdArrStr []string
  111. for _, v := range edbInfoIdArr {
  112. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  113. }
  114. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  115. err = data_manage.ModifyChartInfoAndMapping(edbInfoIdStr, &req, chartItem.ChartType)
  116. if err != nil {
  117. br.Msg = "保存失败"
  118. br.ErrMsg = "保存失败,Err:" + err.Error()
  119. return
  120. }
  121. key := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  122. if utils.Re == nil && utils.Rc != nil {
  123. utils.Rc.Delete(key)
  124. }
  125. //修改es数据
  126. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  127. //修改my eta es数据
  128. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  129. //新增操作日志
  130. {
  131. chartLog := new(data_manage.ChartInfoLog)
  132. chartLog.ChartName = chartItem.ChartName
  133. chartLog.ChartInfoId = req.ChartInfoId
  134. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  135. chartLog.SysUserId = sysUser.AdminId
  136. chartLog.SysUserRealName = sysUser.RealName
  137. chartLog.UniqueCode = chartItem.UniqueCode
  138. chartLog.CreateTime = time.Now()
  139. chartLog.Content = string(this.Ctx.Input.RequestBody)
  140. chartLog.Status = "修改配置项"
  141. chartLog.Method = this.Ctx.Input.URI()
  142. go data_manage.AddChartInfoLog(chartLog)
  143. }
  144. br.Ret = 200
  145. br.Success = true
  146. br.Msg = "保存成功"
  147. br.IsAddLog = true
  148. }
  149. // ChartInfoAdd
  150. // @Title 新增图表接口
  151. // @Description 新增图表接口
  152. // @Param request body data_manage.AddChartInfoReq true "type json string"
  153. // @Success 200 {object} data_manage.AddChartInfoResp
  154. // @router /chart_info/add [post]
  155. func (this *ChartInfoController) ChartInfoAdd() {
  156. br := new(models.BaseResponse).Init()
  157. defer func() {
  158. this.Data["json"] = br
  159. this.ServeJSON()
  160. }()
  161. sysUser := this.SysUser
  162. if sysUser == nil {
  163. br.Msg = "请登录"
  164. br.ErrMsg = "请登录,SysUser Is Empty"
  165. br.Ret = 408
  166. return
  167. }
  168. deleteCache := true
  169. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  170. defer func() {
  171. if deleteCache {
  172. utils.Rc.Delete(cacheKey)
  173. }
  174. }()
  175. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  176. deleteCache = false
  177. br.Msg = "系统处理中,请稍后重试!"
  178. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  179. return
  180. }
  181. var req data_manage.AddChartInfoReq
  182. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  183. if err != nil {
  184. br.Msg = "参数解析异常!"
  185. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  186. return
  187. }
  188. req.ChartName = strings.Trim(req.ChartName, " ")
  189. if req.ChartName == "" {
  190. br.Msg = "请填写图表名称!"
  191. return
  192. }
  193. if req.ChartClassifyId <= 0 {
  194. br.Msg = "分类参数错误!"
  195. return
  196. }
  197. chartInfo, err, errMsg, isSendEmail := data.AddChartInfo(req, sysUser.AdminId, sysUser.RealName, this.Lang)
  198. if err != nil {
  199. br.Msg = "保存失败"
  200. if errMsg != `` {
  201. br.Msg = errMsg
  202. }
  203. br.ErrMsg = err.Error()
  204. br.IsSendEmail = isSendEmail
  205. return
  206. }
  207. resp := new(data_manage.AddChartInfoResp)
  208. resp.ChartInfoId = chartInfo.ChartInfoId
  209. resp.UniqueCode = chartInfo.UniqueCode
  210. resp.ChartType = req.ChartType
  211. //新增操作日志
  212. {
  213. chartLog := new(data_manage.ChartInfoLog)
  214. chartLog.ChartInfoId = chartInfo.ChartInfoId
  215. chartLog.ChartName = req.ChartName
  216. chartLog.ChartClassifyId = req.ChartClassifyId
  217. chartLog.SysUserId = sysUser.AdminId
  218. chartLog.SysUserRealName = sysUser.RealName
  219. chartLog.UniqueCode = chartInfo.UniqueCode
  220. chartLog.CreateTime = time.Now()
  221. chartLog.Content = string(this.Ctx.Input.RequestBody)
  222. chartLog.Status = "新增图表"
  223. chartLog.Method = this.Ctx.Input.URI()
  224. go data_manage.AddChartInfoLog(chartLog)
  225. }
  226. // 试用平台更新用户累计新增图表数
  227. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  228. if e != nil {
  229. br.Msg = "操作失败"
  230. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  231. return
  232. }
  233. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  234. go func() {
  235. var r etaTrialService.EtaTrialUserReq
  236. r.Mobile = adminItem.Mobile
  237. _, _ = etaTrialService.UpdateUserChartNum(r)
  238. }()
  239. }
  240. br.Ret = 200
  241. br.Success = true
  242. br.Msg = "保存成功"
  243. br.Data = resp
  244. br.IsAddLog = true
  245. }
  246. // @Title 编辑图表接口
  247. // @Description 编辑图表接口
  248. // @Param request body data_manage.EditChartInfoReq true "type json string"
  249. // @Success Ret=200 保存成功
  250. // @router /chart_info/edit [post]
  251. func (this *ChartInfoController) ChartInfoEdit() {
  252. br := new(models.BaseResponse).Init()
  253. defer func() {
  254. this.Data["json"] = br
  255. this.ServeJSON()
  256. }()
  257. sysUser := this.SysUser
  258. if sysUser == nil {
  259. br.Msg = "请登录"
  260. br.ErrMsg = "请登录,SysUser Is Empty"
  261. br.Ret = 408
  262. return
  263. }
  264. var req data_manage.EditChartInfoReq
  265. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  266. if err != nil {
  267. br.Msg = "参数解析异常!"
  268. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  269. return
  270. }
  271. chartItem, err, errMsg, isSendEmail := data.EditChartInfo(req, sysUser, this.Lang)
  272. if err != nil {
  273. br.Msg = "保存失败"
  274. if errMsg != `` {
  275. br.Msg = errMsg
  276. }
  277. br.ErrMsg = err.Error()
  278. br.IsSendEmail = isSendEmail
  279. return
  280. }
  281. resp := new(data_manage.AddChartInfoResp)
  282. resp.ChartInfoId = chartItem.ChartInfoId
  283. resp.UniqueCode = chartItem.UniqueCode
  284. resp.ChartType = req.ChartType
  285. //新增操作日志
  286. {
  287. chartLog := new(data_manage.ChartInfoLog)
  288. chartLog.ChartName = chartItem.ChartName
  289. chartLog.ChartInfoId = req.ChartInfoId
  290. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  291. chartLog.SysUserId = sysUser.AdminId
  292. chartLog.SysUserRealName = sysUser.RealName
  293. chartLog.UniqueCode = chartItem.UniqueCode
  294. chartLog.CreateTime = time.Now()
  295. chartLog.Content = string(this.Ctx.Input.RequestBody)
  296. chartLog.Status = "编辑图表"
  297. chartLog.Method = this.Ctx.Input.URL()
  298. go data_manage.AddChartInfoLog(chartLog)
  299. }
  300. br.Ret = 200
  301. br.Success = true
  302. br.Msg = "保存成功"
  303. br.Data = resp
  304. br.IsAddLog = true
  305. }
  306. // ChartEnInfoEdit
  307. // @Title 编辑图表英文信息接口
  308. // @Description 编辑图表英文信息接口
  309. // @Param request body data_manage.EditChartEnInfoReq true "type json string"
  310. // @Success Ret=200 编辑成功
  311. // @router /chart_info/en/edit [post]
  312. func (this *ChartInfoController) ChartEnInfoEdit() {
  313. br := new(models.BaseResponse).Init()
  314. defer func() {
  315. this.Data["json"] = br
  316. this.ServeJSON()
  317. }()
  318. sysUser := this.SysUser
  319. if sysUser == nil {
  320. br.Msg = "请登录"
  321. br.ErrMsg = "请登录,SysUser Is Empty"
  322. br.Ret = 408
  323. return
  324. }
  325. var req data_manage.EditChartEnInfoReq
  326. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  327. if err != nil {
  328. br.Msg = "参数解析异常!"
  329. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  330. return
  331. }
  332. req.ChartNameEn = strings.Trim(req.ChartNameEn, " ")
  333. if req.ChartInfoId <= 0 {
  334. br.Msg = "请选择图表"
  335. return
  336. }
  337. //判断指标名称是否存在
  338. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  339. if err != nil {
  340. if err.Error() == utils.ErrNoRow() {
  341. br.Msg = "图表已被删除,请刷新页面"
  342. br.ErrMsg = "图表已被删除,请刷新页面"
  343. return
  344. }
  345. br.Msg = "获取图表信息失败"
  346. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  347. return
  348. }
  349. // 判断是否有传入对应的指标配置
  350. noEdbInfoType := []int{10}
  351. if len(req.ChartEdbInfoList) <= 0 && !utils.InArrayByInt(noEdbInfoType, chartItem.ChartType) {
  352. br.Msg = "请选择指标!"
  353. return
  354. }
  355. var edbCondition string
  356. var edbPars []interface{}
  357. for _, v := range req.ChartEdbInfoList {
  358. edbInfoId := v.EdbInfoId
  359. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  360. if err != nil {
  361. if err.Error() == utils.ErrNoRow() {
  362. br.Msg = "图表不存在!"
  363. br.ErrMsg = "图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  364. return
  365. } else {
  366. br.Msg = "获取图表信息失败!"
  367. br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
  368. return
  369. }
  370. }
  371. if edbInfo == nil {
  372. br.Msg = "指标不存在!"
  373. br.ErrMsg = "指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  374. return
  375. }
  376. //判断指标名称是否重复
  377. if v.EdbNameEn != "" {
  378. edbCondition = ""
  379. edbPars = make([]interface{}, 0)
  380. edbCondition += " AND edb_info_id<>? "
  381. edbPars = append(edbPars, edbInfo.EdbInfoId)
  382. /*edbCondition += " AND source=? "
  383. edbPars = append(edbPars, edbInfo.Source)*/
  384. edbCondition += " AND edb_name_en=? "
  385. edbPars = append(edbPars, v.EdbNameEn)
  386. edbExist, err := data_manage.GetEdbInfoByCondition(edbCondition, edbPars)
  387. if err != nil {
  388. if err.Error() != utils.ErrNoRow() {
  389. br.Msg = "判断英文指标名称是否存在失败"
  390. br.ErrMsg = "判断英文指标名称是否存在失败,Err:" + err.Error()
  391. return
  392. }
  393. }
  394. if err == nil && edbExist.EdbInfoId > 0 {
  395. br.Msg = edbExist.EdbName + ":" + v.EdbNameEn + "指标名称已存在"
  396. br.ErrMsg = "英文指标名称已存在,请重新填写"
  397. br.IsSendEmail = false
  398. return
  399. }
  400. }
  401. }
  402. if req.ChartNameEn != "" || req.ExtraConfig != `` {
  403. var condition string
  404. var pars []interface{}
  405. condition += " AND chart_info_id<>? "
  406. pars = append(pars, req.ChartInfoId)
  407. /*condition += " AND chart_classify_id=? "
  408. pars = append(pars, chartItem.ChartClassifyId)*/
  409. condition += " AND chart_name_en=? "
  410. pars = append(pars, req.ChartNameEn)
  411. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  412. if err != nil {
  413. if err.Error() != utils.ErrNoRow() {
  414. br.Msg = "判断英文图表名称是否存在失败"
  415. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  416. return
  417. }
  418. }
  419. if err == nil && existItem.ChartInfoId > 0 {
  420. br.Msg = existItem.ChartName + ":" + req.ChartNameEn + "图表名称已存在"
  421. return
  422. }
  423. }
  424. err = data_manage.EditChartEnInfoAndEdbEnInfo(&req)
  425. if err != nil {
  426. br.Msg = "保存失败"
  427. br.ErrMsg = "保存失败,Err:" + err.Error()
  428. return
  429. }
  430. //添加es数据
  431. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  432. //修改my eta es数据
  433. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  434. //edbinfo 修改es信息
  435. for _, v := range req.ChartEdbInfoList {
  436. edbInfoId := v.EdbInfoId
  437. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  438. if err != nil {
  439. if err.Error() == utils.ErrNoRow() {
  440. br.Msg = "图表不存在!"
  441. br.ErrMsg = "图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  442. return
  443. } else {
  444. br.Msg = "获取图表信息失败!"
  445. br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
  446. return
  447. }
  448. }
  449. if edbInfo != nil {
  450. go data.AddOrEditEdbInfoToEs(edbInfoId)
  451. }
  452. }
  453. //新增操作日志
  454. {
  455. chartLog := new(data_manage.ChartInfoLog)
  456. chartLog.ChartName = chartItem.ChartName
  457. chartLog.ChartInfoId = req.ChartInfoId
  458. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  459. chartLog.SysUserId = sysUser.AdminId
  460. chartLog.SysUserRealName = sysUser.RealName
  461. chartLog.UniqueCode = chartItem.UniqueCode
  462. chartLog.CreateTime = time.Now()
  463. chartLog.Content = string(this.Ctx.Input.RequestBody)
  464. chartLog.Status = "编辑图表英文信息"
  465. chartLog.Method = this.Ctx.Input.URL()
  466. go data_manage.AddChartInfoLog(chartLog)
  467. }
  468. //清除缓存
  469. if utils.Re == nil && utils.Rc != nil {
  470. utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  471. utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  472. }
  473. br.Ret = 200
  474. br.Success = true
  475. br.Msg = "编辑成功"
  476. br.IsAddLog = true
  477. }
  478. // ChartInfoBaseEdit
  479. // @Title 编辑图表基础信息接口
  480. // @Description 编辑图表基础信息接口
  481. // @Param request body data_manage.EditChartEnInfoBaseReq true "type json string"
  482. // @Success Ret=200 编辑成功
  483. // @router /chart_info/base/edit [post]
  484. func (this *ChartInfoController) ChartInfoBaseEdit() {
  485. br := new(models.BaseResponse).Init()
  486. defer func() {
  487. this.Data["json"] = br
  488. this.ServeJSON()
  489. }()
  490. sysUser := this.SysUser
  491. if sysUser == nil {
  492. br.Msg = "请登录"
  493. br.ErrMsg = "请登录,SysUser Is Empty"
  494. br.Ret = 408
  495. return
  496. }
  497. var req data_manage.EditChartInfoBaseReq
  498. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  499. if err != nil {
  500. br.Msg = "参数解析异常!"
  501. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  502. return
  503. }
  504. req.ChartName = strings.Trim(req.ChartName, " ")
  505. if req.ChartInfoId <= 0 {
  506. br.Msg = "请选择图表"
  507. return
  508. }
  509. //判断指标名称是否存在
  510. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  511. if err != nil {
  512. if err.Error() == utils.ErrNoRow() {
  513. br.Msg = "图表已被删除,请刷新页面"
  514. br.ErrMsg = "图表已被删除,请刷新页面"
  515. return
  516. }
  517. br.Msg = "获取图表信息失败"
  518. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  519. return
  520. }
  521. // 判断是否有传入对应的指标配置
  522. noEdbInfoType := []int{10}
  523. if len(req.ChartEdbInfoList) <= 0 && !utils.InArrayByInt(noEdbInfoType, chartItem.ChartType) {
  524. br.Msg = "请选择指标!"
  525. return
  526. }
  527. var edbCondition string
  528. var edbPars []interface{}
  529. for _, v := range req.ChartEdbInfoList {
  530. edbInfoId := v.EdbInfoId
  531. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  532. if err != nil {
  533. if err.Error() == utils.ErrNoRow() {
  534. br.Msg = "图表不存在!"
  535. br.ErrMsg = "图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  536. return
  537. } else {
  538. br.Msg = "获取图表信息失败!"
  539. br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
  540. return
  541. }
  542. }
  543. if edbInfo == nil {
  544. br.Msg = "指标不存在!"
  545. br.ErrMsg = "指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  546. return
  547. }
  548. //判断指标名称是否重复
  549. if v.EdbName != "" {
  550. edbCondition = ""
  551. edbPars = make([]interface{}, 0)
  552. edbCondition += " AND edb_info_id<>? "
  553. edbPars = append(edbPars, edbInfo.EdbInfoId)
  554. switch this.Lang {
  555. case utils.EnLangVersion:
  556. edbCondition += " AND edb_name_en =? "
  557. default:
  558. edbCondition += " AND edb_name =? "
  559. }
  560. edbPars = append(edbPars, v.EdbName)
  561. edbExist, err := data_manage.GetEdbInfoByCondition(edbCondition, edbPars)
  562. if err != nil {
  563. if err.Error() != utils.ErrNoRow() {
  564. br.Msg = "判断英文指标名称是否存在失败"
  565. br.ErrMsg = "判断英文指标名称是否存在失败,Err:" + err.Error()
  566. return
  567. }
  568. }
  569. if err == nil && edbExist.EdbInfoId > 0 {
  570. br.Msg = edbExist.EdbName + ":" + v.EdbName + "指标名称已存在"
  571. br.ErrMsg = "指标名称已存在,请重新填写"
  572. br.IsSendEmail = false
  573. return
  574. }
  575. }
  576. }
  577. if req.ChartName != "" || req.ExtraConfig != `` {
  578. var condition string
  579. var pars []interface{}
  580. condition += " AND chart_info_id<>? "
  581. pars = append(pars, req.ChartInfoId)
  582. switch this.Lang {
  583. case utils.EnLangVersion:
  584. condition += " AND chart_name_en = ? "
  585. default:
  586. condition += " AND chart_name = ? "
  587. }
  588. pars = append(pars, req.ChartName)
  589. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  590. if err != nil {
  591. if err.Error() != utils.ErrNoRow() {
  592. br.Msg = "判断英文图表名称是否存在失败"
  593. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  594. return
  595. }
  596. }
  597. if err == nil && existItem.ChartInfoId > 0 {
  598. br.Msg = existItem.ChartName + ":" + req.ChartName + "图表名称已存在"
  599. return
  600. }
  601. }
  602. err = data_manage.EditChartBaseInfoAndEdbEnInfo(&req, chartItem, this.Lang)
  603. if err != nil {
  604. br.Msg = "保存失败"
  605. br.ErrMsg = "保存失败,Err:" + err.Error()
  606. return
  607. }
  608. //添加es数据
  609. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  610. //修改my eta es数据
  611. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  612. //edbinfo 修改es信息
  613. for _, v := range req.ChartEdbInfoList {
  614. edbInfoId := v.EdbInfoId
  615. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  616. if err != nil {
  617. if err.Error() == utils.ErrNoRow() {
  618. br.Msg = "图表不存在!"
  619. br.ErrMsg = "图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  620. return
  621. } else {
  622. br.Msg = "获取图表信息失败!"
  623. br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
  624. return
  625. }
  626. }
  627. if edbInfo != nil {
  628. go data.AddOrEditEdbInfoToEs(edbInfoId)
  629. }
  630. }
  631. //新增操作日志
  632. {
  633. chartLog := new(data_manage.ChartInfoLog)
  634. chartLog.ChartName = chartItem.ChartName
  635. chartLog.ChartInfoId = req.ChartInfoId
  636. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  637. chartLog.SysUserId = sysUser.AdminId
  638. chartLog.SysUserRealName = sysUser.RealName
  639. chartLog.UniqueCode = chartItem.UniqueCode
  640. chartLog.CreateTime = time.Now()
  641. chartLog.Content = string(this.Ctx.Input.RequestBody)
  642. chartLog.Status = "编辑图表英文信息"
  643. chartLog.Method = this.Ctx.Input.URL()
  644. go data_manage.AddChartInfoLog(chartLog)
  645. }
  646. //清除缓存
  647. if utils.Re == nil && utils.Rc != nil {
  648. utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  649. utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  650. }
  651. br.Ret = 200
  652. br.Success = true
  653. br.Msg = "编辑成功"
  654. br.IsAddLog = true
  655. }
  656. // @Title 获取最新图表接口
  657. // @Description 获取最新图表接口
  658. // @Success 200 {object} data_manage.ChartInfoList
  659. // @router /chart_info/newest [get]
  660. func (this *ChartInfoController) ChartInfoNewest() {
  661. br := new(models.BaseResponse).Init()
  662. defer func() {
  663. this.Data["json"] = br
  664. this.ServeJSON()
  665. }()
  666. item, err := data_manage.GetChartInfoByNewest(1)
  667. if err != nil && err.Error() != utils.ErrNoRow() {
  668. br.Msg = "获取最新图表失败"
  669. br.ErrMsg = "获取最新图表失败,Err:" + err.Error()
  670. return
  671. }
  672. br.Ret = 200
  673. br.Success = true
  674. br.Msg = "获取成功"
  675. br.Data = item
  676. }
  677. // ChartInfoMove
  678. // @Title 移动图表接口
  679. // @Description 移动图表接口
  680. // @Success 200 {object} data_manage.MoveChartInfoReq
  681. // @router /chart_info/move [post]
  682. func (this *ChartInfoController) ChartInfoMove() {
  683. br := new(models.BaseResponse).Init()
  684. defer func() {
  685. this.Data["json"] = br
  686. this.ServeJSON()
  687. }()
  688. sysUser := this.SysUser
  689. if sysUser == nil {
  690. br.Msg = "请登录"
  691. br.ErrMsg = "请登录,SysUser Is Empty"
  692. br.Ret = 408
  693. return
  694. }
  695. var req data_manage.MoveChartInfoReq
  696. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  697. if err != nil {
  698. br.Msg = "参数解析异常!"
  699. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  700. return
  701. }
  702. if req.ChartInfoId <= 0 {
  703. br.Msg = "参数错误"
  704. br.ErrMsg = "图表id小于等于0"
  705. return
  706. }
  707. if req.ChartClassifyId <= 0 {
  708. br.Msg = "请选择分类"
  709. return
  710. }
  711. //判断分类是否存在
  712. count, _ := data_manage.GetChartClassifyCountById(req.ChartClassifyId)
  713. if count <= 0 {
  714. br.Msg = "分类已被删除,不可移动,请刷新页面"
  715. return
  716. }
  717. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  718. if err != nil {
  719. br.Msg = "移动失败"
  720. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  721. return
  722. }
  723. if chartInfo.Source != utils.CHART_SOURCE_DEFAULT {
  724. br.Msg = "图表异常"
  725. br.ErrMsg = "分类异常,不是ETA图库的图表"
  726. return
  727. }
  728. // 数据权限校验
  729. {
  730. haveOperaAuth, err := data_manage_permission.CheckChartPermissionByChartInfoId(chartInfo.ChartInfoId, chartInfo.ChartClassifyId, chartInfo.IsJoinPermission, sysUser.AdminId)
  731. if err != nil {
  732. br.Msg = "移动失败"
  733. br.ErrMsg = "获取图表权限失败,Err:" + err.Error()
  734. return
  735. }
  736. if !haveOperaAuth {
  737. br.Msg = "无操作权限"
  738. br.ErrMsg = "无操作权限"
  739. br.IsSendEmail = false
  740. return
  741. }
  742. }
  743. //如果改变了分类,那么移动该图表数据
  744. if chartInfo.ChartClassifyId != req.ChartClassifyId {
  745. //查询需要修改的分类下是否存在同一个图表名称
  746. tmpChartInfo, tmpErr := data_manage.GetChartInfoByClassifyIdAndName(req.ChartClassifyId, chartInfo.ChartName)
  747. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  748. br.Msg = "移动失败"
  749. br.ErrMsg = "移动失败,Err:" + tmpErr.Error()
  750. return
  751. }
  752. if tmpChartInfo != nil {
  753. br.Msg = "移动失败,同一个分类下图表名称不允许重复"
  754. br.ErrMsg = "移动失败,同一个分类下图表名称不允许重复"
  755. return
  756. }
  757. err = data_manage.MoveChartInfo(req.ChartInfoId, req.ChartClassifyId)
  758. if err != nil {
  759. br.Msg = "移动失败"
  760. br.ErrMsg = "移动失败,Err:" + err.Error()
  761. return
  762. }
  763. }
  764. //移动排序
  765. updateCol := make([]string, 0)
  766. //如果有传入 上一个兄弟节点分类id
  767. if req.PrevChartInfoId > 0 {
  768. prevChartInfo, err := data_manage.GetChartInfoById(req.PrevChartInfoId)
  769. if err != nil {
  770. br.Msg = "移动失败"
  771. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  772. return
  773. }
  774. //如果是移动在两个兄弟节点之间
  775. if req.NextChartInfoId > 0 {
  776. //下一个兄弟节点
  777. nextChartInfo, err := data_manage.GetChartInfoById(req.NextChartInfoId)
  778. if err != nil {
  779. br.Msg = "移动失败"
  780. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  781. return
  782. }
  783. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  784. if prevChartInfo.Sort == nextChartInfo.Sort || prevChartInfo.Sort == chartInfo.Sort {
  785. //变更兄弟节点的排序
  786. updateSortStr := `sort + 2`
  787. _ = data_manage.UpdateChartInfoSortByClassifyId(prevChartInfo.ChartClassifyId, prevChartInfo.Sort, prevChartInfo.ChartInfoId, []int{chartInfo.Source}, updateSortStr)
  788. } else {
  789. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  790. if nextChartInfo.Sort-prevChartInfo.Sort == 1 {
  791. //变更兄弟节点的排序
  792. updateSortStr := `sort + 1`
  793. _ = data_manage.UpdateChartInfoSortByClassifyId(prevChartInfo.ChartClassifyId, prevChartInfo.Sort, prevChartInfo.ChartInfoId, []int{chartInfo.Source}, updateSortStr)
  794. }
  795. }
  796. }
  797. chartInfo.Sort = prevChartInfo.Sort + 1
  798. chartInfo.ModifyTime = time.Now()
  799. updateCol = append(updateCol, "Sort", "ModifyTime")
  800. } else {
  801. firstClassify, err := data_manage.GetFirstChartInfoByClassifyId(req.ChartClassifyId)
  802. if err != nil && err.Error() != utils.ErrNoRow() {
  803. br.Msg = "移动失败"
  804. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  805. return
  806. }
  807. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  808. if firstClassify != nil && firstClassify.Sort == 0 {
  809. updateSortStr := ` sort + 1 `
  810. _ = data_manage.UpdateChartInfoSortByClassifyId(firstClassify.ChartClassifyId, 0, firstClassify.ChartInfoId-1, []int{chartInfo.Source}, updateSortStr)
  811. }
  812. chartInfo.Sort = 0 //那就是排在第一位
  813. chartInfo.ModifyTime = time.Now()
  814. updateCol = append(updateCol, "Sort", "ModifyTime")
  815. }
  816. //更新
  817. if len(updateCol) > 0 {
  818. err = chartInfo.Update(updateCol)
  819. if err != nil {
  820. br.Msg = "移动失败"
  821. br.ErrMsg = "修改失败,Err:" + err.Error()
  822. return
  823. }
  824. }
  825. if err != nil {
  826. br.Msg = "移动失败"
  827. br.ErrMsg = "修改失败,Err:" + err.Error()
  828. return
  829. }
  830. //添加es数据
  831. go data.EsAddOrEditChartInfo(req.ChartInfoId)
  832. //修改my eta es数据
  833. go data.EsAddOrEditMyChartInfoByChartInfoId(req.ChartInfoId)
  834. //新增操作日志
  835. {
  836. chartLog := new(data_manage.ChartInfoLog)
  837. chartLog.ChartName = chartInfo.ChartName
  838. chartLog.ChartInfoId = req.ChartInfoId
  839. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  840. chartLog.SysUserId = sysUser.AdminId
  841. chartLog.SysUserRealName = sysUser.RealName
  842. chartLog.UniqueCode = chartInfo.UniqueCode
  843. chartLog.CreateTime = time.Now()
  844. chartLog.Content = string(this.Ctx.Input.RequestBody)
  845. chartLog.Status = "移动图表"
  846. chartLog.Method = this.Ctx.Input.URL()
  847. go data_manage.AddChartInfoLog(chartLog)
  848. }
  849. br.Ret = 200
  850. br.Success = true
  851. br.Msg = "移动成功"
  852. }
  853. // ChartInfoDetail
  854. // @Title 获取图表详情
  855. // @Description 获取图表详情接口
  856. // @Param ChartInfoId query int true "图表id"
  857. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  858. // @Param StartDate query string true "自定义开始日期"
  859. // @Param EndDate query string true "自定义结束日期"
  860. // @Param Calendar query string true "公历/农历"
  861. // @Param SeasonStartDate query string true "季节性图开始日期"
  862. // @Param SeasonEndDate query string true "季节性图结束日期"
  863. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  864. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  865. // @Success 200 {object} data_manage.ChartInfoDetailResp
  866. // @router /chart_info/detail [get]
  867. func (this *ChartInfoController) ChartInfoDetail() {
  868. br := new(models.BaseResponse).Init()
  869. defer func() {
  870. this.Data["json"] = br
  871. this.ServeJSON()
  872. }()
  873. sysUser := this.SysUser
  874. if sysUser == nil {
  875. br.Msg = "请登录"
  876. br.ErrMsg = "请登录,SysUser Is Empty"
  877. br.Ret = 408
  878. return
  879. }
  880. chartInfoId, _ := this.GetInt("ChartInfoId")
  881. dateType, _ := this.GetInt("DateType")
  882. fmt.Println("dateType:", dateType)
  883. if dateType <= 0 {
  884. dateType = 3
  885. }
  886. startDate := this.GetString("StartDate")
  887. endDate := this.GetString("EndDate")
  888. startYear, _ := this.GetInt("StartYear")
  889. edbInfoId := this.GetString("EdbInfoId")
  890. chartType, _ := this.GetInt("ChartType")
  891. calendar := this.GetString("Calendar")
  892. if calendar == "" {
  893. calendar = "公历"
  894. }
  895. var err error
  896. chartInfo := new(data_manage.ChartInfoView)
  897. chartInfo.HaveOperaAuth = true
  898. if chartInfoId > 0 {
  899. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  900. if err != nil {
  901. if err.Error() == utils.ErrNoRow() {
  902. br.Msg = "该图表已删除,自动查看下一图表"
  903. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  904. br.Ret = 406
  905. return
  906. }
  907. br.Msg = "获取失败"
  908. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  909. return
  910. }
  911. chartType = chartInfo.ChartType
  912. // 获取主题样式
  913. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  914. if err != nil {
  915. br.Msg = "获取失败"
  916. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  917. return
  918. }
  919. chartInfo.ChartThemeStyle = chartTheme.Config
  920. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  921. // 图表数据权限
  922. {
  923. // 已授权分类id
  924. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  925. if err != nil {
  926. br.Msg = "获取失败"
  927. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  928. return
  929. }
  930. if chartInfo.ChartClassifyId > 0 {
  931. // 图表分类
  932. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  933. if err != nil {
  934. br.Msg = "获取失败"
  935. br.ErrMsg = "获取图表分类信息失败,Err:" + err.Error()
  936. return
  937. }
  938. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  939. } else {
  940. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  941. }
  942. }
  943. }
  944. resp := new(data_manage.ChartInfoDetailResp)
  945. // 区分是否是来自平衡表图表的数据
  946. var chartInfoDataShow data.ChartInfoDataShow
  947. chartInfoDataShow = &data.BaseChartInfoDataShow{}
  948. mappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  949. if chartInfoId > 0 {
  950. if chartInfo.Source == utils.CHART_SOURCE_BALANCE_EXCEL { //来自平衡表图表的数据
  951. excelChartInfoDataShow := new(data.ExcelChartInfoDataShow)
  952. // 相关联指标
  953. mappingListTmp, dataListMap, err, errMsg := excel.GetBalanceExcelChartSingle(chartInfoId, 0, "")
  954. if err != nil {
  955. br.Msg = errMsg
  956. br.ErrMsg = err.Error()
  957. return
  958. }
  959. excelChartInfoDataShow.DataListMap = dataListMap
  960. excelChartInfoDataShow.MappingListTmp = mappingListTmp
  961. chartInfoDataShow = excelChartInfoDataShow
  962. mappingList, err = chartInfoDataShow.GetChartEdbMappingListByEdbInfoIdList([]int{})
  963. if err != nil {
  964. br.Msg = "获取失败"
  965. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  966. return
  967. }
  968. } else {
  969. mappingList, err = data_manage.GetChartEdbMappingList(chartInfoId)
  970. if err != nil {
  971. br.Msg = "获取失败"
  972. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  973. return
  974. }
  975. }
  976. } else {
  977. if edbInfoId != "" {
  978. mappingList, err = data_manage.GetChartEdbMappingListByEdbInfoId(edbInfoId)
  979. if err != nil {
  980. br.Msg = "获取失败"
  981. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  982. return
  983. }
  984. }
  985. }
  986. // 图表额外数据参数
  987. extraConfigStr := chartInfo.ExtraConfig
  988. // 柱方图的一些配置
  989. var barConfig data_manage.BarChartInfoReq
  990. if chartInfo != nil && chartInfo.ChartType == 7 {
  991. if chartInfo.BarConfig == `` {
  992. br.Msg = "柱方图未配置"
  993. br.ErrMsg = "柱方图未配置"
  994. return
  995. }
  996. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  997. if err != nil {
  998. br.Msg = "柱方图配置异常"
  999. br.ErrMsg = "柱方图配置异常"
  1000. return
  1001. }
  1002. extraConfigStr = chartInfo.BarConfig
  1003. }
  1004. yearMax := 0
  1005. if dateType == utils.DateTypeNYears {
  1006. for _, v := range mappingList {
  1007. if v.LatestDate != "" {
  1008. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  1009. if tErr != nil {
  1010. br.Msg = "获取失败"
  1011. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  1012. return
  1013. }
  1014. if lastDateT.Year() > yearMax {
  1015. yearMax = lastDateT.Year()
  1016. }
  1017. }
  1018. }
  1019. }
  1020. // 开始/结束日期
  1021. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  1022. if chartInfo.HaveOperaAuth {
  1023. // 获取图表中的指标数据
  1024. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbDataV2(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, chartInfoDataShow)
  1025. if err != nil {
  1026. br.Msg = "获取失败"
  1027. if errMsg != `` {
  1028. br.Msg = errMsg
  1029. }
  1030. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1031. return
  1032. }
  1033. // 指标权限
  1034. {
  1035. classifyMap := make(map[int]*data_manage.EdbClassify)
  1036. // 分类
  1037. {
  1038. classifyIdList := make([]int, 0)
  1039. for _, v := range edbList {
  1040. classifyIdList = append(classifyIdList, v.ClassifyId)
  1041. }
  1042. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1043. if tmpErr != nil {
  1044. br.Msg = "获取失败"
  1045. br.ErrMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  1046. return
  1047. }
  1048. for _, v := range classifyList {
  1049. classifyMap[v.ClassifyId] = v
  1050. }
  1051. }
  1052. // 获取所有有权限的指标和分类
  1053. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  1054. if err != nil {
  1055. br.Msg = "获取失败"
  1056. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1057. return
  1058. }
  1059. for _, item := range edbList {
  1060. // 数据权限
  1061. if currClassify, ok := classifyMap[item.ClassifyId]; ok {
  1062. item.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(item.IsJoinPermission, currClassify.IsJoinPermission, item.EdbInfoId, item.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1063. }
  1064. }
  1065. }
  1066. // 单位
  1067. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  1068. chartInfo.Unit = yDataList[0].Unit
  1069. chartInfo.UnitEn = yDataList[0].UnitEn
  1070. }
  1071. warnEdbList := make([]string, 0)
  1072. for _, v := range edbList {
  1073. if v.IsNullData {
  1074. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  1075. }
  1076. }
  1077. if len(warnEdbList) > 0 {
  1078. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  1079. }
  1080. if chartInfoId > 0 && chartInfo != nil {
  1081. //判断是否加入我的图库
  1082. {
  1083. var myChartCondition string
  1084. var myChartPars []interface{}
  1085. myChartCondition += ` AND a.admin_id=? `
  1086. myChartPars = append(myChartPars, sysUser.AdminId)
  1087. myChartCondition += ` AND a.chart_info_id=? `
  1088. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  1089. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  1090. if err != nil && err.Error() != utils.ErrNoRow() {
  1091. br.Msg = "获取失败"
  1092. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1093. return
  1094. }
  1095. if myChartList != nil && len(myChartList) > 0 {
  1096. chartInfo.IsAdd = true
  1097. chartInfo.MyChartId = myChartList[0].MyChartId
  1098. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  1099. }
  1100. }
  1101. if chartInfo.ChartType == 2 {
  1102. if chartInfo.SeasonStartDate != "" {
  1103. chartInfo.StartDate = chartInfo.SeasonStartDate
  1104. chartInfo.EndDate = chartInfo.SeasonEndDate
  1105. if chartInfo.DateType == 3 {
  1106. chartInfo.DateType = 5
  1107. }
  1108. }
  1109. }
  1110. }
  1111. // 面积图 面积堆积 数据处理
  1112. if chartInfo.ChartType == utils.CHART_TYPE_AREA {
  1113. err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
  1114. if err != nil {
  1115. br.Msg = "面积图处理失败"
  1116. br.ErrMsg = errMsg
  1117. return
  1118. }
  1119. }
  1120. // 图表的指标来源
  1121. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  1122. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  1123. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  1124. //判断是否需要展示英文标识
  1125. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  1126. resp.EdbInfoList = edbList
  1127. resp.XEdbIdValue = xEdbIdValue
  1128. resp.YDataList = yDataList
  1129. resp.DataResp = dataResp
  1130. }
  1131. //图表操作权限
  1132. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, chartInfo.HaveOperaAuth)
  1133. chartInfo.Button = data_manage.ChartViewButton{
  1134. IsEdit: chartInfo.IsEdit,
  1135. IsEnChart: chartInfo.IsEnChart,
  1136. IsAdd: chartInfo.IsAdd,
  1137. IsCopy: true,
  1138. IsSetName: chartInfo.IsSetName,
  1139. }
  1140. resp.ChartInfo = chartInfo
  1141. resp.BarChartInfo = barConfig
  1142. br.Ret = 200
  1143. br.Success = true
  1144. br.Msg = "获取成功"
  1145. br.Data = resp
  1146. }
  1147. func fillAreaGraphData(extraConfigStr string, edbDataList []*data_manage.ChartEdbInfoMapping) (err error, errMsg string) {
  1148. var tmpConfig data_manage.AreaExtraConf
  1149. if extraConfigStr != `` {
  1150. err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
  1151. if err != nil {
  1152. errMsg = "面积图配置异常"
  1153. err = errors.New(errMsg)
  1154. return
  1155. }
  1156. if tmpConfig.StandardEdbInfoId <= 0 {
  1157. errMsg = "面积图配置异常"
  1158. err = errors.New(errMsg)
  1159. return
  1160. }
  1161. }
  1162. if tmpConfig.IsHeap == 1 {
  1163. standardIndexMap := make(map[string]*data_manage.EdbDataList)
  1164. var startDate, endDate string
  1165. for _, v := range edbDataList {
  1166. // 判断是否为基准指标
  1167. if v.EdbInfoId == tmpConfig.StandardEdbInfoId {
  1168. if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
  1169. startDate = dataList[0].DataTime
  1170. endDate = dataList[len(dataList)-1].DataTime
  1171. for _, dataObject := range dataList {
  1172. standardIndexMap[dataObject.DataTime] = dataObject
  1173. }
  1174. }
  1175. break
  1176. }
  1177. }
  1178. strategy, err := area_graph.CreateStrategy(tmpConfig.NullDealWay)
  1179. if err != nil {
  1180. return err, "创建空值处理器失败"
  1181. }
  1182. err = strategy.Deal(tmpConfig, edbDataList, standardIndexMap, startDate, endDate)
  1183. if err != nil {
  1184. return err, err.Error()
  1185. }
  1186. }
  1187. return nil, ""
  1188. }
  1189. // PreviewChartInfoDetail
  1190. // @Title 获取图表详情(预览)
  1191. // @Description 获取图表详情接口(预览)
  1192. // @Param request body data_manage.PreviewChartInfoReq true "type json string"
  1193. // @Success 200 {object} data_manage.ChartInfoDetailResp
  1194. // @router /chart_info/detail/preview [post]
  1195. func (this *ChartInfoController) PreviewChartInfoDetail() {
  1196. br := new(models.BaseResponse).Init()
  1197. defer func() {
  1198. this.Data["json"] = br
  1199. this.ServeJSON()
  1200. }()
  1201. sysUser := this.SysUser
  1202. if sysUser == nil {
  1203. br.Msg = "请登录"
  1204. br.ErrMsg = "请登录,SysUser Is Empty"
  1205. br.Ret = 408
  1206. return
  1207. }
  1208. var req data_manage.PreviewChartInfoReq
  1209. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1210. if err != nil {
  1211. br.Msg = "参数解析异常!"
  1212. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1213. return
  1214. }
  1215. if req.DateType <= 0 {
  1216. req.DateType = 3
  1217. }
  1218. // edbInfoId 指标id
  1219. if len(req.ChartEdbInfoList) <= 0 {
  1220. br.Msg = "请选择指标"
  1221. br.ErrMsg = "指标为空"
  1222. br.IsSendEmail = false
  1223. return
  1224. }
  1225. calendar := req.Calendar
  1226. if calendar == "" {
  1227. calendar = "公历"
  1228. }
  1229. chartInfo := new(data_manage.ChartInfoView)
  1230. chartInfo.ChartType = req.ChartType
  1231. edbInfoIdList := make([]int, 0)
  1232. edbInfoIdMapping := make(map[int]*data_manage.ChartSaveItem)
  1233. for _, v := range req.ChartEdbInfoList {
  1234. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  1235. edbInfoIdMapping[v.EdbInfoId] = v
  1236. }
  1237. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  1238. if err != nil {
  1239. br.Msg = "获取失败"
  1240. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1241. return
  1242. }
  1243. yearMax := 0
  1244. for k, v := range mappingList {
  1245. if tmpV, ok := edbInfoIdMapping[v.EdbInfoId]; ok {
  1246. v.EdbInfoType = tmpV.EdbInfoType
  1247. v.MaxData = tmpV.MaxData
  1248. v.MinData = tmpV.MinData
  1249. v.IsOrder = tmpV.IsOrder
  1250. v.IsAxis = tmpV.IsAxis
  1251. v.EdbInfoType = tmpV.EdbInfoType
  1252. v.LeadValue = tmpV.LeadValue
  1253. v.LeadUnit = tmpV.LeadUnit
  1254. v.ChartStyle = tmpV.ChartStyle
  1255. v.ChartColor = tmpV.ChartColor
  1256. v.PredictChartColor = tmpV.PredictChartColor
  1257. v.ChartWidth = tmpV.ChartWidth
  1258. //v.Source = tmpV.Source
  1259. // 得到指标更新的最新的年份
  1260. if req.DateType == utils.DateTypeNYears && v.LatestDate != "" {
  1261. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  1262. if tErr != nil {
  1263. br.Msg = "获取失败"
  1264. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  1265. return
  1266. }
  1267. if lastDateT.Year() > yearMax {
  1268. yearMax = lastDateT.Year()
  1269. }
  1270. }
  1271. }
  1272. if req.ChartEdbInfoList[k].IsConvert == 1 {
  1273. v.IsConvert = 1
  1274. v.ConvertType = req.ChartEdbInfoList[k].ConvertType
  1275. v.ConvertValue = req.ChartEdbInfoList[k].ConvertValue
  1276. v.ConvertUnit = req.ChartEdbInfoList[k].ConvertUnit
  1277. v.ConvertEnUnit = req.ChartEdbInfoList[k].ConvertEnUnit
  1278. }
  1279. mappingList[k] = v
  1280. }
  1281. // 开始/结束日期
  1282. startDate, endDate := utils.GetDateByDateTypeV2(req.DateType, req.StartDate, req.EndDate, req.StartYear, yearMax)
  1283. if startDate == "" {
  1284. br.Msg = "获取失败"
  1285. br.ErrMsg = "获取失败 时间格式错误"
  1286. return
  1287. }
  1288. var seasonExtraConfig string
  1289. if req.ChartType == 2 {
  1290. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  1291. if tErr != nil {
  1292. br.Msg = "季节性图表配置信息异常"
  1293. br.ErrMsg = "季节性图表配置信息异常,Err:" + tErr.Error()
  1294. return
  1295. }
  1296. seasonExtraConfig = string(seasonExtra)
  1297. }
  1298. // 图表额外数据参数
  1299. extraConfigStr := chartInfo.ExtraConfig
  1300. // 柱方图的一些配置
  1301. var barConfig data_manage.BarChartInfoReq
  1302. if chartInfo != nil && chartInfo.ChartType == 7 {
  1303. if chartInfo.BarConfig == `` {
  1304. br.Msg = "柱方图未配置"
  1305. br.ErrMsg = "柱方图未配置"
  1306. return
  1307. }
  1308. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  1309. if err != nil {
  1310. br.Msg = "柱方图配置异常"
  1311. br.ErrMsg = "柱方图配置异常"
  1312. return
  1313. }
  1314. extraConfigStr = chartInfo.BarConfig
  1315. }
  1316. // 获取图表中的指标数据
  1317. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, req.ChartType, calendar, startDate, endDate, mappingList, extraConfigStr, seasonExtraConfig)
  1318. if err != nil {
  1319. br.Msg = "获取失败"
  1320. if errMsg != `` {
  1321. br.Msg = errMsg
  1322. }
  1323. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1324. return
  1325. }
  1326. // 单位
  1327. if req.ChartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  1328. chartInfo.Unit = yDataList[0].Unit
  1329. chartInfo.UnitEn = yDataList[0].UnitEn
  1330. }
  1331. warnEdbList := make([]string, 0)
  1332. for _, v := range edbList {
  1333. if v.IsNullData {
  1334. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  1335. }
  1336. }
  1337. if len(warnEdbList) > 0 {
  1338. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  1339. }
  1340. //图表操作权限
  1341. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  1342. //判断是否需要展示英文标识
  1343. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  1344. chartInfo.Button = data_manage.ChartViewButton{
  1345. IsEdit: chartInfo.IsEdit,
  1346. IsEnChart: chartInfo.IsEnChart,
  1347. IsAdd: chartInfo.IsAdd,
  1348. IsCopy: true,
  1349. IsSetName: chartInfo.IsSetName,
  1350. }
  1351. // 图表的指标来源
  1352. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  1353. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  1354. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  1355. resp := new(data_manage.ChartInfoDetailResp)
  1356. resp.ChartInfo = chartInfo
  1357. resp.EdbInfoList = edbList
  1358. resp.XEdbIdValue = xEdbIdValue
  1359. resp.YDataList = yDataList
  1360. resp.BarChartInfo = barConfig
  1361. resp.DataResp = dataResp
  1362. br.Ret = 200
  1363. br.Success = true
  1364. br.Msg = "获取成功"
  1365. br.Data = resp
  1366. }
  1367. // ChartInfoDetailV2
  1368. // @Title 获取图表详情
  1369. // @Description 获取图表详情接口
  1370. // @Param ChartInfoId query int true "图表id"
  1371. // @Success 200 {object} data_manage.ChartInfoDetailResp
  1372. // @router /chart_info/detail/v2 [get]
  1373. func (this *ChartInfoController) ChartInfoDetailV2() {
  1374. br := new(models.BaseResponse).Init()
  1375. defer func() {
  1376. this.Data["json"] = br
  1377. this.ServeJSON()
  1378. }()
  1379. sysUser := this.SysUser
  1380. if sysUser == nil {
  1381. br.Msg = "请登录"
  1382. br.ErrMsg = "请登录,SysUser Is Empty"
  1383. br.Ret = 408
  1384. return
  1385. }
  1386. chartInfoId, _ := this.GetInt("ChartInfoId")
  1387. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  1388. if chartInfoId <= 0 {
  1389. br.Msg = "请选择图表"
  1390. br.IsSendEmail = false
  1391. return
  1392. }
  1393. chartInfo, err := data_manage.GetChartInfoViewById(chartInfoId)
  1394. if err != nil {
  1395. if err.Error() == utils.ErrNoRow() {
  1396. br.Msg = "该图表已删除,自动查看下一图表"
  1397. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  1398. br.Ret = 406
  1399. return
  1400. }
  1401. br.Msg = "获取失败"
  1402. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1403. return
  1404. }
  1405. resp := new(data_manage.ChartInfoDetailResp)
  1406. // 图表数据权限
  1407. {
  1408. // 图表分类
  1409. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  1410. if err != nil {
  1411. br.Msg = "获取失败"
  1412. br.ErrMsg = "获取图表分类信息失败,Err:" + err.Error()
  1413. return
  1414. }
  1415. // 已授权分类id
  1416. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  1417. if err != nil {
  1418. br.Msg = "获取失败"
  1419. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  1420. return
  1421. }
  1422. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  1423. }
  1424. // 获取主题样式
  1425. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  1426. if err != nil {
  1427. br.Msg = "获取失败"
  1428. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  1429. return
  1430. }
  1431. chartInfo.ChartThemeStyle = chartTheme.Config
  1432. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  1433. dateType := chartInfo.DateType
  1434. fmt.Println("dateType:", dateType)
  1435. chartType := chartInfo.ChartType
  1436. startDate := chartInfo.StartDate
  1437. endDate := chartInfo.EndDate
  1438. seasonStartDate := chartInfo.SeasonStartDate
  1439. seasonEndDate := chartInfo.SeasonEndDate
  1440. startYear := chartInfo.StartYear
  1441. calendar := chartInfo.Calendar
  1442. if calendar == "" {
  1443. calendar = "公历"
  1444. }
  1445. // 相关联指标
  1446. mappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1447. if err != nil {
  1448. br.Msg = "获取失败"
  1449. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1450. return
  1451. }
  1452. if chartType == 2 {
  1453. startDate = seasonStartDate
  1454. endDate = seasonEndDate
  1455. if dateType <= 0 {
  1456. if startDate != "" {
  1457. dateType = 5
  1458. } else {
  1459. dateType = utils.DateTypeNYears
  1460. }
  1461. }
  1462. } else {
  1463. if dateType <= 0 {
  1464. dateType = 3
  1465. }
  1466. }
  1467. yearMax := 0
  1468. if dateType == utils.DateTypeNYears {
  1469. for _, v := range mappingList {
  1470. if v.LatestDate != "" {
  1471. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  1472. if tErr != nil {
  1473. br.Msg = "获取失败"
  1474. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  1475. return
  1476. }
  1477. if lastDateT.Year() > yearMax {
  1478. yearMax = lastDateT.Year()
  1479. }
  1480. }
  1481. }
  1482. }
  1483. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  1484. if chartInfo.ChartType == 2 {
  1485. chartInfo.StartDate = startDate
  1486. chartInfo.EndDate = endDate
  1487. }
  1488. // 图表额外数据参数
  1489. extraConfigStr := chartInfo.ExtraConfig
  1490. // 柱方图的一些配置
  1491. var barConfig data_manage.BarChartInfoReq
  1492. if chartInfo != nil && chartInfo.ChartType == 7 {
  1493. if chartInfo.BarConfig == `` {
  1494. br.Msg = "柱方图未配置"
  1495. br.ErrMsg = "柱方图未配置"
  1496. return
  1497. }
  1498. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  1499. if err != nil {
  1500. br.Msg = "柱方图配置异常"
  1501. br.ErrMsg = "柱方图配置异常"
  1502. return
  1503. }
  1504. extraConfigStr = chartInfo.BarConfig
  1505. }
  1506. if chartInfo.HaveOperaAuth {
  1507. // 获取图表中的指标数据
  1508. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  1509. if err != nil {
  1510. br.Msg = "获取失败"
  1511. if errMsg != `` {
  1512. br.Msg = errMsg
  1513. }
  1514. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1515. return
  1516. }
  1517. // 单位
  1518. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  1519. chartInfo.Unit = yDataList[0].Unit
  1520. chartInfo.UnitEn = yDataList[0].UnitEn
  1521. }
  1522. warnEdbList := make([]string, 0)
  1523. for _, v := range edbList {
  1524. if v.IsNullData {
  1525. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  1526. }
  1527. }
  1528. if len(warnEdbList) > 0 {
  1529. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  1530. }
  1531. if chartInfoId > 0 && chartInfo != nil {
  1532. //判断是否加入我的图库
  1533. {
  1534. var myChartCondition string
  1535. var myChartPars []interface{}
  1536. myChartCondition += ` AND a.admin_id=? `
  1537. myChartPars = append(myChartPars, sysUser.AdminId)
  1538. myChartCondition += ` AND a.chart_info_id=? `
  1539. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  1540. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  1541. if err != nil && err.Error() != utils.ErrNoRow() {
  1542. br.Msg = "获取失败"
  1543. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1544. return
  1545. }
  1546. if myChartList != nil && len(myChartList) > 0 {
  1547. chartInfo.IsAdd = true
  1548. chartInfo.MyChartId = myChartList[0].MyChartId
  1549. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  1550. }
  1551. }
  1552. }
  1553. //判断是否需要展示英文标识
  1554. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  1555. // 图表的指标来源
  1556. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  1557. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  1558. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  1559. // 指标权限
  1560. {
  1561. classifyMap := make(map[int]*data_manage.EdbClassify)
  1562. // 分类
  1563. {
  1564. classifyIdList := make([]int, 0)
  1565. for _, v := range edbList {
  1566. classifyIdList = append(classifyIdList, v.ClassifyId)
  1567. }
  1568. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1569. if tmpErr != nil {
  1570. br.Msg = "获取失败"
  1571. br.ErrMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  1572. return
  1573. }
  1574. for _, v := range classifyList {
  1575. classifyMap[v.ClassifyId] = v
  1576. }
  1577. }
  1578. // 获取所有有权限的指标和分类
  1579. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  1580. if err != nil {
  1581. br.Msg = "获取失败"
  1582. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1583. return
  1584. }
  1585. for _, item := range edbList {
  1586. // 数据权限
  1587. if currClassify, ok := classifyMap[item.ClassifyId]; ok {
  1588. item.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(item.IsJoinPermission, currClassify.IsJoinPermission, item.EdbInfoId, item.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1589. }
  1590. }
  1591. }
  1592. resp.EdbInfoList = edbList
  1593. resp.XEdbIdValue = xEdbIdValue
  1594. resp.YDataList = yDataList
  1595. resp.DataResp = dataResp
  1596. } else {
  1597. resp.EdbInfoList = mappingList
  1598. }
  1599. //图表操作权限
  1600. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, chartInfo.HaveOperaAuth)
  1601. chartInfo.Button = data_manage.ChartViewButton{
  1602. IsEdit: chartInfo.IsEdit,
  1603. IsEnChart: chartInfo.IsEnChart,
  1604. IsAdd: chartInfo.IsAdd,
  1605. IsCopy: true,
  1606. IsSetName: chartInfo.IsSetName,
  1607. }
  1608. resp.ChartInfo = chartInfo
  1609. resp.BarChartInfo = barConfig
  1610. br.Ret = 200
  1611. br.Success = true
  1612. br.Msg = "获取成功"
  1613. br.Data = resp
  1614. }
  1615. // @Title 图表指标模糊搜索
  1616. // @Description 图表指标模糊搜索接口
  1617. // @Param KeyWord query string true "指标名称/ID"
  1618. // @Success 200 {object} data_manage.ChartEdbInfo
  1619. // @router /chart_info/edb_info/search [get]
  1620. func (this *ChartInfoController) ChartInfoEdbInfoSearch() {
  1621. br := new(models.BaseResponse).Init()
  1622. defer func() {
  1623. this.Data["json"] = br
  1624. this.ServeJSON()
  1625. }()
  1626. sysUser := this.SysUser
  1627. if sysUser == nil {
  1628. br.Msg = "请登录"
  1629. br.ErrMsg = "请登录,SysUser Is Empty"
  1630. br.Ret = 408
  1631. return
  1632. }
  1633. KeyWord := this.GetString("KeyWord")
  1634. //if KeyWord == "" {
  1635. // br.Msg = "请输入指标名称/指标ID"
  1636. // return
  1637. //}
  1638. var err error
  1639. searchList := make([]*data_manage.ChartEdbInfo, 0)
  1640. searchList, err = data_manage.EdbInfoSearchByKeyWord(KeyWord)
  1641. if err != nil {
  1642. br.Msg = "获取失败"
  1643. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1644. return
  1645. }
  1646. if searchList == nil {
  1647. searchList = make([]*data_manage.ChartEdbInfo, 0)
  1648. }
  1649. lenSearchList := len(searchList)
  1650. for i := 0; i < lenSearchList; i++ {
  1651. searchList[i].EdbNameAlias = searchList[i].EdbName
  1652. }
  1653. for i := 0; i < lenSearchList; i++ {
  1654. for j := 0; j < lenSearchList; j++ {
  1655. if (searchList[i].EdbNameAlias == searchList[j].EdbNameAlias) &&
  1656. (searchList[i].EdbInfoId != searchList[j].EdbInfoId) &&
  1657. !(strings.Contains(searchList[i].EdbName, searchList[i].SourceName)) {
  1658. searchList[i].EdbName = searchList[i].EdbName + "(" + searchList[i].SourceName + ")"
  1659. }
  1660. }
  1661. }
  1662. br.Ret = 200
  1663. br.Success = true
  1664. br.Msg = "获取成功"
  1665. br.Data = searchList
  1666. }
  1667. // @Title 图表-获取指标详情
  1668. // @Description 图表-获取指标详情接口
  1669. // @Param EdbInfoId query int true "指标ID"
  1670. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今"
  1671. // @Param StartDate query string true "自定义开始日期"
  1672. // @Param EndDate query string true "自定义结束日期"
  1673. // @Param EdbInfoType query int true "1:标准指标,0:领先指标,默认为标准指标"
  1674. // @Param LeadValue query int true "领先值"
  1675. // @Param LeadUnit query string true "领先单位:天,月,季,周"
  1676. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  1677. // @router /chart_info/edb_info/detail [get]
  1678. func (this *ChartInfoController) ChartInfoEdbInfoDetail() {
  1679. br := new(models.BaseResponse).Init()
  1680. defer func() {
  1681. this.Data["json"] = br
  1682. this.ServeJSON()
  1683. }()
  1684. sysUser := this.SysUser
  1685. if sysUser == nil {
  1686. br.Msg = "请登录"
  1687. br.ErrMsg = "请登录,SysUser Is Empty"
  1688. br.Ret = 408
  1689. return
  1690. }
  1691. edbInfoId, _ := this.GetInt("EdbInfoId")
  1692. if edbInfoId <= 0 {
  1693. br.Msg = "参数错误"
  1694. return
  1695. }
  1696. dateType, _ := this.GetInt("DateType")
  1697. if dateType <= 0 {
  1698. dateType = 3
  1699. }
  1700. edbInfoType, _ := this.GetInt("EdbInfoType")
  1701. if edbInfoType < 0 {
  1702. edbInfoType = 1
  1703. }
  1704. leadValue, _ := this.GetInt("LeadValue")
  1705. if leadValue < 0 {
  1706. leadValue = 0
  1707. }
  1708. leadUnit := this.GetString("LeadUnit")
  1709. startDate := this.GetString("StartDate")
  1710. endDate := this.GetString("EndDate")
  1711. startYear, _ := this.GetInt("StartYear")
  1712. isTimeBetween := false //是否是时间区间
  1713. if dateType == 5 {
  1714. isTimeBetween = true
  1715. }
  1716. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1717. if err != nil {
  1718. br.Msg = "获取失败"
  1719. br.Msg = "获取失败,Err:" + err.Error()
  1720. return
  1721. }
  1722. maxYear := 0
  1723. if edbInfo.LatestDate != "" {
  1724. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  1725. maxYear = latestDateT.Year()
  1726. }
  1727. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1728. if startDate == "" {
  1729. br.Msg = "参数错误"
  1730. br.Msg = "参数错误,无效的查询日期"
  1731. return
  1732. }
  1733. var startDateReal string
  1734. var diffSeconds int64
  1735. if leadValue > 0 && leadUnit != "" {
  1736. var startTimeRealTemp time.Time
  1737. startDateParse, err := time.Parse(utils.FormatDate, startDate)
  1738. if err != nil {
  1739. br.Msg = "获取数据失败"
  1740. br.ErrMsg = "获取数据失败,日期格式错误:startDate:" + startDate
  1741. return
  1742. }
  1743. switch leadUnit {
  1744. case "天":
  1745. startTimeRealTemp = startDateParse.AddDate(0, 0, -leadValue)
  1746. case "月":
  1747. startTimeRealTemp = startDateParse.AddDate(0, -leadValue, 0)
  1748. case "季":
  1749. startTimeRealTemp = startDateParse.AddDate(0, -3*leadValue, 0)
  1750. case "周":
  1751. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*leadValue)
  1752. case "年":
  1753. startTimeRealTemp = startDateParse.AddDate(-leadValue, 0, 0)
  1754. }
  1755. if startTimeRealTemp.Before(startDateParse) {
  1756. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  1757. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  1758. } else {
  1759. startDateReal = startDate
  1760. diffSeconds = 0
  1761. }
  1762. } else {
  1763. startDateReal = startDate
  1764. }
  1765. var dataList []*data_manage.EdbDataList
  1766. var minData, maxData float64
  1767. switch edbInfo.EdbInfoType {
  1768. case 0: //普通源指标
  1769. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDateReal, endDate)
  1770. if err != nil {
  1771. br.Msg = "获取失败"
  1772. br.Msg = "获取失败,Err:" + err.Error()
  1773. return
  1774. }
  1775. //查询区间内最大最小值
  1776. minData, maxData, err = data_manage.GetEdbDataListMinAndMax(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDateReal, endDate)
  1777. if err != nil {
  1778. br.Msg = "获取失败"
  1779. br.Msg = "获取指标最新的最大最小值失败,Err:" + err.Error()
  1780. return
  1781. }
  1782. case 1: //预测指标
  1783. tmpDataList, sourceEdbInfoItem, _, err, _ := data.GetPredictDataListByPredictEdbInfo(edbInfo, startDateReal, endDate, isTimeBetween)
  1784. if err != nil {
  1785. br.Msg = "获取失败"
  1786. br.Msg = "获取失败,Err:" + err.Error()
  1787. return
  1788. }
  1789. dataList = tmpDataList
  1790. // 有预测数据,且为普通的预测指标
  1791. if len(dataList) > 0 && edbInfo.EdbInfoType == 0 {
  1792. //查询区间内最大最小值
  1793. minData, maxData, err = data_manage.GetEdbDataListMinAndMax(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDateReal, endDate)
  1794. if err != nil {
  1795. br.Msg = "获取失败"
  1796. br.Msg = "获取指标最新的最大最小值失败,Err:" + err.Error()
  1797. return
  1798. }
  1799. tmpValue := dataList[len(dataList)-1]
  1800. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1801. if edbInfo.MaxValue < tmpValue.Value {
  1802. maxData = tmpValue.Value
  1803. }
  1804. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1805. if edbInfo.MinValue > tmpValue.Value {
  1806. minData = tmpValue.Value
  1807. }
  1808. }
  1809. }
  1810. moveLatestDate := edbInfo.LatestDate
  1811. if diffSeconds != 0 {
  1812. dataListLen := len(dataList)
  1813. for i := 0; i < dataListLen; i++ {
  1814. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  1815. }
  1816. day, err := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  1817. if err != nil {
  1818. br.Msg = "获取失败"
  1819. br.Msg = "获取领先滞后的的日期间隔失败,Err:" + err.Error()
  1820. return
  1821. }
  1822. moveLatestDateTime, err := time.ParseInLocation(utils.FormatDate, moveLatestDate, time.Local)
  1823. if err != nil {
  1824. br.Msg = "获取失败"
  1825. br.Msg = "指标日期转换失败,Err:" + err.Error()
  1826. return
  1827. }
  1828. moveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  1829. }
  1830. mapping := new(data_manage.ChartEdbInfoMapping)
  1831. mapping.EdbInfoId = edbInfo.EdbInfoId
  1832. mapping.EdbInfoCategoryType = edbInfo.EdbInfoType
  1833. mapping.SourceName = edbInfo.SourceName
  1834. mapping.Source = edbInfo.Source
  1835. mapping.EdbCode = edbInfo.EdbCode
  1836. mapping.EdbName = edbInfo.EdbName
  1837. mapping.EdbType = edbInfo.EdbType
  1838. mapping.Frequency = edbInfo.Frequency
  1839. mapping.Unit = edbInfo.Unit
  1840. mapping.StartDate = edbInfo.StartDate
  1841. mapping.EndDate = edbInfo.EndDate
  1842. mapping.ChartEdbMappingId = 0
  1843. mapping.ChartInfoId = 0
  1844. mapping.MaxData = maxData
  1845. mapping.MinData = minData
  1846. mapping.IsOrder = false
  1847. mapping.IsAxis = 1
  1848. mapping.EdbInfoType = 1
  1849. mapping.LeadValue = 0
  1850. mapping.LeadUnit = ""
  1851. mapping.LeadUnitEn = ""
  1852. mapping.ChartStyle = ""
  1853. mapping.ChartColor = ""
  1854. mapping.PredictChartColor = ""
  1855. mapping.ChartWidth = 0
  1856. mapping.LatestDate = edbInfo.LatestDate
  1857. mapping.MoveLatestDate = moveLatestDate
  1858. mapping.DataList = dataList
  1859. resp := new(data_manage.ChartEdbInfoDetailResp)
  1860. resp.EdbInfo = mapping
  1861. br.Ret = 200
  1862. br.Success = true
  1863. br.Msg = "获取成功"
  1864. br.Data = resp
  1865. }
  1866. // ChartInfoSearch
  1867. // @Title 图表模糊搜索
  1868. // @Description 图表模糊搜索接口
  1869. // @Param KeyWord query string true "图表名称"
  1870. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1871. // @Success 200 {object} data_manage.ChartInfo
  1872. // @router /chart_info/search [get]
  1873. func (this *ChartInfoController) ChartInfoSearch() {
  1874. br := new(models.BaseResponse).Init()
  1875. defer func() {
  1876. this.Data["json"] = br
  1877. this.ServeJSON()
  1878. }()
  1879. sysUser := this.SysUser
  1880. if sysUser == nil {
  1881. br.Msg = "请登录"
  1882. br.ErrMsg = "请登录,SysUser Is Empty"
  1883. br.Ret = 408
  1884. return
  1885. }
  1886. keyWord := this.GetString("KeyWord")
  1887. searchList := make([]data_manage.ChartInfoMore, 0)
  1888. //var err error
  1889. //if KeyWord == "" {
  1890. // searchList, err = data_manage.ChartInfoSearchByKeyWord(KeyWord)
  1891. // if err != nil && err.Error() != utils.ErrNoRow() {
  1892. // br.Msg = "获取失败"
  1893. // br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1894. // return
  1895. // }
  1896. //} else {
  1897. // searchList, err = data.EsSearchChartInfo(KeyWord)
  1898. // if err != nil && err.Error() != utils.ErrNoRow() {
  1899. // br.Msg = "获取失败"
  1900. // br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1901. // return
  1902. // }
  1903. //}
  1904. //只看我的
  1905. isShowMe, _ := this.GetBool("IsShowMe")
  1906. showSysId := 0
  1907. if isShowMe {
  1908. showSysId = sysUser.AdminId
  1909. }
  1910. var keyWordArr []string
  1911. keyWordArr = append(keyWordArr, keyWord)
  1912. if keyWord != "" {
  1913. newKeyWord := strings.Split(keyWord, " ")
  1914. keyWordArr = append(keyWordArr, newKeyWord...)
  1915. }
  1916. existMap := make(map[int]string)
  1917. for _, v := range keyWordArr {
  1918. v = strings.Replace(v, " ", "%", -1)
  1919. newSearchList, err := data_manage.ChartInfoSearchByKeyWord(v, showSysId)
  1920. if err != nil && err.Error() != utils.ErrNoRow() {
  1921. br.Msg = "获取失败"
  1922. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1923. return
  1924. }
  1925. for _, nv := range newSearchList {
  1926. if _, ok := existMap[nv.ChartInfoId]; !ok {
  1927. searchList = append(searchList, data_manage.ChartInfoMore{
  1928. ChartInfo: *nv,
  1929. })
  1930. }
  1931. existMap[nv.ChartInfoId] = nv.ChartName
  1932. }
  1933. }
  1934. if searchList == nil {
  1935. searchList = make([]data_manage.ChartInfoMore, 0)
  1936. }
  1937. // 数据权限
  1938. if len(searchList) > 0 {
  1939. // 涉及分类id
  1940. classifyIdList := make([]int, 0)
  1941. for _, v := range searchList {
  1942. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  1943. }
  1944. // 图表分类
  1945. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  1946. if err != nil {
  1947. br.Msg = "获取失败"
  1948. br.ErrMsg = "获取图表分类失败,Err:" + err.Error()
  1949. return
  1950. }
  1951. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  1952. for _, v := range chartClassifyList {
  1953. chartClassifyMap[v.ChartClassifyId] = v
  1954. }
  1955. // 已授权图表和分类id
  1956. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  1957. if err != nil {
  1958. br.Msg = "获取失败"
  1959. br.ErrMsg = "已授权图表和分类id失败,Err:" + err.Error()
  1960. return
  1961. }
  1962. // 权限判断
  1963. for k, v := range searchList {
  1964. if currClassify, ok := chartClassifyMap[v.ChartClassifyId]; ok {
  1965. searchList[k].HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.ChartInfoId, v.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  1966. }
  1967. }
  1968. }
  1969. //新增搜索词记录
  1970. {
  1971. searchKeyword := new(data_manage.SearchKeyword)
  1972. searchKeyword.KeyWord = keyWord
  1973. searchKeyword.CreateTime = time.Now()
  1974. go data_manage.AddSearchKeyword(searchKeyword)
  1975. }
  1976. br.Ret = 200
  1977. br.Success = true
  1978. br.Msg = "获取成功"
  1979. br.Data = searchList
  1980. }
  1981. // ChartInfoSearchByEs
  1982. // @Title 图表模糊搜索(从es获取)
  1983. // @Description 图表模糊搜索(从es获取)
  1984. // @Param Keyword query string true "图表名称"
  1985. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1986. // @Success 200 {object} data_manage.ChartInfo
  1987. // @router /chart_info/search_by_es [get]
  1988. func (this *ChartInfoController) ChartInfoSearchByEs() {
  1989. br := new(models.BaseResponse).Init()
  1990. defer func() {
  1991. this.Data["json"] = br
  1992. this.ServeJSON()
  1993. }()
  1994. sysUser := this.SysUser
  1995. if sysUser == nil {
  1996. br.Msg = "请登录"
  1997. br.ErrMsg = "请登录,SysUser Is Empty"
  1998. br.Ret = 408
  1999. return
  2000. }
  2001. pageSize, _ := this.GetInt("PageSize")
  2002. currentIndex, _ := this.GetInt("CurrentIndex")
  2003. var startSize int
  2004. if pageSize <= 0 {
  2005. pageSize = utils.PageSize20
  2006. }
  2007. if currentIndex <= 0 {
  2008. currentIndex = 1
  2009. }
  2010. startSize = paging.StartIndex(currentIndex, pageSize)
  2011. keyword := this.GetString("Keyword")
  2012. //只看我的
  2013. isShowMe, _ := this.GetBool("IsShowMe")
  2014. showSysId := 0
  2015. if isShowMe {
  2016. showSysId = sysUser.AdminId
  2017. }
  2018. var searchList []*data_manage.ChartInfo
  2019. var total int64
  2020. var err error
  2021. // 获取当前账号的不可见指标
  2022. noPermissionChartIdList := make([]int, 0)
  2023. {
  2024. obj := data_manage.EdbInfoNoPermissionAdmin{}
  2025. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  2026. if err != nil && err.Error() != utils.ErrNoRow() {
  2027. br.Msg = "获取失败"
  2028. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2029. return
  2030. }
  2031. for _, v := range confList {
  2032. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  2033. }
  2034. }
  2035. // 是否走ES
  2036. isEs := false
  2037. if keyword != "" {
  2038. searchList, total, err = data.EsSearchChartInfo(keyword, showSysId, []int{utils.CHART_SOURCE_DEFAULT}, noPermissionChartIdList, startSize, pageSize)
  2039. isEs = true
  2040. } else {
  2041. total, searchList, err = data_manage.ChartInfoSearchByEmptyKeyWord(showSysId, []int{utils.CHART_SOURCE_DEFAULT}, noPermissionChartIdList, startSize, pageSize)
  2042. if err != nil && err.Error() != utils.ErrNoRow() {
  2043. br.Msg = "获取失败"
  2044. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2045. return
  2046. }
  2047. }
  2048. finalList := make([]*data_manage.ChartInfoMore, 0)
  2049. if len(searchList) > 0 {
  2050. // 涉及分类id
  2051. classifyIdList := make([]int, 0)
  2052. // 图表id
  2053. chartInfoIdList := make([]int, 0)
  2054. chartInfoIds := ""
  2055. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  2056. for _, v := range searchList {
  2057. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  2058. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  2059. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  2060. }
  2061. if chartInfoIds != "" {
  2062. chartInfoIds = strings.Trim(chartInfoIds, ",")
  2063. //判断是否需要展示英文标识
  2064. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  2065. if e != nil {
  2066. br.Msg = "获取失败"
  2067. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  2068. return
  2069. }
  2070. for _, v := range edbList {
  2071. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  2072. }
  2073. }
  2074. // 当前列表中的分类map
  2075. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  2076. // 图表分类
  2077. {
  2078. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  2079. if err != nil {
  2080. br.Msg = "获取失败"
  2081. br.ErrMsg = "获取图表分类失败,Err:" + err.Error()
  2082. return
  2083. }
  2084. for _, v := range chartClassifyList {
  2085. chartClassifyMap[v.ChartClassifyId] = v
  2086. }
  2087. }
  2088. // 图表
  2089. if isEs {
  2090. // 当前列表中的图表map
  2091. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  2092. tmpChartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  2093. if err != nil {
  2094. br.Msg = "获取失败"
  2095. br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error()
  2096. return
  2097. }
  2098. for _, tmpChartInfo := range tmpChartList {
  2099. chartInfoMap[tmpChartInfo.ChartInfoId] = tmpChartInfo
  2100. }
  2101. for _, v := range searchList {
  2102. // 图表权限
  2103. chartViewInfo, ok := chartInfoMap[v.ChartInfoId]
  2104. if !ok {
  2105. continue
  2106. }
  2107. v.IsJoinPermission = chartViewInfo.IsJoinPermission
  2108. }
  2109. }
  2110. // 已授权图表和分类id
  2111. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  2112. if err != nil {
  2113. br.Msg = "获取失败"
  2114. br.ErrMsg = "已授权图表和分类id失败,Err:" + err.Error()
  2115. return
  2116. }
  2117. for _, v := range searchList {
  2118. tmp := new(data_manage.ChartInfoMore)
  2119. tmp.ChartInfo = *v
  2120. //判断是否需要展示英文标识
  2121. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  2122. tmp.IsEnChart = data.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  2123. }
  2124. // 图表权限
  2125. if currClassify, ok := chartClassifyMap[v.ChartClassifyId]; ok {
  2126. tmp.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.ChartInfoId, v.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2127. }
  2128. finalList = append(finalList, tmp)
  2129. }
  2130. }
  2131. //新增搜索词记录
  2132. {
  2133. searchKeyword := new(data_manage.SearchKeyword)
  2134. searchKeyword.KeyWord = keyword
  2135. searchKeyword.CreateTime = time.Now()
  2136. go data_manage.AddSearchKeyword(searchKeyword)
  2137. }
  2138. page := paging.GetPaging(currentIndex, pageSize, int(total))
  2139. resp := data_manage.ChartInfoListByEsResp{
  2140. Paging: page,
  2141. List: finalList,
  2142. }
  2143. br.Ret = 200
  2144. br.Success = true
  2145. br.Msg = "获取成功"
  2146. br.Data = resp
  2147. }
  2148. // @Title 图表刷新接口
  2149. // @Description 图表刷新接口
  2150. // @Param ChartInfoId query int true "图表id"
  2151. // @Param UniqueCode query string true "唯一code"
  2152. // @Success Ret=200 刷新成功
  2153. // @router /chart_info/refresh [get]
  2154. func (this *EdbInfoController) ChartInfoRefresh() {
  2155. br := new(models.BaseResponse).Init()
  2156. defer func() {
  2157. this.Data["json"] = br
  2158. this.ServeJSON()
  2159. }()
  2160. sysUser := this.SysUser
  2161. if sysUser == nil {
  2162. br.Msg = "请登录"
  2163. br.ErrMsg = "请登录,SysUser Is Empty"
  2164. br.Ret = 408
  2165. return
  2166. }
  2167. chartInfoId, _ := this.GetInt("ChartInfoId")
  2168. uniqueCode := this.GetString("UniqueCode")
  2169. if chartInfoId <= 0 && uniqueCode == `` {
  2170. br.Msg = "参数错误"
  2171. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  2172. return
  2173. }
  2174. var chartInfo *data_manage.ChartInfo
  2175. var err error
  2176. if chartInfoId > 0 {
  2177. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  2178. } else {
  2179. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  2180. }
  2181. if err != nil {
  2182. if err.Error() == utils.ErrNoRow() {
  2183. br.Msg = "图表已被删除,无需刷新"
  2184. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  2185. return
  2186. }
  2187. br.Msg = "刷新失败"
  2188. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2189. return
  2190. }
  2191. err, isAsync := data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  2192. if err != nil {
  2193. br.Msg = "刷新失败"
  2194. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2195. return
  2196. }
  2197. /*
  2198. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  2199. //if err != nil {
  2200. // br.Msg = "刷新失败"
  2201. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2202. // return
  2203. //}
  2204. */
  2205. //清除图表缓存
  2206. {
  2207. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  2208. _ = utils.Rc.Delete(key)
  2209. }
  2210. br.Ret = 200
  2211. br.Success = true
  2212. br.Msg = "刷新成功"
  2213. if isAsync {
  2214. br.Msg = "图表关联指标较多,请10分钟后刷新页面查看最新数据"
  2215. }
  2216. }
  2217. // @Title 设置图表图片
  2218. // @Description 设置图表图片接口
  2219. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  2220. // @Success Ret=200 保存成功
  2221. // @router /chart_info/image/set [post]
  2222. func (this *ChartInfoController) ChartInfoImageSet() {
  2223. br := new(models.BaseResponse).Init()
  2224. defer func() {
  2225. this.Data["json"] = br
  2226. this.ServeJSON()
  2227. }()
  2228. sysUser := this.SysUser
  2229. if sysUser == nil {
  2230. br.Msg = "请登录"
  2231. br.ErrMsg = "请登录,SysUser Is Empty"
  2232. br.Ret = 408
  2233. return
  2234. }
  2235. var req data_manage.SetChartInfoImageReq
  2236. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2237. if err != nil {
  2238. br.Msg = "参数解析异常!"
  2239. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2240. return
  2241. }
  2242. if req.ChartInfoId > 0 && req.ImageUrl != "" {
  2243. err = data_manage.EditChartInfoImage(&req)
  2244. if err != nil {
  2245. br.Msg = "保存失败"
  2246. br.ErrMsg = "保存失败,Err:" + err.Error()
  2247. return
  2248. }
  2249. //修改es数据
  2250. go data.EsAddOrEditChartInfo(req.ChartInfoId)
  2251. //修改my eta es数据
  2252. go data.EsAddOrEditMyChartInfoByChartInfoId(req.ChartInfoId)
  2253. }
  2254. br.Ret = 200
  2255. br.Success = true
  2256. br.Msg = "保存成功"
  2257. }
  2258. // ChartInfoDetailFromUniqueCode
  2259. // @Title 根据编码获取图表详情
  2260. // @Description 根据编码获取图表详情接口
  2261. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  2262. // @Param IsCache query bool true "是否走缓存,默认false"
  2263. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  2264. // @router /chart_info/detail/from_unique_code [get]
  2265. func (this *ChartInfoController) ChartInfoDetailFromUniqueCode() {
  2266. br := new(models.BaseResponse).Init()
  2267. defer func() {
  2268. this.Data["json"] = br
  2269. this.ServeJSON()
  2270. }()
  2271. sysUser := this.SysUser
  2272. if sysUser == nil {
  2273. br.Msg = "请登录"
  2274. br.ErrMsg = "请登录,SysUser Is Empty"
  2275. br.Ret = 408
  2276. return
  2277. }
  2278. uniqueCode := this.GetString("UniqueCode")
  2279. if uniqueCode == "" {
  2280. br.Msg = "参数错误"
  2281. br.ErrMsg = "参数错误,uniqueCode is empty"
  2282. return
  2283. }
  2284. //是否走缓存
  2285. isCache, _ := this.GetBool("IsCache")
  2286. status := true
  2287. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  2288. if err != nil {
  2289. if err.Error() == utils.ErrNoRow() {
  2290. status = false
  2291. } else {
  2292. br.Msg = "获取失败"
  2293. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2294. return
  2295. }
  2296. }
  2297. if chartInfo == nil {
  2298. status = false
  2299. }
  2300. if !status {
  2301. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  2302. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  2303. resp.EdbInfoList = endInfoList
  2304. resp.ChartInfo = chartInfo
  2305. resp.Status = false
  2306. return
  2307. }
  2308. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  2309. if !isOk {
  2310. br.Msg = msg
  2311. br.ErrMsg = errMsg
  2312. return
  2313. }
  2314. br.Ret = 200
  2315. br.Success = true
  2316. msg = "获取成功"
  2317. br.Data = resp
  2318. }
  2319. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  2320. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  2321. msg = `获取失败`
  2322. adminId := sysUser.AdminId
  2323. // 图表数据权限
  2324. // 图表分类
  2325. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  2326. if err != nil {
  2327. errMsg = "获取图表分类信息失败,Err:" + err.Error()
  2328. return
  2329. }
  2330. // 已授权分类id
  2331. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  2332. if err != nil {
  2333. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  2334. return
  2335. }
  2336. // 指标数据map
  2337. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  2338. defer func() {
  2339. if isOk {
  2340. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  2341. {
  2342. //判断是否加入我的图库
  2343. var myChartCondition string
  2344. var myChartPars []interface{}
  2345. myChartCondition += ` AND a.admin_id=? `
  2346. myChartPars = append(myChartPars, adminId)
  2347. myChartCondition += ` AND a.chart_info_id=? `
  2348. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  2349. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  2350. if err != nil && err.Error() != utils.ErrNoRow() {
  2351. msg = "获取失败"
  2352. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  2353. return
  2354. }
  2355. if myChartList != nil && len(myChartList) > 0 {
  2356. chartInfo.IsAdd = true
  2357. chartInfo.MyChartId = myChartList[0].MyChartId
  2358. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  2359. }
  2360. }
  2361. // 图表权限校验
  2362. {
  2363. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2364. //图表操作权限
  2365. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  2366. chartInfo.Button = data_manage.ChartViewButton{
  2367. IsEdit: chartInfo.IsEdit,
  2368. IsEnChart: chartInfo.IsEnChart,
  2369. IsAdd: chartInfo.IsAdd,
  2370. IsCopy: true,
  2371. IsSetName: chartInfo.IsSetName,
  2372. }
  2373. //if !resp.ChartInfo.HaveOperaAuth {
  2374. // for _, v := range resp.EdbInfoList {
  2375. // v.DataList = nil
  2376. // }
  2377. // resp.DataResp = nil
  2378. // resp.XEdbIdValue = []int{}
  2379. // resp.YDataList = []data_manage.YData{}
  2380. //}
  2381. }
  2382. // 指标权限
  2383. {
  2384. classifyMap := make(map[int]*data_manage.EdbClassify)
  2385. // 分类
  2386. {
  2387. classifyIdList := make([]int, 0)
  2388. for _, v := range resp.EdbInfoList {
  2389. classifyIdList = append(classifyIdList, v.ClassifyId)
  2390. }
  2391. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2392. if tmpErr != nil {
  2393. errMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  2394. return
  2395. }
  2396. for _, v := range classifyList {
  2397. classifyMap[v.ClassifyId] = v
  2398. }
  2399. }
  2400. // 指标
  2401. if len(edbClassifyPermissionMap) < 0 {
  2402. edbInfoIdList := make([]int, 0)
  2403. for _, v := range resp.EdbInfoList {
  2404. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  2405. }
  2406. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  2407. if tmpErr != nil {
  2408. errMsg = "获取指标列表失败,Err:" + tmpErr.Error()
  2409. return
  2410. }
  2411. for _, v := range edbInfoList {
  2412. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2413. ClassifyId: v.ClassifyId,
  2414. IsJoinPermission: v.IsJoinPermission,
  2415. EdbInfoId: v.EdbInfoId,
  2416. }
  2417. }
  2418. }
  2419. // 获取所有有权限的指标和分类
  2420. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  2421. if err != nil {
  2422. errMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2423. return
  2424. }
  2425. for _, v := range resp.EdbInfoList {
  2426. // 数据权限
  2427. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  2428. if !ok {
  2429. continue
  2430. }
  2431. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  2432. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2433. }
  2434. }
  2435. }
  2436. }
  2437. }()
  2438. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  2439. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  2440. if utils.Re == nil && isCache {
  2441. if utils.Re == nil && utils.Rc.IsExist(key) {
  2442. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  2443. err := json.Unmarshal(redisData, &resp)
  2444. if err != nil || resp == nil {
  2445. return
  2446. }
  2447. isOk = true
  2448. fmt.Println("source redis")
  2449. return
  2450. }
  2451. }
  2452. }
  2453. // 获取主题样式
  2454. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  2455. if err != nil {
  2456. msg = "获取失败"
  2457. errMsg = "获取主题信息失败,Err:" + err.Error()
  2458. return
  2459. }
  2460. chartInfo.ChartThemeStyle = chartTheme.Config
  2461. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  2462. chartInfoId := chartInfo.ChartInfoId
  2463. dateType := chartInfo.DateType
  2464. startDate := chartInfo.StartDate
  2465. endDate := chartInfo.EndDate
  2466. startYear := chartInfo.StartYear
  2467. calendar := chartInfo.Calendar
  2468. chartType := chartInfo.ChartType
  2469. if calendar == "" {
  2470. calendar = "公历"
  2471. }
  2472. fmt.Println("a dateType:", dateType)
  2473. mappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  2474. if err != nil {
  2475. msg = "获取失败"
  2476. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2477. return
  2478. }
  2479. yearMax := 0
  2480. if dateType == utils.DateTypeNYears {
  2481. for _, v := range mappingList {
  2482. if v.LatestDate != "" {
  2483. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  2484. if tErr != nil {
  2485. msg = "获取失败"
  2486. errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  2487. return
  2488. }
  2489. if lastDateT.Year() > yearMax {
  2490. yearMax = lastDateT.Year()
  2491. }
  2492. }
  2493. }
  2494. }
  2495. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  2496. extraConfigStr := chartInfo.ExtraConfig //图表额外数据参数
  2497. var barConfig data_manage.BarChartInfoReq
  2498. // 柱方图的一些配置
  2499. if chartInfo != nil && chartInfo.ChartType == 7 {
  2500. if chartInfo.BarConfig == `` {
  2501. msg = "柱方图未配置"
  2502. errMsg = "柱方图未配置"
  2503. return
  2504. }
  2505. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  2506. if err != nil {
  2507. msg = "柱方图配置异常"
  2508. errMsg = "柱方图配置异常"
  2509. return
  2510. }
  2511. extraConfigStr = chartInfo.BarConfig
  2512. }
  2513. // 获取图表中的指标数据
  2514. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  2515. if err != nil {
  2516. msg = "获取失败"
  2517. if errMsg != `` {
  2518. msg = errMsg
  2519. }
  2520. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2521. return
  2522. }
  2523. // 单位
  2524. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  2525. chartInfo.Unit = yDataList[0].Unit
  2526. chartInfo.UnitEn = yDataList[0].UnitEn
  2527. }
  2528. warnEdbList := make([]string, 0)
  2529. for _, v := range edbList {
  2530. if v.IsNullData {
  2531. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  2532. }
  2533. }
  2534. if len(warnEdbList) > 0 {
  2535. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  2536. }
  2537. /*
  2538. ChartClassify []*ChartClassifyView
  2539. */
  2540. chartViewClassify, _ := data_manage.GetChartClassifyViewById(chartInfo.ChartClassifyId)
  2541. if chartViewClassify != nil {
  2542. chartClassifyParent, _ := data_manage.GetChartClassifyViewById(chartClassify.ParentId)
  2543. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassifyParent)
  2544. }
  2545. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartViewClassify)
  2546. // 面积图 面积堆积 数据处理
  2547. if chartInfo.ChartType == utils.CHART_TYPE_AREA {
  2548. err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
  2549. if err != nil {
  2550. msg = "获取失败"
  2551. errMsg = "面积图面积堆积数据处理失败,Err:" + err.Error()
  2552. return
  2553. }
  2554. }
  2555. resp.EdbInfoList = edbList
  2556. //判断是否需要展示英文标识
  2557. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  2558. // 图表的指标来源
  2559. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  2560. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  2561. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  2562. resp.ChartInfo = chartInfo
  2563. resp.XEdbIdValue = xEdbIdValue
  2564. resp.YDataList = yDataList
  2565. resp.Status = true
  2566. resp.BarChartInfo = barConfig
  2567. resp.DataResp = dataResp
  2568. // 将数据加入缓存
  2569. if utils.Re == nil {
  2570. data, _ := json.Marshal(resp)
  2571. utils.Rc.Put(key, data, 2*time.Hour)
  2572. }
  2573. isOk = true
  2574. // 遍历到校验map
  2575. for _, v := range edbList {
  2576. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2577. ClassifyId: v.ClassifyId,
  2578. IsJoinPermission: v.IsJoinPermission,
  2579. EdbInfoId: v.EdbInfoId,
  2580. }
  2581. }
  2582. return
  2583. }
  2584. // @Param Img query string true "图片"
  2585. // @Success 200 {object} models.ResourceResp
  2586. // @router /chart_info/base64Upload [post]
  2587. func (this *ChartInfoController) ChartInfoBase64Upload() {
  2588. br := new(models.BaseResponse).Init()
  2589. defer func() {
  2590. this.Data["json"] = br
  2591. this.ServeJSON()
  2592. }()
  2593. imgData := this.GetString("Img")
  2594. if imgData == "" {
  2595. br.Msg = "图片参数错误"
  2596. br.ErrMsg = "图片参数错误,Img Is Empty"
  2597. return
  2598. }
  2599. resp := new(models.ResourceResp)
  2600. uploadDir := "static/images/"
  2601. if !utils.FileIsExist(uploadDir) {
  2602. err := os.MkdirAll(uploadDir, utils.DIR_MOD)
  2603. if err != nil {
  2604. br.Msg = "图表保存失败"
  2605. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  2606. return
  2607. }
  2608. }
  2609. //var saveToOssPath string
  2610. randStr := utils.GetRandStringNoSpecialChar(28)
  2611. var fileName, outFileName string
  2612. fileName = randStr + ".txt"
  2613. fileName = uploadDir + fileName
  2614. err := utils.SaveToFile(imgData, fileName)
  2615. if err != nil {
  2616. br.Msg = "图片保存失败"
  2617. br.ErrMsg = "图片保存失败,Err:" + err.Error()
  2618. return
  2619. }
  2620. outFileName = randStr + ".png"
  2621. doneChannel := make(chan bool, 1)
  2622. errorChannel := make(chan error, 1)
  2623. cmd := exec.Command("highcharts-export-server", "--infile", fileName, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "3", "--outfile", outFileName)
  2624. go func() {
  2625. output, err := cmd.CombinedOutput()
  2626. if err != nil {
  2627. utils.FileLog.Info("execute command failed, output: , error: \n" + string(output) + err.Error())
  2628. errorChannel <- err
  2629. return
  2630. }
  2631. doneChannel <- true
  2632. }()
  2633. select {
  2634. case <-time.After(30 * time.Second):
  2635. utils.FileLog.Info("执行超过30秒 杀死超时进程")
  2636. e := cmd.Process.Kill()
  2637. if e != nil {
  2638. fmt.Println("cmd kill err: ", e.Error())
  2639. utils.FileLog.Info(fmt.Sprintf("cmd kill err: %s", e.Error()))
  2640. br.Msg = "图片生成失败"
  2641. br.ErrMsg = "图片生成失败, 执行超时" + e.Error()
  2642. return
  2643. }
  2644. fmt.Println("timeout kill process")
  2645. case <-doneChannel:
  2646. fmt.Println("done")
  2647. case err := <-errorChannel:
  2648. br.Msg = "文件上传失败"
  2649. br.ErrMsg = fmt.Sprintf("execute command failure err: %s", err.Error())
  2650. fmt.Println("execute command failure err:" + err.Error())
  2651. return
  2652. }
  2653. defer func() {
  2654. os.Remove(fileName)
  2655. os.Remove(outFileName)
  2656. }()
  2657. //saveToOssPath = uploadDir + time.Now().Format("200601/20060102/")
  2658. //saveToOssPath += outFileName
  2659. //上传到阿里云 和 minio
  2660. resourceUrl := ``
  2661. //if utils.ObjectStorageClient == "minio" {
  2662. // err = services.UploadFileToMinIo("", outFileName, saveToOssPath)
  2663. // if err != nil {
  2664. // br.Msg = "文件上传失败"
  2665. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2666. // return
  2667. // }
  2668. // resourceUrl = utils.MinIoImghost + saveToOssPath
  2669. //} else {
  2670. // err = services.UploadFileToAliyun("", outFileName, saveToOssPath)
  2671. // if err != nil {
  2672. // br.Msg = "文件上传失败"
  2673. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2674. // return
  2675. // }
  2676. // resourceUrl = utils.Imghost + saveToOssPath
  2677. //}
  2678. ossClient := services.NewOssClient()
  2679. if ossClient == nil {
  2680. br.Msg = "上传失败"
  2681. br.ErrMsg = "初始化OSS服务失败"
  2682. return
  2683. }
  2684. resourceUrl, err = ossClient.UploadFile(outFileName, outFileName, "")
  2685. if err != nil {
  2686. br.Msg = "文件上传失败"
  2687. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2688. return
  2689. }
  2690. resp.ResourceUrl = resourceUrl
  2691. resp.Source = "convert"
  2692. //resp.CacheKey = imgDataKey
  2693. br.Msg = "上传成功"
  2694. br.Ret = 200
  2695. br.Success = true
  2696. br.Data = resp
  2697. return
  2698. }
  2699. // BatchChartInfoRefresh
  2700. // @Title 批量刷新图表接口
  2701. // @Description 批量刷新图表接口
  2702. // @Param request body data_manage.BatchChartRefreshReq true "type json string"
  2703. // @Success Ret=200 刷新成功
  2704. // @router /chart_info/batch_refresh [post]
  2705. func (this *EdbInfoController) BatchChartInfoRefresh() {
  2706. br := new(models.BaseResponse).Init()
  2707. defer func() {
  2708. this.Data["json"] = br
  2709. this.ServeJSON()
  2710. }()
  2711. sysUser := this.SysUser
  2712. if sysUser == nil {
  2713. br.Msg = "请登录"
  2714. br.ErrMsg = "请登录,SysUser Is Empty"
  2715. br.Ret = 408
  2716. return
  2717. }
  2718. var req data_manage.BatchChartRefreshReq
  2719. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2720. if err != nil {
  2721. br.Msg = "参数解析异常!"
  2722. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2723. return
  2724. }
  2725. //获取所有的图表列表
  2726. _, chartInfoList, err := data_manage.GetChartInfoListByUniqueCodeSlice(req.ChartInfoCode)
  2727. if len(chartInfoList) <= 0 {
  2728. br.Ret = 200
  2729. br.Success = true
  2730. br.Msg = "刷新成功"
  2731. return
  2732. }
  2733. redisKey := data.GetBatchChartRefreshKey(req.Source, req.ReportId, req.ReportChapterId)
  2734. // 图表中的指标刷新
  2735. err, isAsync := data.BatchChartInfoRefreshV2(chartInfoList, redisKey)
  2736. if err != nil {
  2737. br.Msg = "刷新失败"
  2738. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2739. return
  2740. }
  2741. br.Msg = "刷新成功"
  2742. if isAsync {
  2743. br.Msg = "研报中图表关联指标较多,请10分钟后刷新页面查看最新数据"
  2744. }
  2745. br.Ret = 200
  2746. br.Success = true
  2747. }
  2748. // CopyChartInfo
  2749. // @Title 复制并新增图表接口
  2750. // @Description 新增图表接口
  2751. // @Param request body data_manage.CopyAddChartInfoReq true "type json string"
  2752. // @Success 200 {object} data_manage.AddChartInfoResp
  2753. // @router /chart_info/copy [post]
  2754. func (this *ChartInfoController) CopyChartInfo() {
  2755. br := new(models.BaseResponse).Init()
  2756. defer func() {
  2757. this.Data["json"] = br
  2758. this.ServeJSON()
  2759. }()
  2760. sysUser := this.SysUser
  2761. if sysUser == nil {
  2762. br.Msg = "请登录"
  2763. br.ErrMsg = "请登录,SysUser Is Empty"
  2764. br.Ret = 408
  2765. return
  2766. }
  2767. deleteCache := true
  2768. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  2769. defer func() {
  2770. if deleteCache {
  2771. utils.Rc.Delete(cacheKey)
  2772. }
  2773. }()
  2774. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2775. deleteCache = false
  2776. br.Msg = "系统处理中,请稍后重试!"
  2777. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  2778. return
  2779. }
  2780. var req data_manage.CopyAddChartInfoReq
  2781. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2782. if err != nil {
  2783. br.Msg = "参数解析异常!"
  2784. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2785. return
  2786. }
  2787. // 获取原图表信息
  2788. oldChartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  2789. if err != nil {
  2790. br.Msg = "获取原图表信息失败"
  2791. br.ErrMsg = "获取原图表信息失败,Err:" + err.Error()
  2792. return
  2793. }
  2794. //校验图表名称是否重复
  2795. req.ChartName = strings.Trim(req.ChartName, " ")
  2796. if req.ChartName == "" {
  2797. br.Msg = "请填写图表名称!"
  2798. return
  2799. }
  2800. if req.ChartClassifyId <= 0 {
  2801. br.Msg = "分类参数错误!"
  2802. return
  2803. }
  2804. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  2805. if err != nil {
  2806. if err.Error() == utils.ErrNoRow() {
  2807. br.Msg = "分类不存在"
  2808. br.ErrMsg = "分类不存在"
  2809. return
  2810. }
  2811. br.Msg = "获取分类信息失败"
  2812. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  2813. return
  2814. }
  2815. if chartClassify == nil {
  2816. br.Msg = "分类不存在"
  2817. br.ErrMsg = "分类不存在"
  2818. return
  2819. }
  2820. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  2821. //if err != nil && err.Error() != utils.ErrNoRow() {
  2822. // br.Msg = "保存失败"
  2823. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2824. // return
  2825. //}
  2826. //if count > 0 {
  2827. // br.Msg = "所选指标已存在相同指标"
  2828. // return
  2829. //}
  2830. //判断图表是否存在
  2831. var condition string
  2832. var pars []interface{}
  2833. condition += " AND chart_classify_id=? "
  2834. pars = append(pars, req.ChartClassifyId)
  2835. condition += " AND chart_name=? AND source = ? "
  2836. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  2837. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  2838. if err != nil {
  2839. br.Msg = "判断图表名称是否存在失败"
  2840. br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2841. return
  2842. }
  2843. if count > 0 {
  2844. br.Msg = "图表已存在,请重新填写"
  2845. br.IsSendEmail = false
  2846. return
  2847. }
  2848. //获取原图表关联的指标信息列表
  2849. edbMappingList, err := data_manage.GetChartEdbMappingList(req.ChartInfoId)
  2850. if err != nil {
  2851. br.Msg = "获取图表关联的指标信息失败!"
  2852. return
  2853. }
  2854. //添加图表
  2855. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2856. chartInfo := &data_manage.ChartInfo{
  2857. ChartInfoId: 0,
  2858. ChartName: req.ChartName,
  2859. ChartClassifyId: req.ChartClassifyId,
  2860. SysUserId: sysUser.AdminId,
  2861. SysUserRealName: sysUser.RealName,
  2862. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  2863. CreateTime: time.Now(),
  2864. ModifyTime: time.Now(),
  2865. DateType: oldChartInfo.DateType,
  2866. StartDate: oldChartInfo.StartDate,
  2867. EndDate: oldChartInfo.EndDate,
  2868. IsSetName: oldChartInfo.IsSetName,
  2869. EdbInfoIds: oldChartInfo.EdbInfoIds,
  2870. ChartType: oldChartInfo.ChartType,
  2871. Calendar: oldChartInfo.Calendar,
  2872. SeasonStartDate: oldChartInfo.SeasonStartDate,
  2873. SeasonEndDate: oldChartInfo.SeasonEndDate,
  2874. ChartImage: oldChartInfo.ChartImage,
  2875. BarConfig: oldChartInfo.BarConfig,
  2876. //Sort: sort,
  2877. LeftMin: oldChartInfo.LeftMin,
  2878. LeftMax: oldChartInfo.LeftMax,
  2879. RightMin: oldChartInfo.RightMin,
  2880. RightMax: oldChartInfo.RightMax,
  2881. Right2Min: oldChartInfo.Right2Min,
  2882. Right2Max: oldChartInfo.Right2Max,
  2883. Disabled: oldChartInfo.Disabled,
  2884. Source: oldChartInfo.Source,
  2885. ExtraConfig: oldChartInfo.ExtraConfig,
  2886. SeasonExtraConfig: oldChartInfo.SeasonExtraConfig,
  2887. StartYear: oldChartInfo.StartYear,
  2888. Unit: oldChartInfo.Unit,
  2889. UnitEn: oldChartInfo.UnitEn,
  2890. ChartThemeId: oldChartInfo.ChartThemeId,
  2891. SourcesFrom: oldChartInfo.SourcesFrom,
  2892. Instructions: oldChartInfo.Instructions,
  2893. MarkersLines: oldChartInfo.MarkersLines,
  2894. MarkersAreas: oldChartInfo.MarkersAreas,
  2895. }
  2896. newId, err := data_manage.AddChartInfo(chartInfo)
  2897. if err != nil {
  2898. br.Msg = "保存失败"
  2899. br.ErrMsg = "保存失败,Err:" + err.Error()
  2900. return
  2901. }
  2902. chartInfo.ChartInfoId = int(newId)
  2903. // 添加图表与指标的关联关系
  2904. {
  2905. mapList := make([]*data_manage.ChartEdbMapping, 0)
  2906. for _, v := range edbMappingList {
  2907. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2908. mapItem := &data_manage.ChartEdbMapping{
  2909. //ChartEdbMappingId: 0,
  2910. ChartInfoId: chartInfo.ChartInfoId,
  2911. EdbInfoId: v.EdbInfoId,
  2912. CreateTime: time.Now(),
  2913. ModifyTime: time.Now(),
  2914. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  2915. MaxData: v.MaxData,
  2916. MinData: v.MinData,
  2917. IsOrder: v.IsOrder,
  2918. IsAxis: v.IsAxis,
  2919. EdbInfoType: v.EdbInfoType,
  2920. LeadValue: v.LeadValue,
  2921. LeadUnit: v.LeadUnit,
  2922. ChartStyle: v.ChartStyle,
  2923. ChartColor: v.ChartColor,
  2924. ChartWidth: v.ChartWidth,
  2925. Source: v.Source,
  2926. EdbAliasName: v.EdbAliasName,
  2927. IsConvert: v.IsConvert,
  2928. ConvertType: v.ConvertType,
  2929. ConvertValue: v.ConvertValue,
  2930. ConvertUnit: v.ConvertEnUnit,
  2931. ConvertEnUnit: v.ConvertEnUnit,
  2932. }
  2933. mapList = append(mapList, mapItem)
  2934. }
  2935. err = data_manage.AddChartEdbMapping(mapList)
  2936. if err != nil {
  2937. br.Msg = "保存失败"
  2938. br.ErrMsg = "保存失败,Err:" + err.Error()
  2939. return
  2940. }
  2941. }
  2942. //添加es数据
  2943. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  2944. //修改my eta es数据
  2945. go data.EsAddOrEditMyChartInfoByChartInfoId(chartInfo.ChartInfoId)
  2946. //新增操作日志
  2947. {
  2948. chartLog := new(data_manage.ChartInfoLog)
  2949. chartLog.ChartInfoId = chartInfo.ChartInfoId
  2950. chartLog.ChartName = req.ChartName
  2951. chartLog.ChartClassifyId = req.ChartClassifyId
  2952. chartLog.SysUserId = sysUser.AdminId
  2953. chartLog.SysUserRealName = sysUser.RealName
  2954. chartLog.UniqueCode = chartInfo.UniqueCode
  2955. chartLog.CreateTime = time.Now()
  2956. chartLog.Content = string(this.Ctx.Input.RequestBody)
  2957. chartLog.Status = "新增图表"
  2958. chartLog.Method = this.Ctx.Input.URI()
  2959. go data_manage.AddChartInfoLog(chartLog)
  2960. }
  2961. // 试用平台更新用户累计新增图表数
  2962. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  2963. if e != nil {
  2964. br.Msg = "操作失败"
  2965. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  2966. return
  2967. }
  2968. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  2969. go func() {
  2970. var r etaTrialService.EtaTrialUserReq
  2971. r.Mobile = adminItem.Mobile
  2972. _, _ = etaTrialService.UpdateUserChartNum(r)
  2973. }()
  2974. }
  2975. br.Ret = 200
  2976. br.Success = true
  2977. br.Msg = "保存成功"
  2978. br.Data = data_manage.AddChartInfoResp{
  2979. ChartInfoId: chartInfo.ChartInfoId,
  2980. UniqueCode: chartInfo.UniqueCode,
  2981. ChartType: chartInfo.ChartType,
  2982. }
  2983. br.IsAddLog = true
  2984. }
  2985. // PreviewBarChartInfo
  2986. // @Title 图表-获取预览的柱方图数据
  2987. // @Description 图表-获取指标详情接口
  2988. // @Param request body data_manage.BarChartInfoReq true "type json string"
  2989. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  2990. // @router /chart_info/preview_bar [post]
  2991. func (this *ChartInfoController) PreviewBarChartInfo() {
  2992. br := new(models.BaseResponse).Init()
  2993. defer func() {
  2994. this.Data["json"] = br
  2995. this.ServeJSON()
  2996. }()
  2997. var req data_manage.BarChartInfoReq
  2998. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2999. if err != nil {
  3000. br.Msg = "参数解析异常!"
  3001. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3002. return
  3003. }
  3004. sysUser := this.SysUser
  3005. if sysUser == nil {
  3006. br.Msg = "请登录"
  3007. br.ErrMsg = "请登录,SysUser Is Empty"
  3008. br.Ret = 408
  3009. return
  3010. }
  3011. if len(req.EdbInfoIdList) <= 0 {
  3012. br.Msg = "请选择指标"
  3013. return
  3014. }
  3015. if len(req.DateList) <= 0 {
  3016. br.Msg = "请选择日期"
  3017. return
  3018. }
  3019. chartInfo := new(data_manage.ChartInfoView)
  3020. chartType := utils.CHART_TYPE_BAR
  3021. //edbInfoIdList := make([]int, 0)
  3022. edbInfoIdsList := make([]string, 0)
  3023. convertMap := make(map[int]*data_manage.ChartEdbInfoMapping, 0)
  3024. for _, v := range req.EdbInfoIdList {
  3025. edbInfoIdsList = append(edbInfoIdsList, strconv.Itoa(v.EdbInfoId))
  3026. convertMap[v.EdbInfoId] = &data_manage.ChartEdbInfoMapping{
  3027. IsConvert: v.IsConvert,
  3028. ConvertType: v.ConvertType,
  3029. ConvertValue: v.ConvertValue,
  3030. ConvertUnit: v.ConvertUnit,
  3031. ConvertEnUnit: v.ConvertEnUnit,
  3032. }
  3033. }
  3034. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoId(strings.Join(edbInfoIdsList, ","))
  3035. if err != nil {
  3036. br.Msg = "获取失败"
  3037. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3038. return
  3039. }
  3040. for i, item := range mappingList {
  3041. if _, ok := convertMap[item.EdbInfoId]; ok {
  3042. mappingList[i].ConvertValue = convertMap[item.EdbInfoId].ConvertValue
  3043. mappingList[i].ConvertUnit = convertMap[item.EdbInfoId].ConvertUnit
  3044. mappingList[i].ConvertEnUnit = convertMap[item.EdbInfoId].ConvertEnUnit
  3045. mappingList[i].ConvertType = convertMap[item.EdbInfoId].ConvertType
  3046. mappingList[i].IsConvert = convertMap[item.EdbInfoId].IsConvert
  3047. }
  3048. }
  3049. // 获取图表中的指标数据
  3050. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, string(this.Ctx.Input.RequestBody), "")
  3051. if err != nil {
  3052. br.Msg = "获取失败"
  3053. if errMsg != `` {
  3054. br.Msg = errMsg
  3055. }
  3056. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3057. return
  3058. }
  3059. // 单位
  3060. if len(yDataList) > 0 {
  3061. chartInfo.Unit = yDataList[0].Unit
  3062. chartInfo.UnitEn = yDataList[0].UnitEn
  3063. }
  3064. warnEdbList := make([]string, 0)
  3065. for _, v := range edbList {
  3066. if v.IsNullData {
  3067. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3068. }
  3069. for _, reqEdb := range req.EdbInfoIdList {
  3070. if v.EdbInfoId == reqEdb.EdbInfoId {
  3071. v.EdbAliasName = reqEdb.Name
  3072. }
  3073. }
  3074. }
  3075. if len(warnEdbList) > 0 {
  3076. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3077. }
  3078. // 图表的指标来源
  3079. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3080. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3081. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3082. //图表操作权限
  3083. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3084. //判断是否需要展示英文标识
  3085. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  3086. chartInfo.Button = data_manage.ChartViewButton{
  3087. IsEdit: chartInfo.IsEdit,
  3088. IsEnChart: chartInfo.IsEnChart,
  3089. IsAdd: chartInfo.IsAdd,
  3090. IsCopy: true,
  3091. IsSetName: chartInfo.IsSetName,
  3092. }
  3093. resp := new(data_manage.ChartInfoDetailResp)
  3094. resp.ChartInfo = chartInfo
  3095. resp.EdbInfoList = edbList
  3096. resp.XEdbIdValue = xEdbIdValue
  3097. resp.YDataList = yDataList
  3098. resp.DataResp = dataResp
  3099. br.Ret = 200
  3100. br.Success = true
  3101. br.Msg = "获取成功"
  3102. br.Data = resp
  3103. }
  3104. // PreviewSectionScatterChartInfo
  3105. // @Title 图表-获取预览的截面散点图数据
  3106. // @Description 图表-获取指标详情接口
  3107. // @Param request body data_manage.SectionScatter true "type json string"
  3108. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  3109. // @router /chart_info/preview/time_section [post]
  3110. func (this *ChartInfoController) PreviewSectionScatterChartInfo() {
  3111. br := new(models.BaseResponse).Init()
  3112. defer func() {
  3113. this.Data["json"] = br
  3114. this.ServeJSON()
  3115. }()
  3116. var req data_manage.PreviewSectionScatterChartReq
  3117. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3118. if err != nil {
  3119. br.Msg = "参数解析异常!"
  3120. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3121. return
  3122. }
  3123. chartInfo := new(data_manage.ChartInfoView)
  3124. sysUser := this.SysUser
  3125. if sysUser == nil {
  3126. br.Msg = "请登录"
  3127. br.ErrMsg = "请登录,SysUser Is Empty"
  3128. br.Ret = 408
  3129. return
  3130. }
  3131. chartType := utils.CHART_TYPE_SECTION_SCATTER
  3132. edbInfoIdArr, err, errMsg := data.CheckChartExtraConfig(chartType, req.ExtraConfig)
  3133. if err != nil {
  3134. br.Msg = errMsg
  3135. br.ErrMsg = "添加失败:" + err.Error()
  3136. return
  3137. }
  3138. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdArr)
  3139. // 获取图表中的指标数据
  3140. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, req.ExtraConfig, "")
  3141. if err != nil {
  3142. br.Msg = "获取失败"
  3143. if errMsg != `` {
  3144. br.Msg = errMsg
  3145. }
  3146. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3147. return
  3148. }
  3149. warnEdbList := make([]string, 0)
  3150. for _, v := range edbList {
  3151. if v.IsNullData {
  3152. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3153. }
  3154. }
  3155. if len(warnEdbList) > 0 {
  3156. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3157. }
  3158. // 图表的指标来源
  3159. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3160. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3161. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3162. //图表操作权限
  3163. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3164. //判断是否需要展示英文标识
  3165. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, utils.CHART_SOURCE_DEFAULT, chartType)
  3166. chartInfo.Button = data_manage.ChartViewButton{
  3167. IsEdit: chartInfo.IsEdit,
  3168. IsEnChart: chartInfo.IsEnChart,
  3169. IsAdd: chartInfo.IsAdd,
  3170. IsCopy: true,
  3171. IsSetName: chartInfo.IsSetName,
  3172. }
  3173. resp := new(data_manage.ChartInfoDetailResp)
  3174. resp.ChartInfo = chartInfo
  3175. resp.EdbInfoList = edbList
  3176. resp.XEdbIdValue = xEdbIdValue
  3177. resp.YDataList = yDataList
  3178. resp.DataResp = dataResp
  3179. br.Ret = 200
  3180. br.Success = true
  3181. br.Msg = "获取成功"
  3182. br.Data = resp
  3183. }
  3184. // GetBatchChartRefreshResult
  3185. // @Title 获取批量刷新图表结果的接口
  3186. // @Description 获取批量刷新图表结果的接口
  3187. // @Param request body data_manage.BatchChartRefreshReq true "type json string"
  3188. // @Success Ret=200 刷新成功
  3189. // @router /chart_info/batch_refresh/result [post]
  3190. func (this *EdbInfoController) GetBatchChartRefreshResult() {
  3191. br := new(models.BaseResponse).Init()
  3192. defer func() {
  3193. this.Data["json"] = br
  3194. this.ServeJSON()
  3195. }()
  3196. sysUser := this.SysUser
  3197. if sysUser == nil {
  3198. br.Msg = "请登录"
  3199. br.ErrMsg = "请登录,SysUser Is Empty"
  3200. br.Ret = 408
  3201. return
  3202. }
  3203. var req data_manage.BatchChartRefreshReq
  3204. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3205. if err != nil {
  3206. br.Msg = "参数解析异常!"
  3207. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3208. return
  3209. }
  3210. // 刷新结果,默认是刷新完成了
  3211. refreshResult := data.CheckBatchChartRefreshResult(req.Source, req.ReportId, req.ReportChapterId)
  3212. resp := response.ChartRefreshResp{
  3213. RefreshResult: refreshResult,
  3214. }
  3215. br.Data = resp
  3216. br.Ret = 200
  3217. br.Success = true
  3218. }
  3219. //
  3220. //修复数据时间戳
  3221. //func init() {
  3222. // fmt.Println("start")
  3223. // edbInfoList,err:=data_manage.GetEdbInfo()
  3224. // if err!=nil {
  3225. // fmt.Println("Err:",err.Error())
  3226. // return
  3227. // }
  3228. // startDate:=time.Now().AddDate(-40,0,0).Format(utils.FormatDate)
  3229. // endDate:=time.Now().Format(utils.FormatDate)
  3230. // for _,v:=range edbInfoList{
  3231. // dataList,err:=data_manage.GetEdbDataList(v.Source,v.EdbInfoId,startDate,endDate)
  3232. // if err!=nil {
  3233. // fmt.Println("GetEdbDataList Err:",err.Error())
  3234. // return
  3235. // }
  3236. // for _,dv:=range dataList{
  3237. // dataTime,err:=time.Parse(utils.FormatDate,dv.DataTime)
  3238. // if err!=nil {
  3239. // fmt.Println("time.Parse Err:"+err.Error())
  3240. // return
  3241. // }
  3242. // timestamp:=dataTime.UnixNano()/1e6
  3243. // err=data_manage.ModifyEdbDatadTimestamp(v.Source,dv.EdbDataId,timestamp)
  3244. // if err!=nil{
  3245. // fmt.Println("ModifyEdbDatadTimestamp Err:"+err.Error())
  3246. // return
  3247. // }
  3248. // fmt.Println(v.Source,dv.EdbDataId,timestamp)
  3249. // }
  3250. // }
  3251. // //time.Sleep(2*time.Minute)
  3252. //
  3253. // //startDate:=time.Now().AddDate(-30,0,0).Format(utils.FormatDate)
  3254. // //endDate:=time.Now().Format(utils.FormatDate)
  3255. // //dataList,err:=data_manage.GetEdbDataList(1,100099,startDate,endDate)
  3256. // //if err!=nil {
  3257. // // fmt.Println("GetEdbDataList Err:",err.Error())
  3258. // // return
  3259. // //}
  3260. // //for _,dv:=range dataList{
  3261. // // dataTime,err:=time.Parse(utils.FormatDate,dv.DataTime)
  3262. // // if err!=nil {
  3263. // // fmt.Println("time.Parse Err:"+err.Error())
  3264. // // return
  3265. // // }
  3266. // // timestamp:=dataTime.UnixNano()/1e6
  3267. // // err=data_manage.ModifyEdbDatadTimestamp(2,dv.EdbDataId,timestamp)
  3268. // // if err!=nil{
  3269. // // fmt.Println("ModifyEdbDatadTimestamp Err:"+err.Error())
  3270. // // return
  3271. // // }
  3272. // // fmt.Println(2,dv.EdbDataId,timestamp)
  3273. // //}
  3274. //
  3275. // fmt.Println("end")
  3276. //}
  3277. //修复指标配置数据
  3278. //func init() {
  3279. // fmt.Println("start")
  3280. // edbInfo, err := data_manage.GetEdbInfoAll()
  3281. // fmt.Println(err)
  3282. // for k, v := range edbInfoAll {
  3283. // fmt.Println(k, v.EdbInfoId)
  3284. // item, err := data_manage.GetEdbInfoMaxAndMinInfo(v.Source, v.EdbCode)
  3285. // fmt.Println(item, err)
  3286. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(v.EdbInfoId, item)
  3287. // if err != nil {
  3288. // fmt.Println("ModifyEdbInfoMaxAndMinInfo Err:" + err.Error())
  3289. // }
  3290. // }
  3291. // fmt.Println("end")
  3292. //}
  3293. //func init() {
  3294. // fmt.Println("start")
  3295. // var yearArr []int
  3296. // yearArr = append(yearArr, 2021)
  3297. // yearArr = append(yearArr, 2010)
  3298. // yearArr = append(yearArr, 2020)
  3299. // yearArr = append(yearArr, 2017)
  3300. // yearArr = append(yearArr, 2019)
  3301. // sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
  3302. // fmt.Println(yearArr)
  3303. // fmt.Println("end")
  3304. //}
  3305. //季度指标数据计算(公历转农历)
  3306. //func init() {
  3307. // fmt.Println("start AddCalculateQuarter")
  3308. // list:=make([]*data_manage.EdbDataList,0)
  3309. // data_manage.AddCalculateQuarterV4(list)
  3310. // fmt.Println("end AddCalculateQuarter")
  3311. // //return
  3312. //}
  3313. /*
  3314. {
  3315. "ChartClassifyId": 58,
  3316. "ChartName": "公历转农历6",
  3317. "ChartType": 2,
  3318. "ChartEdbInfoList": [
  3319. {
  3320. "ChartColor": "#00f",
  3321. "ChartStyle": "spline",
  3322. "ChartWidth": 3,
  3323. "EdbInfoId": 100466,
  3324. "EdbInfoType": 1,
  3325. "IsAxis": 1,
  3326. "IsOrder": false,
  3327. "LeadUnit": "",
  3328. "LeadValue": 0,
  3329. "MaxData": 0.2943,
  3330. "MinData": -0.2448
  3331. }
  3332. ]
  3333. }
  3334. */
  3335. //func init() {
  3336. // fmt.Println("start")
  3337. // data_manage.AddCalculateQuarter(100466,6,"C2108252836")
  3338. // fmt.Println("end")
  3339. //}
  3340. //func init() {
  3341. // data.AddAllChartInfo()
  3342. //}
  3343. // 截面散点示例数据
  3344. //func init() {
  3345. //
  3346. // seriesList := []data_manage.SectionScatterSeriesItemReq{
  3347. // {
  3348. // Name: "系列1名称",
  3349. // Color: "红红火火色",
  3350. // EdbList: []data_manage.SectionScatterEdbItemReq{
  3351. // {
  3352. // XEdbInfoId: 1,
  3353. // YEdbInfoId: 2,
  3354. // Name: "破铜",
  3355. // NameEn: "Po Tong",
  3356. // XDateType: 1,
  3357. // XDate: "",
  3358. // XDateValue: 0,
  3359. // YDateType: 2,
  3360. // YDate: "",
  3361. // YDateValue: 30,
  3362. // IsShow: false,
  3363. // },
  3364. // {
  3365. // XEdbInfoId: 3,
  3366. // YEdbInfoId: 4,
  3367. // Name: "烂铁",
  3368. // NameEn: "Lan Tie",
  3369. // XDateType: 2,
  3370. // XDate: "",
  3371. // XDateValue: 30,
  3372. // YDateType: 3,
  3373. // YDate: "2023-02-17",
  3374. // YDateValue: 0,
  3375. // IsShow: false,
  3376. // },
  3377. // },
  3378. // ShowLine: false,
  3379. // ShowEquation: false,
  3380. // ShowR: false,
  3381. // },
  3382. // {
  3383. // Name: "系列2名称",
  3384. // Color: "恍恍惚惚色",
  3385. // EdbList: []data_manage.SectionScatterEdbItemReq{
  3386. // {
  3387. // XEdbInfoId: 1,
  3388. // YEdbInfoId: 2,
  3389. // Name: "破铜",
  3390. // NameEn: "Po Tong",
  3391. // XDateType: 3,
  3392. // XDate: "2023-02-17",
  3393. // XDateValue: 0,
  3394. // YDateType: 2,
  3395. // YDate: "",
  3396. // YDateValue: 60,
  3397. // IsShow: true,
  3398. // },
  3399. // {
  3400. // XEdbInfoId: 3,
  3401. // YEdbInfoId: 4,
  3402. // Name: "烂铁",
  3403. // NameEn: "Lan Tie",
  3404. // XDateType: 3,
  3405. // XDate: "2023-02-17",
  3406. // XDateValue: 0,
  3407. // YDateType: 2,
  3408. // YDate: "",
  3409. // YDateValue: 60,
  3410. // IsShow: false,
  3411. // },
  3412. // },
  3413. // ShowLine: false,
  3414. // ShowEquation: false,
  3415. // ShowR: false,
  3416. // },
  3417. // }
  3418. // rel := data_manage.SectionScatter{
  3419. // XName: "X轴名称",
  3420. // XNameEn: "X english name",
  3421. // XUnitName: "X轴单位",
  3422. // XUnitNameEn: "X unit english name",
  3423. // YName: "Y轴名称",
  3424. // YNameEn: "Y english name",
  3425. // YUnitName: "Y轴单位",
  3426. // YUnitNameEn: "Y unit english name",
  3427. // SeriesList: seriesList,
  3428. // }
  3429. // jsonByte, _ := json.Marshal(rel)
  3430. // utils.FileLog.Info(string(jsonByte))
  3431. //}
  3432. // func init() {
  3433. // var a, b float64
  3434. //
  3435. // testX := []float64{1, 3, 8, 7, 9}
  3436. // testY := []float64{10, 12, 24, 21, 34}
  3437. // {
  3438. // coordinateData := make([]utils.Coordinate, 0)
  3439. // for k, x := range testX {
  3440. // tmpCoordinate1 := utils.Coordinate{
  3441. // X: x,
  3442. // Y: testY[k],
  3443. // }
  3444. // coordinateData = append(coordinateData, tmpCoordinate1)
  3445. // }
  3446. // a, b = utils.GetLinearResult(coordinateData)
  3447. // trendLine := fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  3448. //
  3449. // fmt.Println(trendLine)
  3450. // }
  3451. //
  3452. // {
  3453. // r2 := utils.CalculationDecisive(testX, testY)
  3454. // fmt.Println(r2)
  3455. // }
  3456. // }
  3457. // FixChart 修复季节性图的日期数据
  3458. //func FixChart() {
  3459. // var condition string
  3460. // var pars []interface{}
  3461. //
  3462. // // 普通图表
  3463. // condition += ` AND chart_type = ? AND season_start_date != "" `
  3464. // pars = append(pars, 2)
  3465. //
  3466. // //获取图表信息
  3467. // list, err := data_manage.GetChartInfoListByCondition(condition, pars, 0, 10000)
  3468. // if err != nil {
  3469. // fmt.Println("err:", err)
  3470. // return
  3471. // }
  3472. //
  3473. // for _, v := range list {
  3474. // fmt.Println(v)
  3475. // v.SeasonStartDate = v.SeasonStartDate + "-01"
  3476. // v.SeasonEndDate = v.SeasonEndDate + "-12"
  3477. // err = v.Update([]string{"SeasonStartDate", "SeasonEndDate"})
  3478. // if err != nil {
  3479. // fmt.Println(v.ChartInfoId, ";", v.ChartName, "修改失敗")
  3480. // }
  3481. // data.EsAddOrEditChartInfo(v.ChartInfoId)
  3482. // data.EsAddOrEditMyChartInfoByChartInfoId(v.ChartInfoId)
  3483. // }
  3484. //}
  3485. // PreviewRadarChartInfo
  3486. // @Title 图表-获取预览的雷达图
  3487. // @Description 图表-获取预览的雷达图
  3488. // @Param ExtraConfig body data_manage.PreviewRadarChartReq true "type json string"
  3489. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  3490. // @router /chart_info/preview/radar [post]
  3491. func (this *ChartInfoController) PreviewRadarChartInfo() {
  3492. br := new(models.BaseResponse).Init()
  3493. defer func() {
  3494. this.Data["json"] = br
  3495. this.ServeJSON()
  3496. }()
  3497. var req data_manage.PreviewRadarChartReq
  3498. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3499. if err != nil {
  3500. br.Msg = "参数解析异常!"
  3501. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3502. return
  3503. }
  3504. chartInfo := new(data_manage.ChartInfoView)
  3505. sysUser := this.SysUser
  3506. if sysUser == nil {
  3507. br.Msg = "请登录"
  3508. br.ErrMsg = "请登录,SysUser Is Empty"
  3509. br.Ret = 408
  3510. return
  3511. }
  3512. chartType := utils.CHART_TYPE_RADAR
  3513. if len(req.ChartEdbInfoList) <= 0 {
  3514. br.Msg = "请选择指标"
  3515. return
  3516. }
  3517. edbInfoIdArr := make([]int, 0)
  3518. edbInfoNameMap := make(map[int]string)
  3519. for _, v := range req.ChartEdbInfoList {
  3520. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  3521. edbInfoNameMap[v.EdbInfoId] = v.EdbAliasName
  3522. }
  3523. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdArr)
  3524. for _, v := range mappingList {
  3525. if name, ok := edbInfoNameMap[v.EdbInfoId]; ok {
  3526. v.EdbAliasName = name
  3527. }
  3528. }
  3529. // 获取图表中的指标数据
  3530. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, req.ExtraConfig, "")
  3531. if err != nil {
  3532. br.Msg = "获取失败"
  3533. if errMsg != `` {
  3534. br.Msg = errMsg
  3535. }
  3536. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3537. return
  3538. }
  3539. warnEdbList := make([]string, 0)
  3540. for _, v := range edbList {
  3541. if v.IsNullData {
  3542. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3543. }
  3544. }
  3545. if len(warnEdbList) > 0 {
  3546. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3547. }
  3548. //图表操作权限
  3549. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3550. //判断是否需要展示英文标识
  3551. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, utils.CHART_SOURCE_DEFAULT, chartType)
  3552. chartInfo.Button = data_manage.ChartViewButton{
  3553. IsEdit: chartInfo.IsEdit,
  3554. IsEnChart: chartInfo.IsEnChart,
  3555. IsAdd: chartInfo.IsAdd,
  3556. IsCopy: true,
  3557. IsSetName: chartInfo.IsSetName,
  3558. }
  3559. // 图表的指标来源
  3560. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3561. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3562. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3563. resp := new(data_manage.ChartInfoDetailResp)
  3564. resp.ChartInfo = chartInfo
  3565. resp.EdbInfoList = edbList
  3566. resp.XEdbIdValue = xEdbIdValue
  3567. resp.YDataList = yDataList
  3568. resp.DataResp = dataResp
  3569. br.Ret = 200
  3570. br.Success = true
  3571. br.Msg = "获取成功"
  3572. br.Data = resp
  3573. }
  3574. // ChartInfoDetail
  3575. // @Title 获取图表详情-数据转换
  3576. // @Description 获取图表详情接口-数据转换
  3577. // @Param ChartInfoId query int true "图表id"
  3578. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  3579. // @Param StartDate query string true "自定义开始日期"
  3580. // @Param EndDate query string true "自定义结束日期"
  3581. // @Param Calendar query string true "公历/农历"
  3582. // @Param SeasonStartDate query string true "季节性图开始日期"
  3583. // @Param SeasonEndDate query string true "季节性图结束日期"
  3584. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  3585. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  3586. // @Success 200 {object} data_manage.ChartInfoDetailResp
  3587. // @router /chart_info/convert/detail [get]
  3588. func (this *ChartInfoController) ChartInfoConvertDetail() {
  3589. br := new(models.BaseResponse).Init()
  3590. defer func() {
  3591. this.Data["json"] = br
  3592. this.ServeJSON()
  3593. }()
  3594. sysUser := this.SysUser
  3595. if sysUser == nil {
  3596. br.Msg = "请登录"
  3597. br.ErrMsg = "请登录,SysUser Is Empty"
  3598. br.Ret = 408
  3599. return
  3600. }
  3601. chartInfoId, _ := this.GetInt("ChartInfoId")
  3602. dateType, _ := this.GetInt("DateType")
  3603. fmt.Println("dateType:", dateType)
  3604. if dateType <= 0 {
  3605. dateType = 3
  3606. }
  3607. startDate := this.GetString("StartDate")
  3608. endDate := this.GetString("EndDate")
  3609. startYear, _ := this.GetInt("StartYear")
  3610. edbInfoId := this.GetString("EdbInfoId")
  3611. chartType, _ := this.GetInt("ChartType")
  3612. calendar := this.GetString("Calendar")
  3613. if calendar == "" {
  3614. calendar = "公历"
  3615. }
  3616. isConvert, _ := this.GetInt("IsConvert")
  3617. convertType, _ := this.GetInt("ConvertType")
  3618. convertValue, _ := this.GetFloat("ConvertValue")
  3619. convertUnit := this.GetString("ConvertUnit")
  3620. convertEnUnit := this.GetString("ConvertEnUnit")
  3621. isAxis, _ := this.GetInt("IsAxis")
  3622. resp := new(data_manage.ChartInfoDetailResp)
  3623. var err error
  3624. chartInfo := new(data_manage.ChartInfoView)
  3625. chartInfo.HaveOperaAuth = true
  3626. // 通过id获取详情
  3627. if chartInfoId > 0 {
  3628. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  3629. if err != nil {
  3630. if err.Error() == utils.ErrNoRow() {
  3631. br.Msg = "该图表已删除,自动查看下一图表"
  3632. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  3633. br.Ret = 406
  3634. return
  3635. }
  3636. br.Msg = "获取失败"
  3637. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  3638. return
  3639. }
  3640. chartType = chartInfo.ChartType
  3641. // 获取主题样式
  3642. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  3643. if err != nil {
  3644. br.Msg = "获取失败"
  3645. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  3646. return
  3647. }
  3648. chartInfo.ChartThemeStyle = chartTheme.Config
  3649. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  3650. // 图表数据权限
  3651. {
  3652. // 图表分类
  3653. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  3654. if err != nil {
  3655. br.Msg = "获取失败"
  3656. br.ErrMsg = "获取图表分类信息失败,Err:" + err.Error()
  3657. return
  3658. }
  3659. // 已授权分类id
  3660. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  3661. if err != nil {
  3662. br.Msg = "获取失败"
  3663. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  3664. return
  3665. }
  3666. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  3667. }
  3668. //图表操作权限
  3669. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, chartInfo.HaveOperaAuth)
  3670. chartInfo.Button = data_manage.ChartViewButton{
  3671. IsEdit: chartInfo.IsEdit,
  3672. IsEnChart: chartInfo.IsEnChart,
  3673. IsAdd: chartInfo.IsAdd,
  3674. IsCopy: true,
  3675. IsSetName: chartInfo.IsSetName,
  3676. }
  3677. }
  3678. // 有数据权限的话,获取图表的数据
  3679. if chartInfo.HaveOperaAuth {
  3680. mappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  3681. if chartInfoId > 0 {
  3682. mappingList, err = data_manage.GetChartEdbMappingList(chartInfoId)
  3683. if err != nil {
  3684. br.Msg = "获取失败"
  3685. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3686. return
  3687. }
  3688. } else {
  3689. if edbInfoId != "" {
  3690. mappingList, err = data_manage.GetChartEdbMappingListByEdbInfoId(edbInfoId)
  3691. if err != nil {
  3692. br.Msg = "获取失败"
  3693. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3694. return
  3695. }
  3696. if isConvert == 1 && len(mappingList) > 0 {
  3697. for i, _ := range mappingList {
  3698. mappingList[i].IsConvert = 1
  3699. mappingList[i].ConvertType = convertType
  3700. mappingList[i].ConvertValue = convertValue
  3701. mappingList[i].ConvertUnit = convertUnit
  3702. mappingList[i].ConvertEnUnit = convertEnUnit
  3703. }
  3704. }
  3705. }
  3706. }
  3707. // 图表额外数据参数
  3708. extraConfigStr := chartInfo.ExtraConfig
  3709. // 柱方图的一些配置
  3710. var barConfig data_manage.BarChartInfoReq
  3711. if chartInfo != nil && chartInfo.ChartType == 7 {
  3712. if chartInfo.BarConfig == `` {
  3713. br.Msg = "柱方图未配置"
  3714. br.ErrMsg = "柱方图未配置"
  3715. return
  3716. }
  3717. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  3718. if err != nil {
  3719. br.Msg = "柱方图配置异常"
  3720. br.ErrMsg = "柱方图配置异常"
  3721. return
  3722. }
  3723. extraConfigStr = chartInfo.BarConfig
  3724. }
  3725. yearMax := 0
  3726. if dateType == utils.DateTypeNYears {
  3727. for _, v := range mappingList {
  3728. if v.LatestDate != "" {
  3729. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  3730. if tErr != nil {
  3731. br.Msg = "获取失败"
  3732. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  3733. return
  3734. }
  3735. if lastDateT.Year() > yearMax {
  3736. yearMax = lastDateT.Year()
  3737. }
  3738. }
  3739. }
  3740. }
  3741. // 开始/结束日期
  3742. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  3743. // 获取图表中的指标数据
  3744. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartConvertEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, isAxis)
  3745. if err != nil {
  3746. br.Msg = "获取失败"
  3747. if errMsg != `` {
  3748. br.Msg = errMsg
  3749. }
  3750. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3751. return
  3752. }
  3753. // 单位
  3754. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  3755. chartInfo.Unit = yDataList[0].Unit
  3756. chartInfo.UnitEn = yDataList[0].UnitEn
  3757. }
  3758. warnEdbList := make([]string, 0)
  3759. for _, v := range edbList {
  3760. if v.IsNullData {
  3761. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3762. }
  3763. }
  3764. if len(warnEdbList) > 0 {
  3765. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3766. }
  3767. if chartInfoId > 0 && chartInfo != nil {
  3768. //判断是否加入我的图库
  3769. {
  3770. var myChartCondition string
  3771. var myChartPars []interface{}
  3772. myChartCondition += ` AND a.admin_id=? `
  3773. myChartPars = append(myChartPars, sysUser.AdminId)
  3774. myChartCondition += ` AND a.chart_info_id=? `
  3775. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  3776. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  3777. if err != nil && err.Error() != utils.ErrNoRow() {
  3778. br.Msg = "获取失败"
  3779. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  3780. return
  3781. }
  3782. if myChartList != nil && len(myChartList) > 0 {
  3783. chartInfo.IsAdd = true
  3784. chartInfo.MyChartId = myChartList[0].MyChartId
  3785. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  3786. }
  3787. }
  3788. if chartInfo.ChartType == 2 {
  3789. if chartInfo.SeasonStartDate != "" {
  3790. chartInfo.StartDate = chartInfo.SeasonStartDate
  3791. chartInfo.EndDate = chartInfo.SeasonEndDate
  3792. if chartInfo.DateType == 3 {
  3793. chartInfo.DateType = 5
  3794. }
  3795. }
  3796. }
  3797. }
  3798. // 图表的指标来源
  3799. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3800. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3801. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3802. //判断是否需要展示英文标识
  3803. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  3804. resp.EdbInfoList = edbList
  3805. resp.XEdbIdValue = xEdbIdValue
  3806. resp.YDataList = yDataList
  3807. resp.DataResp = dataResp
  3808. resp.BarChartInfo = barConfig
  3809. }
  3810. resp.ChartInfo = chartInfo
  3811. br.Ret = 200
  3812. br.Success = true
  3813. br.Msg = "获取成功"
  3814. br.Data = resp
  3815. }