predict_edb_info.go 63 KB

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