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. 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. frequencyList := make([]string, 0)
  1000. if frequency != `` {
  1001. frequencyList = []string{frequency}
  1002. }
  1003. total, edbInfoList, err = elastic.SearchEdbInfoData(keyWord, startSize, pageSize, filterSource, source, frequencyList, []string{}, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, []int{}, []int{}, []int{}, []int{1, 2}, 1, 0, this.SysUser.AdminId, map[string]string{})
  1004. isEs = true
  1005. } else {
  1006. var condition string
  1007. var pars []interface{}
  1008. // 预测指标
  1009. condition += ` AND edb_info_type = ? `
  1010. pars = append(pars, 1)
  1011. // 无权限指标id
  1012. lenNoPermissionEdbInfoIdList := len(noPermissionEdbInfoIdList)
  1013. if lenNoPermissionEdbInfoIdList > 0 {
  1014. condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbInfoIdList) + `) `
  1015. pars = append(pars, noPermissionEdbInfoIdList)
  1016. }
  1017. // 无权限指标分类id
  1018. lenNoPermissionEdbClassifyIdList := len(noPermissionEdbClassifyIdList)
  1019. if lenNoPermissionEdbClassifyIdList > 0 {
  1020. condition += ` AND classify_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbClassifyIdList) + `) `
  1021. pars = append(pars, noPermissionEdbClassifyIdList)
  1022. }
  1023. switch filterSource {
  1024. case 2:
  1025. condition += ` AND frequency='月度' `
  1026. case 3:
  1027. condition += ` AND frequency <> '日度' `
  1028. case 4:
  1029. condition += ` AND edb_type = 1 `
  1030. case 5:
  1031. condition += ` AND source = 32 ` //来源(预测同比值)
  1032. case 6:
  1033. condition += ` AND frequency != ? `
  1034. pars = append(pars, "年度")
  1035. }
  1036. //频度
  1037. if frequency != "" {
  1038. condition += ` AND frequency = ? `
  1039. pars = append(pars, frequency)
  1040. }
  1041. if isOnlyMe {
  1042. condition += " AND sys_user_id = ? "
  1043. pars = append(pars, this.SysUser.AdminId)
  1044. }
  1045. total, edbInfoList, err = data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  1046. }
  1047. if err != nil {
  1048. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1049. }
  1050. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1051. edbInfoListLen := len(edbInfoList)
  1052. classifyIdList := make([]int, 0)
  1053. for i := 0; i < edbInfoListLen; i++ {
  1054. edbInfoList[i].ConvertToResp()
  1055. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  1056. classifyIdList = append(classifyIdList, edbInfoList[i].ClassifyId)
  1057. }
  1058. // 当前列表中的分类map
  1059. classifyMap := make(map[int]*data_manage.EdbClassify)
  1060. if edbInfoListLen > 0 {
  1061. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1062. if err != nil {
  1063. br.Msg = "获取失败"
  1064. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  1065. return
  1066. }
  1067. for _, v := range classifyList {
  1068. classifyMap[v.ClassifyId] = v
  1069. }
  1070. // 获取所有有权限的指标和分类
  1071. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1072. if err != nil {
  1073. br.Msg = "获取失败"
  1074. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1075. return
  1076. }
  1077. // 如果是ES的话,需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  1078. if isEs {
  1079. edbInfoIdList := make([]int, 0)
  1080. for i := 0; i < edbInfoListLen; i++ {
  1081. edbInfoIdList = append(edbInfoIdList, edbInfoList[i].EdbInfoId)
  1082. tmpEdbInfo := edbInfoList[i]
  1083. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  1084. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1085. }
  1086. }
  1087. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1088. if err != nil {
  1089. br.Msg = "获取失败"
  1090. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1091. return
  1092. }
  1093. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1094. for _, v := range tmpEdbList {
  1095. edbInfoMap[v.EdbInfoId] = v
  1096. }
  1097. for i := 0; i < edbInfoListLen; i++ {
  1098. tmpEdbInfo, ok := edbInfoMap[edbInfoList[i].EdbInfoId]
  1099. if !ok {
  1100. continue
  1101. }
  1102. edbInfoList[i].IsJoinPermission = tmpEdbInfo.IsJoinPermission
  1103. }
  1104. }
  1105. // 权限校验
  1106. for i := 0; i < edbInfoListLen; i++ {
  1107. tmpEdbInfo := edbInfoList[i]
  1108. if currClassify, ok := classifyMap[tmpEdbInfo.ClassifyId]; ok {
  1109. edbInfoList[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(tmpEdbInfo.IsJoinPermission, currClassify.IsJoinPermission, tmpEdbInfo.EdbInfoId, tmpEdbInfo.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1110. }
  1111. }
  1112. }
  1113. for i := 0; i < edbInfoListLen; i++ {
  1114. for j := 0; j < edbInfoListLen; j++ {
  1115. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1116. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1117. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1118. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1119. }
  1120. }
  1121. }
  1122. //新增搜索词记录
  1123. {
  1124. searchKeyword := new(data_manage.SearchKeyword)
  1125. searchKeyword.KeyWord = keyWord
  1126. searchKeyword.CreateTime = time.Now()
  1127. go data_manage.AddSearchKeyword(searchKeyword)
  1128. }
  1129. resp := data_manage.EdbInfoFilterDataResp{
  1130. Paging: page,
  1131. List: edbInfoList,
  1132. }
  1133. br.Ret = 200
  1134. br.Success = true
  1135. br.Msg = "获取成功"
  1136. br.Data = resp
  1137. }
  1138. // RefreshAll
  1139. // @Title 指标全部刷新接口
  1140. // @Description 指标全部刷新接口
  1141. // @Param EdbInfoId query int true "指标id"
  1142. // @Success Ret=200 全部刷新成功
  1143. // @router /predict_edb_info/refresh/all [get]
  1144. func (this *PredictEdbInfoController) RefreshAll() {
  1145. br := new(models.BaseResponse).Init()
  1146. defer func() {
  1147. this.Data["json"] = br
  1148. this.ServeJSON()
  1149. }()
  1150. sysUser := this.SysUser
  1151. if sysUser == nil {
  1152. br.Msg = "请登录"
  1153. br.ErrMsg = "请登录,SysUser Is Empty"
  1154. br.Ret = 408
  1155. return
  1156. }
  1157. edbInfoId, _ := this.GetInt("EdbInfoId")
  1158. if edbInfoId <= 0 {
  1159. br.Msg = "参数错误"
  1160. br.ErrMsg = "参数错误"
  1161. return
  1162. }
  1163. _, isAsync, err, errMsg := data.RefreshPredictEdbInfo(edbInfoId, true)
  1164. if errMsg != `` {
  1165. br.Msg = errMsg
  1166. br.ErrMsg = errMsg
  1167. if err != nil {
  1168. br.ErrMsg = err.Error()
  1169. } else {
  1170. br.IsSendEmail = false
  1171. }
  1172. return
  1173. }
  1174. br.Ret = 200
  1175. br.Success = true
  1176. br.Msg = "刷新成功"
  1177. if isAsync {
  1178. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  1179. }
  1180. }
  1181. // DataList
  1182. // @Title 获取指标数据
  1183. // @Description 获取指标数据
  1184. // @Param EdbInfoId query int true "指标ID"
  1185. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  1186. // @Param StartDate query string true "自定义开始日期"
  1187. // @Param EndDate query string true "自定义结束日期"
  1188. // @Param Calendar query string true "公历/农历"
  1189. // @Param SeasonStartDate query string true "季节性图开始日期"
  1190. // @Param SeasonEndDate query string true "季节性图结束日期"
  1191. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  1192. // @Success 200 {object} data_manage.EdbInfoDataResp
  1193. // @router /predict_edb_info/data [get]
  1194. func (this *PredictEdbInfoController) DataList() {
  1195. br := new(models.BaseResponse).Init()
  1196. defer func() {
  1197. this.Data["json"] = br
  1198. this.ServeJSON()
  1199. }()
  1200. sysUser := this.SysUser
  1201. if sysUser == nil {
  1202. br.Msg = "请登录"
  1203. br.ErrMsg = "请登录,SysUser Is Empty"
  1204. br.Ret = 408
  1205. return
  1206. }
  1207. edbInfoId, _ := this.GetInt("EdbInfoId")
  1208. if edbInfoId <= 0 {
  1209. br.Msg = "参数错误"
  1210. return
  1211. }
  1212. dateType, _ := this.GetInt("DateType")
  1213. if dateType <= 0 {
  1214. dateType = 3
  1215. }
  1216. edbInfoType, _ := this.GetInt("EdbInfoType")
  1217. if edbInfoType < 0 {
  1218. edbInfoType = 1
  1219. }
  1220. startDate := this.GetString("StartDate")
  1221. endDate := this.GetString("EndDate")
  1222. startYear, _ := this.GetInt("StartYear")
  1223. chartType, _ := this.GetInt("ChartType")
  1224. if chartType <= 0 {
  1225. chartType = 1
  1226. }
  1227. calendar := this.GetString("Calendar")
  1228. if calendar == "" {
  1229. calendar = "公历"
  1230. }
  1231. isTimeBetween := false //是否是时间区间
  1232. if chartType == 2 {
  1233. dateType = 0
  1234. isTimeBetween = true
  1235. }
  1236. if dateType == 5 {
  1237. isTimeBetween = true
  1238. }
  1239. fullEdb := new(response.EdbInfoFull)
  1240. // 获取预测指标信息
  1241. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1242. if err != nil {
  1243. br.Msg = "获取预测指标信息失败"
  1244. br.ErrMsg = "获取预测指标信息失败,Err:" + err.Error()
  1245. return
  1246. }
  1247. edbInfoResp, err := edbInfo.ConvertToResp()
  1248. if err != nil {
  1249. br.Msg = "获取预测指标信息失败"
  1250. br.ErrMsg = "获取预测指标信息失败,Err:" + err.Error()
  1251. return
  1252. }
  1253. fullEdb.EdbInfoResp = edbInfoResp
  1254. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  1255. if err != nil {
  1256. br.Msg = err.Error()
  1257. br.ErrMsg = errMsg
  1258. return
  1259. }
  1260. var currClassifyItem *data_manage.EdbClassifyIdItems
  1261. for _, v := range classifyList {
  1262. if v.ClassifyId == edbInfo.ClassifyId {
  1263. currClassifyItem = v
  1264. break
  1265. }
  1266. }
  1267. if currClassifyItem != nil {
  1268. fullEdb.HaveOperaAuth, err = data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassifyItem.IsJoinPermission, sysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  1269. if err != nil {
  1270. br.Msg = err.Error()
  1271. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  1272. return
  1273. }
  1274. }
  1275. maxYear := 0
  1276. if edbInfo.LatestDate != "" {
  1277. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  1278. maxYear = latestDateT.Year()
  1279. }
  1280. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1281. if endDate == "" {
  1282. endDate = time.Now().Format(utils.FormatDate)
  1283. }
  1284. if dateType == 10 {
  1285. startDate = ""
  1286. endDate = ""
  1287. }
  1288. // 按钮校验
  1289. button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId, fullEdb.HaveOperaAuth)
  1290. button.AddButton = false
  1291. resp := response.PredictEdbInfoDataResp{
  1292. EdbInfo: fullEdb,
  1293. Button: button,
  1294. ClassifyList: classifyList,
  1295. }
  1296. if fullEdb.HaveOperaAuth {
  1297. dataList, _, _, err, _ := data.GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  1298. //lenDataList := len(dataList)
  1299. //if lenDataList > 0 {
  1300. // tmpValue := dataList[lenDataList-1]
  1301. //
  1302. // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1303. // if edbInfo.MaxValue < tmpValue.Value {
  1304. // edbInfo.MaxValue = tmpValue.Value
  1305. // }
  1306. //
  1307. // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1308. // if edbInfo.MinValue > tmpValue.Value {
  1309. // edbInfo.MinValue = tmpValue.Value
  1310. // }
  1311. //}
  1312. // 季节性图、公历、农历处理
  1313. resultDataList, err := data.GetChartDataList(dataList, chartType, calendar, edbInfo.LatestDate, startDate)
  1314. if err != nil {
  1315. br.Msg = "获取失败"
  1316. br.ErrMsg = "获取失败,Err:" + err.Error()
  1317. return
  1318. }
  1319. resp.DataList = resultDataList
  1320. }
  1321. br.Ret = 200
  1322. br.Success = true
  1323. br.Msg = "获取成功"
  1324. br.Data = resp
  1325. }
  1326. // ChartDataList
  1327. // @Title 获取指标数据
  1328. // @Description 获取指标数据
  1329. // @Param request body data_manage.AddPredictEdbInfoReq true "type json string"
  1330. // @Success 200 {object} data_manage.EdbInfoDataResp
  1331. // @router /predict_edb_info/chart/data/list [post]
  1332. func (this *PredictEdbInfoController) ChartDataList() {
  1333. br := new(models.BaseResponse).Init()
  1334. defer func() {
  1335. this.Data["json"] = br
  1336. this.ServeJSON()
  1337. }()
  1338. sysUser := this.SysUser
  1339. if sysUser == nil {
  1340. br.Msg = "请登录"
  1341. br.ErrMsg = "请登录,SysUser Is Empty"
  1342. br.Ret = 408
  1343. return
  1344. }
  1345. var req request.PredictEdbInfoChartDataReq
  1346. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1347. if req.SourceEdbInfoId <= 0 {
  1348. br.Msg = "参数错误"
  1349. return
  1350. }
  1351. dateType := req.DateType
  1352. startDate := req.StartDate
  1353. endDate := req.EndDate
  1354. //seasonStartDate := req.SeasonStartDate
  1355. //seasonEndDate := req.SeasonEndDate
  1356. startYear := req.StartYear
  1357. calendar := req.Calendar
  1358. if calendar == "" {
  1359. calendar = "公历"
  1360. }
  1361. isTimeBetween := false //是否是时间区间
  1362. if req.ChartType == 2 {
  1363. dateType = 0
  1364. isTimeBetween = true
  1365. }
  1366. if dateType == 5 {
  1367. isTimeBetween = true
  1368. }
  1369. // 规则列表
  1370. // 根据截止日期重新排序
  1371. ruleList := make([]request.RuleConfig, 0)
  1372. {
  1373. ruleMap := make(map[string]request.RuleConfig)
  1374. dateIntList := make([]int64, 0)
  1375. for _, v := range req.RuleList {
  1376. confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
  1377. if err != nil {
  1378. br.Msg = "配置项中时间异常,请重新选择"
  1379. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  1380. br.IsSendEmail = false
  1381. return
  1382. }
  1383. dateIntList = append(dateIntList, confEndDate.Unix())
  1384. ruleMap[v.EndDate] = v
  1385. }
  1386. sort.Slice(dateIntList, func(i, j int) bool {
  1387. return dateIntList[i] < dateIntList[j]
  1388. })
  1389. for _, dateInt := range dateIntList {
  1390. currDateTime := time.Unix(dateInt, 0)
  1391. item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
  1392. if !ok {
  1393. br.Msg = "配置项中时间异常,请重新选择"
  1394. br.ErrMsg = "配置项中时间异常,请重新选择"
  1395. br.IsSendEmail = false
  1396. return
  1397. }
  1398. ruleList = append(ruleList, item)
  1399. }
  1400. }
  1401. // 预测指标配置
  1402. predictEdbConfAndDataList := make([]data_manage.PredictEdbConfAndData, 0)
  1403. endDateStr := `` //数据的结束日期
  1404. for _, v := range ruleList {
  1405. endDateStr = v.EndDate //预测指标的结束日期
  1406. confEndDate, err := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  1407. if err != nil {
  1408. br.Msg = "配置项中时间异常,请重新选择"
  1409. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  1410. return
  1411. }
  1412. // 没有数据,自己瞎测试
  1413. //switch v.RuleType {
  1414. //case 3: //3:同比
  1415. // v.Value = "0.1"
  1416. //case 4: //4:同差
  1417. // v.Value = "10"
  1418. //case 5: //5:环比
  1419. // v.Value = "0.1"
  1420. //case 6: //6:环差
  1421. // v.Value = "0.1"
  1422. //case 7: //7:N期移动均值
  1423. // v.Value = "5"
  1424. //case 8: //8:N期段线性外推值
  1425. // v.Value = "5"
  1426. //}
  1427. tmpDataList := make([]*data_manage.EdbDataList, 0)
  1428. switch v.RuleType {
  1429. case 9:
  1430. // 获取计算参数
  1431. /*formula, edbInfoList, edbInfoIdBytes, err, errMsg := data.GetCalculateByRuleByNineParams(v)
  1432. if err != nil {
  1433. br.Msg = "计算失败"
  1434. if errMsg != "" {
  1435. br.Msg = errMsg
  1436. }
  1437. br.Msg = err.Error()
  1438. br.IsSendEmail = false
  1439. return
  1440. }
  1441. // 获取计算数据
  1442. tmpDataList, err = data.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes, v.EmptyType, v.MaxEmptyType)
  1443. if err != nil {
  1444. br.Msg = "计算失败"
  1445. br.ErrMsg = err.Error()
  1446. br.IsSendEmail = false
  1447. return
  1448. }*/
  1449. reqJson, err := json.Marshal(v)
  1450. respItem, err := data.PredictCalculateByNinePreview(string(reqJson), this.Lang)
  1451. if err != nil {
  1452. br.Msg = "获取失败"
  1453. br.ErrMsg = "获取失败,Err:" + err.Error()
  1454. return
  1455. }
  1456. if respItem.Ret != 200 {
  1457. br.Msg = respItem.Msg
  1458. br.ErrMsg = respItem.ErrMsg
  1459. return
  1460. }
  1461. tmpDataList = respItem.Data.DataList
  1462. }
  1463. tmpPredictEdbConfAndData := data_manage.PredictEdbConfAndData{
  1464. ConfigId: 0,
  1465. PredictEdbInfoId: 0,
  1466. SourceEdbInfoId: req.SourceEdbInfoId,
  1467. RuleType: v.RuleType,
  1468. FixedValue: 0,
  1469. Value: v.Value,
  1470. EndDate: confEndDate,
  1471. ModifyTime: time.Now(),
  1472. CreateTime: time.Now(),
  1473. DataList: tmpDataList,
  1474. }
  1475. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  1476. }
  1477. // 来源指标
  1478. sourceEdbInfoItem, err := data_manage.GetEdbInfoById(req.SourceEdbInfoId)
  1479. if err != nil {
  1480. br.Msg = "获取来源指标信息失败"
  1481. br.ErrMsg = "获取来源指标信息失败"
  1482. if !utils.IsErrNoRow(err) {
  1483. br.ErrMsg = "获取来源指标信息失败,Err:" + err.Error()
  1484. }
  1485. br.Success = true
  1486. return
  1487. }
  1488. // todo 确认预测指标是否用的是来源指标的最新日期
  1489. maxYear := 0
  1490. if sourceEdbInfoItem.LatestDate != "" {
  1491. latestDateT, _ := time.Parse(utils.FormatDate, sourceEdbInfoItem.LatestDate)
  1492. maxYear = latestDateT.Year()
  1493. }
  1494. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1495. if endDate == "" {
  1496. endDate = time.Now().Format(utils.FormatDate)
  1497. }
  1498. if dateType == 10 {
  1499. startDate = ""
  1500. endDate = ""
  1501. }
  1502. minValue := sourceEdbInfoItem.MinValue
  1503. maxValue := sourceEdbInfoItem.MaxValue
  1504. allDataList := make([]*data_manage.EdbDataList, 0)
  1505. //获取指标数据(实际已生成)
  1506. dataList, err := data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  1507. if err != nil {
  1508. br.Msg = "获取失败"
  1509. br.Msg = "获取失败,Err:" + err.Error()
  1510. return
  1511. }
  1512. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  1513. if startDate != `` {
  1514. allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, "", "")
  1515. if err != nil {
  1516. br.Msg = "获取失败"
  1517. br.Msg = "获取失败,Err:" + err.Error()
  1518. return
  1519. }
  1520. } else {
  1521. allDataList = dataList
  1522. }
  1523. // 获取预测指标未来的数据
  1524. if len(predictEdbConfAndDataList) > 0 {
  1525. predictDataList := make([]*data_manage.EdbDataList, 0)
  1526. if isTimeBetween { //如果是时间区间,那么
  1527. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1528. endDateTime, _ := time.ParseInLocation(utils.FormatDate, req.PredictEndDate, time.Local)
  1529. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  1530. if reqEndDateTime.Before(endDateTime) {
  1531. endDateStr = endDate
  1532. }
  1533. }
  1534. // 获取预测数据
  1535. var predictMinValue, predictMaxValue float64
  1536. predictDataList, predictMinValue, predictMaxValue, err, errMsg := data.GetChartPredictEdbInfoDataListByConfList(predictEdbConfAndDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, sourceEdbInfoItem.Frequency, req.DataDateType, allDataList)
  1537. if err != nil {
  1538. br.Msg = "获取预测指标数据失败"
  1539. if errMsg != `` {
  1540. br.Msg = errMsg
  1541. }
  1542. br.ErrMsg = "获取预测指标数据失败:" + err.Error()
  1543. return
  1544. }
  1545. dataList = append(dataList, predictDataList...)
  1546. if len(predictDataList) > 0 {
  1547. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1548. if minValue > predictMinValue {
  1549. minValue = predictMinValue
  1550. }
  1551. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1552. if maxValue < predictMaxValue {
  1553. maxValue = predictMaxValue
  1554. }
  1555. }
  1556. }
  1557. // 季节性图、公历、农历处理
  1558. resultDataList, err := data.GetChartDataList(dataList, req.ChartType, calendar, sourceEdbInfoItem.LatestDate, startDate)
  1559. if err != nil {
  1560. br.Msg = "获取失败"
  1561. br.ErrMsg = "获取失败,Err:" + err.Error()
  1562. return
  1563. }
  1564. resp := response.PredictEdbInfoChartDataResp{
  1565. MinValue: minValue,
  1566. MaxValue: maxValue,
  1567. DataList: resultDataList,
  1568. }
  1569. br.Ret = 200
  1570. br.Success = true
  1571. br.Msg = "获取成功"
  1572. br.Data = resp
  1573. }
  1574. // PredictRuleCalculateByNine
  1575. // @Title 根据预测规则9来计算数据
  1576. // @Description 获取指标数据
  1577. // @Param request body data_manage.RuleConfig true "type json string"
  1578. // @Success 200 {object} data_manage.EdbInfoDataResp
  1579. // @router /predict_edb_info/rule/calculate_by_nine [post]
  1580. func (this *PredictEdbInfoController) PredictRuleCalculateByNine() {
  1581. br := new(models.BaseResponse).Init()
  1582. defer func() {
  1583. this.Data["json"] = br
  1584. this.ServeJSON()
  1585. }()
  1586. sysUser := this.SysUser
  1587. if sysUser == nil {
  1588. br.Msg = "请登录"
  1589. br.ErrMsg = "请登录,SysUser Is Empty"
  1590. br.Ret = 408
  1591. return
  1592. }
  1593. var req request.RuleConfig
  1594. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1595. if err != nil {
  1596. br.Msg = "参数解析异常!"
  1597. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1598. return
  1599. }
  1600. // 添加计算指标
  1601. reqJson, err := json.Marshal(req)
  1602. if err != nil {
  1603. br.Msg = "参数解析异常!"
  1604. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1605. return
  1606. }
  1607. respItem, err := data.PredictCalculateByNinePreview(string(reqJson), this.Lang)
  1608. if err != nil {
  1609. br.Msg = "获取失败"
  1610. br.ErrMsg = "获取失败,Err:" + err.Error()
  1611. return
  1612. }
  1613. if respItem.Ret != 200 {
  1614. br.Msg = respItem.Msg
  1615. br.ErrMsg = respItem.ErrMsg
  1616. return
  1617. }
  1618. resp := respItem.Data
  1619. br.Ret = 200
  1620. br.Success = true
  1621. br.Msg = "获取成功"
  1622. br.Data = resp
  1623. }
  1624. // Modify
  1625. // @Title 修改指标信息接口
  1626. // @Description 编辑指标接口
  1627. // @Success 200 {object} request.EditEdbInfoReq
  1628. // @router /predict_edb_info/modify [post]
  1629. func (this *PredictEdbInfoController) Modify() {
  1630. br := new(models.BaseResponse).Init()
  1631. defer func() {
  1632. this.Data["json"] = br
  1633. this.ServeJSON()
  1634. }()
  1635. sysUser := this.SysUser
  1636. if sysUser == nil {
  1637. br.Msg = "请登录"
  1638. br.ErrMsg = "请登录,SysUser Is Empty"
  1639. br.Ret = 408
  1640. return
  1641. }
  1642. var req request.ModifyPredictEdbInfoReq
  1643. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1644. if err != nil {
  1645. br.Msg = "参数解析异常!"
  1646. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1647. return
  1648. }
  1649. edbInfoId := req.EdbInfoId
  1650. if edbInfoId <= 0 {
  1651. br.Msg = "请选择预测指标"
  1652. br.IsSendEmail = false
  1653. return
  1654. }
  1655. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1656. if err != nil {
  1657. br.Msg = "修改失败"
  1658. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1659. return
  1660. }
  1661. var haveOperaAuth bool
  1662. // 数据权限校验
  1663. {
  1664. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, this.SysUser.AdminId)
  1665. if err != nil {
  1666. br.Msg = err.Error()
  1667. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  1668. return
  1669. }
  1670. }
  1671. // 编辑权限校验
  1672. button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId, haveOperaAuth)
  1673. if !button.OpButton {
  1674. br.Msg = "无权限操作"
  1675. br.IsSendEmail = false
  1676. return
  1677. }
  1678. edbInfo.MaxValue = req.MaxValue
  1679. edbInfo.MinValue = req.MinValue
  1680. edbInfo.ModifyTime = time.Now()
  1681. err = edbInfo.Update([]string{"MaxValue", "MinValue", "ModifyTime"})
  1682. if err != nil {
  1683. br.Msg = "获取失败"
  1684. br.ErrMsg = "获取失败,Err:" + err.Error()
  1685. return
  1686. }
  1687. //修改es
  1688. data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  1689. br.Ret = 200
  1690. br.Success = true
  1691. br.Msg = "修改成功"
  1692. br.IsAddLog = true
  1693. }
  1694. // ClassifyEdbInfoItems
  1695. // @Title 获取分类下指标接口
  1696. // @Description 获取分类下指标接口
  1697. // @Param ClassifyId query int true "分类id"
  1698. // @Param IsShowMe query bool false "是否只看我的"
  1699. // @Success 200 {object} data_manage.EdbClassifyListResp
  1700. // @router /predict_edb_info/items [get]
  1701. func (this *PredictEdbInfoController) ClassifyEdbInfoItems() {
  1702. br := new(models.BaseResponse).Init()
  1703. defer func() {
  1704. this.Data["json"] = br
  1705. this.ServeJSON()
  1706. }()
  1707. classifyId, _ := this.GetInt("ClassifyId")
  1708. if classifyId <= 0 {
  1709. br.Msg = "参数错误,请刷新页面"
  1710. return
  1711. }
  1712. // 是否只看我的
  1713. isShowMe, _ := this.GetBool("IsShowMe")
  1714. // 如果选择了只看我的,那么只查询归属于我的账号
  1715. sysUserId := 0
  1716. if isShowMe {
  1717. sysUserId = this.SysUser.AdminId
  1718. }
  1719. // 无权限指标 和 无权限指标分类id
  1720. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err := data_manage_permission.GetUserAllEdbAndClassifyNoPermissionList(this.SysUser.AdminId, utils.PREDICT_EDB_INFO_TYPE, 0)
  1721. if err != nil {
  1722. br.Msg = "获取失败"
  1723. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1724. return
  1725. }
  1726. allEdbInfo, err := data_manage.GetEdbInfoByClassifyId(classifyId, utils.PREDICT_EDB_INFO_TYPE, 0, sysUserId, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList)
  1727. if err != nil && !utils.IsErrNoRow(err) {
  1728. br.Msg = "获取失败"
  1729. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1730. return
  1731. }
  1732. list := make([]*data_manage.EdbClassifyItems, 0)
  1733. edbInfoNum := len(allEdbInfo)
  1734. if edbInfoNum > 0 {
  1735. classifyIdList := make([]int, 0)
  1736. for _, v := range allEdbInfo {
  1737. classifyIdList = append(classifyIdList, v.ClassifyId)
  1738. }
  1739. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1740. if err != nil {
  1741. br.Msg = "获取失败"
  1742. br.ErrMsg = "获取分类数据失败,Err:" + err.Error()
  1743. return
  1744. }
  1745. // 分类map
  1746. classifyMap := make(map[int]*data_manage.EdbClassify)
  1747. for _, v := range classifyList {
  1748. classifyMap[v.ClassifyId] = v
  1749. }
  1750. // 获取所有有权限的指标和分类
  1751. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1752. if err != nil {
  1753. br.Msg = "获取失败"
  1754. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1755. return
  1756. }
  1757. for _, v := range allEdbInfo {
  1758. // 数据权限
  1759. if classifyInfo, ok := classifyMap[v.ClassifyId]; ok {
  1760. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1761. }
  1762. button := data.GetEdbOpButton(this.SysUser, v.SysUserId, v.EdbType, utils.EDB_INFO_TYPE, v.HaveOperaAuth)
  1763. button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
  1764. v.Button = button
  1765. list = append(list, v)
  1766. }
  1767. }
  1768. resp := new(data_manage.ClassifyEdbInfoListResp)
  1769. resp.EdbInfoList = list
  1770. br.Ret = 200
  1771. br.Success = true
  1772. br.Msg = "获取成功"
  1773. br.Data = resp
  1774. }
  1775. // ChartImageSetBySvg
  1776. // @Title 设置指标的图表图片
  1777. // @Description 设置指标的图表图片接口
  1778. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  1779. // @Success Ret=200 保存成功
  1780. // @router /predict_edb_info/image/set_by_svg [post]
  1781. func (this *PredictEdbInfoController) ChartImageSetBySvg() {
  1782. br := new(models.BaseResponse).Init()
  1783. defer func() {
  1784. this.Data["json"] = br
  1785. this.ServeJSON()
  1786. }()
  1787. sysUser := this.SysUser
  1788. if sysUser == nil {
  1789. br.Msg = "请登录"
  1790. br.ErrMsg = "请登录,SysUser Is Empty"
  1791. br.Ret = 408
  1792. return
  1793. }
  1794. imgData := this.GetString("Img")
  1795. if imgData == "" {
  1796. br.Msg = "图片参数错误"
  1797. br.ErrMsg = "图片参数错误,Img Is Empty"
  1798. return
  1799. }
  1800. edbInfoId, _ := this.GetInt("EdbInfoId", 0)
  1801. if edbInfoId <= 0 {
  1802. br.Msg = "指标参数错误"
  1803. br.ErrMsg = "指标参数错误,EdbInfoId Is Empty"
  1804. return
  1805. }
  1806. // 通过svg图片生成图片资源地址
  1807. resourceUrl, err, errMsg := services.GetResourceUrlBySvgImg(imgData)
  1808. if err != nil {
  1809. br.Msg = errMsg
  1810. br.ErrMsg = err.Error()
  1811. return
  1812. }
  1813. if edbInfoId <= 0 || resourceUrl == "" {
  1814. br.Ret = 200
  1815. br.Success = true
  1816. br.Msg = "保存成功"
  1817. return
  1818. }
  1819. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1820. if err != nil {
  1821. br.Msg = "保存失败"
  1822. br.ErrMsg = "保存失败,Err:" + err.Error()
  1823. if utils.IsErrNoRow(err) {
  1824. br.Msg = "找不到该指标"
  1825. br.ErrMsg = "找不到该指标"
  1826. br.IsSendEmail = false
  1827. }
  1828. return
  1829. }
  1830. edbInfo.ChartImage = resourceUrl
  1831. err = edbInfo.Update([]string{"ChartImage"})
  1832. if err != nil {
  1833. br.Msg = "保存失败"
  1834. br.ErrMsg = "保存失败,Err:" + err.Error()
  1835. return
  1836. }
  1837. //修改es数据
  1838. go data.AddOrEditEdbInfoToEs(edbInfoId)
  1839. br.Ret = 200
  1840. br.Success = true
  1841. br.Msg = "保存成功"
  1842. }