predict_edb_info.go 52 KB

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