predict_edb_info.go 63 KB

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