chart_info.go 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963
  1. package line_feature
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/controllers"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/data_manage"
  8. "eta/eta_api/models/data_manage/line_feature/request"
  9. "eta/eta_api/models/data_manage/line_feature/response"
  10. "eta/eta_api/models/system"
  11. "eta/eta_api/services/alarm_msg"
  12. "eta/eta_api/services/data"
  13. "eta/eta_api/services/data/data_manage_permission"
  14. lineFeatureServ "eta/eta_api/services/data/line_feature"
  15. "eta/eta_api/utils"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/rdlucklib/rdluck_tools/paging"
  21. )
  22. // LineFeaturesChartInfoController 统计特征图表管理
  23. type LineFeaturesChartInfoController struct {
  24. controllers.BaseAuthController
  25. }
  26. // MultipleGraphConfigSave
  27. // @Title 保存配置接口
  28. // @Description 保存配置接口
  29. // @Param request body request.SaveMultipleGraphConfigReq true "type json string"
  30. // @Success Ret=200 返回图表id
  31. // @router /config/save [post]
  32. func (this *LineFeaturesChartInfoController) MultipleGraphConfigSave() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. sysUser := this.SysUser
  39. if sysUser == nil {
  40. br.Msg = "请登录"
  41. br.ErrMsg = "请登录,SysUser Is Empty"
  42. br.Ret = 408
  43. return
  44. }
  45. var req request.SaveMultipleGraphConfigReq
  46. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  47. if err != nil {
  48. br.Msg = "参数解析异常!"
  49. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  50. return
  51. }
  52. if req.EdbInfoId <= 0 {
  53. br.Msg = "指标未选择"
  54. br.IsSendEmail = false
  55. return
  56. }
  57. _, err = data_manage.GetEdbInfoById(req.EdbInfoId)
  58. if err != nil {
  59. br.Msg = "指标A异常"
  60. br.ErrMsg = "指标A异常,err:" + err.Error()
  61. if err.Error() == utils.ErrNoRow() {
  62. br.Msg = `指标A不存在`
  63. br.IsSendEmail = false
  64. }
  65. return
  66. }
  67. configSave := request.ConfigSave{
  68. Curve: req.Curve,
  69. StandardDeviation: req.StandardDeviation,
  70. Percentile: req.Percentile,
  71. FrequencyDistribution: req.FrequencyDistribution,
  72. }
  73. configSaveStrByte, err := json.Marshal(configSave)
  74. if err != nil {
  75. br.Msg = `相关性配置异常`
  76. br.ErrMsg = `相关性配置异常,ERR:` + err.Error()
  77. return
  78. }
  79. var multipleGraphConfig *data_manage.MultipleGraphConfig
  80. if req.MultipleGraphConfigId > 0 {
  81. multipleGraphConfig, err = data_manage.GetMultipleGraphConfigById(req.MultipleGraphConfigId)
  82. if err != nil {
  83. br.Msg = `不存在该配置`
  84. br.ErrMsg = `不存在该配置`
  85. return
  86. }
  87. multipleGraphConfig.EdbInfoIdA = req.EdbInfoId
  88. multipleGraphConfig.Curve = string(configSaveStrByte)
  89. multipleGraphConfig.ModifyTime = time.Now()
  90. err = multipleGraphConfig.Update([]string{"EdbInfoIdA", "Curve", "ModifyTime"})
  91. } else {
  92. multipleGraphConfig = &data_manage.MultipleGraphConfig{
  93. //MultipleGraphConfigId: 0,
  94. EdbInfoIdA: req.EdbInfoId,
  95. Curve: string(configSaveStrByte),
  96. SysUserId: this.SysUser.AdminId,
  97. SysUserRealName: this.SysUser.RealName,
  98. ModifyTime: time.Now(),
  99. CreateTime: time.Now(),
  100. }
  101. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  102. }
  103. if err != nil {
  104. br.Msg = `保存失败`
  105. br.ErrMsg = `保存失败,Err:` + err.Error()
  106. return
  107. }
  108. br.Ret = 200
  109. br.Success = true
  110. br.Msg = "保存成功"
  111. br.Data = multipleGraphConfig
  112. br.IsAddLog = true
  113. }
  114. // MultipleGraphPreview
  115. // @Title 批量添加图表的预览接口
  116. // @Description 批量添加图表的预览接口
  117. // @Param request body request.SaveMultipleGraphConfigReq true "type json string"
  118. // @Success Ret=200 返回图表id
  119. // @router /preview [post]
  120. func (this *LineFeaturesChartInfoController) MultipleGraphPreview() {
  121. br := new(models.BaseResponse).Init()
  122. defer func() {
  123. this.Data["json"] = br
  124. this.ServeJSON()
  125. }()
  126. sysUser := this.SysUser
  127. if sysUser == nil {
  128. br.Msg = "请登录"
  129. br.ErrMsg = "请登录,SysUser Is Empty"
  130. br.Ret = 408
  131. return
  132. }
  133. var req request.SaveMultipleGraphConfigReq
  134. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  135. if err != nil {
  136. br.Msg = "参数解析异常!"
  137. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  138. return
  139. }
  140. edbInfoMapping, err := data_manage.GetChartEdbMappingByEdbInfoId(req.EdbInfoId)
  141. if err != nil {
  142. br.Msg = "获取失败"
  143. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  144. return
  145. }
  146. if edbInfoMapping == nil {
  147. br.Msg = "指标不存在"
  148. br.IsSendEmail = false
  149. return
  150. }
  151. resp := response.PreviewMultipleGraphConfigResp{}
  152. chartInfo := new(data_manage.ChartInfoView)
  153. chartInfo.Calendar = `公历`
  154. chartInfo.ChartName = edbInfoMapping.EdbName
  155. // 曲线图表信息
  156. curveConf := req.Curve
  157. var maxDate time.Time
  158. if edbInfoMapping.LatestDate != "" {
  159. latestDateT, _ := time.Parse(utils.FormatDate, edbInfoMapping.LatestDate)
  160. maxDate = latestDateT
  161. }
  162. startDate, endDate := utils.GetDateByDateTypeV2(curveConf.DateType, curveConf.StartDate, curveConf.EndDate, curveConf.StartYear, maxDate)
  163. {
  164. tmpChartInfo := *chartInfo
  165. // 获取图表中的指标数据
  166. edbList, _, _, _, err, errMsg := data.GetChartEdbData(tmpChartInfo.ChartInfoId, tmpChartInfo.ChartType, tmpChartInfo.Calendar, startDate, endDate, []*data_manage.ChartEdbInfoMapping{edbInfoMapping}, tmpChartInfo.ExtraConfig, tmpChartInfo.SeasonExtraConfig)
  167. if err != nil {
  168. br.Msg = "获取失败"
  169. if errMsg != `` {
  170. br.Msg = errMsg
  171. }
  172. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  173. return
  174. }
  175. warnEdbList := make([]string, 0)
  176. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  177. for _, v := range edbList {
  178. if v.IsNullData {
  179. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  180. }
  181. chartEdbInfoMapping := *v
  182. chartEdbInfoMapping.IsOrder = false
  183. chartEdbInfoMapping.IsAxis = 1
  184. chartEdbInfoMapping.MinData = curveConf.LeftMin
  185. chartEdbInfoMapping.MaxData = curveConf.LeftMax
  186. chartEdbInfoMapping.ChartColor = `#00F`
  187. chartEdbInfoMapping.ChartWidth = 3
  188. chartEdbInfoMapping.EdbInfoType = 1
  189. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  190. }
  191. if len(warnEdbList) > 0 {
  192. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  193. }
  194. tmpChartInfo.Source = utils.CHART_SOURCE_DEFAULT
  195. tmpChartInfo.DateType = curveConf.DateType
  196. tmpChartInfo.StartDate = startDate
  197. tmpChartInfo.EndDate = endDate
  198. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  199. resp.CurveData = response.CurveResp{
  200. ChartInfo: tmpChartInfo,
  201. EdbInfoList: tmpEdbList,
  202. }
  203. }
  204. // 标准差图表信息
  205. {
  206. // 配置了数据才有返回
  207. if req.StandardDeviation.CalculateValue > 0 {
  208. tmpChartInfo := *chartInfo
  209. tmpChartInfo.ChartName = fmt.Sprintf("%s%d期滚动标准差", edbInfoMapping.EdbName, req.StandardDeviation.CalculateValue)
  210. // 获取图表中的指标数据
  211. edbList, dataResp, err, errMsg := lineFeatureServ.GetStandardDeviationData(0, startDate, endDate, edbInfoMapping, req.StandardDeviation.CalculateValue)
  212. if err != nil {
  213. br.Msg = "获取失败"
  214. if errMsg != `` {
  215. br.Msg = errMsg
  216. }
  217. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  218. return
  219. }
  220. warnEdbList := make([]string, 0)
  221. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  222. for _, v := range edbList {
  223. if v.IsNullData {
  224. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  225. }
  226. chartEdbInfoMapping := *v
  227. chartEdbInfoMapping.IsOrder = false
  228. chartEdbInfoMapping.IsAxis = 1
  229. //chartEdbInfoMapping.MinData = curveConf.LeftMin
  230. //chartEdbInfoMapping.MaxData = curveConf.LeftMax
  231. chartEdbInfoMapping.ChartColor = `#00F`
  232. chartEdbInfoMapping.ChartWidth = 3
  233. chartEdbInfoMapping.EdbInfoType = 1
  234. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  235. }
  236. if len(warnEdbList) > 0 {
  237. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  238. }
  239. tmpChartInfo.Source = utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION
  240. //tmpChartInfo.DateType = curveConf.DateType
  241. //tmpChartInfo.StartDate = startDate
  242. //tmpChartInfo.EndDate = endDate
  243. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  244. resp.StandardDeviationData = response.CurveResp{
  245. ChartInfo: tmpChartInfo,
  246. EdbInfoList: tmpEdbList,
  247. DataResp: dataResp,
  248. }
  249. }
  250. }
  251. // 百分位图表信息
  252. {
  253. // 配置了数据才有返回
  254. if req.Percentile.CalculateValue > 0 && req.Percentile.CalculateUnit != `` {
  255. tmpChartInfo := *chartInfo
  256. tmpChartInfo.ChartName = fmt.Sprintf("%s%d%s百分位", edbInfoMapping.EdbName, req.Percentile.CalculateValue, req.Percentile.CalculateUnit)
  257. // 获取图表中的指标数据
  258. edbList, dataResp, err, errMsg := lineFeatureServ.GetPercentileData(0, startDate, endDate, edbInfoMapping, req.Percentile.CalculateValue, req.Percentile.CalculateUnit, req.Percentile.PercentType)
  259. if err != nil && errMsg != `` {
  260. br.Msg = errMsg
  261. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  262. return
  263. }
  264. if err == nil {
  265. warnEdbList := make([]string, 0)
  266. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  267. for _, v := range edbList {
  268. if v.IsNullData {
  269. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  270. }
  271. chartEdbInfoMapping := *v
  272. chartEdbInfoMapping.IsOrder = false
  273. chartEdbInfoMapping.IsAxis = 1
  274. //chartEdbInfoMapping.MinData = curveConf.LeftMin
  275. //chartEdbInfoMapping.MaxData = curveConf.LeftMax
  276. chartEdbInfoMapping.ChartColor = `#00F`
  277. chartEdbInfoMapping.ChartWidth = 3
  278. chartEdbInfoMapping.EdbInfoType = 1
  279. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  280. }
  281. if len(warnEdbList) > 0 {
  282. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  283. }
  284. tmpChartInfo.Source = utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE
  285. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  286. resp.PercentileData = response.CurveResp{
  287. ChartInfo: tmpChartInfo,
  288. EdbInfoList: tmpEdbList,
  289. DataResp: dataResp,
  290. }
  291. }
  292. }
  293. }
  294. // 频率分布图表信息
  295. {
  296. // 配置了数据才有返回
  297. if req.FrequencyDistribution.DateType > 0 && req.FrequencyDistribution.FrequencyValue > 0 {
  298. tmpChartInfo := *chartInfo
  299. tmpChartInfo.ChartName = fmt.Sprintf("%s频率和累计频率", edbInfoMapping.EdbName)
  300. // 获取图表中的指标数据
  301. edbList, dataResp, err, errMsg := lineFeatureServ.GetFrequencyDistributionData(0, edbInfoMapping, req.FrequencyDistribution.DateType, req.FrequencyDistribution.FrequencyValue, req.FrequencyDistribution.StartDate, req.FrequencyDistribution.EndDate)
  302. if err != nil && errMsg != `` {
  303. br.Msg = errMsg
  304. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  305. return
  306. }
  307. if err == nil {
  308. warnEdbList := make([]string, 0)
  309. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  310. for _, v := range edbList {
  311. if v.IsNullData {
  312. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  313. }
  314. chartEdbInfoMapping := *v
  315. chartEdbInfoMapping.IsOrder = false
  316. chartEdbInfoMapping.IsAxis = 1
  317. //chartEdbInfoMapping.MinData = curveConf.LeftMin
  318. //chartEdbInfoMapping.MaxData = curveConf.LeftMax
  319. chartEdbInfoMapping.ChartColor = `#00F`
  320. chartEdbInfoMapping.ChartWidth = 3
  321. chartEdbInfoMapping.EdbInfoType = 1
  322. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  323. }
  324. if len(warnEdbList) > 0 {
  325. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  326. }
  327. tmpChartInfo.Source = utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY
  328. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  329. resp.FrequencyDistributionData = response.CurveResp{
  330. ChartInfo: tmpChartInfo,
  331. EdbInfoList: tmpEdbList,
  332. DataResp: dataResp,
  333. }
  334. }
  335. }
  336. }
  337. br.Ret = 200
  338. br.Success = true
  339. br.Msg = "获取成功"
  340. br.Data = resp
  341. }
  342. // MultipleGraphPreviewCurve
  343. // @Title 批量添加图表的预览曲线图接口(只返回曲线图的数据)
  344. // @Description 批量添加图表的预览曲线图接口
  345. // @Param request body request.SaveMultipleGraphConfigReq true "type json string"
  346. // @Success Ret=200 返回图表id
  347. // @router /preview_cure [post]
  348. func (this *LineFeaturesChartInfoController) MultipleGraphPreviewCurve() {
  349. br := new(models.BaseResponse).Init()
  350. defer func() {
  351. this.Data["json"] = br
  352. this.ServeJSON()
  353. }()
  354. sysUser := this.SysUser
  355. if sysUser == nil {
  356. br.Msg = "请登录"
  357. br.ErrMsg = "请登录,SysUser Is Empty"
  358. br.Ret = 408
  359. return
  360. }
  361. var req request.SaveMultipleGraphConfigReq
  362. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  363. if err != nil {
  364. br.Msg = "参数解析异常!"
  365. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  366. return
  367. }
  368. edbInfoMapping, err := data_manage.GetChartEdbMappingByEdbInfoId(req.EdbInfoId)
  369. if err != nil {
  370. br.Msg = "获取失败"
  371. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  372. return
  373. }
  374. if edbInfoMapping == nil {
  375. br.Msg = "指标不存在"
  376. br.IsSendEmail = false
  377. return
  378. }
  379. resp := response.PreviewMultipleGraphConfigResp{}
  380. chartInfo := new(data_manage.ChartInfoView)
  381. chartInfo.Calendar = `公历`
  382. chartInfo.ChartName = edbInfoMapping.EdbName
  383. // 曲线图表信息
  384. {
  385. tmpChartInfo := *chartInfo
  386. curveConf := req.Curve
  387. var maxDate time.Time
  388. if edbInfoMapping.LatestDate != "" {
  389. latestDateT, _ := time.Parse(utils.FormatDate, edbInfoMapping.LatestDate)
  390. maxDate = latestDateT
  391. }
  392. startDate, endDate := utils.GetDateByDateTypeV2(curveConf.DateType, curveConf.StartDate, curveConf.EndDate, curveConf.StartYear, maxDate)
  393. // 获取图表中的指标数据
  394. edbList, _, _, _, err, errMsg := data.GetChartEdbData(tmpChartInfo.ChartInfoId, tmpChartInfo.ChartType, tmpChartInfo.Calendar, startDate, endDate, []*data_manage.ChartEdbInfoMapping{edbInfoMapping}, tmpChartInfo.ExtraConfig, tmpChartInfo.SeasonExtraConfig)
  395. if err != nil {
  396. br.Msg = "获取失败"
  397. if errMsg != `` {
  398. br.Msg = errMsg
  399. }
  400. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  401. return
  402. }
  403. warnEdbList := make([]string, 0)
  404. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  405. for _, v := range edbList {
  406. if v.IsNullData {
  407. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  408. }
  409. chartEdbInfoMapping := *v
  410. chartEdbInfoMapping.IsOrder = false
  411. chartEdbInfoMapping.IsAxis = 1
  412. chartEdbInfoMapping.MinData = curveConf.LeftMin
  413. chartEdbInfoMapping.MaxData = curveConf.LeftMax
  414. chartEdbInfoMapping.ChartColor = `#00F`
  415. chartEdbInfoMapping.ChartWidth = 3
  416. chartEdbInfoMapping.EdbInfoType = 1
  417. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  418. }
  419. if len(warnEdbList) > 0 {
  420. tmpChartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  421. }
  422. tmpChartInfo.Source = utils.CHART_SOURCE_DEFAULT
  423. tmpChartInfo.DateType = curveConf.DateType
  424. tmpChartInfo.StartDate = startDate
  425. tmpChartInfo.EndDate = endDate
  426. tmpChartInfo.ChartType = utils.CHART_TYPE_CURVE
  427. resp.CurveData = response.CurveResp{
  428. ChartInfo: tmpChartInfo,
  429. EdbInfoList: tmpEdbList,
  430. }
  431. }
  432. br.Ret = 200
  433. br.Success = true
  434. br.Msg = "获取成功"
  435. br.Data = resp
  436. }
  437. // MultipleGraphConfigSaveChart
  438. // @Title 保存图表接口
  439. // @Description 保存图表接口
  440. // @Param request body request.SaveMultipleGraphChartReq true "type json string"
  441. // @Success Ret=200 返回图表id
  442. // @router /chart/save [post]
  443. func (this *LineFeaturesChartInfoController) MultipleGraphConfigSaveChart() {
  444. br := new(models.BaseResponse).Init()
  445. defer func() {
  446. this.Data["json"] = br
  447. this.ServeJSON()
  448. }()
  449. sysUser := this.SysUser
  450. if sysUser == nil {
  451. br.Msg = "请登录"
  452. br.ErrMsg = "请登录,SysUser Is Empty"
  453. br.Ret = 408
  454. return
  455. }
  456. var req request.SaveMultipleGraphChartReq
  457. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  458. if err != nil {
  459. br.Msg = "参数解析异常!"
  460. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  461. return
  462. }
  463. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  464. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  465. br.Msg = "系统处理中,请稍后重试!"
  466. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  467. return
  468. }
  469. defer func() {
  470. _ = utils.Rc.Delete(cacheKey)
  471. }()
  472. if req.MultipleGraphConfigId <= 0 {
  473. br.Msg = "配置未保存"
  474. br.IsSendEmail = false
  475. return
  476. }
  477. if req.IsSaveAs {
  478. CopyMultipleGraphConfigSaveChart(req, this, br, sysUser)
  479. return
  480. }
  481. edbInfoMapping, err := data_manage.GetChartEdbMappingByEdbInfoId(req.EdbInfoId)
  482. if err != nil {
  483. br.Msg = "获取失败"
  484. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  485. return
  486. }
  487. if edbInfoMapping == nil {
  488. br.Msg = "指标不存在"
  489. br.IsSendEmail = false
  490. return
  491. }
  492. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  493. if err != nil && err.Error() != utils.ErrNoRow() {
  494. br.Msg = `保存失败`
  495. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  496. return
  497. }
  498. err = nil
  499. var isAdd bool
  500. var chartInfoId int
  501. if multipleGraphConfigChartMapping == nil {
  502. isAdd = true
  503. } else {
  504. chartInfo, err := data_manage.GetChartInfoById(multipleGraphConfigChartMapping.ChartInfoId)
  505. if err != nil && err.Error() != utils.ErrNoRow() {
  506. br.Msg = `保存失败`
  507. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  508. return
  509. }
  510. // 说明图还在,没有被删除
  511. if chartInfo != nil {
  512. chartInfoId = multipleGraphConfigChartMapping.ChartInfoId
  513. req.ChartName = chartInfo.ChartName
  514. req.ClassifyId = chartInfo.ChartClassifyId
  515. }
  516. }
  517. var chartInfo *data_manage.ChartInfo
  518. var errMsg string
  519. isSendEmail := true
  520. curveConf := req.Curve
  521. var maxDate time.Time
  522. if edbInfoMapping.LatestDate != "" {
  523. latestDateT, _ := time.Parse(utils.FormatDate, edbInfoMapping.LatestDate)
  524. maxDate = latestDateT
  525. }
  526. startDate, endDate := utils.GetDateByDateTypeV2(curveConf.DateType, curveConf.StartDate, curveConf.EndDate, curveConf.StartYear, maxDate)
  527. switch req.Source {
  528. case utils.CHART_MULTIPLE_GRAPH_CURVE: // 曲线图
  529. curveConf := req.Curve
  530. chartSaveItemList := []*data_manage.ChartSaveItem{
  531. {
  532. EdbInfoId: edbInfoMapping.EdbInfoId,
  533. MaxData: curveConf.LeftMax,
  534. MinData: curveConf.LeftMin,
  535. IsOrder: false,
  536. IsAxis: 1,
  537. EdbInfoType: 1,
  538. //LeadValue: curveConf.LeadValue,
  539. //LeadUnit: curveConf.LeadUnit,
  540. ChartStyle: "spline",
  541. ChartColor: `#00F`,
  542. PredictChartColor: "",
  543. ChartWidth: 3,
  544. Source: utils.CHART_SOURCE_DEFAULT,
  545. },
  546. }
  547. if chartInfoId <= 0 {
  548. addChartReq := data_manage.AddChartInfoReq{
  549. ChartEdbInfoList: chartSaveItemList,
  550. ChartClassifyId: req.ClassifyId,
  551. ChartName: req.ChartName,
  552. ChartType: utils.CHART_TYPE_CURVE,
  553. DateType: curveConf.DateType,
  554. StartDate: curveConf.StartDate,
  555. EndDate: curveConf.EndDate,
  556. Calendar: "公历",
  557. LeftMin: fmt.Sprint(curveConf.LeftMin),
  558. LeftMax: fmt.Sprint(curveConf.LeftMax),
  559. }
  560. chartInfo, err, errMsg, isSendEmail = data.AddChartInfo(addChartReq, sysUser.AdminId, sysUser.RealName, this.Lang)
  561. } else {
  562. editChartReq := data_manage.EditChartInfoReq{
  563. ChartInfoId: multipleGraphConfigChartMapping.ChartInfoId,
  564. ChartName: req.ChartName,
  565. ChartClassifyId: req.ClassifyId,
  566. ChartEdbInfoList: chartSaveItemList,
  567. ChartType: utils.CHART_TYPE_CURVE,
  568. DateType: curveConf.DateType,
  569. StartDate: curveConf.StartDate,
  570. EndDate: curveConf.EndDate,
  571. Calendar: "公历",
  572. LeftMin: fmt.Sprint(curveConf.LeftMin),
  573. LeftMax: fmt.Sprint(curveConf.LeftMax),
  574. }
  575. chartInfo, err, errMsg, isSendEmail = data.EditChartInfo(editChartReq, sysUser, this.Lang)
  576. if err != nil {
  577. br.Msg = "保存失败"
  578. if errMsg != `` {
  579. br.Msg = errMsg
  580. }
  581. br.ErrMsg = err.Error()
  582. br.IsSendEmail = isSendEmail
  583. return
  584. }
  585. }
  586. default:
  587. var extraConfig string
  588. var chartSource int
  589. var leftMinVal, leftMaxVal, rightMinVal, rightMaxVal float64
  590. // 统计特征-标准差图表
  591. if req.Source == utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_STANDARD_DEVIATION {
  592. chartSource = utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION // 统计特征-标准差图表
  593. standardDeviationConf := req.StandardDeviation
  594. if standardDeviationConf.CalculateValue <= 0 {
  595. br.Msg = "滚动周期需要大于0"
  596. br.IsSendEmail = false
  597. return
  598. }
  599. extraConfig = fmt.Sprint(standardDeviationConf.CalculateValue)
  600. _, dataResp, err, errMsg := lineFeatureServ.GetStandardDeviationData(0, startDate, endDate, edbInfoMapping, req.StandardDeviation.CalculateValue)
  601. if err != nil {
  602. br.Msg = "保存失败"
  603. if errMsg != `` {
  604. br.Msg = errMsg
  605. }
  606. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  607. return
  608. }
  609. leftMinVal = dataResp.MinData
  610. leftMaxVal = dataResp.MaxData
  611. } else if req.Source == utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_PERCENTILE {
  612. chartSource = utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE // 统计特征-百分位图表
  613. percentileConf := req.Percentile
  614. if percentileConf.CalculateUnit == `` {
  615. br.Msg = "时间长度频度必选"
  616. br.IsSendEmail = false
  617. return
  618. }
  619. if percentileConf.CalculateValue <= 0 {
  620. br.Msg = "时间长度期数需要大于0"
  621. br.IsSendEmail = false
  622. return
  623. }
  624. extraConfigByte, _ := json.Marshal(percentileConf)
  625. extraConfig = string(extraConfigByte)
  626. // 获取图表中的指标数据
  627. _, dataResp, err, errMsg := lineFeatureServ.GetPercentileData(0, startDate, endDate, edbInfoMapping, req.Percentile.CalculateValue, req.Percentile.CalculateUnit, req.Percentile.PercentType)
  628. if err != nil && errMsg != `` {
  629. br.Msg = errMsg
  630. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  631. return
  632. }
  633. leftMinVal = dataResp.MinData
  634. leftMaxVal = dataResp.MaxData
  635. } else if req.Source == utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_FREQUENCY {
  636. chartSource = utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY // 统计特征-频率分布图表
  637. frequencyDistributionConf := req.FrequencyDistribution
  638. if frequencyDistributionConf.DateType <= 0 {
  639. br.Msg = "日期类型必选"
  640. br.IsSendEmail = false
  641. return
  642. }
  643. if frequencyDistributionConf.FrequencyValue <= 0 {
  644. br.Msg = "频段数必填"
  645. br.IsSendEmail = false
  646. return
  647. }
  648. extraConfigByte, _ := json.Marshal(frequencyDistributionConf)
  649. extraConfig = string(extraConfigByte)
  650. // 获取图表中的指标数据
  651. _, dataResp, err, errMsg := lineFeatureServ.GetFrequencyDistributionData(0, edbInfoMapping, req.FrequencyDistribution.DateType, req.FrequencyDistribution.FrequencyValue, req.FrequencyDistribution.StartDate, req.FrequencyDistribution.EndDate)
  652. if err != nil && errMsg != `` {
  653. br.Msg = errMsg
  654. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  655. return
  656. }
  657. leftMinVal = dataResp.LeftMinValue
  658. leftMaxVal = dataResp.LeftMaxValue
  659. rightMinVal = dataResp.RightMinValue
  660. rightMaxVal = dataResp.RightMaxValue
  661. }
  662. if chartSource <= 0 {
  663. br.Msg = "错误的来源"
  664. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  665. br.IsSendEmail = false
  666. return
  667. }
  668. if chartInfoId <= 0 {
  669. addChartReq := data_manage.AddChartInfoReq{
  670. DateType: curveConf.DateType,
  671. StartDate: curveConf.StartDate,
  672. EndDate: curveConf.StartDate,
  673. ChartClassifyId: req.ClassifyId,
  674. ChartName: req.ChartName,
  675. ChartType: utils.CHART_TYPE_CURVE,
  676. Calendar: "公历",
  677. LeftMin: fmt.Sprint(leftMinVal),
  678. LeftMax: fmt.Sprint(leftMaxVal),
  679. RightMin: fmt.Sprint(rightMinVal),
  680. RightMax: fmt.Sprint(rightMaxVal),
  681. ExtraConfig: extraConfig,
  682. }
  683. chartInfo, err, errMsg, isSendEmail = lineFeatureServ.AddChartInfo(addChartReq, edbInfoMapping, chartSource, sysUser, this.Lang)
  684. } else {
  685. editChartReq := data_manage.EditChartInfoReq{
  686. ChartInfoId: multipleGraphConfigChartMapping.ChartInfoId,
  687. ChartName: req.ChartName,
  688. ChartClassifyId: req.ClassifyId,
  689. ChartType: utils.CHART_TYPE_CURVE,
  690. DateType: curveConf.DateType,
  691. StartDate: curveConf.StartDate,
  692. EndDate: curveConf.StartDate,
  693. Calendar: "公历",
  694. LeftMin: fmt.Sprint(leftMinVal),
  695. LeftMax: fmt.Sprint(leftMaxVal),
  696. RightMin: fmt.Sprint(rightMinVal),
  697. RightMax: fmt.Sprint(rightMaxVal),
  698. ExtraConfig: extraConfig,
  699. }
  700. chartInfo, err, errMsg, isSendEmail = lineFeatureServ.EditChartInfo(editChartReq, edbInfoMapping, sysUser, this.Lang)
  701. if err != nil {
  702. br.Msg = "保存失败"
  703. if errMsg != `` {
  704. br.Msg = errMsg
  705. }
  706. br.ErrMsg = err.Error()
  707. br.IsSendEmail = isSendEmail
  708. return
  709. }
  710. }
  711. }
  712. if err != nil {
  713. br.Msg = "保存失败"
  714. if errMsg != `` {
  715. br.Msg = errMsg
  716. }
  717. br.ErrMsg = err.Error()
  718. br.IsSendEmail = isSendEmail
  719. return
  720. }
  721. // 如果图没有建立关联关系,那么就需要添加关系
  722. if isAdd {
  723. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  724. //Id: 0,
  725. MultipleGraphConfigId: req.MultipleGraphConfigId,
  726. ChartInfoId: chartInfo.ChartInfoId,
  727. Source: req.Source,
  728. ModifyTime: time.Now(),
  729. CreateTime: time.Now(),
  730. }
  731. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  732. if err != nil {
  733. br.Msg = "保存失败"
  734. br.ErrMsg = "保存配置与图表的关联关系失败,ERR:" + err.Error()
  735. return
  736. }
  737. } else if multipleGraphConfigChartMapping != nil {
  738. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  739. multipleGraphConfigChartMapping.ModifyTime = time.Now()
  740. err = multipleGraphConfigChartMapping.Update([]string{"ChartInfoId", "ModifyTime"})
  741. if err != nil {
  742. br.Msg = "保存失败"
  743. br.ErrMsg = "保存配置与图表的关联关系失败,ERR:" + err.Error()
  744. return
  745. }
  746. }
  747. //新增操作日志
  748. {
  749. chartLog := new(data_manage.ChartInfoLog)
  750. chartLog.ChartInfoId = chartInfo.ChartInfoId
  751. chartLog.ChartName = req.ChartName
  752. chartLog.ChartClassifyId = req.ClassifyId
  753. chartLog.SysUserId = sysUser.AdminId
  754. chartLog.SysUserRealName = sysUser.RealName
  755. chartLog.UniqueCode = chartInfo.UniqueCode
  756. chartLog.CreateTime = time.Now()
  757. chartLog.Content = string(this.Ctx.Input.RequestBody)
  758. chartLog.Status = "统计特征保存图表"
  759. chartLog.Method = this.Ctx.Input.URI()
  760. go data_manage.AddChartInfoLog(chartLog)
  761. }
  762. resp := new(data_manage.AddChartInfoResp)
  763. resp.ChartInfoId = chartInfo.ChartInfoId
  764. resp.UniqueCode = chartInfo.UniqueCode
  765. resp.ChartType = chartInfo.ChartType
  766. br.Ret = 200
  767. br.Success = true
  768. br.Msg = "保存成功"
  769. br.Data = resp
  770. br.IsAddLog = true
  771. }
  772. // CopyMultipleGraphConfigSaveChart 图表另存为
  773. func CopyMultipleGraphConfigSaveChart(req request.SaveMultipleGraphChartReq, this *LineFeaturesChartInfoController, br *models.BaseResponse, sysUser *system.Admin) {
  774. edbInfoMapping, err := data_manage.GetChartEdbMappingByEdbInfoId(req.EdbInfoId)
  775. if err != nil {
  776. br.Msg = "获取失败"
  777. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  778. return
  779. }
  780. if edbInfoMapping == nil {
  781. br.Msg = "指标不存在"
  782. br.IsSendEmail = false
  783. return
  784. }
  785. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  786. if err != nil && err.Error() != utils.ErrNoRow() {
  787. br.Msg = `保存失败`
  788. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  789. return
  790. }
  791. // 原图
  792. oldChartInfo, err := data_manage.GetChartInfoById(multipleGraphConfigChartMapping.ChartInfoId)
  793. if err != nil && err.Error() != utils.ErrNoRow() {
  794. br.Msg = `保存失败`
  795. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  796. return
  797. }
  798. // 说明图还在,没有被删除
  799. if oldChartInfo == nil {
  800. br.Msg = "原图不存在"
  801. br.IsSendEmail = false
  802. }
  803. var chartInfo *data_manage.ChartInfo
  804. var errMsg string
  805. isSendEmail := true
  806. switch req.Source {
  807. case utils.CHART_MULTIPLE_GRAPH_CURVE: // 曲线图
  808. curveConf := req.Curve
  809. chartSaveItemList := []*data_manage.ChartSaveItem{
  810. {
  811. EdbInfoId: edbInfoMapping.EdbInfoId,
  812. MaxData: curveConf.LeftMax,
  813. MinData: curveConf.LeftMin,
  814. IsOrder: false,
  815. IsAxis: 1,
  816. EdbInfoType: 1,
  817. //LeadValue: curveConf.LeadValue,
  818. //LeadUnit: curveConf.LeadUnit,
  819. ChartStyle: "spline",
  820. ChartColor: `#00F`,
  821. PredictChartColor: "",
  822. ChartWidth: 3,
  823. Source: utils.CHART_SOURCE_DEFAULT,
  824. },
  825. }
  826. addChartReq := data_manage.AddChartInfoReq{
  827. ChartEdbInfoList: chartSaveItemList,
  828. ChartClassifyId: req.ClassifyId,
  829. ChartName: req.ChartName,
  830. ChartType: utils.CHART_TYPE_CURVE,
  831. DateType: curveConf.DateType,
  832. StartDate: curveConf.StartDate,
  833. EndDate: curveConf.EndDate,
  834. Calendar: "公历",
  835. LeftMin: fmt.Sprint(curveConf.LeftMin),
  836. LeftMax: fmt.Sprint(curveConf.LeftMax),
  837. }
  838. chartInfo, err, errMsg, isSendEmail = data.AddChartInfo(addChartReq, sysUser.AdminId, sysUser.RealName, this.Lang)
  839. default:
  840. if !utils.InArrayByInt([]int{utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_PERCENTILE, utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_FREQUENCY}, req.Source) {
  841. br.Msg = "错误的来源"
  842. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  843. br.IsSendEmail = false
  844. return
  845. }
  846. chartInfo, err, errMsg, isSendEmail = lineFeatureServ.CopyChartInfo(multipleGraphConfigChartMapping.MultipleGraphConfigId, req.Source, req.ClassifyId, req.ChartName, edbInfoMapping, oldChartInfo, sysUser, this.Lang)
  847. }
  848. if err != nil {
  849. br.Msg = "保存失败"
  850. if errMsg != `` {
  851. br.Msg = errMsg
  852. }
  853. br.ErrMsg = err.Error()
  854. br.IsSendEmail = isSendEmail
  855. return
  856. }
  857. //新增操作日志
  858. {
  859. chartLog := new(data_manage.ChartInfoLog)
  860. chartLog.ChartInfoId = chartInfo.ChartInfoId
  861. chartLog.ChartName = req.ChartName
  862. chartLog.ChartClassifyId = req.ClassifyId
  863. chartLog.SysUserId = sysUser.AdminId
  864. chartLog.SysUserRealName = sysUser.RealName
  865. chartLog.UniqueCode = chartInfo.UniqueCode
  866. chartLog.CreateTime = time.Now()
  867. chartLog.Content = string(this.Ctx.Input.RequestBody)
  868. chartLog.Status = "统计特征另存为图表"
  869. chartLog.Method = this.Ctx.Input.URI()
  870. go data_manage.AddChartInfoLog(chartLog)
  871. }
  872. resp := new(data_manage.AddChartInfoResp)
  873. resp.ChartInfoId = chartInfo.ChartInfoId
  874. resp.UniqueCode = chartInfo.UniqueCode
  875. resp.ChartType = chartInfo.ChartType
  876. br.Ret = 200
  877. br.Success = true
  878. br.Msg = "保存成功"
  879. br.Data = resp
  880. br.IsAddLog = true
  881. }
  882. // MultipleGraphConfigSaveEdb
  883. // @Title 保存指标接口
  884. // @Description 保存指标接口
  885. // @Param request body request.SaveMultipleGraphEdbReq true "type json string"
  886. // @Success Ret=200 返回指标id
  887. // @router /edb/save [post]
  888. func (this *LineFeaturesChartInfoController) MultipleGraphConfigSaveEdb() {
  889. br := new(models.BaseResponse).Init()
  890. defer func() {
  891. this.Data["json"] = br
  892. this.ServeJSON()
  893. }()
  894. sysUser := this.SysUser
  895. if sysUser == nil {
  896. br.Msg = "请登录"
  897. br.ErrMsg = "请登录,SysUser Is Empty"
  898. br.Ret = 408
  899. return
  900. }
  901. var req request.SaveMultipleGraphEdbReq
  902. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  903. if err != nil {
  904. br.Msg = "参数解析异常!"
  905. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  906. return
  907. }
  908. cacheKey := "CACHE_CHART_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  909. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  910. br.Msg = "系统处理中,请稍后重试!"
  911. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  912. return
  913. }
  914. defer func() {
  915. _ = utils.Rc.Delete(cacheKey)
  916. }()
  917. edbInfoMapping, err := data_manage.GetChartEdbMappingByEdbInfoId(req.EdbInfoId)
  918. if err != nil {
  919. br.Msg = "获取失败"
  920. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  921. return
  922. }
  923. if edbInfoMapping == nil {
  924. br.Msg = "指标不存在"
  925. br.IsSendEmail = false
  926. return
  927. }
  928. // 计算公式
  929. var formula string
  930. var edbSource int
  931. switch req.Source {
  932. case utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_STANDARD_DEVIATION:
  933. edbSource = utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION // 统计特征-标准差
  934. if edbInfoMapping.EdbInfoCategoryType == 1 {
  935. edbSource = utils.DATA_SOURCE_PREDICT_CALCULATE_STANDARD_DEVIATION // 统计特征-预测标准差
  936. }
  937. standardDeviationConf := req.StandardDeviation
  938. if standardDeviationConf.CalculateValue <= 0 {
  939. br.Msg = "滚动周期需要大于0"
  940. br.IsSendEmail = false
  941. return
  942. }
  943. formula = fmt.Sprint(standardDeviationConf.CalculateValue)
  944. case utils.CHART_MULTIPLE_GRAPH_LINE_FEATURE_PERCENTILE:
  945. edbSource = utils.DATA_SOURCE_CALCULATE_PERCENTILE // 统计特征-百分位图表
  946. if edbInfoMapping.EdbInfoCategoryType == 1 {
  947. edbSource = utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE // 统计特征-预测标准差
  948. }
  949. percentileConf := req.Percentile
  950. if percentileConf.CalculateUnit == `` {
  951. br.Msg = "时间长度频度必选"
  952. br.IsSendEmail = false
  953. return
  954. }
  955. if percentileConf.CalculateValue <= 0 {
  956. br.Msg = "时间长度期数需要大于0"
  957. br.IsSendEmail = false
  958. return
  959. }
  960. extraConfigByte, _ := json.Marshal(percentileConf)
  961. formula = string(extraConfigByte)
  962. default:
  963. br.Msg = "错误的来源"
  964. br.ErrMsg = "错误的来源,source:" + strconv.Itoa(req.Source)
  965. br.IsSendEmail = false
  966. return
  967. }
  968. multipleGraphConfigEdbMapping, err := data_manage.GetMultipleGraphConfigEdbMappingByIdAndSource(req.MultipleGraphConfigId, req.Source)
  969. if err != nil && err.Error() != utils.ErrNoRow() {
  970. br.Msg = `保存失败`
  971. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  972. return
  973. }
  974. err = nil
  975. var isAdd bool
  976. var edbInfoId int
  977. if multipleGraphConfigEdbMapping == nil {
  978. isAdd = true
  979. } else {
  980. edbInfo, err := data_manage.GetEdbInfoById(multipleGraphConfigEdbMapping.EdbInfoId)
  981. if err != nil && err.Error() != utils.ErrNoRow() {
  982. br.Msg = `保存失败`
  983. br.ErrMsg = "获取图表信息失败,ERR:" + err.Error()
  984. return
  985. }
  986. // 说明指标还在,没有被删除
  987. if edbInfo != nil && !req.IsSaveAs {
  988. edbInfoId = multipleGraphConfigEdbMapping.EdbInfoId
  989. req.EdbName = edbInfo.EdbName
  990. req.ClassifyId = edbInfo.ClassifyId
  991. req.Frequency = edbInfo.Frequency
  992. req.Unit = edbInfo.Unit
  993. }
  994. }
  995. var respItem *data.AddPredictEdbDataResponse
  996. // 0:普通,1:预测
  997. if edbInfoMapping.EdbInfoCategoryType == 0 {
  998. if edbInfoId <= 0 || req.IsSaveAs {
  999. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1000. AdminId: sysUser.AdminId,
  1001. AdminName: sysUser.RealName,
  1002. EdbName: req.EdbName,
  1003. Frequency: req.Frequency,
  1004. Unit: req.Unit,
  1005. ClassifyId: req.ClassifyId,
  1006. Formula: formula,
  1007. Source: edbSource,
  1008. FromEdbInfoId: edbInfoMapping.EdbInfoId,
  1009. Calendar: `公历`,
  1010. }
  1011. // 调用指标库去添加
  1012. reqJson, err := json.Marshal(req2)
  1013. if err != nil {
  1014. br.Msg = "参数解析异常!"
  1015. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1016. return
  1017. }
  1018. respItem, err = data.BatchSaveEdbCalculateData(string(reqJson), this.Lang)
  1019. if err != nil {
  1020. br.Msg = "新增失败"
  1021. br.ErrMsg = "新增失败,Err:" + err.Error()
  1022. return
  1023. }
  1024. } else {
  1025. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  1026. EdbInfoId: edbInfoId,
  1027. EdbName: req.EdbName,
  1028. Frequency: req.Frequency,
  1029. Unit: req.Unit,
  1030. ClassifyId: req.ClassifyId,
  1031. Formula: formula,
  1032. Source: edbSource,
  1033. FromEdbInfoId: edbInfoMapping.EdbInfoId,
  1034. Calendar: `公历`,
  1035. }
  1036. // 调用指标库去更新
  1037. reqJson, err := json.Marshal(req2)
  1038. if err != nil {
  1039. br.Msg = "参数解析异常!"
  1040. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1041. return
  1042. }
  1043. respItem, err = data.BatchEditEdbCalculateData(string(reqJson), this.Lang)
  1044. if err != nil {
  1045. br.Msg = "编辑失败"
  1046. br.ErrMsg = "编辑失败,Err:" + err.Error()
  1047. return
  1048. }
  1049. }
  1050. } else {
  1051. req2 := &data_manage.PredictEdbInfoCalculateBatchSaveReq{
  1052. AdminId: sysUser.AdminId,
  1053. AdminName: sysUser.RealName,
  1054. EdbName: req.EdbName,
  1055. Frequency: req.Frequency,
  1056. Unit: req.Unit,
  1057. ClassifyId: req.ClassifyId,
  1058. Formula: formula,
  1059. Source: edbSource,
  1060. FromEdbInfoId: edbInfoMapping.EdbInfoId,
  1061. Calendar: `公历`,
  1062. }
  1063. if edbInfoId > 0 && !req.IsSaveAs {
  1064. req2.EdbInfoId = edbInfoId
  1065. }
  1066. // 调用指标库去添加
  1067. reqJson, err := json.Marshal(req2)
  1068. if err != nil {
  1069. br.Msg = "参数解析异常!"
  1070. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1071. return
  1072. }
  1073. respItem, err = data.BatchSavePredictEdbData(string(reqJson), this.Lang)
  1074. if err != nil {
  1075. br.Msg = "新增失败"
  1076. br.ErrMsg = "新增失败,Err:" + err.Error()
  1077. return
  1078. }
  1079. }
  1080. if respItem == nil {
  1081. br.Msg = "保存失败"
  1082. br.ErrMsg = "保存失败"
  1083. return
  1084. }
  1085. if respItem.Ret != 200 {
  1086. br.Msg = respItem.Msg
  1087. br.ErrMsg = respItem.ErrMsg
  1088. return
  1089. }
  1090. resp := respItem.Data
  1091. //添加es
  1092. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  1093. // 如果不是另存为的话,那么需要建立配置与指标的关系
  1094. if !req.IsSaveAs {
  1095. // 如果指标没有建立关联关系,那么就需要添加关系
  1096. if isAdd {
  1097. multipleGraphConfigEdbMapping = &data_manage.MultipleGraphConfigEdbMapping{
  1098. //Id: 0,
  1099. MultipleGraphConfigId: req.MultipleGraphConfigId,
  1100. EdbInfoId: resp.EdbInfoId,
  1101. Source: req.Source,
  1102. ModifyTime: time.Now(),
  1103. CreateTime: time.Now(),
  1104. }
  1105. err = data_manage.AddMultipleGraphConfigEdbMapping(multipleGraphConfigEdbMapping)
  1106. if err != nil {
  1107. br.Msg = "保存失败"
  1108. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1109. return
  1110. }
  1111. } else if multipleGraphConfigEdbMapping != nil {
  1112. multipleGraphConfigEdbMapping.EdbInfoId = resp.EdbInfoId
  1113. multipleGraphConfigEdbMapping.ModifyTime = time.Now()
  1114. err = multipleGraphConfigEdbMapping.Update([]string{"EdbInfoId", "ModifyTime"})
  1115. if err != nil {
  1116. br.Msg = "保存失败"
  1117. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1118. return
  1119. }
  1120. }
  1121. }
  1122. //新增操作日志
  1123. //{
  1124. // edbLog := new(data_manage.EdbInfoLog)
  1125. // edbLog.EdbInfoId = resp.EdbInfoId
  1126. // edbLog.SourceName = edbInfo.SourceName
  1127. // edbLog.Source = edbInfo.Source
  1128. // edbLog.EdbCode = edbInfo.EdbCode
  1129. // edbLog.EdbName = req2.EdbName
  1130. // edbLog.ClassifyId = req2.ClassifyId
  1131. // edbLog.SysUserId = sysUser.AdminId
  1132. // edbLog.SysUserRealName = sysUser.RealName
  1133. // edbLog.CreateTime = time.Now()
  1134. // edbLog.Content = string(this.Ctx.Input.RequestBody)
  1135. // edbLog.Status = "多图配置保存指标"
  1136. // edbLog.Method = this.Ctx.Input.URI()
  1137. // go data_manage.AddEdbInfoLog(edbLog)
  1138. //}
  1139. br.Ret = 200
  1140. br.Success = true
  1141. br.Msg = "保存成功"
  1142. br.Data = resp
  1143. br.IsAddLog = true
  1144. }
  1145. // GetMultipleGraphConfig
  1146. // @Title 保存配置接口
  1147. // @Description 保存配置接口
  1148. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  1149. // @Success Ret=200 返回图表id
  1150. // @router /config/detail [get]
  1151. func (this *LineFeaturesChartInfoController) GetMultipleGraphConfig() {
  1152. br := new(models.BaseResponse).Init()
  1153. defer func() {
  1154. this.Data["json"] = br
  1155. this.ServeJSON()
  1156. }()
  1157. sysUser := this.SysUser
  1158. if sysUser == nil {
  1159. br.Msg = "请登录"
  1160. br.ErrMsg = "请登录,SysUser Is Empty"
  1161. br.Ret = 408
  1162. return
  1163. }
  1164. uniqueCode := this.GetString("UniqueCode")
  1165. if uniqueCode == "" {
  1166. br.Msg = "参数错误"
  1167. br.ErrMsg = "参数错误,uniqueCode is empty"
  1168. return
  1169. }
  1170. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  1171. if err != nil {
  1172. br.Msg = "获取失败"
  1173. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1174. return
  1175. }
  1176. if chartInfo == nil {
  1177. br.Msg = "获取失败"
  1178. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1179. return
  1180. }
  1181. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
  1182. if err != nil {
  1183. br.Msg = "获取失败"
  1184. br.ErrMsg = "获取滚动相关性图表的配置信息失败,Err:" + err.Error()
  1185. return
  1186. }
  1187. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1188. if err != nil {
  1189. br.Msg = `获取失败`
  1190. br.ErrMsg = `获取配置失败,ERR:` + err.Error()
  1191. return
  1192. }
  1193. // 生成的图表
  1194. multipleGraphConfigChartMappingList := make([]response.MultipleGraphConfigChartMapping, 0)
  1195. {
  1196. chartMappingList, err := data_manage.GetMultipleGraphConfigChartMappingListById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1197. if err != nil {
  1198. br.Msg = `获取失败`
  1199. br.ErrMsg = `获取关联图表配置失败,ERR:` + err.Error()
  1200. return
  1201. }
  1202. chartInfoIdList := make([]int, 0)
  1203. chartMappingMap := make(map[int]*data_manage.MultipleGraphConfigChartMapping)
  1204. for _, v := range chartMappingList {
  1205. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  1206. chartMappingMap[v.ChartInfoId] = v
  1207. }
  1208. chartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  1209. if err != nil {
  1210. br.Msg = `获取失败`
  1211. br.ErrMsg = `获取关联图表列表失败,ERR:` + err.Error()
  1212. return
  1213. }
  1214. for _, v := range chartList {
  1215. if tmpInfo, ok := chartMappingMap[v.ChartInfoId]; ok {
  1216. multipleGraphConfigChartMappingList = append(multipleGraphConfigChartMappingList, response.MultipleGraphConfigChartMapping{
  1217. ChartInfoId: tmpInfo.ChartInfoId,
  1218. Source: tmpInfo.Source,
  1219. MultipleLocationSource: tmpInfo.Source,
  1220. })
  1221. }
  1222. }
  1223. }
  1224. // 生成的指标
  1225. multipleGraphConfigEdbMappingList := make([]response.MultipleGraphConfigEdbMapping, 0)
  1226. {
  1227. edbMappingList, err := data_manage.GetMultipleGraphConfigEdbMappingListById(multipleGraphConfigChartMapping.MultipleGraphConfigId)
  1228. if err != nil {
  1229. br.Msg = `获取失败`
  1230. br.ErrMsg = `获取关联指标配置失败,ERR:` + err.Error()
  1231. return
  1232. }
  1233. edbInfoIdList := make([]int, 0)
  1234. edbMappingMap := make(map[int]*data_manage.MultipleGraphConfigEdbMapping)
  1235. for _, v := range edbMappingList {
  1236. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  1237. edbMappingMap[v.EdbInfoId] = v
  1238. }
  1239. edbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1240. if err != nil {
  1241. br.Msg = `获取失败`
  1242. br.ErrMsg = `获取关联图表列表失败,ERR:` + err.Error()
  1243. return
  1244. }
  1245. for _, v := range edbList {
  1246. if tmpInfo, ok := edbMappingMap[v.EdbInfoId]; ok {
  1247. multipleGraphConfigEdbMappingList = append(multipleGraphConfigEdbMappingList, response.MultipleGraphConfigEdbMapping{
  1248. EdbInfoId: tmpInfo.EdbInfoId,
  1249. Source: tmpInfo.Source,
  1250. MultipleLocationSource: tmpInfo.Source,
  1251. })
  1252. }
  1253. }
  1254. }
  1255. // 关联指标
  1256. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{multipleGraphConfig.EdbInfoIdA, multipleGraphConfig.EdbInfoIdB})
  1257. if err != nil {
  1258. br.Msg = "获取失败"
  1259. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1260. return
  1261. }
  1262. var config request.ConfigSave
  1263. err = json.Unmarshal([]byte(multipleGraphConfig.Curve), &config)
  1264. if err != nil {
  1265. br.Msg = `相关性配置异常`
  1266. br.ErrMsg = `相关性配置异常,ERR:` + err.Error()
  1267. return
  1268. }
  1269. resp := response.MultipleGraphConfigDetailResp{
  1270. MultipleGraphConfigId: multipleGraphConfigChartMapping.MultipleGraphConfigId,
  1271. Curve: config.Curve,
  1272. StandardDeviation: config.StandardDeviation,
  1273. Percentile: config.Percentile,
  1274. FrequencyDistribution: config.FrequencyDistribution,
  1275. EdbInfoList: mappingList,
  1276. ChartMappingList: multipleGraphConfigChartMappingList,
  1277. EdbMappingList: multipleGraphConfigEdbMappingList,
  1278. }
  1279. br.Ret = 200
  1280. br.Success = true
  1281. br.Msg = "保存成功"
  1282. br.Data = resp
  1283. br.IsAddLog = true
  1284. }
  1285. // Move
  1286. // @Title 移动图表接口
  1287. // @Description 移动图表接口
  1288. // @Success 200 {object} data_manage.MoveChartInfoReq
  1289. // @router /chart_info/move [post]
  1290. func (this *LineFeaturesChartInfoController) Move() {
  1291. br := new(models.BaseResponse).Init()
  1292. defer func() {
  1293. this.Data["json"] = br
  1294. this.ServeJSON()
  1295. }()
  1296. sysUser := this.SysUser
  1297. if sysUser == nil {
  1298. br.Msg = "请登录"
  1299. br.ErrMsg = "请登录,SysUser Is Empty"
  1300. br.Ret = 408
  1301. return
  1302. }
  1303. var req data_manage.MoveChartInfoReq
  1304. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1305. if err != nil {
  1306. br.Msg = "参数解析异常!"
  1307. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1308. return
  1309. }
  1310. if req.ChartInfoId <= 0 {
  1311. br.Msg = "参数错误"
  1312. br.ErrMsg = "图表id小于等于0"
  1313. return
  1314. }
  1315. if req.ChartClassifyId <= 0 {
  1316. br.Msg = "请选择分类"
  1317. return
  1318. }
  1319. //判断分类是否存在
  1320. count, _ := data_manage.GetChartClassifyCountById(req.ChartClassifyId)
  1321. if count <= 0 {
  1322. br.Msg = "分类已被删除,不可移动,请刷新页面"
  1323. return
  1324. }
  1325. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  1326. if err != nil {
  1327. br.Msg = "移动失败"
  1328. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1329. return
  1330. }
  1331. if !utils.InArrayByInt([]int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, chartInfo.Source) {
  1332. br.Msg = "图表异常"
  1333. br.ErrMsg = "分类异常,不是统计特征的图表"
  1334. return
  1335. }
  1336. //如果改变了分类,那么移动该图表数据
  1337. if chartInfo.ChartClassifyId != req.ChartClassifyId {
  1338. //查询需要修改的分类下是否存在同一个图表名称
  1339. tmpChartInfo, tmpErr := data_manage.GetChartInfoByClassifyIdAndName(req.ChartClassifyId, chartInfo.ChartName)
  1340. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1341. br.Msg = "移动失败"
  1342. br.ErrMsg = "移动失败,Err:" + tmpErr.Error()
  1343. return
  1344. }
  1345. if tmpChartInfo != nil {
  1346. br.Msg = "移动失败,同一个分类下图表名称不允许重复"
  1347. br.ErrMsg = "移动失败,同一个分类下图表名称不允许重复"
  1348. return
  1349. }
  1350. err = data_manage.MoveChartInfo(req.ChartInfoId, req.ChartClassifyId)
  1351. if err != nil {
  1352. br.Msg = "移动失败"
  1353. br.ErrMsg = "移动失败,Err:" + err.Error()
  1354. return
  1355. }
  1356. }
  1357. //移动排序
  1358. updateCol := make([]string, 0)
  1359. //如果有传入 上一个兄弟节点分类id
  1360. if req.PrevChartInfoId > 0 {
  1361. prevChartInfo, err := data_manage.GetChartInfoById(req.PrevChartInfoId)
  1362. if err != nil {
  1363. br.Msg = "移动失败"
  1364. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  1365. return
  1366. }
  1367. //如果是移动在两个兄弟节点之间
  1368. if req.NextChartInfoId > 0 {
  1369. //下一个兄弟节点
  1370. nextChartInfo, err := data_manage.GetChartInfoById(req.NextChartInfoId)
  1371. if err != nil {
  1372. br.Msg = "移动失败"
  1373. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  1374. return
  1375. }
  1376. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1377. if prevChartInfo.Sort == nextChartInfo.Sort || prevChartInfo.Sort == chartInfo.Sort {
  1378. //变更兄弟节点的排序
  1379. updateSortStr := `sort + 2`
  1380. _ = data_manage.UpdateChartInfoSortByClassifyId(prevChartInfo.ChartClassifyId, prevChartInfo.Sort, prevChartInfo.ChartInfoId, []int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, updateSortStr)
  1381. } else {
  1382. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1383. if nextChartInfo.Sort-prevChartInfo.Sort == 1 {
  1384. //变更兄弟节点的排序
  1385. updateSortStr := `sort + 1`
  1386. _ = data_manage.UpdateChartInfoSortByClassifyId(prevChartInfo.ChartClassifyId, prevChartInfo.Sort, prevChartInfo.ChartInfoId, []int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, updateSortStr)
  1387. }
  1388. }
  1389. }
  1390. chartInfo.Sort = prevChartInfo.Sort + 1
  1391. chartInfo.ModifyTime = time.Now()
  1392. updateCol = append(updateCol, "Sort", "ModifyTime")
  1393. } else {
  1394. firstClassify, err := data_manage.GetFirstChartInfoByClassifyId(req.ChartClassifyId)
  1395. if err != nil && err.Error() != utils.ErrNoRow() {
  1396. br.Msg = "移动失败"
  1397. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  1398. return
  1399. }
  1400. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1401. if firstClassify != nil && firstClassify.Sort == 0 {
  1402. updateSortStr := ` sort + 1 `
  1403. _ = data_manage.UpdateChartInfoSortByClassifyId(firstClassify.ChartClassifyId, 0, firstClassify.ChartInfoId-1, []int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, updateSortStr)
  1404. }
  1405. chartInfo.Sort = 0 //那就是排在第一位
  1406. chartInfo.ModifyTime = time.Now()
  1407. updateCol = append(updateCol, "Sort", "ModifyTime")
  1408. }
  1409. //更新
  1410. if len(updateCol) > 0 {
  1411. err = chartInfo.Update(updateCol)
  1412. if err != nil {
  1413. br.Msg = "移动失败"
  1414. br.ErrMsg = "修改失败,Err:" + err.Error()
  1415. return
  1416. }
  1417. }
  1418. if err != nil {
  1419. br.Msg = "移动失败"
  1420. br.ErrMsg = "修改失败,Err:" + err.Error()
  1421. return
  1422. }
  1423. //添加es数据
  1424. go data.EsAddOrEditChartInfo(req.ChartInfoId)
  1425. //修改my eta es数据
  1426. go data.EsAddOrEditMyChartInfoByChartInfoId(req.ChartInfoId)
  1427. //新增操作日志
  1428. {
  1429. chartLog := new(data_manage.ChartInfoLog)
  1430. chartLog.ChartName = chartInfo.ChartName
  1431. chartLog.ChartInfoId = req.ChartInfoId
  1432. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  1433. chartLog.SysUserId = sysUser.AdminId
  1434. chartLog.SysUserRealName = sysUser.RealName
  1435. chartLog.UniqueCode = chartInfo.UniqueCode
  1436. chartLog.CreateTime = time.Now()
  1437. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1438. chartLog.Status = "移动统计特征图表"
  1439. chartLog.Method = this.Ctx.Input.URL()
  1440. go data_manage.AddChartInfoLog(chartLog)
  1441. }
  1442. br.Ret = 200
  1443. br.Success = true
  1444. br.Msg = "移动成功"
  1445. }
  1446. // List
  1447. // @Title 统计特征图表列表接口
  1448. // @Description 统计特征图表列表接口
  1449. // @Param PageSize query int true "每页数据条数"
  1450. // @Param CurrentIndex query int true "当前页页码,从1开始"
  1451. // @Param ChartClassifyId query int true "分类id"
  1452. // @Param Keyword query string true "搜索关键词"
  1453. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1454. // @Param Source query int true "图表类型,3:统计特征,4:滚动统计特征"
  1455. // @Success 200 {object} data_manage.ChartListResp
  1456. // @router /chart_info/list [get]
  1457. func (this *LineFeaturesChartInfoController) List() {
  1458. br := new(models.BaseResponse).Init()
  1459. defer func() {
  1460. this.Data["json"] = br
  1461. this.ServeJSON()
  1462. }()
  1463. sysUser := this.SysUser
  1464. if sysUser == nil {
  1465. br.Msg = "请登录"
  1466. br.ErrMsg = "请登录,SysUser Is Empty"
  1467. br.Ret = 408
  1468. return
  1469. }
  1470. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  1471. pageSize, _ := this.GetInt("PageSize")
  1472. currentIndex, _ := this.GetInt("CurrentIndex")
  1473. keyword := this.GetString("KeyWord")
  1474. var total int
  1475. page := paging.GetPaging(currentIndex, pageSize, total)
  1476. var startSize int
  1477. if pageSize <= 0 {
  1478. pageSize = utils.PageSize20
  1479. }
  1480. if currentIndex <= 0 {
  1481. currentIndex = 1
  1482. }
  1483. startSize = paging.StartIndex(currentIndex, pageSize)
  1484. var condition string
  1485. var pars []interface{}
  1486. // 普通图表
  1487. condition += ` AND source in(?,?,?) `
  1488. pars = append(pars, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY)
  1489. if chartClassifyId > 0 {
  1490. chartClassifyId, err := data_manage.GetChartClassify(chartClassifyId)
  1491. if err != nil && err.Error() != utils.ErrNoRow() {
  1492. br.Msg = "获取图表信息失败"
  1493. br.ErrMsg = "获取信息失败,GetChartClassify,Err:" + err.Error()
  1494. return
  1495. }
  1496. condition += " AND chart_classify_id IN(" + chartClassifyId + ") "
  1497. }
  1498. var keyWordArr []string
  1499. if keyword != "" {
  1500. newKeyWord := strings.Split(keyword, " ")
  1501. keywordStr := strings.Replace(keyword, " ", "", -1)
  1502. condition += " AND ( "
  1503. condition += ` chart_name LIKE '%` + keywordStr + `%' OR chart_name_en LIKE '%` + keywordStr + `%' OR`
  1504. keyWordArr = append(keyWordArr, newKeyWord...)
  1505. if len(keyWordArr) > 0 {
  1506. for _, v := range keyWordArr {
  1507. condition += ` chart_name LIKE '%` + v + `%' OR chart_name_en LIKE '%` + v + `%' OR`
  1508. }
  1509. }
  1510. condition = strings.TrimRight(condition, "OR")
  1511. condition += " ) "
  1512. }
  1513. //只看我的
  1514. isShowMe, _ := this.GetBool("IsShowMe")
  1515. if isShowMe {
  1516. condition += ` AND sys_user_id = ? `
  1517. pars = append(pars, sysUser.AdminId)
  1518. }
  1519. // 获取当前账号的不可见指标
  1520. noPermissionChartIdList := make([]int, 0)
  1521. {
  1522. obj := data_manage.EdbInfoNoPermissionAdmin{}
  1523. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  1524. if err != nil && err.Error() != utils.ErrNoRow() {
  1525. br.Msg = "获取失败"
  1526. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1527. return
  1528. }
  1529. for _, v := range confList {
  1530. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  1531. }
  1532. }
  1533. lenNoPermissionChartIdList := len(noPermissionChartIdList)
  1534. if lenNoPermissionChartIdList > 0 {
  1535. condition += ` AND chart_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionChartIdList) + `) `
  1536. pars = append(pars, noPermissionChartIdList)
  1537. }
  1538. //获取图表信息
  1539. list, err := data_manage.GetChartListByCondition(condition, pars, startSize, pageSize)
  1540. if err != nil && err.Error() != utils.ErrNoRow() {
  1541. br.Success = true
  1542. br.Msg = "获取图表信息失败"
  1543. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1544. return
  1545. }
  1546. myChartList, err := data_manage.GetMyChartListByAdminId(sysUser.AdminId)
  1547. if err != nil && err.Error() != utils.ErrNoRow() {
  1548. br.Msg = "获取图表信息失败"
  1549. br.ErrMsg = "获取我的图表信息失败,Err:" + err.Error()
  1550. return
  1551. }
  1552. myChartMap := make(map[int]*data_manage.MyChartView)
  1553. for _, v := range myChartList {
  1554. myChartMap[v.ChartInfoId] = v
  1555. }
  1556. listLen := len(list)
  1557. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  1558. if listLen > 0 {
  1559. chartInfoIds := ""
  1560. for _, v := range list {
  1561. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  1562. }
  1563. if chartInfoIds != "" {
  1564. chartInfoIds = strings.Trim(chartInfoIds, ",")
  1565. //判断是否需要展示英文标识
  1566. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  1567. if e != nil {
  1568. br.Msg = "获取失败"
  1569. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  1570. return
  1571. }
  1572. for _, v := range edbList {
  1573. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  1574. }
  1575. }
  1576. }
  1577. for i := 0; i < listLen; i++ {
  1578. //判断是否需要展示英文标识
  1579. if _, ok := chartEdbMap[list[i].ChartInfoId]; ok {
  1580. list[i].IsEnChart = data.CheckIsEnChart(list[i].ChartNameEn, []*data_manage.ChartEdbInfoMapping{}, list[i].Source, list[i].ChartType)
  1581. }
  1582. if existItem, ok := myChartMap[list[i].ChartInfoId]; ok {
  1583. list[i].IsAdd = true
  1584. list[i].MyChartId = existItem.MyChartId
  1585. list[i].MyChartClassifyId = existItem.MyChartClassifyId
  1586. }
  1587. }
  1588. resp := new(data_manage.ChartListResp)
  1589. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  1590. items := make([]*data_manage.ChartInfoView, 0)
  1591. resp.Paging = page
  1592. resp.List = items
  1593. br.Ret = 200
  1594. br.Success = true
  1595. br.Msg = "获取成功"
  1596. return
  1597. }
  1598. dataCount, err := data_manage.GetChartListCountByCondition(condition, pars)
  1599. if err != nil && err.Error() != utils.ErrNoRow() {
  1600. br.Msg = "获取指标信息失败"
  1601. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  1602. return
  1603. }
  1604. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  1605. resp.Paging = page
  1606. resp.List = list
  1607. br.Ret = 200
  1608. br.Success = true
  1609. br.Msg = "获取成功"
  1610. br.Data = resp
  1611. }
  1612. // Detail
  1613. // @Title 获取图表详情
  1614. // @Description 获取图表详情接口
  1615. // @Param ChartInfoId query int true "图表id"
  1616. // @Success 200 {object} data_manage.ChartInfoDetailResp
  1617. // @router /chart_info/detail [get]
  1618. func (this *LineFeaturesChartInfoController) Detail() {
  1619. br := new(models.BaseResponse).Init()
  1620. defer func() {
  1621. this.Data["json"] = br
  1622. this.ServeJSON()
  1623. }()
  1624. sysUser := this.SysUser
  1625. if sysUser == nil {
  1626. br.Msg = "请登录"
  1627. br.ErrMsg = "请登录,SysUser Is Empty"
  1628. br.Ret = 408
  1629. return
  1630. }
  1631. chartInfoId, _ := this.GetInt("ChartInfoId")
  1632. if chartInfoId <= 0 {
  1633. br.Msg = "参数有误"
  1634. return
  1635. }
  1636. var err error
  1637. chartInfo := new(data_manage.ChartInfoView)
  1638. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  1639. if err != nil {
  1640. if err.Error() == utils.ErrNoRow() {
  1641. br.Msg = "图被删除,请刷新页面"
  1642. br.ErrMsg = "图被删除,请刷新页面,Err:" + err.Error()
  1643. return
  1644. }
  1645. br.Msg = "获取失败"
  1646. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1647. return
  1648. }
  1649. if chartInfo.ExtraConfig == `` {
  1650. br.Msg = "获取失败"
  1651. br.ErrMsg = "获取配置信息失败,图表的额外配置信息为空"
  1652. return
  1653. }
  1654. // 获取主题样式
  1655. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  1656. if err != nil {
  1657. br.Msg = "获取失败"
  1658. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  1659. return
  1660. }
  1661. chartInfo.ChartThemeStyle = chartTheme.Config
  1662. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  1663. // 获取图表关联指标
  1664. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1665. if err != nil {
  1666. br.Msg = "获取失败"
  1667. br.ErrMsg = "获取图表关联指标信息失败,Err:" + err.Error()
  1668. return
  1669. }
  1670. if len(edbMappingList) != 1 {
  1671. br.Msg = "获取失败"
  1672. br.ErrMsg = fmt.Sprint("获取图表关联指标信息异常,数量:", len(edbMappingList))
  1673. return
  1674. }
  1675. edbMapping := edbMappingList[0]
  1676. var edbList []*data_manage.ChartEdbInfoMapping
  1677. var errMsg string
  1678. var resultResp interface{}
  1679. switch chartInfo.Source {
  1680. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION:
  1681. calculateValue, err := strconv.Atoi(chartInfo.ExtraConfig)
  1682. if err != nil {
  1683. br.Msg = "获取失败"
  1684. br.ErrMsg = "格式化配置项失败,Err:" + err.Error()
  1685. return
  1686. }
  1687. var maxDate time.Time
  1688. if edbMapping.LatestDate != "" {
  1689. latestDateT, _ := time.Parse(utils.FormatDate, edbMapping.LatestDate)
  1690. maxDate = latestDateT
  1691. }
  1692. if chartInfo.ChartType == utils.CHART_TYPE_SEASON && chartInfo.DateType == utils.DateTypeNYears {
  1693. maxDate = time.Date(maxDate.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  1694. }
  1695. startDate, endDate := utils.GetDateByDateTypeV2(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate, chartInfo.StartYear, maxDate)
  1696. edbList, resultResp, err, errMsg = lineFeatureServ.GetStandardDeviationData(0, startDate, endDate, edbMapping, calculateValue)
  1697. case utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE:
  1698. var percentileConfig request.Percentile
  1699. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &percentileConfig)
  1700. if err != nil {
  1701. br.Msg = "获取失败"
  1702. br.ErrMsg = "格式化配置项失败,Err:" + err.Error()
  1703. return
  1704. }
  1705. var maxDate time.Time
  1706. if edbMapping.LatestDate != "" {
  1707. latestDateT, _ := time.Parse(utils.FormatDate, edbMapping.LatestDate)
  1708. maxDate = latestDateT
  1709. }
  1710. if chartInfo.ChartType == utils.CHART_TYPE_SEASON && chartInfo.DateType == utils.DateTypeNYears {
  1711. maxDate = time.Date(maxDate.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  1712. }
  1713. startDate, endDate := utils.GetDateByDateTypeV2(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate, chartInfo.StartYear, maxDate)
  1714. edbList, resultResp, err, errMsg = lineFeatureServ.GetPercentileData(0, startDate, endDate, edbMapping, percentileConfig.CalculateValue, percentileConfig.CalculateUnit, percentileConfig.PercentType)
  1715. case utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  1716. var frequencyDistributionConfig request.FrequencyDistribution
  1717. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &frequencyDistributionConfig)
  1718. if err != nil {
  1719. br.Msg = "获取失败"
  1720. br.ErrMsg = "格式化配置项失败,Err:" + err.Error()
  1721. return
  1722. }
  1723. // 获取图表中的指标数据
  1724. edbList, resultResp, err, errMsg = lineFeatureServ.GetFrequencyDistributionData(0, edbMapping, frequencyDistributionConfig.DateType, frequencyDistributionConfig.FrequencyValue, frequencyDistributionConfig.StartDate, frequencyDistributionConfig.EndDate)
  1725. default:
  1726. errMsg = `错误的图表`
  1727. err = errors.New(fmt.Sprint("错误的图表来源,source", chartInfo.Source))
  1728. }
  1729. if err != nil {
  1730. br.Msg = "获取失败"
  1731. if errMsg != `` {
  1732. br.Msg = errMsg
  1733. }
  1734. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1735. return
  1736. }
  1737. warnEdbList := make([]string, 0)
  1738. tmpEdbList := make([]data_manage.ChartEdbInfoMapping, 0)
  1739. for _, v := range edbList {
  1740. if v.IsNullData {
  1741. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  1742. }
  1743. chartEdbInfoMapping := *v
  1744. chartEdbInfoMapping.IsOrder = false
  1745. chartEdbInfoMapping.IsAxis = 1
  1746. //chartEdbInfoMapping.MinData = curveConf.LeftMin
  1747. //chartEdbInfoMapping.MaxData = curveConf.LeftMax
  1748. chartEdbInfoMapping.ChartColor = `#00F`
  1749. chartEdbInfoMapping.ChartWidth = 3
  1750. chartEdbInfoMapping.EdbInfoType = 1
  1751. tmpEdbList = append(tmpEdbList, chartEdbInfoMapping)
  1752. }
  1753. if len(warnEdbList) > 0 {
  1754. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  1755. }
  1756. // 判断是否加入我的图库
  1757. if chartInfoId > 0 && chartInfo != nil {
  1758. {
  1759. var myChartCondition string
  1760. var myChartPars []interface{}
  1761. myChartCondition += ` AND a.admin_id=? `
  1762. myChartPars = append(myChartPars, sysUser.AdminId)
  1763. myChartCondition += ` AND a.chart_info_id=? `
  1764. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  1765. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  1766. if err != nil && err.Error() != utils.ErrNoRow() {
  1767. br.Msg = "获取失败"
  1768. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1769. return
  1770. }
  1771. if myChartList != nil && len(myChartList) > 0 {
  1772. chartInfo.IsAdd = true
  1773. chartInfo.MyChartId = myChartList[0].MyChartId
  1774. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  1775. }
  1776. }
  1777. }
  1778. //图表操作权限
  1779. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  1780. //判断是否需要展示英文标识
  1781. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, []*data_manage.ChartEdbInfoMapping{}, chartInfo.Source, chartInfo.ChartType)
  1782. //chartInfo.UnitEn = edbInfoMappingA.UnitEn
  1783. // 另存为
  1784. isSaveAs := true
  1785. chartInfo.Button = data_manage.ChartViewButton{
  1786. IsEdit: chartInfo.IsEdit,
  1787. IsEnChart: chartInfo.IsEnChart,
  1788. IsAdd: chartInfo.IsAdd,
  1789. IsCopy: isSaveAs,
  1790. IsSetName: chartInfo.IsSetName,
  1791. }
  1792. chartInfo.HaveOperaAuth = true
  1793. // 指标权限
  1794. {
  1795. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  1796. classifyMap := make(map[int]*data_manage.EdbClassify)
  1797. // 分类
  1798. {
  1799. classifyIdList := make([]int, 0)
  1800. for _, v := range edbList {
  1801. classifyIdList = append(classifyIdList, v.ClassifyId)
  1802. }
  1803. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1804. if tmpErr != nil {
  1805. br.Msg = "获取失败"
  1806. br.ErrMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  1807. return
  1808. }
  1809. for _, v := range classifyList {
  1810. classifyMap[v.ClassifyId] = v
  1811. }
  1812. }
  1813. // 遍历到校验map
  1814. for _, v := range edbList {
  1815. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  1816. ClassifyId: v.ClassifyId,
  1817. IsJoinPermission: v.IsJoinPermission,
  1818. EdbInfoId: v.EdbInfoId,
  1819. }
  1820. }
  1821. // 获取所有有权限的指标和分类
  1822. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  1823. if err != nil {
  1824. br.Msg = "获取失败"
  1825. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1826. return
  1827. }
  1828. for _, v := range edbList {
  1829. // 数据权限
  1830. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  1831. if !ok {
  1832. continue
  1833. }
  1834. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  1835. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1836. }
  1837. }
  1838. }
  1839. resp := new(data_manage.ChartInfoDetailResp)
  1840. resp.ChartInfo = chartInfo
  1841. resp.EdbInfoList = edbList
  1842. resp.DataResp = resultResp
  1843. br.Ret = 200
  1844. br.Success = true
  1845. br.Msg = "获取成功"
  1846. br.Data = resp
  1847. }
  1848. // DetailFromUniqueCode
  1849. // @Title 根据编码获取图表详情
  1850. // @Description 根据编码获取图表详情接口
  1851. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  1852. // @Param IsCache query bool true "是否走缓存,默认false"
  1853. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  1854. // @router /chart_info/detail/from_unique_code [get]
  1855. func (this *LineFeaturesChartInfoController) DetailFromUniqueCode() {
  1856. br := new(models.BaseResponse).Init()
  1857. defer func() {
  1858. this.Data["json"] = br
  1859. this.ServeJSON()
  1860. }()
  1861. sysUser := this.SysUser
  1862. if sysUser == nil {
  1863. br.Msg = "请登录"
  1864. br.ErrMsg = "请登录,SysUser Is Empty"
  1865. br.Ret = 408
  1866. return
  1867. }
  1868. adminId := sysUser.AdminId
  1869. uniqueCode := this.GetString("UniqueCode")
  1870. if uniqueCode == "" {
  1871. br.Msg = "参数错误"
  1872. br.ErrMsg = "参数错误,uniqueCode is empty"
  1873. return
  1874. }
  1875. //是否走缓存
  1876. isCache, _ := this.GetBool("IsCache")
  1877. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  1878. status := true
  1879. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  1880. if err != nil {
  1881. if err.Error() == utils.ErrNoRow() {
  1882. status = false
  1883. } else {
  1884. br.Msg = "获取失败"
  1885. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1886. return
  1887. }
  1888. }
  1889. if chartInfo == nil {
  1890. status = false
  1891. }
  1892. if !status {
  1893. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  1894. resp.EdbInfoList = endInfoList
  1895. resp.ChartInfo = chartInfo
  1896. resp.Status = false
  1897. br.Ret = 200
  1898. br.Success = true
  1899. br.Msg = "获取成功"
  1900. br.Data = resp
  1901. return
  1902. }
  1903. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  1904. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  1905. if utils.Re == nil && isCache {
  1906. if utils.Re == nil && utils.Rc.IsExist(key) {
  1907. if d, e := utils.Rc.RedisBytes(key); e == nil {
  1908. err := json.Unmarshal(d, &resp)
  1909. if err == nil && resp != nil {
  1910. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  1911. var myCond string
  1912. var myPars []interface{}
  1913. myCond += ` AND a.admin_id=? `
  1914. myPars = append(myPars, adminId)
  1915. myCond += ` AND a.chart_info_id=? `
  1916. myPars = append(myPars, chartInfo.ChartInfoId)
  1917. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  1918. if err != nil && err.Error() != utils.ErrNoRow() {
  1919. br.Msg = "获取失败"
  1920. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1921. return
  1922. }
  1923. resp.ChartInfo.IsAdd = false
  1924. resp.ChartInfo.MyChartId = 0
  1925. resp.ChartInfo.MyChartClassifyId = ""
  1926. if myList != nil && len(myList) > 0 {
  1927. resp.ChartInfo.IsAdd = true
  1928. resp.ChartInfo.MyChartId = myList[0].MyChartId
  1929. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  1930. }
  1931. br.Ret = 200
  1932. br.Success = true
  1933. br.Msg = "获取成功"
  1934. br.Data = resp
  1935. return
  1936. }
  1937. }
  1938. }
  1939. }
  1940. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  1941. if !isOk {
  1942. br.Msg = msg
  1943. br.ErrMsg = errMsg
  1944. return
  1945. }
  1946. if !isOk {
  1947. br.Msg = msg
  1948. br.ErrMsg = errMsg
  1949. return
  1950. }
  1951. br.Ret = 200
  1952. br.Success = true
  1953. br.Msg = "获取成功"
  1954. br.Data = resp
  1955. }
  1956. // Refresh
  1957. // @Title 图表刷新接口
  1958. // @Description 图表刷新接口
  1959. // @Param ChartInfoId query int true "图表id"
  1960. // @Param UniqueCode query string true "唯一code"
  1961. // @Success Ret=200 刷新成功
  1962. // @router /chart_info/refresh [get]
  1963. func (this *LineFeaturesChartInfoController) Refresh() {
  1964. br := new(models.BaseResponse).Init()
  1965. defer func() {
  1966. this.Data["json"] = br
  1967. this.ServeJSON()
  1968. }()
  1969. sysUser := this.SysUser
  1970. if sysUser == nil {
  1971. br.Msg = "请登录"
  1972. br.ErrMsg = "请登录,SysUser Is Empty"
  1973. br.Ret = 408
  1974. return
  1975. }
  1976. chartInfoId, _ := this.GetInt("ChartInfoId")
  1977. uniqueCode := this.GetString("UniqueCode")
  1978. if chartInfoId <= 0 && uniqueCode == `` {
  1979. br.Msg = "参数错误"
  1980. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  1981. return
  1982. }
  1983. var chartInfo *data_manage.ChartInfo
  1984. var err error
  1985. if chartInfoId > 0 {
  1986. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  1987. } else {
  1988. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  1989. }
  1990. if err != nil {
  1991. if err.Error() == utils.ErrNoRow() {
  1992. br.Msg = "图表已被删除,无需刷新"
  1993. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1994. return
  1995. }
  1996. br.Msg = "刷新失败"
  1997. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1998. return
  1999. }
  2000. // 刷新图表的指标信息
  2001. err, isAsync := data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  2002. //清除图表缓存
  2003. {
  2004. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  2005. _ = utils.Rc.Delete(key)
  2006. }
  2007. br.Ret = 200
  2008. br.Success = true
  2009. br.Msg = "刷新成功"
  2010. if isAsync {
  2011. br.Msg = "图表关联指标较多,请10分钟后刷新页面查看最新数据"
  2012. }
  2013. }
  2014. // Copy
  2015. // @Title 复制并新增图表接口
  2016. // @Description 复制并新增图表接口
  2017. // @Param request body data_manage.CopyAddChartInfoReq true "type json string"
  2018. // @Success 200 {object} data_manage.AddChartInfoResp
  2019. // @router /chart_info/copy [post]
  2020. func (this *LineFeaturesChartInfoController) Copy() {
  2021. br := new(models.BaseResponse).Init()
  2022. defer func() {
  2023. this.Data["json"] = br
  2024. this.ServeJSON()
  2025. }()
  2026. sysUser := this.SysUser
  2027. if sysUser == nil {
  2028. br.Msg = "请登录"
  2029. br.ErrMsg = "请登录,SysUser Is Empty"
  2030. br.Ret = 408
  2031. return
  2032. }
  2033. deleteCache := true
  2034. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  2035. defer func() {
  2036. if deleteCache {
  2037. utils.Rc.Delete(cacheKey)
  2038. }
  2039. }()
  2040. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2041. deleteCache = false
  2042. br.Msg = "系统处理中,请稍后重试!"
  2043. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  2044. return
  2045. }
  2046. var req data_manage.CopyAddChartInfoReq
  2047. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2048. if err != nil {
  2049. br.Msg = "参数解析异常!"
  2050. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2051. return
  2052. }
  2053. // 获取原图表信息
  2054. oldChartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  2055. if err != nil {
  2056. br.Msg = "获取原图表信息失败"
  2057. br.ErrMsg = "获取原图表信息失败,Err:" + err.Error()
  2058. return
  2059. }
  2060. if !utils.InArrayByInt([]int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, oldChartInfo.Source) {
  2061. br.Msg = `错误的图表类型`
  2062. br.IsSendEmail = false
  2063. return
  2064. }
  2065. multipleGraphConfigChartMapping, err := data_manage.GetMultipleGraphConfigChartMappingByChartId(oldChartInfo.ChartInfoId)
  2066. if err != nil && err.Error() != utils.ErrNoRow() {
  2067. br.Msg = `保存失败`
  2068. br.ErrMsg = "获取配置与图表的关联关系失败,ERR:" + err.Error()
  2069. return
  2070. }
  2071. // 获取图表关联指标
  2072. edbMappingList, err := data_manage.GetChartEdbMappingList(oldChartInfo.ChartInfoId)
  2073. if err != nil {
  2074. br.Msg = "获取失败"
  2075. br.ErrMsg = "获取图表关联指标信息失败,Err:" + err.Error()
  2076. return
  2077. }
  2078. if len(edbMappingList) != 1 {
  2079. br.Msg = "获取失败"
  2080. br.ErrMsg = fmt.Sprint("获取图表关联指标信息异常,数量:", len(edbMappingList))
  2081. return
  2082. }
  2083. edbInfoMapping := edbMappingList[0]
  2084. // 批量设置图
  2085. chartInfo, err, errMsg, isSendEmail := lineFeatureServ.CopyChartInfo(multipleGraphConfigChartMapping.MultipleGraphConfigId, multipleGraphConfigChartMapping.Source, req.ChartClassifyId, req.ChartName, edbInfoMapping, oldChartInfo, sysUser, this.Lang)
  2086. if err != nil {
  2087. br.Msg = "复制失败"
  2088. if errMsg != `` {
  2089. br.Msg = errMsg
  2090. }
  2091. br.ErrMsg = "复制失败,ERR:" + err.Error()
  2092. br.IsSendEmail = isSendEmail
  2093. return
  2094. }
  2095. resp := data_manage.AddChartInfoResp{
  2096. ChartInfoId: chartInfo.ChartInfoId,
  2097. UniqueCode: chartInfo.UniqueCode,
  2098. ChartType: chartInfo.ChartType,
  2099. }
  2100. //新增操作日志
  2101. {
  2102. chartLog := new(data_manage.ChartInfoLog)
  2103. chartLog.ChartInfoId = chartInfo.ChartInfoId
  2104. chartLog.ChartName = chartInfo.ChartName
  2105. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  2106. chartLog.SysUserId = sysUser.AdminId
  2107. chartLog.SysUserRealName = sysUser.RealName
  2108. chartLog.UniqueCode = chartInfo.UniqueCode
  2109. chartLog.CreateTime = time.Now()
  2110. chartLog.Content = string(this.Ctx.Input.RequestBody)
  2111. chartLog.Status = "新增统计特征图表"
  2112. chartLog.Method = this.Ctx.Input.URI()
  2113. go data_manage.AddChartInfoLog(chartLog)
  2114. }
  2115. br.Ret = 200
  2116. br.Data = resp
  2117. br.Success = true
  2118. br.Msg = "保存成功"
  2119. br.IsAddLog = true
  2120. }
  2121. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  2122. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  2123. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  2124. adminId := sysUser.AdminId
  2125. // 指标数据map
  2126. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  2127. defer func() {
  2128. if isOk {
  2129. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  2130. {
  2131. //判断是否加入我的图库
  2132. var myChartCondition string
  2133. var myChartPars []interface{}
  2134. myChartCondition += ` AND a.admin_id=? `
  2135. myChartPars = append(myChartPars, adminId)
  2136. myChartCondition += ` AND a.chart_info_id=? `
  2137. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  2138. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  2139. if err != nil && err.Error() != utils.ErrNoRow() {
  2140. msg = "获取失败"
  2141. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  2142. return
  2143. }
  2144. if myChartList != nil && len(myChartList) > 0 {
  2145. chartInfo.IsAdd = true
  2146. chartInfo.MyChartId = myChartList[0].MyChartId
  2147. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  2148. }
  2149. }
  2150. resp.ChartInfo.HaveOperaAuth = true
  2151. // 指标权限
  2152. {
  2153. classifyMap := make(map[int]*data_manage.EdbClassify)
  2154. // 分类
  2155. {
  2156. classifyIdList := make([]int, 0)
  2157. for _, v := range resp.EdbInfoList {
  2158. classifyIdList = append(classifyIdList, v.ClassifyId)
  2159. }
  2160. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2161. if tmpErr != nil {
  2162. errMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  2163. return
  2164. }
  2165. for _, v := range classifyList {
  2166. classifyMap[v.ClassifyId] = v
  2167. }
  2168. }
  2169. // 指标
  2170. if len(edbClassifyPermissionMap) < 0 {
  2171. edbInfoIdList := make([]int, 0)
  2172. for _, v := range resp.EdbInfoList {
  2173. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  2174. }
  2175. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  2176. if tmpErr != nil {
  2177. errMsg = "获取指标列表失败,Err:" + tmpErr.Error()
  2178. return
  2179. }
  2180. for _, v := range edbInfoList {
  2181. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2182. ClassifyId: v.ClassifyId,
  2183. IsJoinPermission: v.IsJoinPermission,
  2184. EdbInfoId: v.EdbInfoId,
  2185. }
  2186. }
  2187. }
  2188. // 获取所有有权限的指标和分类
  2189. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  2190. if err != nil {
  2191. errMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2192. return
  2193. }
  2194. for _, v := range resp.EdbInfoList {
  2195. // 数据权限
  2196. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  2197. if !ok {
  2198. continue
  2199. }
  2200. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  2201. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2202. }
  2203. }
  2204. }
  2205. }
  2206. if !isOk {
  2207. if errMsg != "" {
  2208. uniqueCode := ""
  2209. if chartInfo != nil {
  2210. uniqueCode = chartInfo.UniqueCode
  2211. }
  2212. tmpTip := fmt.Sprintf("查询图表详情失败,line_feature.GetChartInfoDetailFromUniqueCode UniqueCode:%s,err:%s", uniqueCode, errMsg)
  2213. utils.FileLog.Info(tmpTip)
  2214. go alarm_msg.SendAlarmMsg(tmpTip, 3)
  2215. }
  2216. }
  2217. }()
  2218. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  2219. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  2220. if utils.Re == nil && isCache {
  2221. if utils.Re == nil && utils.Rc.IsExist(key) {
  2222. if chartData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  2223. err := json.Unmarshal(chartData, &resp)
  2224. if err != nil || resp == nil {
  2225. return
  2226. }
  2227. isOk = true
  2228. fmt.Println("source redis")
  2229. return
  2230. }
  2231. }
  2232. }
  2233. // 获取主题样式
  2234. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  2235. if err != nil {
  2236. msg = "获取失败"
  2237. errMsg = "获取主题信息失败,Err:" + err.Error()
  2238. return
  2239. }
  2240. chartInfo.ChartThemeStyle = chartTheme.Config
  2241. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  2242. if chartInfo.ExtraConfig == `` {
  2243. msg = "获取失败"
  2244. errMsg = "获取配置信息失败,图表的额外配置信息为空"
  2245. return
  2246. }
  2247. // 获取图表关联指标
  2248. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfo.ChartInfoId)
  2249. if err != nil {
  2250. msg = "获取失败"
  2251. errMsg = "获取图表关联指标信息失败,Err:" + err.Error()
  2252. return
  2253. }
  2254. if len(edbMappingList) != 1 {
  2255. msg = "获取失败"
  2256. errMsg = fmt.Sprint("获取图表关联指标信息异常,数量:", len(edbMappingList))
  2257. return
  2258. }
  2259. edbMapping := edbMappingList[0]
  2260. var edbList []*data_manage.ChartEdbInfoMapping
  2261. var resultResp interface{}
  2262. switch chartInfo.Source {
  2263. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION:
  2264. calculateValue, tmpErr := strconv.Atoi(chartInfo.ExtraConfig)
  2265. if tmpErr != nil {
  2266. msg = "获取失败"
  2267. errMsg = "格式化配置项失败,Err:" + tmpErr.Error()
  2268. return
  2269. }
  2270. var maxDate time.Time
  2271. if edbMapping.LatestDate != "" {
  2272. latestDateT, _ := time.Parse(utils.FormatDate, edbMapping.LatestDate)
  2273. maxDate = latestDateT
  2274. }
  2275. if chartInfo.ChartType == utils.CHART_TYPE_SEASON && chartInfo.DateType == utils.DateTypeNYears {
  2276. maxDate = time.Date(maxDate.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  2277. }
  2278. startDate, endDate := utils.GetDateByDateTypeV2(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate, chartInfo.StartYear, maxDate)
  2279. edbList, resultResp, err, msg = lineFeatureServ.GetStandardDeviationData(0, startDate, endDate, edbMapping, calculateValue)
  2280. case utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE:
  2281. var percentileConfig request.Percentile
  2282. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &percentileConfig)
  2283. if err != nil {
  2284. msg = "获取失败"
  2285. errMsg = "格式化配置项失败,Err:" + err.Error()
  2286. return
  2287. }
  2288. var maxDate time.Time
  2289. if edbMapping.LatestDate != "" {
  2290. latestDateT, _ := time.Parse(utils.FormatDate, edbMapping.LatestDate)
  2291. maxDate = latestDateT
  2292. }
  2293. if chartInfo.ChartType == utils.CHART_TYPE_SEASON && chartInfo.DateType == utils.DateTypeNYears {
  2294. maxDate = time.Date(maxDate.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  2295. }
  2296. startDate, endDate := utils.GetDateByDateTypeV2(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate, chartInfo.StartYear, maxDate)
  2297. edbList, resultResp, err, msg = lineFeatureServ.GetPercentileData(0, startDate, endDate, edbMapping, percentileConfig.CalculateValue, percentileConfig.CalculateUnit, percentileConfig.PercentType)
  2298. case utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  2299. var frequencyDistributionConfig request.FrequencyDistribution
  2300. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &frequencyDistributionConfig)
  2301. if err != nil {
  2302. msg = "获取失败"
  2303. errMsg = "格式化配置项失败,Err:" + err.Error()
  2304. return
  2305. }
  2306. // 获取图表中的指标数据
  2307. edbList, resultResp, err, errMsg = lineFeatureServ.GetFrequencyDistributionData(0, edbMapping, frequencyDistributionConfig.DateType, frequencyDistributionConfig.FrequencyValue, frequencyDistributionConfig.StartDate, frequencyDistributionConfig.EndDate)
  2308. default:
  2309. msg = `错误的图表`
  2310. errMsg = fmt.Sprint("错误的图表来源,source", chartInfo.Source)
  2311. return
  2312. }
  2313. if err != nil {
  2314. if msg == `` {
  2315. msg = "获取失败"
  2316. }
  2317. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2318. return
  2319. }
  2320. if chartInfo.ChartInfoId > 0 && chartInfo != nil {
  2321. //判断是否加入我的图库
  2322. {
  2323. var myChartCondition string
  2324. var myChartPars []interface{}
  2325. myChartCondition += ` AND a.admin_id=? `
  2326. myChartPars = append(myChartPars, sysUser.AdminId)
  2327. myChartCondition += ` AND a.chart_info_id=? `
  2328. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  2329. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  2330. if err != nil && err.Error() != utils.ErrNoRow() {
  2331. msg = "获取失败"
  2332. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  2333. return
  2334. }
  2335. if myChartList != nil && len(myChartList) > 0 {
  2336. chartInfo.IsAdd = true
  2337. chartInfo.MyChartId = myChartList[0].MyChartId
  2338. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  2339. }
  2340. }
  2341. }
  2342. //图表操作权限
  2343. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  2344. //判断是否需要展示英文标识
  2345. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, []*data_manage.ChartEdbInfoMapping{}, chartInfo.Source, chartInfo.ChartType)
  2346. //chartInfo.UnitEn = edbInfoMappingA.UnitEn
  2347. isSaveAs := true
  2348. // 另存为
  2349. chartInfo.Button = data_manage.ChartViewButton{
  2350. IsEdit: chartInfo.IsEdit,
  2351. IsEnChart: chartInfo.IsEnChart,
  2352. IsAdd: chartInfo.IsAdd,
  2353. IsCopy: isSaveAs,
  2354. IsSetName: chartInfo.IsSetName,
  2355. }
  2356. // 图表的指标来源
  2357. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  2358. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  2359. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  2360. resp.ChartInfo = chartInfo
  2361. resp.EdbInfoList = edbList
  2362. resp.DataResp = resultResp
  2363. resp.Status = true
  2364. // 遍历到校验map
  2365. for _, v := range edbList {
  2366. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2367. ClassifyId: v.ClassifyId,
  2368. IsJoinPermission: v.IsJoinPermission,
  2369. EdbInfoId: v.EdbInfoId,
  2370. }
  2371. }
  2372. // 将数据加入缓存
  2373. if utils.Re == nil {
  2374. d, _ := json.Marshal(resp)
  2375. _ = utils.Rc.Put(key, d, 2*time.Hour)
  2376. }
  2377. isOk = true
  2378. return
  2379. }
  2380. // EnInfoEdit
  2381. // @Title 编辑图表英文信息接口
  2382. // @Description 编辑图表英文信息接口
  2383. // @Param request body data_manage.EditChartEnInfoReq true "type json string"
  2384. // @Success Ret=200 编辑成功
  2385. // @router /chart_info/en/edit [post]
  2386. func (this *LineFeaturesChartInfoController) EnInfoEdit() {
  2387. br := new(models.BaseResponse).Init()
  2388. defer func() {
  2389. this.Data["json"] = br
  2390. this.ServeJSON()
  2391. }()
  2392. sysUser := this.SysUser
  2393. if sysUser == nil {
  2394. br.Msg = "请登录"
  2395. br.ErrMsg = "请登录,SysUser Is Empty"
  2396. br.Ret = 408
  2397. return
  2398. }
  2399. var req request.EditChartEnInfoReq
  2400. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2401. if err != nil {
  2402. br.Msg = "参数解析异常!"
  2403. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2404. return
  2405. }
  2406. req.ChartNameEn = strings.Trim(req.ChartNameEn, " ")
  2407. if req.ChartInfoId <= 0 {
  2408. br.Msg = "请选择图表"
  2409. return
  2410. }
  2411. if req.ChartNameEn == "" {
  2412. br.Msg = "请输入英文图表名称"
  2413. return
  2414. }
  2415. //判断指标名称是否存在
  2416. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  2417. if err != nil {
  2418. if err.Error() == utils.ErrNoRow() {
  2419. br.Msg = "图表已被删除,请刷新页面"
  2420. br.ErrMsg = "图表已被删除,请刷新页面"
  2421. return
  2422. }
  2423. br.Msg = "获取图表信息失败"
  2424. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2425. return
  2426. }
  2427. // 校验名称是否有重复
  2428. {
  2429. var condition string
  2430. var pars []interface{}
  2431. condition += " AND chart_info_id <> ? AND chart_name_en = ? AND source in (?,?,?)"
  2432. pars = append(pars, req.ChartInfoId, req.ChartNameEn, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY)
  2433. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  2434. if err != nil {
  2435. if err.Error() != utils.ErrNoRow() {
  2436. br.Msg = "判断英文图表名称是否存在失败"
  2437. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  2438. return
  2439. }
  2440. }
  2441. if err == nil && existItem.ChartInfoId > 0 {
  2442. br.Msg = existItem.ChartName + ":" + req.ChartNameEn + "图表名称已存在"
  2443. return
  2444. }
  2445. }
  2446. chartItem.ChartNameEn = req.ChartNameEn
  2447. chartItem.ModifyTime = time.Now().Local()
  2448. if e := chartItem.Update([]string{"ChartNameEn", "ModifyTime"}); e != nil {
  2449. br.Msg = "操作失败"
  2450. br.ErrMsg = "更新图表信息失败, Err: " + e.Error()
  2451. return
  2452. }
  2453. //添加es数据
  2454. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  2455. //修改my eta es数据
  2456. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  2457. //指标 修改es信息
  2458. //go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  2459. //新增操作日志
  2460. {
  2461. chartLog := new(data_manage.ChartInfoLog)
  2462. chartLog.ChartName = chartItem.ChartName
  2463. chartLog.ChartInfoId = req.ChartInfoId
  2464. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  2465. chartLog.SysUserId = sysUser.AdminId
  2466. chartLog.SysUserRealName = sysUser.RealName
  2467. chartLog.UniqueCode = chartItem.UniqueCode
  2468. chartLog.CreateTime = time.Now()
  2469. chartLog.Content = string(this.Ctx.Input.RequestBody)
  2470. chartLog.Status = "编辑统计特征图表英文信息"
  2471. chartLog.Method = this.Ctx.Input.URL()
  2472. go data_manage.AddChartInfoLog(chartLog)
  2473. }
  2474. // 清除缓存
  2475. if utils.Re == nil && utils.Rc != nil {
  2476. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  2477. _ = utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  2478. }
  2479. br.Ret = 200
  2480. br.Success = true
  2481. br.Msg = "编辑成功"
  2482. br.IsAddLog = true
  2483. }
  2484. // SearchByEs
  2485. // @Title 图表模糊搜索(从es获取)
  2486. // @Description 图表模糊搜索(从es获取)
  2487. // @Param Keyword query string true "图表名称"
  2488. // @Param IsShowMe query bool true "是否只看我的,true、false"
  2489. // @Param Source query int true "来源,3:统计特征,4:滚动统计特征,默认0:全部"
  2490. // @Success 200 {object} data_manage.ChartInfo
  2491. // @router /chart_info/search_by_es [get]
  2492. func (this *LineFeaturesChartInfoController) SearchByEs() {
  2493. br := new(models.BaseResponse).Init()
  2494. defer func() {
  2495. this.Data["json"] = br
  2496. this.ServeJSON()
  2497. }()
  2498. sysUser := this.SysUser
  2499. if sysUser == nil {
  2500. br.Msg = "请登录"
  2501. br.ErrMsg = "请登录,SysUser Is Empty"
  2502. br.Ret = 408
  2503. return
  2504. }
  2505. pageSize, _ := this.GetInt("PageSize")
  2506. currentIndex, _ := this.GetInt("CurrentIndex")
  2507. var startSize int
  2508. if pageSize <= 0 {
  2509. pageSize = utils.PageSize20
  2510. }
  2511. if currentIndex <= 0 {
  2512. currentIndex = 1
  2513. }
  2514. startSize = paging.StartIndex(currentIndex, pageSize)
  2515. keyword := this.GetString("Keyword")
  2516. //只看我的
  2517. isShowMe, _ := this.GetBool("IsShowMe")
  2518. showSysId := 0
  2519. if isShowMe {
  2520. showSysId = sysUser.AdminId
  2521. }
  2522. sourceList := []int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}
  2523. var searchList []*data_manage.ChartInfo
  2524. var total int64
  2525. var err error
  2526. // 获取当前账号的不可见指标
  2527. noPermissionChartIdList := make([]int, 0)
  2528. {
  2529. obj := data_manage.EdbInfoNoPermissionAdmin{}
  2530. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  2531. if err != nil && err.Error() != utils.ErrNoRow() {
  2532. br.Msg = "获取失败"
  2533. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2534. return
  2535. }
  2536. for _, v := range confList {
  2537. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  2538. }
  2539. }
  2540. if keyword != "" {
  2541. searchList, total, err = data.EsSearchChartInfo(keyword, showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  2542. } else {
  2543. total, searchList, err = data_manage.ChartInfoSearchByEmptyKeyWord(showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  2544. if err != nil && err.Error() != utils.ErrNoRow() {
  2545. br.Msg = "获取失败"
  2546. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2547. return
  2548. }
  2549. }
  2550. finalList := make([]*data_manage.ChartInfoMore, 0)
  2551. if len(searchList) > 0 {
  2552. chartInfoIds := ""
  2553. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  2554. for _, v := range searchList {
  2555. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  2556. }
  2557. if chartInfoIds != "" {
  2558. chartInfoIds = strings.Trim(chartInfoIds, ",")
  2559. //判断是否需要展示英文标识
  2560. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  2561. if e != nil {
  2562. br.Msg = "获取失败"
  2563. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  2564. return
  2565. }
  2566. for _, v := range edbList {
  2567. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  2568. }
  2569. }
  2570. for _, v := range searchList {
  2571. tmp := new(data_manage.ChartInfoMore)
  2572. tmp.ChartInfo = *v
  2573. // 图表数据权限
  2574. tmp.HaveOperaAuth = true
  2575. //判断是否需要展示英文标识
  2576. if _, ok := chartEdbMap[v.ChartInfoId]; ok {
  2577. tmp.IsEnChart = data.CheckIsEnChart(v.ChartNameEn, []*data_manage.ChartEdbInfoMapping{}, v.Source, v.ChartType)
  2578. }
  2579. finalList = append(finalList, tmp)
  2580. }
  2581. }
  2582. //新增搜索词记录
  2583. {
  2584. searchKeyword := new(data_manage.SearchKeyword)
  2585. searchKeyword.KeyWord = keyword
  2586. searchKeyword.CreateTime = time.Now()
  2587. go data_manage.AddSearchKeyword(searchKeyword)
  2588. }
  2589. page := paging.GetPaging(currentIndex, pageSize, int(total))
  2590. resp := data_manage.ChartInfoListByEsResp{
  2591. Paging: page,
  2592. List: finalList,
  2593. }
  2594. br.Ret = 200
  2595. br.Success = true
  2596. br.Msg = "获取成功"
  2597. br.Data = resp
  2598. }
  2599. // BaseInfoEdit
  2600. // @Title 编辑图表基础信息接口
  2601. // @Description 编辑图表基础信息接口
  2602. // @Param request body data_manage.EditChartInfoBaseReq true "type json string"
  2603. // @Success Ret=200 编辑成功
  2604. // @router /chart_info/base/edit [post]
  2605. func (this *LineFeaturesChartInfoController) BaseInfoEdit() {
  2606. br := new(models.BaseResponse).Init()
  2607. defer func() {
  2608. this.Data["json"] = br
  2609. this.ServeJSON()
  2610. }()
  2611. sysUser := this.SysUser
  2612. if sysUser == nil {
  2613. br.Msg = "请登录"
  2614. br.ErrMsg = "请登录,SysUser Is Empty"
  2615. br.Ret = 408
  2616. return
  2617. }
  2618. var req request.EditChartInfoBaseReq
  2619. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2620. if err != nil {
  2621. br.Msg = "参数解析异常!"
  2622. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2623. return
  2624. }
  2625. req.ChartName = strings.Trim(req.ChartName, " ")
  2626. if req.ChartInfoId <= 0 {
  2627. br.Msg = "请选择图表"
  2628. return
  2629. }
  2630. if req.ChartName == "" {
  2631. br.Msg = "请输入英文图表名称"
  2632. return
  2633. }
  2634. //判断指标名称是否存在
  2635. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  2636. if err != nil {
  2637. if err.Error() == utils.ErrNoRow() {
  2638. br.Msg = "图表已被删除,请刷新页面"
  2639. br.ErrMsg = "图表已被删除,请刷新页面"
  2640. return
  2641. }
  2642. br.Msg = "获取图表信息失败"
  2643. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2644. return
  2645. }
  2646. // 校验名称是否有重复
  2647. {
  2648. var condition string
  2649. var pars []interface{}
  2650. condition += " AND chart_info_id <> ? AND source in (?,?,?) "
  2651. pars = append(pars, req.ChartInfoId, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY)
  2652. switch this.Lang {
  2653. case utils.EnLangVersion:
  2654. condition += " AND chart_name_en = ? "
  2655. default:
  2656. condition += " AND chart_name = ? "
  2657. }
  2658. pars = append(pars, req.ChartName)
  2659. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  2660. if err != nil {
  2661. if err.Error() != utils.ErrNoRow() {
  2662. br.Msg = "判断英文图表名称是否存在失败"
  2663. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  2664. return
  2665. }
  2666. }
  2667. if err == nil && existItem.ChartInfoId > 0 {
  2668. br.Msg = existItem.ChartName + ":" + req.ChartName + "图表名称已存在"
  2669. return
  2670. }
  2671. }
  2672. switch this.Lang {
  2673. case utils.EnLangVersion:
  2674. chartItem.ChartNameEn = req.ChartName
  2675. default:
  2676. chartItem.ChartName = req.ChartName
  2677. }
  2678. chartItem.ModifyTime = time.Now().Local()
  2679. if e := chartItem.Update([]string{"ChartName", "ChartNameEn", "ModifyTime"}); e != nil {
  2680. br.Msg = "操作失败"
  2681. br.ErrMsg = "更新图表信息失败, Err: " + e.Error()
  2682. return
  2683. }
  2684. //添加es数据
  2685. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  2686. //修改my eta es数据
  2687. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  2688. //新增操作日志
  2689. {
  2690. chartLog := new(data_manage.ChartInfoLog)
  2691. chartLog.ChartName = chartItem.ChartName
  2692. chartLog.ChartInfoId = req.ChartInfoId
  2693. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  2694. chartLog.SysUserId = sysUser.AdminId
  2695. chartLog.SysUserRealName = sysUser.RealName
  2696. chartLog.UniqueCode = chartItem.UniqueCode
  2697. chartLog.CreateTime = time.Now()
  2698. chartLog.Content = string(this.Ctx.Input.RequestBody)
  2699. chartLog.Status = "编辑统计特征图表基础信息"
  2700. chartLog.Method = this.Ctx.Input.URL()
  2701. go data_manage.AddChartInfoLog(chartLog)
  2702. }
  2703. // 清除缓存
  2704. if utils.Re == nil && utils.Rc != nil {
  2705. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  2706. _ = utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  2707. }
  2708. br.Ret = 200
  2709. br.Success = true
  2710. br.Msg = "编辑成功"
  2711. br.IsAddLog = true
  2712. }