predict_edb_info.go 62 KB

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