predict_edb_info.go 59 KB

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