predict_edb_info.go 53 KB

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