chart_info.go 102 KB

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