predict_edb_info.go 57 KB

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