predict_edb_info.go 62 KB

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