predict_edb_info.go 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/models/data_manage/request"
  8. "eta_gn/eta_api/models/data_manage/response"
  9. "eta_gn/eta_api/services"
  10. "eta_gn/eta_api/services/data"
  11. "eta_gn/eta_api/services/data/data_manage_permission"
  12. "eta_gn/eta_api/services/elastic"
  13. "eta_gn/eta_api/utils"
  14. "fmt"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "github.com/rdlucklib/rdluck_tools/paging"
  20. "github.com/shopspring/decimal"
  21. )
  22. // PredictEdbInfoController 预测指标
  23. type PredictEdbInfoController struct {
  24. controllers.BaseAuthController
  25. }
  26. // EdbChartList
  27. // @Title 预测指标列表接口
  28. // @Description 预测指标列表接口
  29. // @Param PageSize query int true "每页数据条数"
  30. // @Param CurrentIndex query int true "当前页页码,从1开始"
  31. // @Param ClassifyId query int true "分类id"
  32. // @Param AdminId query int false "创建人id"
  33. // @Param IsOnlyMe query bool false "是否只看我的"
  34. // @Success 200 {object} response.PredictEdbInfoListResp
  35. // @router /predict_edb_info/chart/list [get]
  36. func (this *PredictEdbInfoController) EdbChartList() {
  37. br := new(models.BaseResponse).Init()
  38. defer func() {
  39. this.Data["json"] = br
  40. this.ServeJSON()
  41. }()
  42. sysUser := this.SysUser
  43. if sysUser == nil {
  44. br.Msg = "请登录"
  45. br.ErrMsg = "请登录,SysUser Is Empty"
  46. br.Ret = 408
  47. return
  48. }
  49. classifyId, _ := this.GetInt("ClassifyId")
  50. pageSize, _ := this.GetInt("PageSize")
  51. currentIndex, _ := this.GetInt("CurrentIndex")
  52. adminId, _ := this.GetInt("AdminId")
  53. var total int
  54. page := paging.GetPaging(currentIndex, pageSize, total)
  55. var startSize int
  56. if pageSize <= 0 {
  57. pageSize = utils.PageSize20
  58. }
  59. if currentIndex <= 0 {
  60. currentIndex = 1
  61. }
  62. startSize = paging.StartIndex(currentIndex, pageSize)
  63. var condition string
  64. var pars []interface{}
  65. // 预测指标
  66. condition += ` AND edb_info_type = ? `
  67. pars = append(pars, 1)
  68. // 筛选分类
  69. if classifyId > 0 {
  70. childClassify, e, _ := data.GetChildClassifyByClassifyId(classifyId)
  71. if e != nil && !utils.IsErrNoRow(e) {
  72. br.Msg = "获取失败"
  73. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  74. return
  75. }
  76. var classifyIds []int
  77. for _, v := range childClassify {
  78. classifyIds = append(classifyIds, v.ClassifyId)
  79. }
  80. condition += fmt.Sprintf(` AND classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  81. pars = append(pars, classifyIds)
  82. }
  83. if adminId > 0 {
  84. condition += " AND sys_user_id = ? "
  85. pars = append(pars, adminId)
  86. }
  87. condition += " AND sys_user_id = ? "
  88. pars = append(pars, sysUser.AdminId)
  89. // 获取当前账号的不可见指标
  90. obj := data_manage.EdbInfoNoPermissionAdmin{}
  91. confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
  92. if err != nil && !utils.IsErrNoRow(err) {
  93. br.Msg = "获取失败"
  94. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  95. return
  96. }
  97. noPermissionEdbInfoIdList := make([]int, 0)
  98. for _, v := range confList {
  99. noPermissionEdbInfoIdList = append(noPermissionEdbInfoIdList, v.EdbInfoId)
  100. }
  101. noPermissionEdbInfoIdNum := len(noPermissionEdbInfoIdList)
  102. if noPermissionEdbInfoIdNum > 0 {
  103. condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(noPermissionEdbInfoIdNum) + `) `
  104. pars = append(pars, noPermissionEdbInfoIdList)
  105. }
  106. //获取指标信息
  107. dataCount, list, err := data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  108. if err != nil && !utils.IsErrNoRow(err) {
  109. br.Success = true
  110. br.Msg = "获取预测指标列表失败"
  111. br.ErrMsg = "获取预测指标列表失败,Err:" + err.Error()
  112. return
  113. }
  114. respList := make([]*data_manage.EdbInfoList, 0)
  115. if len(list) > 0 {
  116. classifyIdList := make([]int, 0)
  117. for _, v := range list {
  118. v.ConvertToResp()
  119. classifyIdList = append(classifyIdList, v.ClassifyId)
  120. }
  121. // 指标权限
  122. {
  123. // 当前的分类
  124. classifyMap := make(map[int]*data_manage.EdbClassify)
  125. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  126. if err != nil {
  127. if err != nil {
  128. br.Msg = "获取失败"
  129. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  130. return
  131. }
  132. }
  133. for _, v := range classifyList {
  134. classifyMap[v.ClassifyId] = v
  135. }
  136. // 获取所有有权限的指标和分类
  137. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  138. if err != nil {
  139. br.Msg = "获取失败"
  140. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  141. return
  142. }
  143. for _, v := range list {
  144. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  145. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  146. // 不展示无权限的
  147. if !v.HaveOperaAuth {
  148. continue
  149. }
  150. v.Button = data.GetEdbOpButton(sysUser, v.SysUserId, v.EdbType, v.EdbInfoType, v.HaveOperaAuth)
  151. respList = append(respList, v)
  152. }
  153. }
  154. }
  155. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  156. }
  157. resp := response.PredictEdbInfoListResp{
  158. Paging: page,
  159. List: respList,
  160. }
  161. br.Ret = 200
  162. br.Success = true
  163. br.Msg = "获取成功"
  164. br.Data = resp
  165. }
  166. // List
  167. // @Title 指标数据列表接口
  168. // @Description 指标数据列表接口
  169. // @Param PageSize query int true "每页数据条数"
  170. // @Param CurrentIndex query int true "当前页页码,从1开始"
  171. // @Param EdbInfoId query int true "指标id"
  172. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  173. // @Success 200 {object} data_manage.EdbInfoListResp
  174. // @router /predict_edb_info/list [get]
  175. func (this *PredictEdbInfoController) List() {
  176. br := new(models.BaseResponse).Init()
  177. defer func() {
  178. this.Data["json"] = br
  179. this.ServeJSON()
  180. }()
  181. edbInfoId, _ := this.GetInt("EdbInfoId")
  182. keyWord := this.GetString("KeyWord")
  183. pageSize, _ := this.GetInt("PageSize")
  184. currentIndex, _ := this.GetInt("CurrentIndex")
  185. var total int
  186. page := paging.GetPaging(currentIndex, pageSize, total)
  187. var startSize int
  188. if pageSize <= 0 {
  189. pageSize = utils.PageSize20
  190. }
  191. if currentIndex <= 0 {
  192. currentIndex = 1
  193. }
  194. startSize = paging.StartIndex(currentIndex, pageSize)
  195. var condition string
  196. var pars []interface{}
  197. // 指标类型,0:普通指标,1:预测指标
  198. condition += " AND edb_info_type=? "
  199. pars = append(pars, 1)
  200. if edbInfoId > 0 {
  201. condition += " AND edb_info_id=? "
  202. pars = append(pars, edbInfoId)
  203. }
  204. if keyWord != "" {
  205. condition += ` AND ( edb_code LIKE '%` + keyWord + `%' OR edb_name LIKE '%` + keyWord + `%' )`
  206. //pars = append(pars, keyWord)
  207. //pars = append(pars, keyWord)
  208. }
  209. resp := data_manage.EdbInfoListResp{}
  210. if condition == "" {
  211. item := new(data_manage.EdbInfoList)
  212. resp.Paging = page
  213. resp.Item = item
  214. br.Ret = 200
  215. br.Success = true
  216. br.Msg = "获取成功"
  217. return
  218. }
  219. //获取指标信息
  220. edbInfoItem, err := data_manage.GetEdbInfoByCondition(condition, pars)
  221. if err != nil && !utils.IsErrNoRow(err) {
  222. br.Success = true
  223. br.Msg = "获取指标信息失败"
  224. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  225. return
  226. }
  227. if edbInfoItem == nil || (err != nil && utils.IsErrNoRow(err)) || edbInfoItem.EdbInfoId <= 0 {
  228. item := new(data_manage.EdbInfoList)
  229. resp.Paging = page
  230. resp.Item = item
  231. br.Ret = 200
  232. br.Success = true
  233. br.Msg = "获取成功"
  234. return
  235. }
  236. edbInfoItem.ConvertToResp()
  237. //获取英文频度
  238. edbInfoItem.FrequencyEn = data.GetFrequencyEn(edbInfoItem.Frequency)
  239. // 未来的指标预测数据
  240. predictDataList := make([]*data_manage.EdbData, 0)
  241. var existCondition string
  242. var existPars []interface{}
  243. existCondition += " AND edb_info_id=? "
  244. existPars = append(existPars, edbInfoId)
  245. //查询出所有的关联指标
  246. sourceEdbInfoCalculateMappingList, err := data_manage.GetEdbInfoCalculateListByCondition(existCondition, existPars)
  247. if err != nil {
  248. br.Msg = "获取失败"
  249. br.ErrMsg = "获取预测指标关联信息失败,Err:" + err.Error()
  250. return
  251. }
  252. if len(sourceEdbInfoCalculateMappingList) <= 0 {
  253. br.Msg = "获取失败"
  254. br.ErrMsg = "查找预测指标关联信息失败"
  255. return
  256. }
  257. //获取指标数据(实际已生成)
  258. {
  259. dataCount, dataList, err := data.GetPageData(edbInfoItem.EdbInfoId, edbInfoItem.Source, edbInfoItem.SubSource, edbInfoItem.LatestDate, startSize, pageSize)
  260. if err != nil && !utils.IsErrNoRow(err) {
  261. br.Msg = "获取指标信息失败"
  262. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  263. return
  264. }
  265. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  266. edbInfoItem.DataList = dataList
  267. }
  268. // 第一页才需要 获取预测指标未来的数据
  269. if currentIndex == 1 {
  270. predictDataList, err = data_manage.GetAllEdbDataListData(edbInfoItem.EdbInfoId, edbInfoItem.Source, edbInfoItem.SubSource, edbInfoItem.LatestDate)
  271. if err != nil {
  272. br.Msg = "获取预测指标数据失败"
  273. br.ErrMsg = "获取预测指标数据失败" + err.Error()
  274. return
  275. }
  276. }
  277. // 如果预测数据没有,那么返回空数组
  278. if predictDataList == nil {
  279. predictDataList = make([]*data_manage.EdbData, 0)
  280. }
  281. edbInfoItem.PredictDataList = predictDataList
  282. // 如果实际数据没有,那么返回空数组
  283. if edbInfoItem.DataList == nil {
  284. edbInfoItem.DataList = make([]*data_manage.EdbData, 0)
  285. }
  286. //查询目录
  287. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfoItem.ClassifyId)
  288. if err != nil {
  289. br.Msg = err.Error()
  290. br.ErrMsg = errMsg
  291. return
  292. }
  293. // 权限校验
  294. {
  295. classifyMap := make(map[int]*data_manage.EdbClassifyIdItems)
  296. for _, v := range classifyList {
  297. classifyMap[v.ClassifyId] = v
  298. }
  299. if currClassify, ok := classifyMap[edbInfoItem.ClassifyId]; ok {
  300. haveOperaAuth, err := data_manage_permission.CheckEdbPermission(edbInfoItem.IsJoinPermission, currClassify.IsJoinPermission, this.SysUser.AdminId, edbInfoItem.EdbInfoId, edbInfoItem.ClassifyId)
  301. if err != nil {
  302. br.Msg = err.Error()
  303. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  304. return
  305. }
  306. edbInfoItem.HaveOperaAuth = haveOperaAuth
  307. }
  308. }
  309. resp.Paging = page
  310. resp.Item = edbInfoItem
  311. resp.ClassifyList = classifyList
  312. br.Ret = 200
  313. br.Success = true
  314. br.Msg = "获取成功"
  315. br.Data = resp
  316. }
  317. // Add
  318. // @Title 新增指标接口
  319. // @Description 新增指标接口
  320. // @Param request body request.AddPredictEdbInfoReq true "type json string"
  321. // @Success 200 {object} response.AddEdbInfoResp
  322. // @router /predict_edb_info/add [post]
  323. func (this *PredictEdbInfoController) Add() {
  324. br := new(models.BaseResponse).Init()
  325. defer func() {
  326. this.Data["json"] = br
  327. this.ServeJSON()
  328. }()
  329. sysUser := this.SysUser
  330. if sysUser == nil {
  331. br.Msg = "请登录"
  332. br.ErrMsg = "请登录,SysUser Is Empty"
  333. br.Ret = 408
  334. return
  335. }
  336. deleteCache := true
  337. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  338. defer func() {
  339. if deleteCache {
  340. _ = utils.Rc.Delete(cacheKey)
  341. }
  342. }()
  343. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  344. deleteCache = false
  345. br.Msg = "系统处理中,请稍后重试!"
  346. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  347. return
  348. }
  349. var req request.AddPredictEdbInfoReq
  350. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  351. if err != nil {
  352. br.Msg = "参数解析异常!"
  353. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  354. return
  355. }
  356. sourceEdbInfoId := req.SourceEdbInfoId
  357. if sourceEdbInfoId <= 0 {
  358. br.Msg = "请选择来源指标"
  359. br.IsSendEmail = false
  360. return
  361. }
  362. if req.EdbName == "" {
  363. br.Msg = "指标名称不能为空"
  364. br.IsSendEmail = false
  365. return
  366. }
  367. if req.ClassifyId <= 0 {
  368. br.Msg = "请选择分类"
  369. br.IsSendEmail = false
  370. return
  371. }
  372. if len(req.RuleList) <= 0 {
  373. br.Msg = "请填写预测规则"
  374. br.IsSendEmail = false
  375. return
  376. }
  377. // 根据截止日期重新排序
  378. ruleList := make([]request.RuleConfig, 0)
  379. {
  380. ruleMap := make(map[string]request.RuleConfig)
  381. dateIntList := make([]int64, 0)
  382. for _, v := range req.RuleList {
  383. confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
  384. if err != nil {
  385. br.Msg = "配置项中时间异常,请重新选择"
  386. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  387. br.IsSendEmail = false
  388. return
  389. }
  390. dateIntList = append(dateIntList, confEndDate.Unix())
  391. ruleMap[v.EndDate] = v
  392. }
  393. sort.Slice(dateIntList, func(i, j int) bool {
  394. return dateIntList[i] < dateIntList[j]
  395. })
  396. for _, dateInt := range dateIntList {
  397. currDateTime := time.Unix(dateInt, 0)
  398. item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
  399. if !ok {
  400. br.Msg = "配置项中时间异常,请重新选择"
  401. br.ErrMsg = "配置项中时间异常,请重新选择"
  402. br.IsSendEmail = false
  403. return
  404. }
  405. ruleList = append(ruleList, item)
  406. }
  407. }
  408. req.RuleList = ruleList
  409. //传入操作人
  410. req.AdminId = sysUser.AdminId
  411. req.AdminName = sysUser.RealName
  412. // 添加计算指标
  413. reqJson, err := json.Marshal(req)
  414. if err != nil {
  415. br.Msg = "参数解析异常!"
  416. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  417. return
  418. }
  419. respItem, err := data.SaveBasePredictEdbData(string(reqJson), this.Lang)
  420. if err != nil {
  421. br.Msg = "获取失败"
  422. br.ErrMsg = "获取失败,Err:" + err.Error()
  423. return
  424. }
  425. if respItem.Ret != 200 {
  426. br.Msg = respItem.Msg
  427. br.ErrMsg = respItem.ErrMsg
  428. return
  429. }
  430. resp := respItem.Data
  431. //添加es
  432. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  433. br.Ret = 200
  434. br.Success = true
  435. br.Msg = "保存成功"
  436. br.Data = resp
  437. br.IsAddLog = true
  438. }
  439. // ChartImageSet
  440. // @Title 设置指标的图表图片
  441. // @Description 设置指标的图表图片接口
  442. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  443. // @Success Ret=200 保存成功
  444. // @router /predict_edb_info/image/set [post]
  445. func (this *PredictEdbInfoController) ChartImageSet() {
  446. br := new(models.BaseResponse).Init()
  447. defer func() {
  448. this.Data["json"] = br
  449. this.ServeJSON()
  450. }()
  451. sysUser := this.SysUser
  452. if sysUser == nil {
  453. br.Msg = "请登录"
  454. br.ErrMsg = "请登录,SysUser Is Empty"
  455. br.Ret = 408
  456. return
  457. }
  458. var req data_manage.SetEdbInfoImageReq
  459. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  460. if err != nil {
  461. br.Msg = "参数解析异常!"
  462. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  463. return
  464. }
  465. if req.EdbInfoId > 0 && req.ImageUrl != "" {
  466. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  467. if err != nil {
  468. br.Msg = "保存失败"
  469. br.ErrMsg = "保存失败,Err:" + err.Error()
  470. if utils.IsErrNoRow(err) {
  471. br.Msg = "找不到该指标"
  472. br.ErrMsg = "找不到该指标"
  473. br.IsSendEmail = false
  474. }
  475. return
  476. }
  477. edbInfo.ChartImage = req.ImageUrl
  478. err = edbInfo.Update([]string{"ChartImage"})
  479. if err != nil {
  480. br.Msg = "保存失败"
  481. br.ErrMsg = "保存失败,Err:" + err.Error()
  482. return
  483. }
  484. //修改es数据
  485. go data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  486. }
  487. br.Ret = 200
  488. br.Success = true
  489. br.Msg = "保存成功"
  490. }
  491. // Edit
  492. // @Title 编辑指标接口
  493. // @Description 编辑指标接口
  494. // @Success 200 {object} request.EditEdbInfoReq
  495. // @router /predict_edb_info/edit [post]
  496. func (this *PredictEdbInfoController) Edit() {
  497. br := new(models.BaseResponse).Init()
  498. defer func() {
  499. this.Data["json"] = br
  500. this.ServeJSON()
  501. }()
  502. sysUser := this.SysUser
  503. if sysUser == nil {
  504. br.Msg = "请登录"
  505. br.ErrMsg = "请登录,SysUser Is Empty"
  506. br.Ret = 408
  507. return
  508. }
  509. var req request.AddPredictEdbInfoReq
  510. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  511. if err != nil {
  512. br.Msg = "参数解析异常!"
  513. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  514. return
  515. }
  516. edbInfoId := req.EdbInfoId
  517. if edbInfoId <= 0 {
  518. br.Msg = "请选择预测指标"
  519. br.IsSendEmail = false
  520. return
  521. }
  522. if req.EdbName == "" {
  523. br.Msg = "指标名称不能为空"
  524. br.IsSendEmail = false
  525. return
  526. }
  527. if req.ClassifyId <= 0 {
  528. br.Msg = "请选择分类"
  529. br.IsSendEmail = false
  530. return
  531. }
  532. if len(req.RuleList) <= 0 {
  533. br.Msg = "请填写预测规则"
  534. br.IsSendEmail = false
  535. return
  536. }
  537. // 根据截止日期重新排序
  538. ruleList := make([]request.RuleConfig, 0)
  539. {
  540. ruleMap := make(map[string]request.RuleConfig)
  541. dateIntList := make([]int64, 0)
  542. for _, v := range req.RuleList {
  543. confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
  544. if err != nil {
  545. br.Msg = "配置项中时间异常,请重新选择"
  546. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  547. br.IsSendEmail = false
  548. return
  549. }
  550. dateIntList = append(dateIntList, confEndDate.Unix())
  551. ruleMap[v.EndDate] = v
  552. }
  553. sort.Slice(dateIntList, func(i, j int) bool {
  554. return dateIntList[i] < dateIntList[j]
  555. })
  556. for _, dateInt := range dateIntList {
  557. currDateTime := time.Unix(dateInt, 0)
  558. item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
  559. if !ok {
  560. br.Msg = "配置项中时间异常,请重新选择"
  561. br.ErrMsg = "配置项中时间异常,请重新选择"
  562. br.IsSendEmail = false
  563. return
  564. }
  565. ruleList = append(ruleList, item)
  566. }
  567. }
  568. req.RuleList = ruleList
  569. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  570. if err != nil {
  571. br.Msg = "修改失败"
  572. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  573. return
  574. }
  575. var haveOperaAuth bool
  576. // 数据权限校验
  577. {
  578. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, this.SysUser.AdminId)
  579. if err != nil {
  580. br.Msg = err.Error()
  581. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  582. return
  583. }
  584. }
  585. // 编辑权限校验
  586. button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId, haveOperaAuth)
  587. if !button.OpButton {
  588. br.Msg = "无权限操作"
  589. br.IsSendEmail = false
  590. return
  591. }
  592. //传入操作人
  593. req.AdminId = sysUser.AdminId
  594. req.AdminName = sysUser.RealName
  595. // 添加计算指标
  596. reqJson, err := json.Marshal(req)
  597. if err != nil {
  598. br.Msg = "参数解析异常!"
  599. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  600. return
  601. }
  602. respItem, err := data.SaveBasePredictEdbData(string(reqJson), this.Lang)
  603. if err != nil {
  604. br.Msg = "获取失败"
  605. br.ErrMsg = "获取失败,Err:" + err.Error()
  606. return
  607. }
  608. if respItem.Ret != 200 {
  609. br.Msg = respItem.Msg
  610. br.ErrMsg = respItem.ErrMsg
  611. return
  612. }
  613. resp := respItem.Data
  614. //修改es
  615. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  616. // 添加操作日志
  617. newEdbRecord := new(data_manage.EdbInfoEditRecord)
  618. newEdbRecord.EdbName = req.EdbName
  619. // 频度和单位在此接口不处理
  620. newEdbRecord.Frequency = edbInfo.Frequency
  621. newEdbRecord.Unit = edbInfo.Unit
  622. newEdbRecord.OperateUserId = sysUser.AdminId
  623. newEdbRecord.OperateUserRealName = sysUser.RealName
  624. go data.AddEditEdbInfoRcord(edbInfo, newEdbRecord)
  625. // 刷新关联指标
  626. go data.EdbInfoRefreshAllFromBaseV2(resp.EdbInfoId, true, false)
  627. br.Ret = 200
  628. br.Success = true
  629. br.Msg = "修改成功"
  630. br.IsAddLog = true
  631. }
  632. // Refresh
  633. // @Title 指标刷新接口
  634. // @Description 指标刷新接口
  635. // @Param EdbInfoId query int true "指标id"
  636. // @Success Ret=200 刷新成功
  637. // @router /predict_edb_info/refresh [get]
  638. func (this *PredictEdbInfoController) Refresh() {
  639. br := new(models.BaseResponse).Init()
  640. defer func() {
  641. this.Data["json"] = br
  642. this.ServeJSON()
  643. }()
  644. sysUser := this.SysUser
  645. if sysUser == nil {
  646. br.Msg = "请登录"
  647. br.ErrMsg = "请登录,SysUser Is Empty"
  648. br.Ret = 408
  649. return
  650. }
  651. edbInfoId, _ := this.GetInt("EdbInfoId")
  652. if edbInfoId <= 0 {
  653. br.Msg = "参数错误"
  654. br.ErrMsg = "参数错误"
  655. return
  656. }
  657. _, isAsync, err, errMsg := data.RefreshPredictEdbInfo(edbInfoId, false)
  658. if errMsg != `` {
  659. br.Msg = errMsg
  660. br.ErrMsg = errMsg
  661. if err != nil {
  662. br.ErrMsg = err.Error()
  663. } else {
  664. br.IsSendEmail = false
  665. }
  666. return
  667. }
  668. br.Ret = 200
  669. br.Success = true
  670. br.Msg = "刷新成功"
  671. if isAsync {
  672. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  673. }
  674. }
  675. // Move
  676. // @Title 移动指标接口
  677. // @Description 移动指标接口
  678. // @Success 200 {object} data_manage.MoveEdbInfoReq
  679. // @router /predict_edb_info/move [post]
  680. func (this *PredictEdbInfoController) Move() {
  681. br := new(models.BaseResponse).Init()
  682. defer func() {
  683. this.Data["json"] = br
  684. this.ServeJSON()
  685. }()
  686. sysUser := this.SysUser
  687. if sysUser == nil {
  688. br.Msg = "请登录"
  689. br.ErrMsg = "请登录,SysUser Is Empty"
  690. br.Ret = 408
  691. return
  692. }
  693. var req data_manage.MoveEdbInfoReq
  694. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  695. if err != nil {
  696. br.Msg = "参数解析异常!"
  697. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  698. return
  699. }
  700. if req.EdbInfoId <= 0 {
  701. br.Msg = "参数错误"
  702. br.ErrMsg = "指标id小于等于0"
  703. return
  704. }
  705. if req.ClassifyId <= 0 {
  706. br.Msg = "请选择分类"
  707. return
  708. }
  709. // 移动预测指标
  710. err, errMsg := data.MovePredictEdbInfo(req.EdbInfoId, req.ClassifyId, req.PrevEdbInfoId, req.NextEdbInfoId, sysUser, string(this.Ctx.Input.RequestBody), this.Ctx.Input.URI())
  711. if errMsg != `` {
  712. br.Msg = errMsg
  713. br.ErrMsg = errMsg
  714. if err != nil {
  715. br.ErrMsg = errMsg + ";Err:" + err.Error()
  716. } else {
  717. br.IsSendEmail = false
  718. }
  719. return
  720. }
  721. br.Ret = 200
  722. br.Success = true
  723. br.Msg = "移动成功"
  724. }
  725. // Detail
  726. // @Title 获取指标详情
  727. // @Description 获取指标详情接口
  728. // @Param EdbInfoId query int true "指标id"
  729. // @Success 200 {object} response.PredictEdbInfo
  730. // @router /predict_edb_info/detail [get]
  731. func (this *PredictEdbInfoController) Detail() {
  732. br := new(models.BaseResponse).Init()
  733. defer func() {
  734. this.Data["json"] = br
  735. this.ServeJSON()
  736. }()
  737. sysUser := this.SysUser
  738. if sysUser == nil {
  739. br.Msg = "请登录"
  740. br.ErrMsg = "请登录,SysUser Is Empty"
  741. br.Ret = 408
  742. return
  743. }
  744. edbInfoId, _ := this.GetInt("EdbInfoId")
  745. if edbInfoId <= 0 {
  746. br.Msg = "参数错误"
  747. br.ErrMsg = "参数错误"
  748. return
  749. }
  750. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  751. if err != nil {
  752. br.Msg = "获取失败"
  753. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  754. return
  755. }
  756. var ruleType int
  757. var fixedValue float64
  758. calculateList := make([]*data_manage.EdbInfoCalculateDetail, 0)
  759. predictEdbConfList := make([]data_manage.PredictEdbConfDetail, 0)
  760. calculateList, err = data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
  761. if err != nil {
  762. br.Msg = "获取失败"
  763. br.ErrMsg = "获取失败,Err:" + err.Error()
  764. return
  765. }
  766. if edbInfo.EdbType == 1 {
  767. // 获取所有的指标预测规则配置
  768. tmpPredictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfoId)
  769. if err != nil && !utils.IsErrNoRow(err) {
  770. br.Msg = "获取失败"
  771. br.ErrMsg = "获取预测指标配置信息失败,Err:" + err.Error()
  772. return
  773. }
  774. for _, v := range tmpPredictEdbConfList {
  775. var tmpPredictEdbConfCalculateMappingDetail []*data_manage.PredictEdbConfCalculateMappingDetail
  776. if v.RuleType == 9 || v.RuleType == 14 {
  777. tmpPredictEdbConfCalculateMappingDetail, err = data_manage.GetPredictEdbConfCalculateMappingDetailListByConfigId(v.PredictEdbInfoId, v.ConfigId)
  778. if err != nil && !utils.IsErrNoRow(err) {
  779. br.Msg = "获取失败"
  780. br.ErrMsg = "获取预测指标配置与关联指标信息失败,Err:" + err.Error()
  781. return
  782. }
  783. }
  784. if len(tmpPredictEdbConfCalculateMappingDetail) <= 0 {
  785. tmpPredictEdbConfCalculateMappingDetail = make([]*data_manage.PredictEdbConfCalculateMappingDetail, 0)
  786. }
  787. tmp := data_manage.PredictEdbConfDetail{
  788. ConfigId: v.ConfigId,
  789. PredictEdbInfoId: v.PredictEdbInfoId,
  790. SourceEdbInfoId: v.SourceEdbInfoId,
  791. RuleType: v.RuleType,
  792. FixedValue: v.FixedValue,
  793. Value: v.Value,
  794. EmptyType: v.EmptyType,
  795. MaxEmptyType: v.MaxEmptyType,
  796. EndDate: v.EndDate.Format(utils.FormatDate),
  797. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  798. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  799. CalculateList: tmpPredictEdbConfCalculateMappingDetail,
  800. }
  801. predictEdbConfList = append(predictEdbConfList, tmp)
  802. }
  803. //calculateList, err = data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
  804. //if err != nil {
  805. // br.Msg = "获取失败"
  806. // br.ErrMsg = "获取失败,Err:" + err.Error()
  807. // return
  808. //}
  809. if len(predictEdbConfList) == 0 {
  810. br.Msg = "找不到该预测指标配置"
  811. br.ErrMsg = "找不到该预测指标配置"
  812. return
  813. }
  814. predictEdbConf := predictEdbConfList[0]
  815. ruleType = predictEdbConf.RuleType
  816. fixedValue = predictEdbConf.FixedValue
  817. }
  818. findEdbInfoIdList := make([]int, 0)
  819. for k, v := range calculateList {
  820. if v.LatestDate != v.EndDate && v.LatestDate != `` && v.EndDate != `` {
  821. tmpLatestDate, _ := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
  822. tmpEndDate, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  823. if tmpEndDate.After(tmpLatestDate) {
  824. findEdbInfoIdList = append(findEdbInfoIdList, v.FromEdbInfoId)
  825. }
  826. }
  827. v.EndValue = v.LatestValue
  828. calculateList[k] = v
  829. }
  830. if len(findEdbInfoIdList) > 0 {
  831. edbDataInsertConfigList, err := data_manage.GetEdbDataInsertConfigByEdbIdList(findEdbInfoIdList)
  832. if err != nil {
  833. br.Msg = "获取失败"
  834. br.ErrMsg = "获取失败插入值失败,Err:" + err.Error()
  835. return
  836. }
  837. edbDataInsertConfigMap := make(map[int]*data_manage.EdbDataInsertConfig)
  838. for _, v := range edbDataInsertConfigList {
  839. edbDataInsertConfigMap[v.EdbInfoId] = v
  840. }
  841. for k, v := range calculateList {
  842. edbDataInsertConfig, ok := edbDataInsertConfigMap[v.FromEdbInfoId]
  843. if !ok {
  844. continue
  845. }
  846. if v.EndDate != `` && v.EndDate == edbDataInsertConfig.Date.Format(utils.FormatDate) {
  847. t, tmpErr := decimal.NewFromString(edbDataInsertConfig.Value)
  848. if tmpErr != nil {
  849. continue
  850. }
  851. v.EndValue, _ = t.Float64()
  852. }
  853. calculateList[k] = v
  854. }
  855. }
  856. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  857. if err != nil {
  858. br.Msg = err.Error()
  859. br.ErrMsg = errMsg
  860. return
  861. }
  862. edbInfoResp, err := edbInfo.ConvertToResp()
  863. if err != nil {
  864. br.Msg = err.Error()
  865. br.ErrMsg = errMsg
  866. return
  867. }
  868. edbInfoFull := new(response.EdbInfoFull)
  869. edbInfoFull.EdbInfoResp = edbInfoResp
  870. // 权限校验
  871. {
  872. classifyMap := make(map[int]*data_manage.EdbClassifyIdItems)
  873. for _, v := range classifyList {
  874. classifyMap[v.ClassifyId] = v
  875. }
  876. if currClassify, ok := classifyMap[edbInfo.ClassifyId]; ok {
  877. haveOperaAuth, err := data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassify.IsJoinPermission, this.SysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  878. if err != nil {
  879. br.Msg = err.Error()
  880. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  881. return
  882. }
  883. edbInfoFull.HaveOperaAuth = haveOperaAuth
  884. }
  885. }
  886. // 拟合残差计算相关系数
  887. var correlationStr string
  888. if edbInfo.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC {
  889. var aEdbInfo, bEdbInfo *data_manage.EdbInfoCalculateDetail
  890. for _, v := range calculateList {
  891. if v.FromTag == "A" {
  892. aEdbInfo = v
  893. continue
  894. }
  895. if v.FromTag == "B" {
  896. bEdbInfo = v
  897. continue
  898. }
  899. }
  900. if aEdbInfo != nil && bEdbInfo != nil {
  901. edbInfoFromTagList := []data_manage.EdbInfoFromTag{
  902. {
  903. EdbInfoId: aEdbInfo.FromEdbInfoId,
  904. FromTag: aEdbInfo.FromTag,
  905. MoveValue: aEdbInfo.MoveValue,
  906. }, {
  907. EdbInfoId: bEdbInfo.FromEdbInfoId,
  908. FromTag: bEdbInfo.FromTag,
  909. MoveValue: bEdbInfo.MoveValue,
  910. },
  911. }
  912. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  913. AdminId: sysUser.AdminId,
  914. AdminName: sysUser.RealName,
  915. Formula: edbInfo.CalculateFormula,
  916. EdbInfoIdArr: edbInfoFromTagList,
  917. }
  918. // 计算
  919. val, err, _ := data.CallCalculateComputeCorrelation(req2, this.Lang)
  920. if err == nil {
  921. correlationStr = val
  922. }
  923. }
  924. }
  925. resp := response.PredictEdbInfo{
  926. EdbInfoFull: edbInfoFull,
  927. RuleType: ruleType,
  928. FixedValue: fixedValue,
  929. CalculateList: calculateList,
  930. RuleList: predictEdbConfList,
  931. CorrelationStr: correlationStr,
  932. ClassifyList: classifyList,
  933. }
  934. br.Ret = 200
  935. br.Success = true
  936. br.Msg = "获取成功"
  937. br.Data = resp
  938. }
  939. // FilterByEs
  940. // @Title 指标筛选接口
  941. // @Description 指标筛选接口
  942. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  943. // @Param FilterSource query int false "搜索来源:1:其他搜索,2:累计值转月值搜索,3:变频,4:基础指标,5:同比"
  944. // @Param Frequency query string false "频度"
  945. // @Param IsOnlyMe query bool false "是否只看我的"
  946. // @Param PageSize query int true "每页数据条数"
  947. // @Param CurrentIndex query int true "当前页页码,从1开始"
  948. // @Success 200 {object} data_manage.EdbInfoList
  949. // @router /predict_edb_info/filter_by_es [get]
  950. func (this *PredictEdbInfoController) FilterByEs() {
  951. br := new(models.BaseResponse).Init()
  952. defer func() {
  953. this.Data["json"] = br
  954. this.ServeJSON()
  955. }()
  956. pageSize, _ := this.GetInt("PageSize")
  957. currentIndex, _ := this.GetInt("CurrentIndex")
  958. var total int64
  959. var startSize int
  960. if pageSize <= 0 {
  961. pageSize = utils.PageSize20
  962. }
  963. if currentIndex <= 0 {
  964. currentIndex = 1
  965. }
  966. startSize = paging.StartIndex(currentIndex, pageSize)
  967. keyWord := this.GetString("Keyword")
  968. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  969. //兼容前端
  970. if keyWord == `` {
  971. keyWord = this.GetString("KeyWord")
  972. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  973. }
  974. filterSource, _ := this.GetInt("FilterSource")
  975. if filterSource <= 0 {
  976. filterSource = 1
  977. }
  978. //指标来源
  979. source, _ := this.GetInt("Source")
  980. frequency := this.GetString("Frequency") //频度
  981. isOnlyMe, _ := this.GetBool("IsOnlyMe")
  982. isOnlyMe = true
  983. var edbInfoList []*data_manage.EdbInfoList
  984. var err error
  985. // 无权限指标 和 无权限指标分类id
  986. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(this.SysUser.AdminId, utils.PREDICT_EDB_INFO_TYPE, 0)
  987. if err != nil {
  988. br.Msg = "获取失败"
  989. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  990. return
  991. }
  992. // 是否走ES
  993. isEs := false
  994. if keyWord != "" {
  995. var keyWordArr []string
  996. keyWordArr = append(keyWordArr, keyWord)
  997. newKeyWord := strings.Split(keyWord, " ")
  998. keyWordArr = append(keyWordArr, newKeyWord...)
  999. total, edbInfoList, err = elastic.SearchEdbInfoData(keyWord, startSize, pageSize, filterSource, source, frequency, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, []int{}, []int{}, []int{}, []int{1, 2}, 1, 0, this.SysUser.AdminId, map[string]string{})
  1000. isEs = true
  1001. } else {
  1002. var condition string
  1003. var pars []interface{}
  1004. // 预测指标
  1005. condition += ` AND edb_info_type = ? `
  1006. pars = append(pars, 1)
  1007. // 无权限指标id
  1008. lenNoPermissionEdbInfoIdList := len(noPermissionEdbInfoIdList)
  1009. if lenNoPermissionEdbInfoIdList > 0 {
  1010. condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbInfoIdList) + `) `
  1011. pars = append(pars, noPermissionEdbInfoIdList)
  1012. }
  1013. // 无权限指标分类id
  1014. lenNoPermissionEdbClassifyIdList := len(noPermissionEdbClassifyIdList)
  1015. if lenNoPermissionEdbClassifyIdList > 0 {
  1016. condition += ` AND classify_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbClassifyIdList) + `) `
  1017. pars = append(pars, noPermissionEdbClassifyIdList)
  1018. }
  1019. switch filterSource {
  1020. case 2:
  1021. condition += ` AND frequency='月度' `
  1022. case 3:
  1023. condition += ` AND frequency <> '日度' `
  1024. case 4:
  1025. condition += ` AND edb_type = 1 `
  1026. case 5:
  1027. condition += ` AND source = 32 ` //来源(预测同比值)
  1028. case 6:
  1029. condition += ` AND frequency != ? `
  1030. pars = append(pars, "年度")
  1031. }
  1032. //频度
  1033. if frequency != "" {
  1034. condition += ` AND frequency = ? `
  1035. pars = append(pars, frequency)
  1036. }
  1037. if isOnlyMe {
  1038. condition += " AND sys_user_id = ? "
  1039. pars = append(pars, this.SysUser.AdminId)
  1040. }
  1041. total, edbInfoList, err = data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  1042. }
  1043. if err != nil {
  1044. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1045. }
  1046. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1047. edbInfoListLen := len(edbInfoList)
  1048. classifyIdList := make([]int, 0)
  1049. for i := 0; i < edbInfoListLen; i++ {
  1050. edbInfoList[i].ConvertToResp()
  1051. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  1052. classifyIdList = append(classifyIdList, edbInfoList[i].ClassifyId)
  1053. }
  1054. // 当前列表中的分类map
  1055. classifyMap := make(map[int]*data_manage.EdbClassify)
  1056. if edbInfoListLen > 0 {
  1057. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1058. if err != nil {
  1059. br.Msg = "获取失败"
  1060. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  1061. return
  1062. }
  1063. for _, v := range classifyList {
  1064. classifyMap[v.ClassifyId] = v
  1065. }
  1066. // 获取所有有权限的指标和分类
  1067. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1068. if err != nil {
  1069. br.Msg = "获取失败"
  1070. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1071. return
  1072. }
  1073. // 如果是ES的话,需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  1074. if isEs {
  1075. edbInfoIdList := make([]int, 0)
  1076. for i := 0; i < edbInfoListLen; i++ {
  1077. edbInfoIdList = append(edbInfoIdList, edbInfoList[i].EdbInfoId)
  1078. tmpEdbInfo := edbInfoList[i]
  1079. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  1080. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1081. }
  1082. }
  1083. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1084. if err != nil {
  1085. br.Msg = "获取失败"
  1086. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1087. return
  1088. }
  1089. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1090. for _, v := range tmpEdbList {
  1091. edbInfoMap[v.EdbInfoId] = v
  1092. }
  1093. for i := 0; i < edbInfoListLen; i++ {
  1094. tmpEdbInfo, ok := edbInfoMap[edbInfoList[i].EdbInfoId]
  1095. if !ok {
  1096. continue
  1097. }
  1098. edbInfoList[i].IsJoinPermission = tmpEdbInfo.IsJoinPermission
  1099. }
  1100. }
  1101. // 权限校验
  1102. for i := 0; i < edbInfoListLen; i++ {
  1103. tmpEdbInfo := edbInfoList[i]
  1104. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  1105. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1106. }
  1107. }
  1108. }
  1109. for i := 0; i < edbInfoListLen; i++ {
  1110. for j := 0; j < edbInfoListLen; j++ {
  1111. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1112. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1113. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1114. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1115. }
  1116. }
  1117. }
  1118. //新增搜索词记录
  1119. {
  1120. searchKeyword := new(data_manage.SearchKeyword)
  1121. searchKeyword.KeyWord = keyWord
  1122. searchKeyword.CreateTime = time.Now()
  1123. go data_manage.AddSearchKeyword(searchKeyword)
  1124. }
  1125. resp := data_manage.EdbInfoFilterDataResp{
  1126. Paging: page,
  1127. List: edbInfoList,
  1128. }
  1129. br.Ret = 200
  1130. br.Success = true
  1131. br.Msg = "获取成功"
  1132. br.Data = resp
  1133. }
  1134. // RefreshAll
  1135. // @Title 指标全部刷新接口
  1136. // @Description 指标全部刷新接口
  1137. // @Param EdbInfoId query int true "指标id"
  1138. // @Success Ret=200 全部刷新成功
  1139. // @router /predict_edb_info/refresh/all [get]
  1140. func (this *PredictEdbInfoController) RefreshAll() {
  1141. br := new(models.BaseResponse).Init()
  1142. defer func() {
  1143. this.Data["json"] = br
  1144. this.ServeJSON()
  1145. }()
  1146. sysUser := this.SysUser
  1147. if sysUser == nil {
  1148. br.Msg = "请登录"
  1149. br.ErrMsg = "请登录,SysUser Is Empty"
  1150. br.Ret = 408
  1151. return
  1152. }
  1153. edbInfoId, _ := this.GetInt("EdbInfoId")
  1154. if edbInfoId <= 0 {
  1155. br.Msg = "参数错误"
  1156. br.ErrMsg = "参数错误"
  1157. return
  1158. }
  1159. _, isAsync, err, errMsg := data.RefreshPredictEdbInfo(edbInfoId, true)
  1160. if errMsg != `` {
  1161. br.Msg = errMsg
  1162. br.ErrMsg = errMsg
  1163. if err != nil {
  1164. br.ErrMsg = err.Error()
  1165. } else {
  1166. br.IsSendEmail = false
  1167. }
  1168. return
  1169. }
  1170. br.Ret = 200
  1171. br.Success = true
  1172. br.Msg = "刷新成功"
  1173. if isAsync {
  1174. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  1175. }
  1176. }
  1177. // DataList
  1178. // @Title 获取指标数据
  1179. // @Description 获取指标数据
  1180. // @Param EdbInfoId query int true "指标ID"
  1181. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  1182. // @Param StartDate query string true "自定义开始日期"
  1183. // @Param EndDate query string true "自定义结束日期"
  1184. // @Param Calendar query string true "公历/农历"
  1185. // @Param SeasonStartDate query string true "季节性图开始日期"
  1186. // @Param SeasonEndDate query string true "季节性图结束日期"
  1187. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  1188. // @Success 200 {object} data_manage.EdbInfoDataResp
  1189. // @router /predict_edb_info/data [get]
  1190. func (this *PredictEdbInfoController) DataList() {
  1191. br := new(models.BaseResponse).Init()
  1192. defer func() {
  1193. this.Data["json"] = br
  1194. this.ServeJSON()
  1195. }()
  1196. sysUser := this.SysUser
  1197. if sysUser == nil {
  1198. br.Msg = "请登录"
  1199. br.ErrMsg = "请登录,SysUser Is Empty"
  1200. br.Ret = 408
  1201. return
  1202. }
  1203. edbInfoId, _ := this.GetInt("EdbInfoId")
  1204. if edbInfoId <= 0 {
  1205. br.Msg = "参数错误"
  1206. return
  1207. }
  1208. dateType, _ := this.GetInt("DateType")
  1209. if dateType <= 0 {
  1210. dateType = 3
  1211. }
  1212. edbInfoType, _ := this.GetInt("EdbInfoType")
  1213. if edbInfoType < 0 {
  1214. edbInfoType = 1
  1215. }
  1216. startDate := this.GetString("StartDate")
  1217. endDate := this.GetString("EndDate")
  1218. startYear, _ := this.GetInt("StartYear")
  1219. chartType, _ := this.GetInt("ChartType")
  1220. if chartType <= 0 {
  1221. chartType = 1
  1222. }
  1223. calendar := this.GetString("Calendar")
  1224. if calendar == "" {
  1225. calendar = "公历"
  1226. }
  1227. isTimeBetween := false //是否是时间区间
  1228. if chartType == 2 {
  1229. dateType = 0
  1230. isTimeBetween = true
  1231. }
  1232. if dateType == 5 {
  1233. isTimeBetween = true
  1234. }
  1235. fullEdb := new(response.EdbInfoFull)
  1236. // 获取预测指标信息
  1237. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1238. if err != nil {
  1239. br.Msg = "获取预测指标信息失败"
  1240. br.ErrMsg = "获取预测指标信息失败,Err:" + err.Error()
  1241. return
  1242. }
  1243. edbInfoResp, err := edbInfo.ConvertToResp()
  1244. if err != nil {
  1245. br.Msg = "获取预测指标信息失败"
  1246. br.ErrMsg = "获取预测指标信息失败,Err:" + err.Error()
  1247. return
  1248. }
  1249. fullEdb.EdbInfoResp = edbInfoResp
  1250. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  1251. if err != nil {
  1252. br.Msg = err.Error()
  1253. br.ErrMsg = errMsg
  1254. return
  1255. }
  1256. var currClassifyItem *data_manage.EdbClassifyIdItems
  1257. for _, v := range classifyList {
  1258. if v.ClassifyId == edbInfo.ClassifyId {
  1259. currClassifyItem = v
  1260. break
  1261. }
  1262. }
  1263. if currClassifyItem != nil {
  1264. fullEdb.HaveOperaAuth, err = data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassifyItem.IsJoinPermission, sysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  1265. if err != nil {
  1266. br.Msg = err.Error()
  1267. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  1268. return
  1269. }
  1270. }
  1271. maxYear := 0
  1272. if edbInfo.LatestDate != "" {
  1273. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  1274. maxYear = latestDateT.Year()
  1275. }
  1276. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1277. if endDate == "" {
  1278. endDate = time.Now().Format(utils.FormatDate)
  1279. }
  1280. if dateType == 10 {
  1281. startDate = ""
  1282. endDate = ""
  1283. }
  1284. // 按钮校验
  1285. button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId, fullEdb.HaveOperaAuth)
  1286. button.AddButton = false
  1287. resp := response.PredictEdbInfoDataResp{
  1288. EdbInfo: fullEdb,
  1289. Button: button,
  1290. ClassifyList: classifyList,
  1291. }
  1292. if fullEdb.HaveOperaAuth {
  1293. dataList, _, _, err, _ := data.GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  1294. //lenDataList := len(dataList)
  1295. //if lenDataList > 0 {
  1296. // tmpValue := dataList[lenDataList-1]
  1297. //
  1298. // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1299. // if edbInfo.MaxValue < tmpValue.Value {
  1300. // edbInfo.MaxValue = tmpValue.Value
  1301. // }
  1302. //
  1303. // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1304. // if edbInfo.MinValue > tmpValue.Value {
  1305. // edbInfo.MinValue = tmpValue.Value
  1306. // }
  1307. //}
  1308. // 季节性图、公历、农历处理
  1309. resultDataList, err := data.GetChartDataList(dataList, chartType, calendar, edbInfo.LatestDate, startDate)
  1310. if err != nil {
  1311. br.Msg = "获取失败"
  1312. br.ErrMsg = "获取失败,Err:" + err.Error()
  1313. return
  1314. }
  1315. resp.DataList = resultDataList
  1316. }
  1317. br.Ret = 200
  1318. br.Success = true
  1319. br.Msg = "获取成功"
  1320. br.Data = resp
  1321. }
  1322. // ChartDataList
  1323. // @Title 获取指标数据
  1324. // @Description 获取指标数据
  1325. // @Param request body data_manage.AddPredictEdbInfoReq true "type json string"
  1326. // @Success 200 {object} data_manage.EdbInfoDataResp
  1327. // @router /predict_edb_info/chart/data/list [post]
  1328. func (this *PredictEdbInfoController) ChartDataList() {
  1329. br := new(models.BaseResponse).Init()
  1330. defer func() {
  1331. this.Data["json"] = br
  1332. this.ServeJSON()
  1333. }()
  1334. sysUser := this.SysUser
  1335. if sysUser == nil {
  1336. br.Msg = "请登录"
  1337. br.ErrMsg = "请登录,SysUser Is Empty"
  1338. br.Ret = 408
  1339. return
  1340. }
  1341. var req request.PredictEdbInfoChartDataReq
  1342. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1343. if req.SourceEdbInfoId <= 0 {
  1344. br.Msg = "参数错误"
  1345. return
  1346. }
  1347. dateType := req.DateType
  1348. startDate := req.StartDate
  1349. endDate := req.EndDate
  1350. //seasonStartDate := req.SeasonStartDate
  1351. //seasonEndDate := req.SeasonEndDate
  1352. startYear := req.StartYear
  1353. calendar := req.Calendar
  1354. if calendar == "" {
  1355. calendar = "公历"
  1356. }
  1357. isTimeBetween := false //是否是时间区间
  1358. if req.ChartType == 2 {
  1359. dateType = 0
  1360. isTimeBetween = true
  1361. }
  1362. if dateType == 5 {
  1363. isTimeBetween = true
  1364. }
  1365. // 规则列表
  1366. // 根据截止日期重新排序
  1367. ruleList := make([]request.RuleConfig, 0)
  1368. {
  1369. ruleMap := make(map[string]request.RuleConfig)
  1370. dateIntList := make([]int64, 0)
  1371. for _, v := range req.RuleList {
  1372. confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
  1373. if err != nil {
  1374. br.Msg = "配置项中时间异常,请重新选择"
  1375. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  1376. br.IsSendEmail = false
  1377. return
  1378. }
  1379. dateIntList = append(dateIntList, confEndDate.Unix())
  1380. ruleMap[v.EndDate] = v
  1381. }
  1382. sort.Slice(dateIntList, func(i, j int) bool {
  1383. return dateIntList[i] < dateIntList[j]
  1384. })
  1385. for _, dateInt := range dateIntList {
  1386. currDateTime := time.Unix(dateInt, 0)
  1387. item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
  1388. if !ok {
  1389. br.Msg = "配置项中时间异常,请重新选择"
  1390. br.ErrMsg = "配置项中时间异常,请重新选择"
  1391. br.IsSendEmail = false
  1392. return
  1393. }
  1394. ruleList = append(ruleList, item)
  1395. }
  1396. }
  1397. // 预测指标配置
  1398. predictEdbConfAndDataList := make([]data_manage.PredictEdbConfAndData, 0)
  1399. endDateStr := `` //数据的结束日期
  1400. for _, v := range ruleList {
  1401. endDateStr = v.EndDate //预测指标的结束日期
  1402. confEndDate, err := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  1403. if err != nil {
  1404. br.Msg = "配置项中时间异常,请重新选择"
  1405. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  1406. return
  1407. }
  1408. // 没有数据,自己瞎测试
  1409. //switch v.RuleType {
  1410. //case 3: //3:同比
  1411. // v.Value = "0.1"
  1412. //case 4: //4:同差
  1413. // v.Value = "10"
  1414. //case 5: //5:环比
  1415. // v.Value = "0.1"
  1416. //case 6: //6:环差
  1417. // v.Value = "0.1"
  1418. //case 7: //7:N期移动均值
  1419. // v.Value = "5"
  1420. //case 8: //8:N期段线性外推值
  1421. // v.Value = "5"
  1422. //}
  1423. tmpDataList := make([]*data_manage.EdbDataList, 0)
  1424. switch v.RuleType {
  1425. case 9:
  1426. // 获取计算参数
  1427. /*formula, edbInfoList, edbInfoIdBytes, err, errMsg := data.GetCalculateByRuleByNineParams(v)
  1428. if err != nil {
  1429. br.Msg = "计算失败"
  1430. if errMsg != "" {
  1431. br.Msg = errMsg
  1432. }
  1433. br.Msg = err.Error()
  1434. br.IsSendEmail = false
  1435. return
  1436. }
  1437. // 获取计算数据
  1438. tmpDataList, err = data.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes, v.EmptyType, v.MaxEmptyType)
  1439. if err != nil {
  1440. br.Msg = "计算失败"
  1441. br.ErrMsg = err.Error()
  1442. br.IsSendEmail = false
  1443. return
  1444. }*/
  1445. reqJson, err := json.Marshal(v)
  1446. respItem, err := data.PredictCalculateByNinePreview(string(reqJson), this.Lang)
  1447. if err != nil {
  1448. br.Msg = "获取失败"
  1449. br.ErrMsg = "获取失败,Err:" + err.Error()
  1450. return
  1451. }
  1452. if respItem.Ret != 200 {
  1453. br.Msg = respItem.Msg
  1454. br.ErrMsg = respItem.ErrMsg
  1455. return
  1456. }
  1457. tmpDataList = respItem.Data.DataList
  1458. }
  1459. tmpPredictEdbConfAndData := data_manage.PredictEdbConfAndData{
  1460. ConfigId: 0,
  1461. PredictEdbInfoId: 0,
  1462. SourceEdbInfoId: req.SourceEdbInfoId,
  1463. RuleType: v.RuleType,
  1464. FixedValue: 0,
  1465. Value: v.Value,
  1466. EndDate: confEndDate,
  1467. ModifyTime: time.Now(),
  1468. CreateTime: time.Now(),
  1469. DataList: tmpDataList,
  1470. }
  1471. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  1472. }
  1473. // 来源指标
  1474. sourceEdbInfoItem, err := data_manage.GetEdbInfoById(req.SourceEdbInfoId)
  1475. if err != nil {
  1476. br.Msg = "获取来源指标信息失败"
  1477. br.ErrMsg = "获取来源指标信息失败"
  1478. if !utils.IsErrNoRow(err) {
  1479. br.ErrMsg = "获取来源指标信息失败,Err:" + err.Error()
  1480. }
  1481. br.Success = true
  1482. return
  1483. }
  1484. // todo 确认预测指标是否用的是来源指标的最新日期
  1485. maxYear := 0
  1486. if sourceEdbInfoItem.LatestDate != "" {
  1487. latestDateT, _ := time.Parse(utils.FormatDate, sourceEdbInfoItem.LatestDate)
  1488. maxYear = latestDateT.Year()
  1489. }
  1490. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1491. if endDate == "" {
  1492. endDate = time.Now().Format(utils.FormatDate)
  1493. }
  1494. if dateType == 10 {
  1495. startDate = ""
  1496. endDate = ""
  1497. }
  1498. minValue := sourceEdbInfoItem.MinValue
  1499. maxValue := sourceEdbInfoItem.MaxValue
  1500. allDataList := make([]*data_manage.EdbDataList, 0)
  1501. //获取指标数据(实际已生成)
  1502. dataList, err := data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  1503. if err != nil {
  1504. br.Msg = "获取失败"
  1505. br.Msg = "获取失败,Err:" + err.Error()
  1506. return
  1507. }
  1508. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  1509. if startDate != `` {
  1510. allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, "", "")
  1511. if err != nil {
  1512. br.Msg = "获取失败"
  1513. br.Msg = "获取失败,Err:" + err.Error()
  1514. return
  1515. }
  1516. } else {
  1517. allDataList = dataList
  1518. }
  1519. // 获取预测指标未来的数据
  1520. if len(predictEdbConfAndDataList) > 0 {
  1521. predictDataList := make([]*data_manage.EdbDataList, 0)
  1522. if isTimeBetween { //如果是时间区间,那么
  1523. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1524. endDateTime, _ := time.ParseInLocation(utils.FormatDate, req.PredictEndDate, time.Local)
  1525. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  1526. if reqEndDateTime.Before(endDateTime) {
  1527. endDateStr = endDate
  1528. }
  1529. }
  1530. // 获取预测数据
  1531. var predictMinValue, predictMaxValue float64
  1532. predictDataList, predictMinValue, predictMaxValue, err, errMsg := data.GetChartPredictEdbInfoDataListByConfList(predictEdbConfAndDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, sourceEdbInfoItem.Frequency, req.DataDateType, allDataList)
  1533. if err != nil {
  1534. br.Msg = "获取预测指标数据失败"
  1535. if errMsg != `` {
  1536. br.Msg = errMsg
  1537. }
  1538. br.ErrMsg = "获取预测指标数据失败:" + err.Error()
  1539. return
  1540. }
  1541. dataList = append(dataList, predictDataList...)
  1542. if len(predictDataList) > 0 {
  1543. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1544. if minValue > predictMinValue {
  1545. minValue = predictMinValue
  1546. }
  1547. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1548. if maxValue < predictMaxValue {
  1549. maxValue = predictMaxValue
  1550. }
  1551. }
  1552. }
  1553. // 季节性图、公历、农历处理
  1554. resultDataList, err := data.GetChartDataList(dataList, req.ChartType, calendar, sourceEdbInfoItem.LatestDate, startDate)
  1555. if err != nil {
  1556. br.Msg = "获取失败"
  1557. br.ErrMsg = "获取失败,Err:" + err.Error()
  1558. return
  1559. }
  1560. resp := response.PredictEdbInfoChartDataResp{
  1561. MinValue: minValue,
  1562. MaxValue: maxValue,
  1563. DataList: resultDataList,
  1564. }
  1565. br.Ret = 200
  1566. br.Success = true
  1567. br.Msg = "获取成功"
  1568. br.Data = resp
  1569. }
  1570. // PredictRuleCalculateByNine
  1571. // @Title 根据预测规则9来计算数据
  1572. // @Description 获取指标数据
  1573. // @Param request body data_manage.RuleConfig true "type json string"
  1574. // @Success 200 {object} data_manage.EdbInfoDataResp
  1575. // @router /predict_edb_info/rule/calculate_by_nine [post]
  1576. func (this *PredictEdbInfoController) PredictRuleCalculateByNine() {
  1577. br := new(models.BaseResponse).Init()
  1578. defer func() {
  1579. this.Data["json"] = br
  1580. this.ServeJSON()
  1581. }()
  1582. sysUser := this.SysUser
  1583. if sysUser == nil {
  1584. br.Msg = "请登录"
  1585. br.ErrMsg = "请登录,SysUser Is Empty"
  1586. br.Ret = 408
  1587. return
  1588. }
  1589. var req request.RuleConfig
  1590. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1591. if err != nil {
  1592. br.Msg = "参数解析异常!"
  1593. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1594. return
  1595. }
  1596. // 添加计算指标
  1597. reqJson, err := json.Marshal(req)
  1598. if err != nil {
  1599. br.Msg = "参数解析异常!"
  1600. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1601. return
  1602. }
  1603. respItem, err := data.PredictCalculateByNinePreview(string(reqJson), this.Lang)
  1604. if err != nil {
  1605. br.Msg = "获取失败"
  1606. br.ErrMsg = "获取失败,Err:" + err.Error()
  1607. return
  1608. }
  1609. if respItem.Ret != 200 {
  1610. br.Msg = respItem.Msg
  1611. br.ErrMsg = respItem.ErrMsg
  1612. return
  1613. }
  1614. resp := respItem.Data
  1615. br.Ret = 200
  1616. br.Success = true
  1617. br.Msg = "获取成功"
  1618. br.Data = resp
  1619. }
  1620. // Modify
  1621. // @Title 修改指标信息接口
  1622. // @Description 编辑指标接口
  1623. // @Success 200 {object} request.EditEdbInfoReq
  1624. // @router /predict_edb_info/modify [post]
  1625. func (this *PredictEdbInfoController) Modify() {
  1626. br := new(models.BaseResponse).Init()
  1627. defer func() {
  1628. this.Data["json"] = br
  1629. this.ServeJSON()
  1630. }()
  1631. sysUser := this.SysUser
  1632. if sysUser == nil {
  1633. br.Msg = "请登录"
  1634. br.ErrMsg = "请登录,SysUser Is Empty"
  1635. br.Ret = 408
  1636. return
  1637. }
  1638. var req request.ModifyPredictEdbInfoReq
  1639. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1640. if err != nil {
  1641. br.Msg = "参数解析异常!"
  1642. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1643. return
  1644. }
  1645. edbInfoId := req.EdbInfoId
  1646. if edbInfoId <= 0 {
  1647. br.Msg = "请选择预测指标"
  1648. br.IsSendEmail = false
  1649. return
  1650. }
  1651. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1652. if err != nil {
  1653. br.Msg = "修改失败"
  1654. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1655. return
  1656. }
  1657. var haveOperaAuth bool
  1658. // 数据权限校验
  1659. {
  1660. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, this.SysUser.AdminId)
  1661. if err != nil {
  1662. br.Msg = err.Error()
  1663. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  1664. return
  1665. }
  1666. }
  1667. // 编辑权限校验
  1668. button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId, haveOperaAuth)
  1669. if !button.OpButton {
  1670. br.Msg = "无权限操作"
  1671. br.IsSendEmail = false
  1672. return
  1673. }
  1674. edbInfo.MaxValue = req.MaxValue
  1675. edbInfo.MinValue = req.MinValue
  1676. edbInfo.ModifyTime = time.Now()
  1677. err = edbInfo.Update([]string{"MaxValue", "MinValue", "ModifyTime"})
  1678. if err != nil {
  1679. br.Msg = "获取失败"
  1680. br.ErrMsg = "获取失败,Err:" + err.Error()
  1681. return
  1682. }
  1683. //修改es
  1684. data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  1685. br.Ret = 200
  1686. br.Success = true
  1687. br.Msg = "修改成功"
  1688. br.IsAddLog = true
  1689. }
  1690. // ClassifyEdbInfoItems
  1691. // @Title 获取分类下指标接口
  1692. // @Description 获取分类下指标接口
  1693. // @Param ClassifyId query int true "分类id"
  1694. // @Param IsShowMe query bool false "是否只看我的"
  1695. // @Success 200 {object} data_manage.EdbClassifyListResp
  1696. // @router /predict_edb_info/items [get]
  1697. func (this *PredictEdbInfoController) ClassifyEdbInfoItems() {
  1698. br := new(models.BaseResponse).Init()
  1699. defer func() {
  1700. this.Data["json"] = br
  1701. this.ServeJSON()
  1702. }()
  1703. classifyId, _ := this.GetInt("ClassifyId")
  1704. if classifyId <= 0 {
  1705. br.Msg = "参数错误,请刷新页面"
  1706. return
  1707. }
  1708. // 是否只看我的
  1709. isShowMe, _ := this.GetBool("IsShowMe")
  1710. // 如果选择了只看我的,那么只查询归属于我的账号
  1711. sysUserId := 0
  1712. if isShowMe {
  1713. sysUserId = this.SysUser.AdminId
  1714. }
  1715. // 无权限指标 和 无权限指标分类id
  1716. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(this.SysUser.AdminId, utils.PREDICT_EDB_INFO_TYPE, 0)
  1717. if err != nil {
  1718. br.Msg = "获取失败"
  1719. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1720. return
  1721. }
  1722. allEdbInfo, err := data_manage.GetEdbInfoByClassifyId(classifyId, utils.PREDICT_EDB_INFO_TYPE, 0, sysUserId, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList)
  1723. if err != nil && !utils.IsErrNoRow(err) {
  1724. br.Msg = "获取失败"
  1725. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1726. return
  1727. }
  1728. list := make([]*data_manage.EdbClassifyItems, 0)
  1729. edbInfoNum := len(allEdbInfo)
  1730. if edbInfoNum > 0 {
  1731. classifyIdList := make([]int, 0)
  1732. for _, v := range allEdbInfo {
  1733. classifyIdList = append(classifyIdList, v.ClassifyId)
  1734. }
  1735. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1736. if err != nil {
  1737. br.Msg = "获取失败"
  1738. br.ErrMsg = "获取分类数据失败,Err:" + err.Error()
  1739. return
  1740. }
  1741. // 分类map
  1742. classifyMap := make(map[int]*data_manage.EdbClassify)
  1743. for _, v := range classifyList {
  1744. classifyMap[v.ClassifyId] = v
  1745. }
  1746. // 获取所有有权限的指标和分类
  1747. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1748. if err != nil {
  1749. br.Msg = "获取失败"
  1750. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1751. return
  1752. }
  1753. for _, v := range allEdbInfo {
  1754. // 数据权限
  1755. if classifyInfo, ok := classifyMap[v.ClassifyId]; ok {
  1756. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1757. }
  1758. button := data.GetEdbOpButton(this.SysUser, v.SysUserId, v.EdbType, utils.EDB_INFO_TYPE, v.HaveOperaAuth)
  1759. button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
  1760. v.Button = button
  1761. list = append(list, v)
  1762. }
  1763. }
  1764. resp := new(data_manage.ClassifyEdbInfoListResp)
  1765. resp.EdbInfoList = list
  1766. br.Ret = 200
  1767. br.Success = true
  1768. br.Msg = "获取成功"
  1769. br.Data = resp
  1770. }
  1771. // ChartImageSetBySvg
  1772. // @Title 设置指标的图表图片
  1773. // @Description 设置指标的图表图片接口
  1774. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  1775. // @Success Ret=200 保存成功
  1776. // @router /predict_edb_info/image/set_by_svg [post]
  1777. func (this *PredictEdbInfoController) ChartImageSetBySvg() {
  1778. br := new(models.BaseResponse).Init()
  1779. defer func() {
  1780. this.Data["json"] = br
  1781. this.ServeJSON()
  1782. }()
  1783. sysUser := this.SysUser
  1784. if sysUser == nil {
  1785. br.Msg = "请登录"
  1786. br.ErrMsg = "请登录,SysUser Is Empty"
  1787. br.Ret = 408
  1788. return
  1789. }
  1790. imgData := this.GetString("Img")
  1791. if imgData == "" {
  1792. br.Msg = "图片参数错误"
  1793. br.ErrMsg = "图片参数错误,Img Is Empty"
  1794. return
  1795. }
  1796. edbInfoId, _ := this.GetInt("EdbInfoId", 0)
  1797. if edbInfoId <= 0 {
  1798. br.Msg = "指标参数错误"
  1799. br.ErrMsg = "指标参数错误,EdbInfoId Is Empty"
  1800. return
  1801. }
  1802. // 通过svg图片生成图片资源地址
  1803. resourceUrl, err, errMsg := services.GetResourceUrlBySvgImg(imgData)
  1804. if err != nil {
  1805. br.Msg = errMsg
  1806. br.ErrMsg = err.Error()
  1807. return
  1808. }
  1809. if edbInfoId <= 0 || resourceUrl == "" {
  1810. br.Ret = 200
  1811. br.Success = true
  1812. br.Msg = "保存成功"
  1813. return
  1814. }
  1815. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1816. if err != nil {
  1817. br.Msg = "保存失败"
  1818. br.ErrMsg = "保存失败,Err:" + err.Error()
  1819. if utils.IsErrNoRow(err) {
  1820. br.Msg = "找不到该指标"
  1821. br.ErrMsg = "找不到该指标"
  1822. br.IsSendEmail = false
  1823. }
  1824. return
  1825. }
  1826. edbInfo.ChartImage = resourceUrl
  1827. err = edbInfo.Update([]string{"ChartImage"})
  1828. if err != nil {
  1829. br.Msg = "保存失败"
  1830. br.ErrMsg = "保存失败,Err:" + err.Error()
  1831. return
  1832. }
  1833. //修改es数据
  1834. go data.AddOrEditEdbInfoToEs(edbInfoId)
  1835. br.Ret = 200
  1836. br.Success = true
  1837. br.Msg = "保存成功"
  1838. }