predict_edb_info.go 51 KB

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