predict_edb_info.go 57 KB

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