predict_edb_info.go 53 KB

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