gpr_risk_data.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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/system"
  8. "eta/eta_api/services/data"
  9. etaTrialService "eta/eta_api/services/eta_trial"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/rdlucklib/rdluck_tools/paging"
  18. "github.com/tealeg/xlsx"
  19. )
  20. type BaseFromGprRiskController struct {
  21. controllers.BaseAuthController
  22. }
  23. // GprRiskClassify
  24. // @Title GPR地缘风险指数数据分类
  25. // @Description GPR地缘风险指数数据分类接口
  26. // @Success 200 {object} data_manage.BaseFromGprRiskClassify
  27. // @router /gpr_risk/classify [get]
  28. func (this *BaseFromGprRiskController) GprRiskClassify() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. classifyAll, err := data_manage.GetAllBaseFromGprRiskClassify()
  42. if err != nil && err.Error() != utils.ErrNoRow() {
  43. br.Msg = "获取失败"
  44. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  45. return
  46. }
  47. //组装一级分类
  48. rootMap := make(map[int][]*data_manage.BaseFromGprRiskClassifyItems)
  49. list := make([]*data_manage.BaseFromGprRiskClassifyItems, 0)
  50. for _, classify := range classifyAll {
  51. classify.UniqueCode = strconv.Itoa(classify.ClassifyId)
  52. if classify.ParentId == 0 {
  53. if _, ok := rootMap[classify.ClassifyId]; !ok {
  54. rootMap[classify.ClassifyId] = make([]*data_manage.BaseFromGprRiskClassifyItems, 0)
  55. list = append(list, classify)
  56. }
  57. } else {
  58. child, ok := rootMap[classify.ParentId]
  59. if ok {
  60. child = append(child, classify)
  61. rootMap[classify.ParentId] = child
  62. }
  63. }
  64. }
  65. for k, v := range list {
  66. child, ok := rootMap[v.ClassifyId]
  67. if ok {
  68. list[k].Children = child
  69. }
  70. }
  71. //组装二级分类
  72. var ret data_manage.BaseFromGprRiskClassifyResp
  73. ret.List = list
  74. br.Ret = 200
  75. br.Success = true
  76. br.Msg = "获取成功"
  77. br.Data = ret
  78. }
  79. // GprRiskIndexData
  80. // @Title 获取GPR地缘风险指数数据
  81. // @Description 获取GPR地缘风险指数数据接口
  82. // @Param PageSize query int true "每页数据条数"
  83. // @Param CurrentIndex query int true "当前页页码,从1开始"
  84. // @Param ClassifyId query string true "分类id"
  85. // @Success 200 {object} data_manage.LzFrequency
  86. // @router /gpr_risk/index/data [get]
  87. func (this *BaseFromGprRiskController) GprRiskIndexData() {
  88. br := new(models.BaseResponse).Init()
  89. defer func() {
  90. this.Data["json"] = br
  91. this.ServeJSON()
  92. }()
  93. sysUser := this.SysUser
  94. if sysUser == nil {
  95. br.Msg = "请登录"
  96. br.ErrMsg = "请登录,SysUser Is Empty"
  97. br.Ret = 408
  98. return
  99. }
  100. pageSize, _ := this.GetInt("PageSize")
  101. currentIndex, _ := this.GetInt("CurrentIndex")
  102. var startSize int
  103. if pageSize <= 0 {
  104. pageSize = utils.PageSize20
  105. }
  106. if currentIndex <= 0 {
  107. currentIndex = 1
  108. }
  109. startSize = utils.StartIndex(currentIndex, pageSize)
  110. classifyId, _ := this.GetInt("ClassifyId")
  111. if classifyId < 0 {
  112. br.Msg = "请选择分类"
  113. br.ErrMsg = "请选择分类"
  114. return
  115. }
  116. // 增加频度请求入参
  117. frequency := this.GetString("Frequency")
  118. //获取指标
  119. var condition string
  120. var pars []interface{}
  121. if classifyId > 0 {
  122. condition += ` AND classify_id=? `
  123. pars = append(pars, classifyId)
  124. }
  125. if frequency != "" {
  126. condition += ` AND frequency=? `
  127. pars = append(pars, frequency)
  128. }
  129. GprRiskList, err := data_manage.GetGprRiskIndex(condition, pars)
  130. if err != nil {
  131. br.Msg = "获取数据失败"
  132. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  133. return
  134. }
  135. edbCodeList := make([]string, 0)
  136. for _, v := range GprRiskList {
  137. edbCodeList = append(edbCodeList, v.IndexCode)
  138. }
  139. edbInfoMap := make(map[string]*data_manage.EdbInfo)
  140. dataMap := make(map[string][]*data_manage.BaseFromGprRiskData)
  141. total := 0
  142. if len(edbCodeList) > 0 {
  143. edbInfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_GPR_RISK, edbCodeList)
  144. if err != nil {
  145. br.Msg = "获取数据源失败"
  146. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  147. return
  148. }
  149. for _, v := range edbInfoList {
  150. edbInfoMap[v.EdbCode] = v
  151. }
  152. // 首先对分类下的指标按照日期进行分页,再针对日期,进行排序
  153. dataTimes, err := data_manage.GetGprRiskIndexDataTimePageByCodes(edbCodeList, startSize, pageSize)
  154. if err != nil {
  155. br.Msg = "获取数据失败"
  156. br.ErrMsg = "获取指标数据日期信息失败,Err:" + err.Error()
  157. return
  158. }
  159. if len(dataTimes) > 0 {
  160. startDate := dataTimes[len(dataTimes)-1]
  161. endDate := dataTimes[0]
  162. // 把截止日往后加1天
  163. endDateT, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  164. endDate = endDateT.AddDate(0, 0, 1).Format(utils.FormatDate)
  165. dataList, e := data_manage.GetGprRiskIndexDataByDataTime(edbCodeList, startDate, endDate)
  166. if e != nil {
  167. br.Msg = "获取数据失败"
  168. br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
  169. return
  170. }
  171. //将数据按照指标进行分类
  172. for _, v := range dataList {
  173. dataMap[v.IndexCode] = append(dataMap[v.IndexCode], v)
  174. }
  175. }
  176. total, err = data_manage.GetGprRiskIndexDataTimePageCount(edbCodeList)
  177. if err != nil {
  178. br.Msg = "获取数据失败"
  179. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  180. return
  181. }
  182. }
  183. page := paging.GetPaging(currentIndex, pageSize, total)
  184. resultList := make([]*data_manage.BaseFromGprRiskIndexList, 0)
  185. for _, v := range GprRiskList {
  186. product := new(data_manage.BaseFromGprRiskIndexList)
  187. product.BaseFromGprRiskIndexId = v.BaseFromGprRiskIndexId
  188. product.Unit = v.Unit
  189. product.IndexCode = v.IndexCode
  190. product.IndexName = v.IndexName
  191. product.Frequency = v.Frequency
  192. product.ModifyTime = v.ModifyTime
  193. product.ClassifyId = v.ClassifyId
  194. if edb, ok := edbInfoMap[v.IndexCode]; ok {
  195. product.EdbInfoId = edb.EdbInfoId
  196. product.EdbExist = 1
  197. }
  198. dataListTmp, ok := dataMap[v.IndexCode]
  199. if !ok {
  200. dataListTmp = make([]*data_manage.BaseFromGprRiskData, 0)
  201. }
  202. product.DataList = dataListTmp
  203. product.Paging = page
  204. resultList = append(resultList, product)
  205. }
  206. br.Ret = 200
  207. br.Success = true
  208. br.Msg = "获取成功"
  209. br.Data = resultList
  210. }
  211. // GprRiskSearchList
  212. // @Title GprRisk模糊搜索
  213. // @Description GprRisk模糊搜索
  214. // @Param Keyword query string ture "关键字搜索"
  215. // @Success 200 {object} models.BaseResponse
  216. // @router /gpr_risk/search_list [get]
  217. func (this *BaseFromGprRiskController) GprRiskSearchList() {
  218. br := new(models.BaseResponse).Init()
  219. defer func() {
  220. this.Data["json"] = br
  221. this.ServeJSON()
  222. }()
  223. sysUser := this.SysUser
  224. if sysUser == nil {
  225. br.Msg = "请重新登录"
  226. return
  227. }
  228. list := make([]*data_manage.BaseFromGprRiskIndexSearchItem, 0)
  229. var err error
  230. //关键字
  231. keyword := this.GetString("Keyword")
  232. if keyword != "" {
  233. keyWordArr := strings.Split(keyword, " ")
  234. if len(keyWordArr) > 0 {
  235. condition := ""
  236. for _, v := range keyWordArr {
  237. condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
  238. }
  239. list, err = data_manage.GetGprRiskItemList(condition)
  240. if err != nil {
  241. br.ErrMsg = "获取失败,Err:" + err.Error()
  242. br.Msg = "获取失败"
  243. return
  244. }
  245. }
  246. } else {
  247. list, err = data_manage.GetGprRiskItemList("")
  248. if err != nil {
  249. br.ErrMsg = "获取失败,Err:" + err.Error()
  250. br.Msg = "获取失败"
  251. return
  252. }
  253. }
  254. classifyIds := make([]int, 0)
  255. for _, v := range list {
  256. classifyIds = append(classifyIds, v.ClassifyId)
  257. }
  258. classifyList, err := data_manage.GetBaseFromGprRiskClassifyByIds(classifyIds)
  259. if err != nil {
  260. br.Msg = "搜索失败"
  261. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  262. return
  263. }
  264. classifyMap := make(map[int]int)
  265. for _, v := range classifyList {
  266. classifyMap[v.ClassifyId] = v.ParentId
  267. }
  268. for _, v := range list {
  269. v.ParentClassifyId = classifyMap[v.ClassifyId]
  270. }
  271. br.Ret = 200
  272. br.Success = true
  273. br.Msg = "获取成功"
  274. br.Data = list
  275. }
  276. // GprRiskSingleData
  277. // @Title 获取GprRisk数据
  278. // @Description 获取GprRisk单条数据接口
  279. // @Param IndexCode query string true "指标唯一编码"
  280. // @Success 200 {object} models.BaseResponse
  281. // @router /gpr_risk/single_data [get]
  282. func (this *BaseFromGprRiskController) GprRiskSingleData() {
  283. br := new(models.BaseResponse).Init()
  284. defer func() {
  285. this.Data["json"] = br
  286. this.ServeJSON()
  287. }()
  288. sysUser := this.SysUser
  289. if sysUser == nil {
  290. br.Msg = "请登录"
  291. br.ErrMsg = "请登录,SysUser Is Empty"
  292. br.Ret = 408
  293. return
  294. }
  295. indexCode := this.GetString("IndexCode")
  296. pageSize, _ := this.GetInt("PageSize")
  297. currentIndex, _ := this.GetInt("CurrentIndex")
  298. var total int64
  299. page := paging.GetPaging(currentIndex, pageSize, int(total))
  300. var startSize int
  301. if pageSize <= 0 {
  302. pageSize = utils.PageSize20
  303. }
  304. if currentIndex <= 0 {
  305. currentIndex = 1
  306. }
  307. startSize = paging.StartIndex(currentIndex, pageSize)
  308. indexInfo, err := data_manage.GetBaseFromGprRiskIndexByIndexCode(indexCode)
  309. if err != nil {
  310. br.Msg = "获取指标信息失败"
  311. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  312. return
  313. }
  314. total, err = data_manage.GetGprRiskIndexDataTotalByCode(indexCode)
  315. if err != nil {
  316. br.Msg = "获取数据失败"
  317. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  318. return
  319. }
  320. page = paging.GetPaging(currentIndex, pageSize, int(total))
  321. dataTmpList, err := data_manage.GetGprRiskIndexDataByCode(indexCode, startSize, pageSize)
  322. if err != nil {
  323. br.Msg = "获取数据失败"
  324. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  325. return
  326. }
  327. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_GPR_RISK, indexCode)
  328. if err != nil && err.Error() != utils.ErrNoRow() {
  329. br.Msg = "获取数据源失败"
  330. br.ErrMsg = "获取数据源失败,Err:" + err.Error()
  331. return
  332. }
  333. var ret data_manage.GprRiskSingleDataResp
  334. var dataList []*data_manage.GprRiskSingleData
  335. if edbInfo != nil {
  336. ret.EdbInfoId = edbInfo.EdbInfoId
  337. ret.EdbExist = 1
  338. }
  339. ret.ClassifyId = indexInfo.ClassifyId
  340. ret.BaseFromGprRiskIndexId = indexInfo.BaseFromGprRiskIndexId
  341. ret.IndexCode = indexInfo.IndexCode
  342. ret.IndexName = indexInfo.IndexName
  343. ret.Frequency = indexInfo.Frequency
  344. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  345. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  346. ret.Unit = indexInfo.Unit
  347. for _, v := range dataTmpList {
  348. tmp := &data_manage.GprRiskSingleData{
  349. Value: v.Value,
  350. DataTime: v.DataTime,
  351. }
  352. dataList = append(dataList, tmp)
  353. }
  354. ret.Data = dataList
  355. ret.Paging = page
  356. br.Ret = 200
  357. br.Success = true
  358. br.Msg = "获取成功"
  359. br.Data = ret
  360. }
  361. // GprRiskIndexList
  362. // @Title GPR地缘风险指数指标列表
  363. // @Description GPR地缘风险指数指标列表
  364. // @Param ClassifyId query int true "分类id"
  365. // @Success 200 {object} data_manage.BaseFromGprRiskClassifyResp
  366. // @router /gpr_risk/classify/index/list [get]
  367. func (this *BaseFromGprRiskController) GprRiskIndexList() {
  368. br := new(models.BaseResponse).Init()
  369. defer func() {
  370. this.Data["json"] = br
  371. this.ServeJSON()
  372. }()
  373. classifyId, _ := this.GetInt("ClassifyId", 0)
  374. indexList, err := data_manage.GetGprRiskIndexByClassifyId(classifyId)
  375. if err != nil && err.Error() != utils.ErrNoRow() {
  376. br.Msg = "获取失败"
  377. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  378. return
  379. }
  380. var ret data_manage.BaseFromGprRiskClassifyResp
  381. list := make([]*data_manage.BaseFromGprRiskClassifyItems, 0)
  382. for _, v := range indexList {
  383. classify := new(data_manage.BaseFromGprRiskClassifyItems)
  384. classify.ClassifyId = classifyId
  385. classify.BaseFromGprRiskIndexId = v.BaseFromGprRiskIndexId
  386. classify.IndexCode = v.IndexCode
  387. classify.ClassifyName = v.IndexName
  388. classify.UniqueCode = fmt.Sprintf("%d_%d", classifyId, v.BaseFromGprRiskIndexId)
  389. list = append(list, classify)
  390. }
  391. ret.List = list
  392. br.Ret = 200
  393. br.Success = true
  394. br.Msg = "获取成功"
  395. br.Data = ret
  396. }
  397. // GprRiskNameCheck
  398. // @Title 加入指标库的重名检测
  399. // @Description 加入指标库的重名检测
  400. // @Param ClassifyIds query string true "分类id, 多个分类用英文"
  401. // @Param Keyword query string true "关键词, 指标ID/指标名称"
  402. // @Success 200 {object} NameCheckResult
  403. // @router /gpr_risk/edb_info/name_check [post]
  404. func (this *BaseFromGprRiskController) GprRiskNameCheck() {
  405. br := new(models.BaseResponse).Init()
  406. defer func() {
  407. if br.ErrMsg == "" {
  408. br.IsSendEmail = false
  409. }
  410. this.Data["json"] = br
  411. this.ServeJSON()
  412. }()
  413. sysUser := this.SysUser
  414. if sysUser == nil {
  415. br.Msg = "请登录"
  416. br.ErrMsg = "请登录,SysUser Is Empty"
  417. br.Ret = 408
  418. return
  419. }
  420. var req []*data_manage.NameCheckEdbInfoReq
  421. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  422. br.Msg = "参数解析异常!"
  423. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  424. return
  425. }
  426. if len(req) == 0 {
  427. br.Msg = "请选择指标"
  428. return
  429. }
  430. codeMaxT := 30
  431. codeLen := len(req)
  432. if codeLen > codeMaxT {
  433. br.Msg = "批量添加指标数量不得超过30个"
  434. return
  435. }
  436. indexNames := make([]string, 0)
  437. resp := make([]*data_manage.EdbNameCheckResult, 0)
  438. for _, v := range req {
  439. v.EdbCode = strings.TrimSpace(v.EdbCode)
  440. if v.EdbCode == "" {
  441. br.Msg = "指标ID不可为空"
  442. return
  443. }
  444. v.EdbName = strings.TrimSpace(v.EdbName)
  445. if v.EdbName == "" {
  446. br.Msg = "请输入指标名称"
  447. return
  448. }
  449. indexNames = append(indexNames, v.EdbName)
  450. resp = append(resp, &data_manage.EdbNameCheckResult{
  451. EdbCode: v.EdbCode,
  452. EdbName: v.EdbName,
  453. })
  454. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_GPR_RISK, 0, utils.EDB_DATA_LIMIT)
  455. if err != nil && err.Error() != utils.ErrNoRow() {
  456. br.Msg = "获取失败"
  457. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  458. return
  459. }
  460. if len(dataItems) <= 0 {
  461. respItem, err := data.AddEdbData(utils.DATA_SOURCE_GPR_RISK, v.EdbCode, v.Frequency)
  462. if err != nil {
  463. br.Msg = "获取失败"
  464. br.ErrMsg = "获取失败,Err:" + err.Error()
  465. return
  466. }
  467. if respItem.Ret != 200 {
  468. br.Msg = "未搜索到该指标"
  469. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  470. return
  471. }
  472. }
  473. }
  474. // 重名校验
  475. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  476. if e != nil {
  477. br.Msg = "操作失败"
  478. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  479. return
  480. }
  481. nameExists := make(map[string]bool)
  482. for _, v := range edbList {
  483. nameExists[v.EdbName] = true
  484. }
  485. if len(nameExists) > 0 {
  486. for _, v := range resp {
  487. v.Exist = nameExists[v.EdbName]
  488. }
  489. }
  490. br.Data = resp
  491. br.Msg = "校验成功"
  492. br.Ret = 200
  493. br.Success = true
  494. }
  495. // GprRiskAddCheck
  496. // @Title 加入指标库指标Id检测
  497. // @Description 加入指标库指标Id检测
  498. // @Param request body request.BatchAddCheckReq true "type json string"
  499. // @Success 200 string "操作成功"
  500. // @router /gpr_risk/edb_info/add_check [post]
  501. func (c *BaseFromGprRiskController) GprRiskAddCheck() {
  502. br := new(models.BaseResponse).Init()
  503. defer func() {
  504. if br.ErrMsg == "" {
  505. br.IsSendEmail = false
  506. }
  507. c.Data["json"] = br
  508. c.ServeJSON()
  509. }()
  510. sysUser := c.SysUser
  511. if sysUser == nil {
  512. br.Msg = "请登录"
  513. br.ErrMsg = "请登录,SysUser Is Empty"
  514. br.Ret = 408
  515. return
  516. }
  517. var req data_manage.BatchAddCheckReq
  518. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  519. br.Msg = "参数解析异常!"
  520. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  521. return
  522. }
  523. codeMaxT := 30
  524. codeLen := len(req.IndexCodes)
  525. // 获取指标库已有指标
  526. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_GPR_RISK)
  527. if e != nil {
  528. br.Msg = "获取失败"
  529. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  530. return
  531. }
  532. existMap := make(map[string]*data_manage.EdbInfo)
  533. for _, v := range existsEdb {
  534. existMap[v.EdbCode] = v
  535. }
  536. if codeLen == 0 {
  537. br.Msg = "请选择指标"
  538. return
  539. }
  540. if codeLen > codeMaxT {
  541. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  542. return
  543. }
  544. // 查询选中的指标
  545. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  546. pars := make([]interface{}, 0)
  547. pars = append(pars, req.IndexCodes)
  548. list, err := data_manage.GetGprRiskIndex(cond, pars)
  549. if err != nil {
  550. br.Msg = "获取失败"
  551. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  552. return
  553. }
  554. if len(list) > codeMaxT {
  555. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  556. return
  557. }
  558. resp := make([]*data_manage.BaseFromGprRiskIndexList, 0)
  559. for _, v := range list {
  560. if edb, ok := existMap[v.IndexCode]; ok {
  561. v.EdbInfoId = edb.EdbInfoId
  562. v.EdbClassifyId = edb.ClassifyId
  563. v.EdbUniqueCode = edb.UniqueCode
  564. v.EdbExist = 1
  565. }
  566. resp = append(resp, v)
  567. }
  568. br.Data = resp
  569. br.Msg = "校验成功"
  570. br.Ret = 200
  571. br.Success = true
  572. }
  573. // GprRiskEdbInfoAdd
  574. // @Title 新增指标接口
  575. // @Description 新增指标接口
  576. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  577. // @Success Ret=200 保存成功
  578. // @router /gpr_risk/edb_info/add [post]
  579. func (this *BaseFromGprRiskController) GprRiskEdbInfoAdd() {
  580. br := new(models.BaseResponse).Init()
  581. defer func() {
  582. this.Data["json"] = br
  583. this.ServeJSON()
  584. }()
  585. sysUser := this.SysUser
  586. if sysUser == nil {
  587. br.Msg = "请登录"
  588. br.ErrMsg = "请登录,SysUser Is Empty"
  589. br.Ret = 408
  590. return
  591. }
  592. deleteCache := true
  593. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  594. defer func() {
  595. if deleteCache {
  596. utils.Rc.Delete(cacheKey)
  597. }
  598. }()
  599. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  600. deleteCache = false
  601. br.Msg = "系统处理中,请稍后重试!"
  602. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  603. return
  604. }
  605. var req data_manage.AddEdbInfoReq
  606. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  607. if err != nil {
  608. br.Msg = "参数解析异常!"
  609. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  610. return
  611. }
  612. req.EdbName = strings.Trim(req.EdbName, " ")
  613. req.EdbCode = strings.Trim(req.EdbCode, " ")
  614. if req.EdbCode == "" {
  615. br.Msg = "指标ID不能为空"
  616. return
  617. }
  618. if req.EdbName == "" {
  619. br.Msg = "指标名称不能为空"
  620. return
  621. }
  622. if req.Frequency == "" {
  623. br.Msg = "频率不能为空"
  624. return
  625. }
  626. if req.Unit == "" {
  627. br.Msg = "单位不能为空"
  628. return
  629. }
  630. if req.ClassifyId <= 0 {
  631. br.Msg = "请选择分类"
  632. return
  633. }
  634. count, err := data_manage.GetGprRiskIndexDataCount(req.EdbCode)
  635. if err != nil {
  636. br.Msg = "获取失败"
  637. br.ErrMsg = "获取失败,Err:" + err.Error()
  638. return
  639. }
  640. if count == 0 {
  641. br.Msg = "指标不存在"
  642. }
  643. // 指标入库
  644. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(utils.DATA_SOURCE_GPR_RISK, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, req.StartDate, req.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
  645. if err != nil {
  646. br.Msg = "保存失败"
  647. if errMsg != `` {
  648. br.Msg = errMsg
  649. }
  650. br.ErrMsg = err.Error()
  651. br.IsSendEmail = isSendEmail
  652. return
  653. }
  654. // 试用平台更新用户累计新增指标数
  655. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  656. if e != nil {
  657. br.Msg = "操作失败"
  658. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  659. return
  660. }
  661. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  662. go func() {
  663. var r etaTrialService.EtaTrialUserReq
  664. r.Mobile = adminItem.Mobile
  665. _, _ = etaTrialService.UpdateUserIndexNum(r)
  666. }()
  667. }
  668. //新增操作日志
  669. {
  670. edbLog := new(data_manage.EdbInfoLog)
  671. edbLog.EdbInfoId = edbInfo.EdbInfoId
  672. edbLog.SourceName = edbInfo.SourceName
  673. edbLog.Source = edbInfo.Source
  674. edbLog.EdbCode = edbInfo.EdbCode
  675. edbLog.EdbName = edbInfo.EdbName
  676. edbLog.ClassifyId = edbInfo.ClassifyId
  677. edbLog.SysUserId = sysUser.AdminId
  678. edbLog.SysUserRealName = sysUser.RealName
  679. edbLog.CreateTime = time.Now()
  680. edbLog.Content = string(this.Ctx.Input.RequestBody)
  681. edbLog.Status = "新增指标"
  682. edbLog.Method = this.Ctx.Input.URI()
  683. go data_manage.AddEdbInfoLog(edbLog)
  684. }
  685. // 更新es
  686. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  687. resp := new(data_manage.AddEdbInfoResp)
  688. resp.EdbInfoId = edbInfo.EdbInfoId
  689. resp.UniqueCode = edbInfo.UniqueCode
  690. br.Ret = 200
  691. br.Success = true
  692. br.Msg = "保存成功"
  693. br.Data = resp
  694. br.IsAddLog = true
  695. }
  696. // ExportGprRiskList
  697. // @Title 导出GPR地缘风险指数数据
  698. // @Description 导出GPR地缘风险指数数据
  699. // @Param ClassifyId query int true "关键字搜索"
  700. // @Param IndexCode query string true "指标编码"
  701. // @Success 200 导出成功
  702. // @router /gpr_risk/export [get]
  703. func (this *BaseFromGprRiskController) ExportGprRiskList() {
  704. br := new(models.BaseResponse).Init()
  705. defer func() {
  706. this.Data["json"] = br
  707. this.ServeJSON()
  708. }()
  709. sysUser := this.SysUser
  710. if sysUser == nil {
  711. br.Msg = "请重新登录"
  712. return
  713. }
  714. classifyId, _ := this.GetInt("ClassifyId")
  715. indexCode := this.GetString("IndexCode")
  716. if classifyId <= 0 && indexCode == "" {
  717. br.Msg = "请选择分类或者指标"
  718. return
  719. }
  720. dir, _ := os.Executable()
  721. exPath := filepath.Dir(dir)
  722. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  723. xlsxFile := xlsx.NewFile()
  724. var condition string
  725. var pars []interface{}
  726. var classifyName string
  727. if classifyId > 0 {
  728. classifyInfo, err := data_manage.GetBaseFromGprRiskClassifyById(classifyId)
  729. if err != nil {
  730. if err.Error() == utils.ErrNoRow() {
  731. br.Msg = "分类不存在"
  732. return
  733. }
  734. br.Msg = "下载失败"
  735. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  736. return
  737. }
  738. classifyName = classifyInfo.ClassifyName
  739. childClassify, err := data_manage.GetBaseFromGprRiskClassifyByParentId(classifyId)
  740. if err != nil {
  741. br.Msg = "下载失败"
  742. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  743. return
  744. }
  745. if len(childClassify) > 0 {
  746. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  747. for _, child := range childClassify {
  748. pars = append(pars, child.ClassifyId)
  749. }
  750. } else {
  751. condition += ` AND classify_id=?`
  752. pars = append(pars, classifyId)
  753. }
  754. }
  755. if indexCode != "" {
  756. condition += ` AND index_code=? `
  757. pars = append(pars, indexCode)
  758. }
  759. indexList, err := data_manage.GetGprRiskIndex(condition, pars)
  760. if err != nil {
  761. br.Msg = "下载失败"
  762. br.ErrMsg = "获取指标失败,Err:" + err.Error()
  763. fmt.Println("获取数据失败,Err:" + err.Error())
  764. return
  765. }
  766. if len(indexList) <= 0 {
  767. fmt.Println("indexList 为空")
  768. br.Ret = 200
  769. br.Success = true
  770. br.Msg = "success"
  771. return
  772. }
  773. codeList := make([]string, 0)
  774. frequenciesMap := make(map[string][]*data_manage.BaseFromGprRiskIndexList)
  775. for _, v := range indexList {
  776. codeList = append(codeList, v.IndexCode)
  777. frequenciesMap[v.Frequency] = append(frequenciesMap[v.Frequency], v)
  778. }
  779. dataListMap := make(map[string][]*data_manage.BaseFromGprRiskData)
  780. if len(indexList) > 0 {
  781. allDataList, e := data_manage.GetGprRiskIndexDataByCodes(codeList)
  782. if e != nil {
  783. br.Msg = "获取数据失败"
  784. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  785. return
  786. }
  787. for _, v := range allDataList {
  788. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  789. }
  790. }
  791. // 按照频率分组排序
  792. frequencies := []string{
  793. "日度", "周度", "旬度", "月度", "季度", "半年度", "年度",
  794. }
  795. for _, frequency := range frequencies {
  796. //获取指标
  797. indexCodeList, ok := frequenciesMap[frequency]
  798. if !ok {
  799. continue
  800. }
  801. if len(indexCodeList) <= 0 {
  802. fmt.Printf("sheet:%s, 不存在指标", frequency)
  803. return
  804. }
  805. var sheetName string
  806. switch frequency {
  807. case "日度":
  808. sheetName = "日度(Daily)"
  809. case "周度":
  810. sheetName = "周度(Weekly)"
  811. case "旬度":
  812. sheetName = "旬度(ten-day)"
  813. case "月度":
  814. sheetName = "月度(Monthly)"
  815. case "季度":
  816. sheetName = "季度(Quarterly)"
  817. case "半年度":
  818. sheetName = "半年度(Semi-annual)"
  819. case "年度":
  820. sheetName = "年度(Annual)"
  821. default:
  822. sheetName = "其他数据"
  823. }
  824. sheetNew, err := xlsxFile.AddSheet(sheetName)
  825. if err != nil {
  826. fmt.Println("新增Sheet失败", err.Error())
  827. return
  828. }
  829. secNameRow := sheetNew.AddRow()
  830. frequencyRow := sheetNew.AddRow()
  831. unitRow := sheetNew.AddRow()
  832. lastModifyDateRow := sheetNew.AddRow()
  833. var indexIdList []int
  834. for _, idx := range frequenciesMap[frequency] {
  835. indexIdList = append(indexIdList, idx.BaseFromGprRiskIndexId)
  836. }
  837. dataTimeList, err := data_manage.GetGprRiskDataDataTimeByIndexId(indexIdList)
  838. if err != nil {
  839. br.Msg = "下载失败"
  840. br.ErrMsg = "获取数据时间失败,Err:" + err.Error()
  841. fmt.Println("获取数据时间失败", err.Error())
  842. return
  843. }
  844. // 添加excel左侧指标日期
  845. setRowIndex := 4
  846. for rk, dv := range dataTimeList {
  847. rowIndex := setRowIndex + rk
  848. row := sheetNew.Row(rowIndex)
  849. displayDate, _ := time.Parse(utils.FormatDate, dv)
  850. displayDateCell := row.AddCell()
  851. style := new(xlsx.Style)
  852. style.ApplyAlignment = true
  853. style.Alignment.WrapText = true
  854. displayDateCell.SetStyle(style)
  855. displayDateCell.SetDate(displayDate)
  856. }
  857. for k, icl := range indexCodeList {
  858. // 获取数据
  859. dataList, ok := dataListMap[icl.IndexCode]
  860. if !ok {
  861. continue
  862. }
  863. if k == 0 {
  864. secNameRow.AddCell().SetValue("指标名称/Metric Name")
  865. frequencyRow.AddCell().SetValue("频度/Frequency")
  866. unitRow.AddCell().SetValue("单位/Unit")
  867. lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
  868. min := k * 3
  869. sheetNew.SetColWidth(min, min, 15)
  870. }
  871. if len(dataList) == 0 {
  872. continue
  873. }
  874. secNameRow.AddCell().SetValue(icl.IndexName)
  875. frequencyRow.AddCell().SetValue(icl.Frequency)
  876. unitRow.AddCell().SetValue(icl.Unit)
  877. timeDate, err := time.Parse(utils.FormatDateTime, dataList[0].ModifyTime)
  878. if err != nil {
  879. continue
  880. }
  881. lastModifyDateRow.AddCell().SetValue(timeDate.Format(utils.FormatDate))
  882. dataInfoMap := make(map[string]*data_manage.BaseFromGprRiskData)
  883. for _, v := range dataList {
  884. dataInfoMap[v.DataTime] = v
  885. }
  886. for rk, dtv := range dataTimeList {
  887. rowIndex := setRowIndex + rk
  888. row := sheetNew.Row(rowIndex)
  889. displayDateCell := row.AddCell()
  890. tmpData, ok := dataInfoMap[dtv]
  891. if ok {
  892. displayDateCell.SetValue(tmpData.Value)
  893. }
  894. }
  895. }
  896. }
  897. err = xlsxFile.Save(downLoadnFilePath)
  898. if err != nil {
  899. //有指标无数据时先导出一遍空表
  900. sheet, err := xlsxFile.AddSheet("无数据")
  901. if err != nil {
  902. br.Msg = "新增Sheet失败"
  903. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  904. return
  905. }
  906. rowSecName := sheet.AddRow()
  907. celSecName := rowSecName.AddCell()
  908. celSecName.SetValue("")
  909. e := xlsxFile.Save(downLoadnFilePath)
  910. if e != nil {
  911. br.Msg = "保存文件失败"
  912. br.ErrMsg = "保存文件失败"
  913. return
  914. }
  915. }
  916. fileName := classifyName
  917. if indexCode != "" && len(indexList) == 1 {
  918. fileName = indexList[0].IndexName
  919. }
  920. fileName = strings.Replace(fileName, ": ", "_", -1)
  921. fileName = strings.Replace(fileName, ", ", "_", -1)
  922. fileName = strings.Replace(fileName, " ", "_", -1)
  923. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  924. fmt.Println(fileName)
  925. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  926. defer func() {
  927. os.Remove(downLoadnFilePath)
  928. }()
  929. br.Ret = 200
  930. br.Success = true
  931. br.Msg = "success"
  932. }
  933. // GetFrequency
  934. // @Title GPR地缘风险指数数据频度
  935. // @Description GPR地缘风险指数数据频度接口
  936. // @Param ClassifyId query string true "分类Id"
  937. // @Success 200 {object} data_manage.LzFrequency
  938. // @router /gpr_risk/frequency [get]
  939. func (this *BaseFromGprRiskController) GetFrequency() {
  940. br := new(models.BaseResponse).Init()
  941. defer func() {
  942. this.Data["json"] = br
  943. this.ServeJSON()
  944. }()
  945. sysUser := this.SysUser
  946. if sysUser == nil {
  947. br.Msg = "请登录"
  948. br.ErrMsg = "请登录,SysUser Is Empty"
  949. br.Ret = 408
  950. return
  951. }
  952. classifyId, _ := this.GetInt("ClassifyId")
  953. if classifyId < 0 {
  954. br.Msg = "请选择分类"
  955. br.ErrMsg = "请选择分类"
  956. return
  957. }
  958. frequencyList, err := data_manage.GetGprRiskFrequencyByClassifyId(classifyId)
  959. if err != nil {
  960. br.Msg = "获取频度失败"
  961. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  962. return
  963. }
  964. br.Ret = 200
  965. br.Success = true
  966. br.Msg = "获取成功"
  967. br.Data = frequencyList
  968. }