predict_edb_info.go 57 KB

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