predict_edb_info.go 67 KB

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