gpr_risk_data.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. indexInfo, err := data_manage.GetBaseFromGprRiskIndexByIndexCode(indexCode)
  297. if err != nil {
  298. br.Msg = "获取指标信息失败"
  299. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  300. return
  301. }
  302. dataTmpList, err := data_manage.GetGprRiskIndexDataByCode(indexCode)
  303. if err != nil {
  304. br.Msg = "获取数据失败"
  305. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  306. return
  307. }
  308. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_GPR_RISK, indexCode)
  309. if err != nil && err.Error() != utils.ErrNoRow() {
  310. br.Msg = "获取数据源失败"
  311. br.ErrMsg = "获取数据源失败,Err:" + err.Error()
  312. return
  313. }
  314. var ret data_manage.GprRiskSingleDataResp
  315. var dataList []*data_manage.GprRiskSingleData
  316. if edbInfo != nil {
  317. ret.EdbInfoId = edbInfo.EdbInfoId
  318. ret.EdbExist = 1
  319. }
  320. ret.ClassifyId = indexInfo.ClassifyId
  321. ret.BaseFromGprRiskIndexId = indexInfo.BaseFromGprRiskIndexId
  322. ret.IndexCode = indexInfo.IndexCode
  323. ret.IndexName = indexInfo.IndexName
  324. ret.Frequency = indexInfo.Frequency
  325. ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
  326. ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
  327. ret.Unit = indexInfo.Unit
  328. for _, v := range dataTmpList {
  329. tmp := &data_manage.GprRiskSingleData{
  330. Value: v.Value,
  331. DataTime: v.DataTime,
  332. }
  333. dataList = append(dataList, tmp)
  334. }
  335. ret.Data = dataList
  336. br.Ret = 200
  337. br.Success = true
  338. br.Msg = "获取成功"
  339. br.Data = ret
  340. }
  341. // GprRiskIndexList
  342. // @Title GPR地缘风险指数指标列表
  343. // @Description GPR地缘风险指数指标列表
  344. // @Param ClassifyId query int true "分类id"
  345. // @Success 200 {object} data_manage.BaseFromGprRiskClassifyResp
  346. // @router /gpr_risk/classify/index/list [get]
  347. func (this *BaseFromGprRiskController) GprRiskIndexList() {
  348. br := new(models.BaseResponse).Init()
  349. defer func() {
  350. this.Data["json"] = br
  351. this.ServeJSON()
  352. }()
  353. classifyId, _ := this.GetInt("ClassifyId", 0)
  354. indexList, err := data_manage.GetGprRiskIndexByClassifyId(classifyId)
  355. if err != nil && err.Error() != utils.ErrNoRow() {
  356. br.Msg = "获取失败"
  357. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  358. return
  359. }
  360. var ret data_manage.BaseFromGprRiskClassifyResp
  361. list := make([]*data_manage.BaseFromGprRiskClassifyItems, 0)
  362. for _, v := range indexList {
  363. classify := new(data_manage.BaseFromGprRiskClassifyItems)
  364. classify.ClassifyId = classifyId
  365. classify.BaseFromGprRiskIndexId = v.BaseFromGprRiskIndexId
  366. classify.IndexCode = v.IndexCode
  367. classify.ClassifyName = v.IndexName
  368. classify.UniqueCode = fmt.Sprintf("%d_%d", classifyId, v.BaseFromGprRiskIndexId)
  369. list = append(list, classify)
  370. }
  371. ret.List = list
  372. br.Ret = 200
  373. br.Success = true
  374. br.Msg = "获取成功"
  375. br.Data = ret
  376. }
  377. // GprRiskNameCheck
  378. // @Title 加入指标库的重名检测
  379. // @Description 加入指标库的重名检测
  380. // @Param ClassifyIds query string true "分类id, 多个分类用英文"
  381. // @Param Keyword query string true "关键词, 指标ID/指标名称"
  382. // @Success 200 {object} NameCheckResult
  383. // @router /gpr_risk/edb_info/name_check [post]
  384. func (this *BaseFromGprRiskController) GprRiskNameCheck() {
  385. br := new(models.BaseResponse).Init()
  386. defer func() {
  387. if br.ErrMsg == "" {
  388. br.IsSendEmail = false
  389. }
  390. this.Data["json"] = br
  391. this.ServeJSON()
  392. }()
  393. sysUser := this.SysUser
  394. if sysUser == nil {
  395. br.Msg = "请登录"
  396. br.ErrMsg = "请登录,SysUser Is Empty"
  397. br.Ret = 408
  398. return
  399. }
  400. var req []*data_manage.NameCheckEdbInfoReq
  401. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  402. br.Msg = "参数解析异常!"
  403. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  404. return
  405. }
  406. if len(req) == 0 {
  407. br.Msg = "请选择指标"
  408. return
  409. }
  410. codeMaxT := 30
  411. codeLen := len(req)
  412. if codeLen > codeMaxT {
  413. br.Msg = "批量添加指标数量不得超过30个"
  414. return
  415. }
  416. indexNames := make([]string, 0)
  417. resp := make([]*data_manage.EdbNameCheckResult, 0)
  418. for _, v := range req {
  419. v.EdbCode = strings.TrimSpace(v.EdbCode)
  420. if v.EdbCode == "" {
  421. br.Msg = "指标ID不可为空"
  422. return
  423. }
  424. v.EdbName = strings.TrimSpace(v.EdbName)
  425. if v.EdbName == "" {
  426. br.Msg = "请输入指标名称"
  427. return
  428. }
  429. indexNames = append(indexNames, v.EdbName)
  430. resp = append(resp, &data_manage.EdbNameCheckResult{
  431. EdbCode: v.EdbCode,
  432. EdbName: v.EdbName,
  433. })
  434. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_GPR_RISK, 0, utils.EDB_DATA_LIMIT)
  435. if err != nil && err.Error() != utils.ErrNoRow() {
  436. br.Msg = "获取失败"
  437. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  438. return
  439. }
  440. if len(dataItems) <= 0 {
  441. respItem, err := data.AddEdbData(utils.DATA_SOURCE_GPR_RISK, v.EdbCode, v.Frequency)
  442. if err != nil {
  443. br.Msg = "获取失败"
  444. br.ErrMsg = "获取失败,Err:" + err.Error()
  445. return
  446. }
  447. if respItem.Ret != 200 {
  448. br.Msg = "未搜索到该指标"
  449. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  450. return
  451. }
  452. }
  453. }
  454. // 重名校验
  455. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  456. if e != nil {
  457. br.Msg = "操作失败"
  458. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  459. return
  460. }
  461. nameExists := make(map[string]bool)
  462. for _, v := range edbList {
  463. nameExists[v.EdbName] = true
  464. }
  465. if len(nameExists) > 0 {
  466. for _, v := range resp {
  467. v.Exist = nameExists[v.EdbName]
  468. }
  469. }
  470. br.Data = resp
  471. br.Msg = "校验成功"
  472. br.Ret = 200
  473. br.Success = true
  474. }
  475. // GprRiskAddCheck
  476. // @Title 加入指标库指标Id检测
  477. // @Description 加入指标库指标Id检测
  478. // @Param request body request.BatchAddCheckReq true "type json string"
  479. // @Success 200 string "操作成功"
  480. // @router /gpr_risk/edb_info/add_check [post]
  481. func (c *BaseFromGprRiskController) GprRiskAddCheck() {
  482. br := new(models.BaseResponse).Init()
  483. defer func() {
  484. if br.ErrMsg == "" {
  485. br.IsSendEmail = false
  486. }
  487. c.Data["json"] = br
  488. c.ServeJSON()
  489. }()
  490. sysUser := c.SysUser
  491. if sysUser == nil {
  492. br.Msg = "请登录"
  493. br.ErrMsg = "请登录,SysUser Is Empty"
  494. br.Ret = 408
  495. return
  496. }
  497. var req data_manage.BatchAddCheckReq
  498. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  499. br.Msg = "参数解析异常!"
  500. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  501. return
  502. }
  503. codeMaxT := 30
  504. codeLen := len(req.IndexCodes)
  505. // 获取指标库已有指标
  506. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_GPR_RISK)
  507. if e != nil {
  508. br.Msg = "获取失败"
  509. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  510. return
  511. }
  512. existMap := make(map[string]*data_manage.EdbInfo)
  513. for _, v := range existsEdb {
  514. existMap[v.EdbCode] = v
  515. }
  516. if codeLen == 0 {
  517. br.Msg = "请选择指标"
  518. return
  519. }
  520. if codeLen > codeMaxT {
  521. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  522. return
  523. }
  524. // 查询选中的指标
  525. cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
  526. pars := make([]interface{}, 0)
  527. pars = append(pars, req.IndexCodes)
  528. list, err := data_manage.GetGprRiskIndex(cond, pars)
  529. if err != nil {
  530. br.Msg = "获取失败"
  531. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  532. return
  533. }
  534. if len(list) > codeMaxT {
  535. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMaxT)
  536. return
  537. }
  538. resp := make([]*data_manage.BaseFromGprRiskIndexList, 0)
  539. for _, v := range list {
  540. if edb, ok := existMap[v.IndexCode]; ok {
  541. v.EdbInfoId = edb.EdbInfoId
  542. v.EdbClassifyId = edb.ClassifyId
  543. v.EdbUniqueCode = edb.UniqueCode
  544. v.EdbExist = 1
  545. }
  546. resp = append(resp, v)
  547. }
  548. br.Data = resp
  549. br.Msg = "校验成功"
  550. br.Ret = 200
  551. br.Success = true
  552. }
  553. // GprRiskEdbInfoAdd
  554. // @Title 新增指标接口
  555. // @Description 新增指标接口
  556. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  557. // @Success Ret=200 保存成功
  558. // @router /gpr_risk/edb_info/add [post]
  559. func (this *BaseFromGprRiskController) GprRiskEdbInfoAdd() {
  560. br := new(models.BaseResponse).Init()
  561. defer func() {
  562. this.Data["json"] = br
  563. this.ServeJSON()
  564. }()
  565. sysUser := this.SysUser
  566. if sysUser == nil {
  567. br.Msg = "请登录"
  568. br.ErrMsg = "请登录,SysUser Is Empty"
  569. br.Ret = 408
  570. return
  571. }
  572. deleteCache := true
  573. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  574. defer func() {
  575. if deleteCache {
  576. utils.Rc.Delete(cacheKey)
  577. }
  578. }()
  579. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  580. deleteCache = false
  581. br.Msg = "系统处理中,请稍后重试!"
  582. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  583. return
  584. }
  585. var req data_manage.AddEdbInfoReq
  586. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  587. if err != nil {
  588. br.Msg = "参数解析异常!"
  589. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  590. return
  591. }
  592. req.EdbName = strings.Trim(req.EdbName, " ")
  593. req.EdbCode = strings.Trim(req.EdbCode, " ")
  594. if req.EdbCode == "" {
  595. br.Msg = "指标ID不能为空"
  596. return
  597. }
  598. if req.EdbName == "" {
  599. br.Msg = "指标名称不能为空"
  600. return
  601. }
  602. if req.Frequency == "" {
  603. br.Msg = "频率不能为空"
  604. return
  605. }
  606. if req.Unit == "" {
  607. br.Msg = "单位不能为空"
  608. return
  609. }
  610. if req.ClassifyId <= 0 {
  611. br.Msg = "请选择分类"
  612. return
  613. }
  614. count, err := data_manage.GetGprRiskIndexDataCount(req.EdbCode)
  615. if err != nil {
  616. br.Msg = "获取失败"
  617. br.ErrMsg = "获取失败,Err:" + err.Error()
  618. return
  619. }
  620. if count == 0 {
  621. br.Msg = "指标不存在"
  622. }
  623. // 指标入库
  624. 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)
  625. if err != nil {
  626. br.Msg = "保存失败"
  627. if errMsg != `` {
  628. br.Msg = errMsg
  629. }
  630. br.ErrMsg = err.Error()
  631. br.IsSendEmail = isSendEmail
  632. return
  633. }
  634. // 试用平台更新用户累计新增指标数
  635. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  636. if e != nil {
  637. br.Msg = "操作失败"
  638. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  639. return
  640. }
  641. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  642. go func() {
  643. var r etaTrialService.EtaTrialUserReq
  644. r.Mobile = adminItem.Mobile
  645. _, _ = etaTrialService.UpdateUserIndexNum(r)
  646. }()
  647. }
  648. //新增操作日志
  649. {
  650. edbLog := new(data_manage.EdbInfoLog)
  651. edbLog.EdbInfoId = edbInfo.EdbInfoId
  652. edbLog.SourceName = edbInfo.SourceName
  653. edbLog.Source = edbInfo.Source
  654. edbLog.EdbCode = edbInfo.EdbCode
  655. edbLog.EdbName = edbInfo.EdbName
  656. edbLog.ClassifyId = edbInfo.ClassifyId
  657. edbLog.SysUserId = sysUser.AdminId
  658. edbLog.SysUserRealName = sysUser.RealName
  659. edbLog.CreateTime = time.Now()
  660. edbLog.Content = string(this.Ctx.Input.RequestBody)
  661. edbLog.Status = "新增指标"
  662. edbLog.Method = this.Ctx.Input.URI()
  663. go data_manage.AddEdbInfoLog(edbLog)
  664. }
  665. // 更新es
  666. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  667. resp := new(data_manage.AddEdbInfoResp)
  668. resp.EdbInfoId = edbInfo.EdbInfoId
  669. resp.UniqueCode = edbInfo.UniqueCode
  670. br.Ret = 200
  671. br.Success = true
  672. br.Msg = "保存成功"
  673. br.Data = resp
  674. br.IsAddLog = true
  675. }
  676. // ExportGprRiskList
  677. // @Title 导出GPR地缘风险指数数据
  678. // @Description 导出GPR地缘风险指数数据
  679. // @Param ClassifyId query int true "关键字搜索"
  680. // @Param IndexCode query string true "指标编码"
  681. // @Success 200 导出成功
  682. // @router /gpr_risk/export [get]
  683. func (this *BaseFromGprRiskController) ExportGprRiskList() {
  684. br := new(models.BaseResponse).Init()
  685. defer func() {
  686. this.Data["json"] = br
  687. this.ServeJSON()
  688. }()
  689. sysUser := this.SysUser
  690. if sysUser == nil {
  691. br.Msg = "请重新登录"
  692. return
  693. }
  694. classifyId, _ := this.GetInt("ClassifyId")
  695. indexCode := this.GetString("IndexCode")
  696. if classifyId <= 0 && indexCode == "" {
  697. br.Msg = "请选择分类或者指标"
  698. return
  699. }
  700. dir, _ := os.Executable()
  701. exPath := filepath.Dir(dir)
  702. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  703. xlsxFile := xlsx.NewFile()
  704. var condition string
  705. var pars []interface{}
  706. var classifyName string
  707. if classifyId > 0 {
  708. classifyInfo, err := data_manage.GetBaseFromGprRiskClassifyById(classifyId)
  709. if err != nil {
  710. if err.Error() == utils.ErrNoRow() {
  711. br.Msg = "分类不存在"
  712. return
  713. }
  714. br.Msg = "下载失败"
  715. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  716. return
  717. }
  718. classifyName = classifyInfo.ClassifyName
  719. childClassify, err := data_manage.GetBaseFromGprRiskClassifyByParentId(classifyId)
  720. if err != nil {
  721. br.Msg = "下载失败"
  722. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  723. return
  724. }
  725. if len(childClassify) > 0 {
  726. condition += `AND classify_id IN (` + utils.GetOrmInReplace(len(childClassify)) + `)`
  727. for _, child := range childClassify {
  728. pars = append(pars, child.ClassifyId)
  729. }
  730. } else {
  731. condition += ` AND classify_id=?`
  732. pars = append(pars, classifyId)
  733. }
  734. }
  735. if indexCode != "" {
  736. condition += ` AND index_code=? `
  737. pars = append(pars, indexCode)
  738. }
  739. indexList, err := data_manage.GetGprRiskIndex(condition, pars)
  740. if err != nil {
  741. br.Msg = "下载失败"
  742. br.ErrMsg = "获取指标失败,Err:" + err.Error()
  743. fmt.Println("获取数据失败,Err:" + err.Error())
  744. return
  745. }
  746. if len(indexList) <= 0 {
  747. fmt.Println("indexList 为空")
  748. br.Ret = 200
  749. br.Success = true
  750. br.Msg = "success"
  751. return
  752. }
  753. codeList := make([]string, 0)
  754. frequenciesMap := make(map[string][]*data_manage.BaseFromGprRiskIndexList)
  755. for _, v := range indexList {
  756. codeList = append(codeList, v.IndexCode)
  757. frequenciesMap[v.Frequency] = append(frequenciesMap[v.Frequency], v)
  758. }
  759. dataListMap := make(map[string][]*data_manage.BaseFromGprRiskData)
  760. if len(indexList) > 0 {
  761. allDataList, e := data_manage.GetGprRiskIndexDataByCodes(codeList)
  762. if e != nil {
  763. br.Msg = "获取数据失败"
  764. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  765. return
  766. }
  767. for _, v := range allDataList {
  768. dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
  769. }
  770. }
  771. // 按照频率分组排序
  772. frequencies := []string{
  773. "日度", "周度", "旬度", "月度", "季度", "半年度", "年度",
  774. }
  775. for _, frequency := range frequencies {
  776. //获取指标
  777. indexCodeList, ok := frequenciesMap[frequency]
  778. if !ok {
  779. continue
  780. }
  781. if len(indexCodeList) <= 0 {
  782. fmt.Printf("sheet:%s, 不存在指标", frequency)
  783. return
  784. }
  785. var sheetName string
  786. switch frequency {
  787. case "日度":
  788. sheetName = "日度(Daily)"
  789. case "周度":
  790. sheetName = "周度(Weekly)"
  791. case "旬度":
  792. sheetName = "旬度(ten-day)"
  793. case "月度":
  794. sheetName = "月度(Monthly)"
  795. case "季度":
  796. sheetName = "季度(Quarterly)"
  797. case "半年度":
  798. sheetName = "半年度(Semi-annual)"
  799. case "年度":
  800. sheetName = "年度(Annual)"
  801. default:
  802. sheetName = "其他数据"
  803. }
  804. sheetNew, err := xlsxFile.AddSheet(sheetName)
  805. if err != nil {
  806. fmt.Println("新增Sheet失败", err.Error())
  807. return
  808. }
  809. secNameRow := sheetNew.AddRow()
  810. frequencyRow := sheetNew.AddRow()
  811. unitRow := sheetNew.AddRow()
  812. lastModifyDateRow := sheetNew.AddRow()
  813. var indexIdList []int
  814. for _, idx := range frequenciesMap[frequency] {
  815. indexIdList = append(indexIdList, idx.BaseFromGprRiskIndexId)
  816. }
  817. dataTimeList, err := data_manage.GetGprRiskDataDataTimeByIndexId(indexIdList)
  818. if err != nil {
  819. br.Msg = "下载失败"
  820. br.ErrMsg = "获取数据时间失败,Err:" + err.Error()
  821. fmt.Println("获取数据时间失败", err.Error())
  822. return
  823. }
  824. // 添加excel左侧指标日期
  825. setRowIndex := 4
  826. for rk, dv := range dataTimeList {
  827. rowIndex := setRowIndex + rk
  828. row := sheetNew.Row(rowIndex)
  829. displayDate, _ := time.Parse(utils.FormatDate, dv)
  830. displayDateCell := row.AddCell()
  831. style := new(xlsx.Style)
  832. style.ApplyAlignment = true
  833. style.Alignment.WrapText = true
  834. displayDateCell.SetStyle(style)
  835. displayDateCell.SetDate(displayDate)
  836. }
  837. for k, icl := range indexCodeList {
  838. // 获取数据
  839. dataList, ok := dataListMap[icl.IndexCode]
  840. if !ok {
  841. continue
  842. }
  843. if k == 0 {
  844. secNameRow.AddCell().SetValue("指标名称/Metric Name")
  845. frequencyRow.AddCell().SetValue("频度/Frequency")
  846. unitRow.AddCell().SetValue("单位/Unit")
  847. lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
  848. min := k * 3
  849. sheetNew.SetColWidth(min, min, 15)
  850. }
  851. if len(dataList) == 0 {
  852. continue
  853. }
  854. secNameRow.AddCell().SetValue(icl.IndexName)
  855. frequencyRow.AddCell().SetValue(icl.Frequency)
  856. unitRow.AddCell().SetValue(icl.Unit)
  857. timeDate, err := time.Parse(utils.FormatDateTime, dataList[0].ModifyTime)
  858. if err != nil {
  859. continue
  860. }
  861. lastModifyDateRow.AddCell().SetValue(timeDate.Format(utils.FormatDate))
  862. dataInfoMap := make(map[string]*data_manage.BaseFromGprRiskData)
  863. for _, v := range dataList {
  864. dataInfoMap[v.DataTime] = v
  865. }
  866. for rk, dtv := range dataTimeList {
  867. rowIndex := setRowIndex + rk
  868. row := sheetNew.Row(rowIndex)
  869. displayDateCell := row.AddCell()
  870. tmpData, ok := dataInfoMap[dtv]
  871. if ok {
  872. displayDateCell.SetValue(tmpData.Value)
  873. }
  874. }
  875. }
  876. }
  877. err = xlsxFile.Save(downLoadnFilePath)
  878. if err != nil {
  879. //有指标无数据时先导出一遍空表
  880. sheet, err := xlsxFile.AddSheet("无数据")
  881. if err != nil {
  882. br.Msg = "新增Sheet失败"
  883. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  884. return
  885. }
  886. rowSecName := sheet.AddRow()
  887. celSecName := rowSecName.AddCell()
  888. celSecName.SetValue("")
  889. e := xlsxFile.Save(downLoadnFilePath)
  890. if e != nil {
  891. br.Msg = "保存文件失败"
  892. br.ErrMsg = "保存文件失败"
  893. return
  894. }
  895. }
  896. fileName := classifyName
  897. if indexCode != "" && len(indexList) == 1 {
  898. fileName = indexList[0].IndexName
  899. }
  900. fileName = strings.Replace(fileName, ": ", "_", -1)
  901. fileName = strings.Replace(fileName, ", ", "_", -1)
  902. fileName = strings.Replace(fileName, " ", "_", -1)
  903. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  904. fmt.Println(fileName)
  905. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  906. defer func() {
  907. os.Remove(downLoadnFilePath)
  908. }()
  909. br.Ret = 200
  910. br.Success = true
  911. br.Msg = "success"
  912. }
  913. // GetFrequency
  914. // @Title GPR地缘风险指数数据频度
  915. // @Description GPR地缘风险指数数据频度接口
  916. // @Param ClassifyId query string true "分类Id"
  917. // @Success 200 {object} data_manage.LzFrequency
  918. // @router /gpr_risk/frequency [get]
  919. func (this *BaseFromGprRiskController) GetFrequency() {
  920. br := new(models.BaseResponse).Init()
  921. defer func() {
  922. this.Data["json"] = br
  923. this.ServeJSON()
  924. }()
  925. sysUser := this.SysUser
  926. if sysUser == nil {
  927. br.Msg = "请登录"
  928. br.ErrMsg = "请登录,SysUser Is Empty"
  929. br.Ret = 408
  930. return
  931. }
  932. classifyId, _ := this.GetInt("ClassifyId")
  933. if classifyId < 0 {
  934. br.Msg = "请选择分类"
  935. br.ErrMsg = "请选择分类"
  936. return
  937. }
  938. frequencyList, err := data_manage.GetGprRiskFrequencyByClassifyId(classifyId)
  939. if err != nil {
  940. br.Msg = "获取频度失败"
  941. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  942. return
  943. }
  944. br.Ret = 200
  945. br.Success = true
  946. br.Msg = "获取成功"
  947. br.Data = frequencyList
  948. }