predict_edb_info.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  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. fullEdb := new(data_manage.EdbInfoFullClassify)
  890. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  891. if err != nil {
  892. br.Msg = err.Error()
  893. br.ErrMsg = errMsg
  894. return
  895. }
  896. fullEdb.EdbInfo = edbInfo
  897. fullEdb.ClassifyList = classifyList
  898. // 拟合残差计算相关系数
  899. var correlationStr string
  900. if edbInfo.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC {
  901. var aEdbInfo, bEdbInfo *data_manage.EdbInfoCalculateDetail
  902. for _, v := range calculateList {
  903. if v.FromTag == "A" {
  904. aEdbInfo = v
  905. continue
  906. }
  907. if v.FromTag == "B" {
  908. bEdbInfo = v
  909. continue
  910. }
  911. }
  912. if aEdbInfo != nil && bEdbInfo != nil {
  913. edbInfoFromTagList := []data_manage.EdbInfoFromTag{
  914. {
  915. EdbInfoId: aEdbInfo.FromEdbInfoId,
  916. FromTag: aEdbInfo.FromTag,
  917. MoveValue: aEdbInfo.MoveValue,
  918. }, {
  919. EdbInfoId: bEdbInfo.FromEdbInfoId,
  920. FromTag: bEdbInfo.FromTag,
  921. MoveValue: bEdbInfo.MoveValue,
  922. },
  923. }
  924. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  925. AdminId: sysUser.AdminId,
  926. AdminName: sysUser.RealName,
  927. Formula: edbInfo.CalculateFormula,
  928. EdbInfoIdArr: edbInfoFromTagList,
  929. }
  930. // 计算
  931. val, err, _ := data.CallCalculateComputeCorrelation(req2)
  932. if err == nil {
  933. correlationStr = val
  934. }
  935. }
  936. }
  937. resp := response.PredictEdbInfo{
  938. EdbInfo: *fullEdb,
  939. RuleType: ruleType,
  940. FixedValue: fixedValue,
  941. CalculateList: calculateList,
  942. RuleList: predictEdbConfList,
  943. CorrelationStr: correlationStr,
  944. }
  945. br.Ret = 200
  946. br.Success = true
  947. br.Msg = "获取成功"
  948. br.Data = resp
  949. }
  950. // FilterByEs
  951. // @Title 指标筛选接口
  952. // @Description 指标筛选接口
  953. // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
  954. // @Param FilterSource query int false "搜索来源:1:其他搜索,2:累计值转月值搜索,3:变频,4:基础指标,5:同比"
  955. // @Param Frequency query string false "频度"
  956. // @Param IsOnlyMe query bool false "是否只看我的"
  957. // @Param PageSize query int true "每页数据条数"
  958. // @Param CurrentIndex query int true "当前页页码,从1开始"
  959. // @Success 200 {object} data_manage.EdbInfoList
  960. // @router /predict_edb_info/filter_by_es [get]
  961. func (this *PredictEdbInfoController) FilterByEs() {
  962. br := new(models.BaseResponse).Init()
  963. defer func() {
  964. this.Data["json"] = br
  965. this.ServeJSON()
  966. }()
  967. pageSize, _ := this.GetInt("PageSize")
  968. currentIndex, _ := this.GetInt("CurrentIndex")
  969. var total int64
  970. var startSize int
  971. if pageSize <= 0 {
  972. pageSize = utils.PageSize20
  973. }
  974. if currentIndex <= 0 {
  975. currentIndex = 1
  976. }
  977. startSize = paging.StartIndex(currentIndex, pageSize)
  978. keyWord := this.GetString("Keyword")
  979. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  980. //兼容前端
  981. if keyWord == `` {
  982. keyWord = this.GetString("KeyWord")
  983. keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
  984. }
  985. filterSource, _ := this.GetInt("FilterSource")
  986. if filterSource <= 0 {
  987. filterSource = 1
  988. }
  989. //指标来源
  990. source, _ := this.GetInt("Source")
  991. frequency := this.GetString("Frequency") //频度
  992. isOnlyMe, _ := this.GetBool("IsOnlyMe")
  993. var edbInfoList []*data_manage.EdbInfoList
  994. var err error
  995. noPermissionEdbInfoIdList := make([]int, 0) //无权限指标
  996. // 获取当前账号的不可见指标
  997. {
  998. obj := data_manage.EdbInfoNoPermissionAdmin{}
  999. confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
  1000. if err != nil && err.Error() != utils.ErrNoRow() {
  1001. br.Msg = "获取失败"
  1002. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1003. return
  1004. }
  1005. for _, v := range confList {
  1006. noPermissionEdbInfoIdList = append(noPermissionEdbInfoIdList, v.EdbInfoId)
  1007. }
  1008. }
  1009. if keyWord != "" {
  1010. var keyWordArr []string
  1011. keyWordArr = append(keyWordArr, keyWord)
  1012. newKeyWord := strings.Split(keyWord, " ")
  1013. keyWordArr = append(keyWordArr, newKeyWord...)
  1014. if isOnlyMe {
  1015. total, edbInfoList, err = elastic.SearchEdbInfoDataByAdminId(utils.DATA_INDEX_NAME, keyWord, startSize, pageSize, filterSource, source, 1, frequency, this.SysUser.AdminId)
  1016. } else {
  1017. total, edbInfoList, err = elastic.SearchEdbInfoData(utils.DATA_INDEX_NAME, keyWord, startSize, pageSize, filterSource, source, 1, frequency, noPermissionEdbInfoIdList)
  1018. }
  1019. } else {
  1020. var condition string
  1021. var pars []interface{}
  1022. // 预测指标
  1023. condition += ` AND edb_info_type = ? `
  1024. pars = append(pars, 1)
  1025. // 无权限指标id
  1026. lenNoPermissionEdbInfoIdList := len(noPermissionEdbInfoIdList)
  1027. if lenNoPermissionEdbInfoIdList > 0 {
  1028. condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbInfoIdList) + `) `
  1029. pars = append(pars, noPermissionEdbInfoIdList)
  1030. }
  1031. switch filterSource {
  1032. case 2:
  1033. condition += ` AND frequency='月度' `
  1034. case 3:
  1035. condition += ` AND frequency <> '日度' `
  1036. case 4:
  1037. condition += ` AND edb_type = 1 `
  1038. case 5:
  1039. condition += ` AND source = 32 ` //来源(预测同比值)
  1040. case 6:
  1041. condition += ` AND frequency != ? `
  1042. pars = append(pars, "年度")
  1043. }
  1044. //频度
  1045. if frequency != "" {
  1046. condition += ` AND frequency = ? `
  1047. pars = append(pars, frequency)
  1048. }
  1049. if isOnlyMe {
  1050. condition += " AND sys_user_id = ? "
  1051. pars = append(pars, this.SysUser.AdminId)
  1052. }
  1053. total, edbInfoList, err = data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
  1054. }
  1055. if err != nil {
  1056. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  1057. }
  1058. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1059. edbInfoListLen := len(edbInfoList)
  1060. for i := 0; i < edbInfoListLen; i++ {
  1061. edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
  1062. }
  1063. for i := 0; i < edbInfoListLen; i++ {
  1064. for j := 0; j < edbInfoListLen; j++ {
  1065. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  1066. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  1067. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  1068. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  1069. }
  1070. }
  1071. }
  1072. //新增搜索词记录
  1073. {
  1074. searchKeyword := new(data_manage.SearchKeyword)
  1075. searchKeyword.KeyWord = keyWord
  1076. searchKeyword.CreateTime = time.Now()
  1077. go data_manage.AddSearchKeyword(searchKeyword)
  1078. }
  1079. resp := data_manage.EdbInfoFilterDataResp{
  1080. Paging: page,
  1081. List: edbInfoList,
  1082. }
  1083. br.Ret = 200
  1084. br.Success = true
  1085. br.Msg = "获取成功"
  1086. br.Data = resp
  1087. }
  1088. // RefreshAll
  1089. // @Title 指标全部刷新接口
  1090. // @Description 指标全部刷新接口
  1091. // @Param EdbInfoId query int true "指标id"
  1092. // @Success Ret=200 全部刷新成功
  1093. // @router /predict_edb_info/refresh/all [get]
  1094. func (this *PredictEdbInfoController) RefreshAll() {
  1095. br := new(models.BaseResponse).Init()
  1096. defer func() {
  1097. this.Data["json"] = br
  1098. this.ServeJSON()
  1099. }()
  1100. sysUser := this.SysUser
  1101. if sysUser == nil {
  1102. br.Msg = "请登录"
  1103. br.ErrMsg = "请登录,SysUser Is Empty"
  1104. br.Ret = 408
  1105. return
  1106. }
  1107. edbInfoId, _ := this.GetInt("EdbInfoId")
  1108. if edbInfoId <= 0 {
  1109. br.Msg = "参数错误"
  1110. br.ErrMsg = "参数错误"
  1111. return
  1112. }
  1113. _, isAsync, err, errMsg := data.RefreshPredictEdbInfo(edbInfoId, true)
  1114. if errMsg != `` {
  1115. br.Msg = errMsg
  1116. br.ErrMsg = errMsg
  1117. if err != nil {
  1118. br.ErrMsg = err.Error()
  1119. } else {
  1120. br.IsSendEmail = false
  1121. }
  1122. return
  1123. }
  1124. br.Ret = 200
  1125. br.Success = true
  1126. br.Msg = "刷新成功"
  1127. if isAsync {
  1128. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  1129. }
  1130. }
  1131. // DataList
  1132. // @Title 获取指标数据
  1133. // @Description 获取指标数据
  1134. // @Param EdbInfoId query int true "指标ID"
  1135. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
  1136. // @Param StartDate query string true "自定义开始日期"
  1137. // @Param EndDate query string true "自定义结束日期"
  1138. // @Param Calendar query string true "公历/农历"
  1139. // @Param SeasonStartDate query string true "季节性图开始日期"
  1140. // @Param SeasonEndDate query string true "季节性图结束日期"
  1141. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  1142. // @Success 200 {object} data_manage.EdbInfoDataResp
  1143. // @router /predict_edb_info/data [get]
  1144. func (this *PredictEdbInfoController) DataList() {
  1145. br := new(models.BaseResponse).Init()
  1146. defer func() {
  1147. this.Data["json"] = br
  1148. this.ServeJSON()
  1149. }()
  1150. sysUser := this.SysUser
  1151. if sysUser == nil {
  1152. br.Msg = "请登录"
  1153. br.ErrMsg = "请登录,SysUser Is Empty"
  1154. br.Ret = 408
  1155. return
  1156. }
  1157. edbInfoId, _ := this.GetInt("EdbInfoId")
  1158. if edbInfoId <= 0 {
  1159. br.Msg = "参数错误"
  1160. return
  1161. }
  1162. dateType, _ := this.GetInt("DateType")
  1163. if dateType <= 0 {
  1164. dateType = 3
  1165. }
  1166. edbInfoType, _ := this.GetInt("EdbInfoType")
  1167. if edbInfoType < 0 {
  1168. edbInfoType = 1
  1169. }
  1170. startDate := this.GetString("StartDate")
  1171. endDate := this.GetString("EndDate")
  1172. startYear, _ := this.GetInt("StartYear")
  1173. chartType, _ := this.GetInt("ChartType")
  1174. if chartType <= 0 {
  1175. chartType = 1
  1176. }
  1177. calendar := this.GetString("Calendar")
  1178. if calendar == "" {
  1179. calendar = "公历"
  1180. }
  1181. isTimeBetween := false //是否是时间区间
  1182. if chartType == 2 {
  1183. dateType = 0
  1184. isTimeBetween = true
  1185. }
  1186. if dateType == 5 {
  1187. isTimeBetween = true
  1188. }
  1189. // 获取预测指标信息
  1190. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1191. if err != nil {
  1192. br.Msg = "获取预测指标信息失败"
  1193. br.ErrMsg = "获取预测指标信息失败,Err:" + err.Error()
  1194. return
  1195. }
  1196. maxYear := 0
  1197. if edbInfo.LatestDate != "" {
  1198. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  1199. maxYear = latestDateT.Year()
  1200. }
  1201. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1202. if endDate == "" {
  1203. endDate = time.Now().Format(utils.FormatDate)
  1204. }
  1205. if dateType == 10 {
  1206. startDate = ""
  1207. endDate = ""
  1208. }
  1209. // 按钮校验
  1210. button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId)
  1211. button.AddButton = false
  1212. dataList, _, _, err, _ := data.GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
  1213. //lenDataList := len(dataList)
  1214. //if lenDataList > 0 {
  1215. // tmpValue := dataList[lenDataList-1]
  1216. //
  1217. // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1218. // if edbInfo.MaxValue < tmpValue.Value {
  1219. // edbInfo.MaxValue = tmpValue.Value
  1220. // }
  1221. //
  1222. // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1223. // if edbInfo.MinValue > tmpValue.Value {
  1224. // edbInfo.MinValue = tmpValue.Value
  1225. // }
  1226. //}
  1227. // 季节性图、公历、农历处理
  1228. resultDataList, err := data.GetChartDataList(dataList, chartType, calendar, edbInfo.LatestDate, startDate)
  1229. if err != nil {
  1230. br.Msg = "获取失败"
  1231. br.ErrMsg = "获取失败,Err:" + err.Error()
  1232. return
  1233. }
  1234. fullEdb := new(data_manage.EdbInfoFullClassify)
  1235. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  1236. if err != nil {
  1237. br.Msg = err.Error()
  1238. br.ErrMsg = errMsg
  1239. return
  1240. }
  1241. fullEdb.EdbInfo = edbInfo
  1242. fullEdb.ClassifyList = classifyList
  1243. resp := response.PredictEdbInfoDataResp{
  1244. EdbInfo: fullEdb,
  1245. DataList: resultDataList,
  1246. Button: button,
  1247. }
  1248. br.Ret = 200
  1249. br.Success = true
  1250. br.Msg = "获取成功"
  1251. br.Data = resp
  1252. }
  1253. // ChartDataList
  1254. // @Title 获取指标数据
  1255. // @Description 获取指标数据
  1256. // @Param request body data_manage.AddPredictEdbInfoReq true "type json string"
  1257. // @Success 200 {object} data_manage.EdbInfoDataResp
  1258. // @router /predict_edb_info/chart/data/list [post]
  1259. func (this *PredictEdbInfoController) ChartDataList() {
  1260. br := new(models.BaseResponse).Init()
  1261. defer func() {
  1262. this.Data["json"] = br
  1263. this.ServeJSON()
  1264. }()
  1265. sysUser := this.SysUser
  1266. if sysUser == nil {
  1267. br.Msg = "请登录"
  1268. br.ErrMsg = "请登录,SysUser Is Empty"
  1269. br.Ret = 408
  1270. return
  1271. }
  1272. var req request.PredictEdbInfoChartDataReq
  1273. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1274. if req.SourceEdbInfoId <= 0 {
  1275. br.Msg = "参数错误"
  1276. return
  1277. }
  1278. dateType := req.DateType
  1279. startDate := req.StartDate
  1280. endDate := req.EndDate
  1281. //seasonStartDate := req.SeasonStartDate
  1282. //seasonEndDate := req.SeasonEndDate
  1283. startYear := req.StartYear
  1284. calendar := req.Calendar
  1285. if calendar == "" {
  1286. calendar = "公历"
  1287. }
  1288. isTimeBetween := false //是否是时间区间
  1289. if req.ChartType == 2 {
  1290. dateType = 0
  1291. isTimeBetween = true
  1292. }
  1293. if dateType == 5 {
  1294. isTimeBetween = true
  1295. }
  1296. // 规则列表
  1297. // 根据截止日期重新排序
  1298. ruleList := make([]request.RuleConfig, 0)
  1299. {
  1300. ruleMap := make(map[string]request.RuleConfig)
  1301. dateIntList := make([]int64, 0)
  1302. for _, v := range req.RuleList {
  1303. confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
  1304. if err != nil {
  1305. br.Msg = "配置项中时间异常,请重新选择"
  1306. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  1307. br.IsSendEmail = false
  1308. return
  1309. }
  1310. dateIntList = append(dateIntList, confEndDate.Unix())
  1311. ruleMap[v.EndDate] = v
  1312. }
  1313. sort.Slice(dateIntList, func(i, j int) bool {
  1314. return dateIntList[i] < dateIntList[j]
  1315. })
  1316. for _, dateInt := range dateIntList {
  1317. currDateTime := time.Unix(dateInt, 0)
  1318. item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
  1319. if !ok {
  1320. br.Msg = "配置项中时间异常,请重新选择"
  1321. br.ErrMsg = "配置项中时间异常,请重新选择"
  1322. br.IsSendEmail = false
  1323. return
  1324. }
  1325. ruleList = append(ruleList, item)
  1326. }
  1327. }
  1328. // 预测指标配置
  1329. predictEdbConfAndDataList := make([]data_manage.PredictEdbConfAndData, 0)
  1330. endDateStr := `` //数据的结束日期
  1331. for _, v := range ruleList {
  1332. endDateStr = v.EndDate //预测指标的结束日期
  1333. confEndDate, err := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  1334. if err != nil {
  1335. br.Msg = "配置项中时间异常,请重新选择"
  1336. br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
  1337. return
  1338. }
  1339. // 没有数据,自己瞎测试
  1340. //switch v.RuleType {
  1341. //case 3: //3:同比
  1342. // v.Value = "0.1"
  1343. //case 4: //4:同差
  1344. // v.Value = "10"
  1345. //case 5: //5:环比
  1346. // v.Value = "0.1"
  1347. //case 6: //6:环差
  1348. // v.Value = "0.1"
  1349. //case 7: //7:N期移动均值
  1350. // v.Value = "5"
  1351. //case 8: //8:N期段线性外推值
  1352. // v.Value = "5"
  1353. //}
  1354. tmpDataList := make([]*data_manage.EdbDataList, 0)
  1355. switch v.RuleType {
  1356. case 9:
  1357. // 获取计算参数
  1358. formula, edbInfoList, edbInfoIdBytes, err, errMsg := data.GetCalculateByRuleByNineParams(v)
  1359. if err != nil {
  1360. br.Msg = "计算失败"
  1361. if errMsg != "" {
  1362. br.Msg = errMsg
  1363. }
  1364. br.Msg = err.Error()
  1365. br.IsSendEmail = false
  1366. return
  1367. }
  1368. // 获取计算数据
  1369. tmpDataList, err = data.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes)
  1370. if err != nil {
  1371. br.Msg = "计算失败"
  1372. br.ErrMsg = err.Error()
  1373. br.IsSendEmail = false
  1374. return
  1375. }
  1376. }
  1377. tmpPredictEdbConfAndData := data_manage.PredictEdbConfAndData{
  1378. ConfigId: 0,
  1379. PredictEdbInfoId: 0,
  1380. SourceEdbInfoId: req.SourceEdbInfoId,
  1381. RuleType: v.RuleType,
  1382. FixedValue: 0,
  1383. Value: v.Value,
  1384. EndDate: confEndDate,
  1385. ModifyTime: time.Now(),
  1386. CreateTime: time.Now(),
  1387. DataList: tmpDataList,
  1388. }
  1389. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  1390. }
  1391. // 来源指标
  1392. sourceEdbInfoItem, err := data_manage.GetEdbInfoById(req.SourceEdbInfoId)
  1393. if err != nil {
  1394. br.Msg = "获取来源指标信息失败"
  1395. br.ErrMsg = "获取来源指标信息失败"
  1396. if err.Error() != utils.ErrNoRow() {
  1397. br.ErrMsg = "获取来源指标信息失败,Err:" + err.Error()
  1398. }
  1399. br.Success = true
  1400. return
  1401. }
  1402. // todo 确认预测指标是否用的是来源指标的最新日期
  1403. maxYear := 0
  1404. if sourceEdbInfoItem.LatestDate != "" {
  1405. latestDateT, _ := time.Parse(utils.FormatDate, sourceEdbInfoItem.LatestDate)
  1406. maxYear = latestDateT.Year()
  1407. }
  1408. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1409. if endDate == "" {
  1410. endDate = time.Now().Format(utils.FormatDate)
  1411. }
  1412. if dateType == 10 {
  1413. startDate = ""
  1414. endDate = ""
  1415. }
  1416. minValue := sourceEdbInfoItem.MinValue
  1417. maxValue := sourceEdbInfoItem.MaxValue
  1418. allDataList := make([]*data_manage.EdbDataList, 0)
  1419. //获取指标数据(实际已生成)
  1420. dataList, err := data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
  1421. if err != nil {
  1422. br.Msg = "获取失败"
  1423. br.Msg = "获取失败,Err:" + err.Error()
  1424. return
  1425. }
  1426. // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
  1427. if startDate != `` {
  1428. allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, "", "")
  1429. if err != nil {
  1430. br.Msg = "获取失败"
  1431. br.Msg = "获取失败,Err:" + err.Error()
  1432. return
  1433. }
  1434. } else {
  1435. allDataList = dataList
  1436. }
  1437. // 获取预测指标未来的数据
  1438. if len(predictEdbConfAndDataList) > 0 {
  1439. predictDataList := make([]*data_manage.EdbDataList, 0)
  1440. if isTimeBetween { //如果是时间区间,那么
  1441. reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1442. endDateTime, _ := time.ParseInLocation(utils.FormatDate, req.PredictEndDate, time.Local)
  1443. // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
  1444. if reqEndDateTime.Before(endDateTime) {
  1445. endDateStr = endDate
  1446. }
  1447. }
  1448. // 获取预测数据
  1449. var predictMinValue, predictMaxValue float64
  1450. predictDataList, predictMinValue, predictMaxValue, err, errMsg := data.GetChartPredictEdbInfoDataListByConfList(predictEdbConfAndDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, sourceEdbInfoItem.Frequency, req.DataDateType, allDataList)
  1451. if err != nil {
  1452. br.Msg = "获取预测指标数据失败"
  1453. if errMsg != `` {
  1454. br.Msg = errMsg
  1455. }
  1456. br.ErrMsg = "获取预测指标数据失败:" + err.Error()
  1457. return
  1458. }
  1459. dataList = append(dataList, predictDataList...)
  1460. if len(predictDataList) > 0 {
  1461. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1462. if minValue > predictMinValue {
  1463. minValue = predictMinValue
  1464. }
  1465. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1466. if maxValue < predictMaxValue {
  1467. maxValue = predictMaxValue
  1468. }
  1469. }
  1470. }
  1471. // 季节性图、公历、农历处理
  1472. resultDataList, err := data.GetChartDataList(dataList, req.ChartType, calendar, sourceEdbInfoItem.LatestDate, startDate)
  1473. if err != nil {
  1474. br.Msg = "获取失败"
  1475. br.ErrMsg = "获取失败,Err:" + err.Error()
  1476. return
  1477. }
  1478. resp := response.PredictEdbInfoChartDataResp{
  1479. MinValue: minValue,
  1480. MaxValue: maxValue,
  1481. DataList: resultDataList,
  1482. }
  1483. br.Ret = 200
  1484. br.Success = true
  1485. br.Msg = "获取成功"
  1486. br.Data = resp
  1487. }
  1488. // PredictRuleCalculateByNine
  1489. // @Title 根据预测规则9来计算数据
  1490. // @Description 获取指标数据
  1491. // @Param request body data_manage.RuleConfig true "type json string"
  1492. // @Success 200 {object} data_manage.EdbInfoDataResp
  1493. // @router /predict_edb_info/rule/calculate_by_nine [post]
  1494. func (this *PredictEdbInfoController) PredictRuleCalculateByNine() {
  1495. br := new(models.BaseResponse).Init()
  1496. defer func() {
  1497. this.Data["json"] = br
  1498. this.ServeJSON()
  1499. }()
  1500. sysUser := this.SysUser
  1501. time.Now().AddDate(0, -1, -1).Format(utils.FormatDate)
  1502. if sysUser == nil {
  1503. br.Msg = "请登录"
  1504. br.ErrMsg = "请登录,SysUser Is Empty"
  1505. br.Ret = 408
  1506. return
  1507. }
  1508. var req request.RuleConfig
  1509. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1510. // 获取计算参数
  1511. formula, edbInfoList, edbInfoIdBytes, err, errMsg := data.GetCalculateByRuleByNineParams(req)
  1512. if err != nil {
  1513. br.Msg = "计算失败"
  1514. if errMsg != "" {
  1515. br.Msg = errMsg
  1516. }
  1517. br.Msg = err.Error()
  1518. br.IsSendEmail = false
  1519. return
  1520. }
  1521. // 获取计算数据
  1522. dataList, err := data.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes)
  1523. if err != nil {
  1524. br.Msg = "数据计算失败"
  1525. br.ErrMsg = "数据计算失败:Err:" + err.Error()
  1526. br.IsSendEmail = false
  1527. return
  1528. }
  1529. latestDate := time.Now()
  1530. for _, v := range edbInfoList {
  1531. tmpLatestDate, err := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
  1532. if err != nil {
  1533. continue
  1534. }
  1535. if tmpLatestDate.Before(latestDate) {
  1536. latestDate = tmpLatestDate
  1537. }
  1538. }
  1539. newDataList := make([]*data_manage.EdbDataList, 0)
  1540. lenData := len(dataList)
  1541. if lenData > 0 {
  1542. for i := lenData - 1; i >= 0; i-- {
  1543. newDataList = append(newDataList, dataList[i])
  1544. }
  1545. }
  1546. resp := response.PredictRuleCalculateByNineResp{
  1547. LatestDate: latestDate.Format(utils.FormatDate),
  1548. DataList: newDataList,
  1549. }
  1550. br.Ret = 200
  1551. br.Success = true
  1552. br.Msg = "获取成功"
  1553. br.Data = resp
  1554. }
  1555. // Modify
  1556. // @Title 修改指标信息接口
  1557. // @Description 编辑指标接口
  1558. // @Success 200 {object} request.EditEdbInfoReq
  1559. // @router /predict_edb_info/modify [post]
  1560. func (this *PredictEdbInfoController) Modify() {
  1561. br := new(models.BaseResponse).Init()
  1562. defer func() {
  1563. this.Data["json"] = br
  1564. this.ServeJSON()
  1565. }()
  1566. sysUser := this.SysUser
  1567. if sysUser == nil {
  1568. br.Msg = "请登录"
  1569. br.ErrMsg = "请登录,SysUser Is Empty"
  1570. br.Ret = 408
  1571. return
  1572. }
  1573. var req request.ModifyPredictEdbInfoReq
  1574. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1575. if err != nil {
  1576. br.Msg = "参数解析异常!"
  1577. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1578. return
  1579. }
  1580. edbInfoId := req.EdbInfoId
  1581. if edbInfoId <= 0 {
  1582. br.Msg = "请选择预测指标"
  1583. br.IsSendEmail = false
  1584. return
  1585. }
  1586. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1587. if err != nil {
  1588. br.Msg = "修改失败"
  1589. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1590. return
  1591. }
  1592. // 编辑权限校验
  1593. button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId)
  1594. if !button.OpButton {
  1595. br.Msg = "无权限操作"
  1596. br.IsSendEmail = false
  1597. return
  1598. }
  1599. edbInfo.MaxValue = req.MaxValue
  1600. edbInfo.MinValue = req.MinValue
  1601. edbInfo.ModifyTime = time.Now()
  1602. err = edbInfo.Update([]string{"MaxValue", "MinValue", "ModifyTime"})
  1603. if err != nil {
  1604. br.Msg = "获取失败"
  1605. br.ErrMsg = "获取失败,Err:" + err.Error()
  1606. return
  1607. }
  1608. //修改es
  1609. data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  1610. br.Ret = 200
  1611. br.Success = true
  1612. br.Msg = "修改成功"
  1613. br.IsAddLog = true
  1614. }
  1615. //func init() {
  1616. // r := new(regression.Regression)
  1617. // r.SetObserved("Murders per annum per 1,000,000 inhabitants")
  1618. // r.SetVar(0, "Inhabitants")
  1619. // r.SetVar(1, "Percent with incomes below $5000")
  1620. // r.SetVar(2, "Percent unemployed")
  1621. // r.Train(
  1622. // regression.DataPoint(11.2, []float64{587000, 16.5, 6.2}),
  1623. // regression.DataPoint(13.4, []float64{643000, 20.5, 6.4}),
  1624. // regression.DataPoint(40.7, []float64{635000, 26.3, 9.3}),
  1625. // regression.DataPoint(5.3, []float64{692000, 16.5, 5.3}),
  1626. // regression.DataPoint(24.8, []float64{1248000, 19.2, 7.3}),
  1627. // regression.DataPoint(12.7, []float64{643000, 16.5, 5.9}),
  1628. // regression.DataPoint(20.9, []float64{1964000, 20.2, 6.4}),
  1629. // regression.DataPoint(35.7, []float64{1531000, 21.3, 7.6}),
  1630. // regression.DataPoint(8.7, []float64{713000, 17.2, 4.9}),
  1631. // regression.DataPoint(9.6, []float64{749000, 14.3, 6.4}),
  1632. // regression.DataPoint(14.5, []float64{7895000, 18.1, 6}),
  1633. // regression.DataPoint(26.9, []float64{762000, 23.1, 7.4}),
  1634. // regression.DataPoint(15.7, []float64{2793000, 19.1, 5.8}),
  1635. // regression.DataPoint(36.2, []float64{741000, 24.7, 8.6}),
  1636. // regression.DataPoint(18.1, []float64{625000, 18.6, 6.5}),
  1637. // regression.DataPoint(28.9, []float64{854000, 24.9, 8.3}),
  1638. // regression.DataPoint(14.9, []float64{716000, 17.9, 6.7}),
  1639. // regression.DataPoint(25.8, []float64{921000, 22.4, 8.6}),
  1640. // regression.DataPoint(21.7, []float64{595000, 20.2, 8.4}),
  1641. // regression.DataPoint(25.7, []float64{3353000, 16.9, 6.7}),
  1642. // )
  1643. // r.Run()
  1644. //
  1645. // fmt.Printf("Regression formula:\n%v\n", r.Formula)
  1646. // //fmt.Printf("Regression:\n%s\n", r)
  1647. //}
  1648. //func init() {
  1649. // //data := []float64{4080, 4070, 4070, 4020, 4020, 4010, 3950, 3980, 4000, 3990, 4020}
  1650. // //median, _ := stats.Median(data)
  1651. // //fmt.Println(median) // 3.65
  1652. // //stats.LinearRegression()
  1653. //
  1654. // //data := []float64{ , , , , , , , , }
  1655. //
  1656. // data := []Coordinate{
  1657. // {1, 4080},
  1658. // {2, 4070},
  1659. // {3, 4070},
  1660. // {4, 4020},
  1661. // {5, 4020},
  1662. // {6, 4010},
  1663. // {7, 3950},
  1664. // {8, 3980},
  1665. // {9, 4000},
  1666. // {10, 3990},
  1667. // {11, 4020},
  1668. // }
  1669. //
  1670. // //r, _ := ct(data)
  1671. //
  1672. // ct(data)
  1673. // //fmt.Println(r)
  1674. //
  1675. //}
  1676. //func init() {
  1677. // data.AddOrEditAllEdbInfoToEs()
  1678. //}
  1679. // ClassifyEdbInfoItems
  1680. // @Title 获取分类下指标接口
  1681. // @Description 获取分类下指标接口
  1682. // @Param ClassifyId query int true "分类id"
  1683. // @Param IsShowMe query bool false "是否只看我的"
  1684. // @Success 200 {object} data_manage.EdbClassifyListResp
  1685. // @router /predict_edb_info/items [get]
  1686. func (this *PredictEdbInfoController) ClassifyEdbInfoItems() {
  1687. br := new(models.BaseResponse).Init()
  1688. defer func() {
  1689. this.Data["json"] = br
  1690. this.ServeJSON()
  1691. }()
  1692. classifyId, _ := this.GetInt("ClassifyId")
  1693. if classifyId <= 0 {
  1694. br.Msg = "参数错误,请刷新页面"
  1695. return
  1696. }
  1697. // 是否只看我的
  1698. isShowMe, _ := this.GetBool("IsShowMe")
  1699. // 获取当前账号的不可见指标
  1700. obj := data_manage.EdbInfoNoPermissionAdmin{}
  1701. confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
  1702. if err != nil && err.Error() != utils.ErrNoRow() {
  1703. br.Msg = "获取失败"
  1704. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1705. return
  1706. }
  1707. noPermissionEdbInfoIdMap := make(map[int]bool)
  1708. for _, v := range confList {
  1709. noPermissionEdbInfoIdMap[v.EdbInfoId] = true
  1710. }
  1711. // 如果选择了只看我的,那么只查询归属于我的账号
  1712. sysUserId := 0
  1713. if isShowMe {
  1714. sysUserId = this.SysUser.AdminId
  1715. }
  1716. allEdbInfo, err := data_manage.GetEdbInfoByClassifyId(classifyId, 1, sysUserId)
  1717. if err != nil && err.Error() != utils.ErrNoRow() {
  1718. br.Msg = "获取失败"
  1719. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1720. return
  1721. }
  1722. list := make([]*data_manage.EdbClassifyItems, 0)
  1723. for _, v := range allEdbInfo {
  1724. // 如果指标不可见,那么就不返回该指标
  1725. if _, ok := noPermissionEdbInfoIdMap[v.EdbInfoId]; ok {
  1726. continue
  1727. }
  1728. button := data.GetEdbOpButton(this.SysUser, v.SysUserId, v.EdbType, utils.EDB_INFO_TYPE)
  1729. button.AddButton = false //不管有没有权限,指标都是没有添加按钮的
  1730. v.Button = button
  1731. list = append(list, v)
  1732. }
  1733. resp := new(data_manage.ClassifyEdbInfoListResp)
  1734. resp.EdbInfoList = list
  1735. br.Ret = 200
  1736. br.Success = true
  1737. br.Msg = "获取成功"
  1738. br.Data = resp
  1739. }