eia_steo.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/system"
  7. "eta/eta_api/services/data"
  8. "eta/eta_api/services/data_stat"
  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. // EiaSteoClassify
  21. // @Title EiaSteo数据分类
  22. // @Description EiaSteo数据分类接口
  23. // @Success 200 {object} data_manage.LzClassify
  24. // @router /eia_steo/classify [get]
  25. func (this *EdbInfoController) EiaSteoClassify() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. sysUser := this.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. classifyList, err := data_manage.GetEiaSteoClassifyList()
  39. if err != nil {
  40. br.Msg = "获取分类失败"
  41. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  42. return
  43. }
  44. childClassifyMap := make(map[int][]*data_manage.BaseFromEiaSteoClassifyView)
  45. rootList := make([]*data_manage.BaseFromEiaSteoClassifyView, 0)
  46. for _, v := range classifyList {
  47. v.UniqueCode = strconv.Itoa(v.BaseFromEiaSteoClassifyId)
  48. if v.Level == 1 {
  49. rootList = append(rootList, v)
  50. } else {
  51. childClassifyMap[v.ParentId] = append(childClassifyMap[v.ParentId], v)
  52. }
  53. }
  54. for _, v := range rootList {
  55. if existItems, ok := childClassifyMap[v.BaseFromEiaSteoClassifyId]; ok {
  56. v.Child = existItems
  57. }
  58. }
  59. br.Ret = 200
  60. br.Success = true
  61. br.Msg = "获取成功"
  62. br.Data = rootList
  63. }
  64. // EiaSteoData
  65. // @Title 获取EiaSteo数据
  66. // @Description 获取EiaSteo数据接口
  67. // @Param BaseFromEiaSteoClassifyId query int true "指标分类id"
  68. // @Param IndexCode query string true "指标code"
  69. // @Param IsExport query bool false "是否导出excel,默认是false"
  70. // @Param PageSize query int true "每页数据条数"
  71. // @Param CurrentIndex query int true "当前页页码,从1开始"
  72. // @Success 200 {object} data_manage.LzFrequency
  73. // @router /eia_steo/data [get]
  74. func (this *EdbInfoController) EiaSteoData() {
  75. br := new(models.BaseResponse).Init()
  76. defer func() {
  77. this.Data["json"] = br
  78. this.ServeJSON()
  79. }()
  80. sysUser := this.SysUser
  81. if sysUser == nil {
  82. br.Msg = "请登录"
  83. br.ErrMsg = "请登录,SysUser Is Empty"
  84. br.Ret = 408
  85. return
  86. }
  87. pageSize, _ := this.GetInt("PageSize")
  88. currentIndex, _ := this.GetInt("CurrentIndex")
  89. var startSize int
  90. //是否导出报表
  91. isExport, _ := this.GetBool("IsExport")
  92. if isExport {
  93. pageSize = 10000
  94. currentIndex = 1
  95. }
  96. if pageSize <= 0 {
  97. pageSize = utils.PageSize20
  98. }
  99. if currentIndex <= 0 {
  100. currentIndex = 1
  101. }
  102. startSize = utils.StartIndex(currentIndex, pageSize)
  103. var condition string
  104. var pars []interface{}
  105. indexCode := this.GetString("IndexCode")
  106. if indexCode != "" {
  107. condition += ` AND index_code = ? `
  108. pars = append(pars, utils.TrimStr(indexCode))
  109. }
  110. classifyId, _ := this.GetInt("BaseFromEiaSteoClassifyId")
  111. if classifyId > 0 {
  112. classifyList, err := data.GetClassifyALLById(classifyId)
  113. if err != nil {
  114. br.Msg = "获取数据失败"
  115. br.ErrMsg = "获取分类数据失败,Err:" + err.Error()
  116. return
  117. }
  118. var classifyIds []int
  119. for _, v := range classifyList {
  120. classifyIds = append(classifyIds, v.BaseFromEiaSteoClassifyId)
  121. }
  122. condition += ` AND base_from_eia_steo_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) `
  123. pars = append(pars, classifyIds)
  124. }
  125. //获取指标
  126. indexList, err := data_manage.GetEiaSteoIndexList(condition, pars)
  127. if err != nil {
  128. br.Msg = "获取数据失败"
  129. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  130. return
  131. }
  132. indexCodes := make([]string, 0)
  133. for _, v := range indexList {
  134. indexCodes = append(indexCodes, v.IndexCode)
  135. }
  136. edbinfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_EIA_STEO, indexCodes)
  137. if err != nil {
  138. br.Msg = "获取数据失败"
  139. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  140. return
  141. }
  142. existEdbInfo := make(map[string]int)
  143. for _, v := range edbinfoList {
  144. existEdbInfo[v.EdbCode] = v.EdbInfoId
  145. }
  146. resultList := make([]data_manage.EiaSteoIndexListResp, 0)
  147. for _, v := range indexList {
  148. indexName := v.IndexName
  149. if indexName == `` {
  150. indexName = v.IndexNameOriginal
  151. }
  152. product := data_manage.EiaSteoIndexListResp{
  153. BaseFromEiaSteoIndexId: v.BaseFromEiaSteoIndexId,
  154. BaseFromEiaSteoClassifyId: v.BaseFromEiaSteoClassifyId,
  155. IndexCode: v.IndexCode,
  156. IndexName: indexName,
  157. //IndexNameOriginal: v.IndexNameOriginal,
  158. EdbInfoId: existEdbInfo[v.IndexCode],
  159. Unit: v.Unit,
  160. Frequency: v.Frequency,
  161. StartDate: v.StartDate.Format(utils.FormatDate),
  162. EndDate: v.EndDate.Format(utils.FormatDate),
  163. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  164. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  165. //Paging: tmpPaging,
  166. //DataList: nil,
  167. }
  168. total, err := data_manage.GetEiaSteoIndexDataCount(v.IndexCode)
  169. if err != nil {
  170. br.Msg = "获取数据失败"
  171. br.ErrMsg = "获取指标统计数据失败,Err:" + err.Error()
  172. return
  173. }
  174. page := paging.GetPaging(currentIndex, pageSize, total)
  175. dataList, err := data_manage.GetEiaSteoIndexDataList(v.IndexCode, startSize, pageSize)
  176. if err != nil {
  177. br.Msg = "获取数据失败"
  178. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  179. return
  180. }
  181. if dataList == nil {
  182. dataList = make([]*data_manage.BaseFromEiaSteoDataItem, 0)
  183. }
  184. product.DataList = dataList
  185. product.Paging = page
  186. resultList = append(resultList, product)
  187. }
  188. //导出excel
  189. if isExport {
  190. EiaSteoDataExport(this, resultList, br)
  191. return
  192. }
  193. br.Ret = 200
  194. br.Success = true
  195. br.Msg = "获取成功"
  196. br.Data = resultList
  197. }
  198. // EiaSteoSearchList
  199. // @Title EiaSteo模糊搜索
  200. // @Description EiaSteo模糊搜索
  201. // @Param Keyword query string ture "关键字搜索"
  202. // @Success 200 {object} models.BaseResponse
  203. // @router /eia_steo/search_list [get]
  204. func (this *EdbInfoController) EiaSteoSearchList() {
  205. br := new(models.BaseResponse).Init()
  206. defer func() {
  207. this.Data["json"] = br
  208. this.ServeJSON()
  209. }()
  210. sysUser := this.SysUser
  211. if sysUser == nil {
  212. br.Msg = "请重新登录"
  213. return
  214. }
  215. //关键字
  216. keyword := this.GetString("Keyword")
  217. list, err := data_manage.GetEiaSteoItemList(utils.TrimStr(keyword))
  218. if err != nil {
  219. br.ErrMsg = "获取失败,Err:" + err.Error()
  220. br.Msg = "获取失败"
  221. return
  222. }
  223. br.Ret = 200
  224. br.Success = true
  225. br.Msg = "获取成功"
  226. br.Data = list
  227. }
  228. // EiaSteoDataExport 表格导出
  229. func EiaSteoDataExport(this *EdbInfoController, list []data_manage.EiaSteoIndexListResp, br *models.BaseResponse) {
  230. sysUser := this.SysUser
  231. if sysUser == nil {
  232. br.Msg = "请重新登录"
  233. return
  234. }
  235. dir, _ := os.Executable()
  236. exPath := filepath.Dir(dir)
  237. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  238. xlsxFile := xlsx.NewFile()
  239. sheet, err := xlsxFile.AddSheet("数据报表")
  240. //获取指标数据
  241. rowSecName := sheet.AddRow()
  242. celSecName := rowSecName.AddCell()
  243. celSecName.SetValue("指标名称/Metric Name")
  244. frequency := sheet.AddRow()
  245. freqCell := frequency.AddCell()
  246. freqCell.SetValue("频度/Frequency")
  247. rowUnit := sheet.AddRow()
  248. celUnit := rowUnit.AddCell()
  249. celUnit.SetValue("单位/Unit")
  250. rowModifyDate := sheet.AddRow()
  251. rowModifyCell := rowModifyDate.AddCell()
  252. rowModifyCell.SetValue("更新时间/Update Time")
  253. dataMap := make(map[string]map[string]*data_manage.BaseFromEiaSteoDataItem)
  254. var indexCodeList []string
  255. for _, v := range list {
  256. cellSenName := rowSecName.AddCell()
  257. cellSenName.SetValue(v.IndexName)
  258. freqCell := frequency.AddCell()
  259. freqCell.SetValue(v.Frequency)
  260. celUnit := rowUnit.AddCell()
  261. celUnit.SetValue(v.Unit)
  262. rowModifyCell := rowModifyDate.AddCell()
  263. rowModifyCell.SetValue(v.ModifyTime)
  264. indexCodeList = append(indexCodeList, v.IndexCode)
  265. dataMap[v.IndexCode] = make(map[string]*data_manage.BaseFromEiaSteoDataItem)
  266. for _, item := range v.DataList {
  267. dataMap[item.IndexCode][item.DataTime] = item
  268. }
  269. }
  270. dateList, err := data_manage.GetEiaSteoDateList(indexCodeList)
  271. if err != nil {
  272. br.Msg = "获取数据失败"
  273. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  274. return
  275. }
  276. for _, n := range dateList {
  277. dateStr := n.DataTime //当前日期
  278. rowData := sheet.AddRow()
  279. celDate := rowData.AddCell()
  280. celDate.SetValue(dateStr)
  281. for _, m := range list {
  282. celData := rowData.AddCell()
  283. if dataMap[m.IndexCode][dateStr] != nil {
  284. celData.SetValue(dataMap[m.IndexCode][dateStr].Value)
  285. }
  286. }
  287. }
  288. err = xlsxFile.Save(downLoadnFilePath)
  289. if err != nil {
  290. //有指标无数据时先导出一遍空表
  291. sheet, err := xlsxFile.AddSheet("无数据")
  292. if err != nil {
  293. br.Msg = "新增Sheet失败"
  294. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  295. return
  296. }
  297. rowSecName := sheet.AddRow()
  298. celSecName := rowSecName.AddCell()
  299. celSecName.SetValue("")
  300. err = xlsxFile.Save(downLoadnFilePath)
  301. if err != nil {
  302. br.Msg = "保存文件失败"
  303. br.ErrMsg = "保存文件失败"
  304. return
  305. }
  306. }
  307. fileName := `EiaSteo数据`
  308. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  309. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  310. defer func() {
  311. os.Remove(downLoadnFilePath)
  312. }()
  313. br.Ret = 200
  314. br.Success = true
  315. br.Msg = "success"
  316. }
  317. // EiaSteoBatchSearch
  318. // @Title EiaSteo批量操作查询接口
  319. // @Description EiaSteo批量操作查询接口
  320. // @Param BaseFromEiaSteoClassifyIds query string true "分类id"
  321. // @Param Keyword query string true "名称关键词"
  322. // @Success 200 {object} data_manage.LzFrequency
  323. // @router /eia_steo/batch_search [get]
  324. func (this *EdbInfoController) EiaSteoBatchSearch() {
  325. br := new(models.BaseResponse).Init()
  326. defer func() {
  327. this.Data["json"] = br
  328. this.ServeJSON()
  329. }()
  330. sysUser := this.SysUser
  331. if sysUser == nil {
  332. br.Msg = "请登录"
  333. br.ErrMsg = "请登录,SysUser Is Empty"
  334. br.Ret = 408
  335. return
  336. }
  337. classifyIdStr := this.GetString("BaseFromEiaSteoClassifyIds")
  338. var condition string
  339. var pars []interface{}
  340. classifyIds := strings.Split(classifyIdStr, ",")
  341. if len(classifyIds) > 0 && classifyIds[0] != `` {
  342. condition += " AND base_from_eia_steo_classify_id IN (" + utils.GetOrmInReplace(len(classifyIds)) + " ) "
  343. pars = append(pars, classifyIds)
  344. }
  345. keyword := this.GetString("Keyword")
  346. if keyword != `` {
  347. condition += " AND (index_name like ? OR index_code like ?) "
  348. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  349. }
  350. if classifyIdStr == `` && keyword == `` {
  351. var list = make([]*data_manage.BaseFromEiaSteoIndex, 0)
  352. br.Ret = 200
  353. br.Success = true
  354. br.Msg = "获取成功"
  355. br.Data = list
  356. return
  357. }
  358. list, err := data_manage.GetEiaSteoIndexList(condition, pars)
  359. if err != nil {
  360. br.Msg = "获取失败"
  361. br.ErrMsg = "获取失败,Err:" + err.Error()
  362. return
  363. }
  364. br.Ret = 200
  365. br.Success = true
  366. br.Msg = "获取成功"
  367. br.Data = list
  368. }
  369. // AddCheck
  370. // @Title 新增校验
  371. // @Description 新增校验
  372. // @Param request body request.BusinessDataBatchAddCheckReq true "type json string"
  373. // @Success 200 string "操作成功"
  374. // @router /eia_steo/edb_info/add_check [post]
  375. func (c *EdbInfoController) EiaSteoAddCheck() {
  376. br := new(models.BaseResponse).Init()
  377. defer func() {
  378. if br.ErrMsg == "" {
  379. br.IsSendEmail = false
  380. }
  381. c.Data["json"] = br
  382. c.ServeJSON()
  383. }()
  384. sysUser := c.SysUser
  385. if sysUser == nil {
  386. br.Msg = "请登录"
  387. br.ErrMsg = "请登录,SysUser Is Empty"
  388. br.Ret = 408
  389. return
  390. }
  391. var req data_manage.EiaSteoDataBatchAddCheckReq
  392. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  393. br.Msg = "参数解析异常!"
  394. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  395. return
  396. }
  397. codeMax := 30
  398. codeLen := len(req.IndexCodes)
  399. if codeLen == 0 {
  400. br.Msg = "请选择指标"
  401. return
  402. }
  403. if codeLen > codeMax {
  404. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  405. return
  406. }
  407. // 获取指标库已有指标
  408. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_EIA_STEO)
  409. if e != nil {
  410. br.Msg = "获取失败"
  411. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  412. return
  413. }
  414. existMap := make(map[string]*data_manage.EdbInfo)
  415. for _, v := range existsEdb {
  416. existMap[v.EdbCode] = v
  417. }
  418. // 查询选中的指标
  419. cond := fmt.Sprintf(` AND index_code IN (%s) `, utils.GetOrmInReplace(len(req.IndexCodes)))
  420. pars := make([]interface{}, 0)
  421. pars = append(pars, req.IndexCodes)
  422. list, err := data_manage.GetEiaSteoIndexList(cond, pars)
  423. if err != nil {
  424. br.Msg = "获取失败"
  425. br.ErrMsg = "获取eiaSteo原始指标列表失败, Err: " + err.Error()
  426. return
  427. }
  428. resp := make([]*data_manage.BaseFromEiaSteoIndexItem, 0)
  429. for _, v := range list {
  430. if edbInfo, ok := existMap[v.IndexCode]; ok {
  431. resp = append(resp, &data_manage.BaseFromEiaSteoIndexItem{
  432. BaseFromEiaSteoIndexId: v.BaseFromEiaSteoIndexId,
  433. BaseFromEiaSteoClassifyId: v.BaseFromEiaSteoClassifyId,
  434. IndexCode: v.IndexCode,
  435. IndexName: v.IndexName,
  436. EdbInfoId: edbInfo.EdbInfoId,
  437. EdbUniqueCode: edbInfo.UniqueCode,
  438. EdbClassifyId: edbInfo.ClassifyId,
  439. EdbExist: 1,
  440. })
  441. } else {
  442. resp = append(resp, &data_manage.BaseFromEiaSteoIndexItem{
  443. BaseFromEiaSteoIndexId: v.BaseFromEiaSteoIndexId,
  444. BaseFromEiaSteoClassifyId: v.BaseFromEiaSteoClassifyId,
  445. IndexCode: v.IndexCode,
  446. IndexName: v.IndexName,
  447. EdbInfoId: 0,
  448. EdbUniqueCode: "",
  449. EdbClassifyId: 0,
  450. EdbExist: 0,
  451. })
  452. }
  453. }
  454. br.Data = resp
  455. br.Msg = "校验成功"
  456. br.Ret = 200
  457. br.Success = true
  458. }
  459. // EiaSteoNameCheck
  460. // @Title 重名校验
  461. // @Description 批量新增
  462. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  463. // @Success 200 string "操作成功"
  464. // @router /eia_steo/name_check [post]
  465. func (c *EdbInfoController) EiaSteoNameCheck() {
  466. br := new(models.BaseResponse).Init()
  467. defer func() {
  468. if br.ErrMsg == "" {
  469. br.IsSendEmail = false
  470. }
  471. c.Data["json"] = br
  472. c.ServeJSON()
  473. }()
  474. sysUser := c.SysUser
  475. if sysUser == nil {
  476. br.Msg = "请登录"
  477. br.ErrMsg = "请登录,SysUser Is Empty"
  478. br.Ret = 408
  479. return
  480. }
  481. var req []*data_manage.NameCheckEdbInfoReq
  482. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  483. br.Msg = "参数解析异常!"
  484. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  485. return
  486. }
  487. if len(req) == 0 {
  488. br.Msg = "请选择指标"
  489. return
  490. }
  491. codeMax := 30
  492. codeLen := len(req)
  493. if codeLen > codeMax {
  494. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  495. return
  496. }
  497. type NameCheckResult struct {
  498. EdbCode string
  499. EdbName string
  500. Exist bool
  501. }
  502. indexNames := make([]string, 0)
  503. resp := make([]*NameCheckResult, 0)
  504. for _, v := range req {
  505. v.EdbCode = strings.TrimSpace(v.EdbCode)
  506. if v.EdbCode == "" {
  507. br.Msg = "指标ID不可为空"
  508. return
  509. }
  510. v.EdbName = strings.TrimSpace(v.EdbName)
  511. if v.EdbName == "" {
  512. br.Msg = "请输入指标名称"
  513. return
  514. }
  515. indexNames = append(indexNames, v.EdbName)
  516. resp = append(resp, &NameCheckResult{
  517. EdbCode: v.EdbCode,
  518. EdbName: v.EdbName,
  519. })
  520. dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_EIA_STEO, 0, utils.EDB_DATA_LIMIT)
  521. if err != nil && err.Error() != utils.ErrNoRow() {
  522. br.Msg = "获取失败"
  523. br.ErrMsg = "获取钢联已存在信息失败,Err:" + err.Error()
  524. return
  525. }
  526. if len(dataItems) <= 0 {
  527. respItem, err := data.AddEdbData(utils.DATA_SOURCE_EIA_STEO, v.EdbCode, v.Frequency)
  528. if err != nil {
  529. br.Msg = "获取失败"
  530. br.ErrMsg = "获取失败,Err:" + err.Error()
  531. return
  532. }
  533. if respItem.Ret != 200 {
  534. br.Msg = "未搜索到该指标"
  535. br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
  536. return
  537. }
  538. }
  539. }
  540. // 重名校验
  541. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  542. if e != nil {
  543. br.Msg = "操作失败"
  544. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  545. return
  546. }
  547. nameExists := make(map[string]bool)
  548. for _, v := range edbList {
  549. nameExists[v.EdbName] = true
  550. }
  551. if len(nameExists) > 0 {
  552. for _, v := range resp {
  553. v.Exist = nameExists[v.EdbName]
  554. }
  555. }
  556. br.Data = resp
  557. br.Msg = "校验成功"
  558. br.Ret = 200
  559. br.Success = true
  560. }
  561. // EiaSteoBatchAdd
  562. // @Title eiaSteo批量新增
  563. // @Description eiaSteo批量新增
  564. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  565. // @Success 200 string "操作成功"
  566. // @router /eia_steo/batch_add [post]
  567. func (this *EdbInfoController) EiaSteoBatchAdd() {
  568. br := new(models.BaseResponse).Init()
  569. defer func() {
  570. if br.ErrMsg == "" {
  571. br.IsSendEmail = false
  572. }
  573. this.Data["json"] = br
  574. this.ServeJSON()
  575. }()
  576. sysUser := this.SysUser
  577. if sysUser == nil {
  578. br.Msg = "请登录"
  579. br.ErrMsg = "请登录,SysUser Is Empty"
  580. br.Ret = 408
  581. return
  582. }
  583. deleteCache := true
  584. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_EIA_STEO_" + strconv.Itoa(sysUser.AdminId)
  585. defer func() {
  586. if deleteCache {
  587. _ = utils.Rc.Delete(cacheKey)
  588. }
  589. }()
  590. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  591. deleteCache = false
  592. br.Msg = "系统处理中,请稍后重试!"
  593. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  594. return
  595. }
  596. var req []*data_manage.AddEdbInfoReq
  597. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  598. br.Msg = "参数解析异常!"
  599. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  600. return
  601. }
  602. if len(req) == 0 {
  603. br.Msg = "请选择指标"
  604. return
  605. }
  606. if len(req) > 30 {
  607. br.Msg = "批量添加指标数量不得超过30个"
  608. return
  609. }
  610. for _, v := range req {
  611. v.EdbCode = strings.TrimSpace(v.EdbCode)
  612. if v.EdbCode == "" {
  613. br.Msg = "指标ID不可为空"
  614. return
  615. }
  616. v.EdbName = strings.TrimSpace(v.EdbName)
  617. if v.EdbName == "" {
  618. br.Msg = "请输入指标名称"
  619. return
  620. }
  621. v.Frequency = strings.TrimSpace(v.Frequency)
  622. if v.Frequency == "" {
  623. br.Msg = "请选择频度"
  624. return
  625. }
  626. v.Unit = strings.TrimSpace(v.Unit)
  627. if v.Unit == "" {
  628. br.Msg = "请输入单位"
  629. return
  630. }
  631. if v.ClassifyId <= 0 {
  632. br.Msg = "请选择分类"
  633. return
  634. }
  635. }
  636. // 限定同一时间最多批量新增30个指标
  637. for _, v := range req {
  638. var r data.EiaSteoIndexSource2EdbReq
  639. r.EdbCode = v.EdbCode
  640. r.EdbName = v.EdbName
  641. r.Frequency = v.Frequency
  642. r.Unit = v.Unit
  643. r.ClassifyId = v.ClassifyId
  644. r.AdminId = sysUser.AdminId
  645. r.AdminRealName = sysUser.RealName
  646. edbInfo, e, errMsg, skip := data.EiaSteoIndexSource2Edb(r, this.Lang)
  647. if e != nil {
  648. br.Msg = "操作失败"
  649. if errMsg != "" {
  650. br.Msg = errMsg
  651. }
  652. br.ErrMsg = e.Error()
  653. return
  654. }
  655. if skip {
  656. continue
  657. }
  658. // 试用平台更新用户累计新增指标数
  659. if utils.BusinessCode == utils.BusinessCodeSandbox {
  660. go func() {
  661. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  662. if e != nil {
  663. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  664. utils.FileLog.Info(tips)
  665. return
  666. }
  667. if adminItem.DepartmentName != "ETA试用客户" {
  668. return
  669. }
  670. var ur etaTrialService.EtaTrialUserReq
  671. ur.Mobile = adminItem.Mobile
  672. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  673. }()
  674. }
  675. // 新增操作日志
  676. {
  677. edbLog := new(data_manage.EdbInfoLog)
  678. edbLog.EdbInfoId = edbInfo.EdbInfoId
  679. edbLog.SourceName = edbInfo.SourceName
  680. edbLog.Source = edbInfo.Source
  681. edbLog.EdbCode = edbInfo.EdbCode
  682. edbLog.EdbName = edbInfo.EdbName
  683. edbLog.ClassifyId = edbInfo.ClassifyId
  684. edbLog.SysUserId = sysUser.AdminId
  685. edbLog.SysUserRealName = sysUser.RealName
  686. edbLog.CreateTime = time.Now()
  687. edbLog.Content = string(this.Ctx.Input.RequestBody)
  688. edbLog.Status = "新增指标"
  689. edbLog.Method = this.Ctx.Input.URI()
  690. go data_manage.AddEdbInfoLog(edbLog)
  691. }
  692. }
  693. br.Msg = "操作成功"
  694. br.Ret = 200
  695. br.Success = true
  696. br.IsAddLog = true
  697. }
  698. // EiaSteoAdd
  699. // @Title 新增eiaSteo指标接口
  700. // @Description 新增eiaSteo指标接口
  701. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  702. // @Success Ret=200 保存成功
  703. // @router /eia_steo/edb_info/add [post]
  704. func (this *EdbInfoController) EiaSteoAdd() {
  705. br := new(models.BaseResponse).Init()
  706. defer func() {
  707. this.Data["json"] = br
  708. this.ServeJSON()
  709. }()
  710. sysUser := this.SysUser
  711. if sysUser == nil {
  712. br.Msg = "请登录"
  713. br.ErrMsg = "请登录,SysUser Is Empty"
  714. br.Ret = 408
  715. return
  716. }
  717. deleteCache := true
  718. cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  719. defer func() {
  720. if deleteCache {
  721. utils.Rc.Delete(cacheKey)
  722. }
  723. }()
  724. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  725. deleteCache = false
  726. br.Msg = "系统处理中,请稍后重试!"
  727. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  728. return
  729. }
  730. var req data_manage.AddEdbInfoReq
  731. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  732. if err != nil {
  733. br.Msg = "参数解析异常!"
  734. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  735. return
  736. }
  737. req.EdbName = strings.Trim(req.EdbName, " ")
  738. req.EdbCode = strings.Trim(req.EdbCode, " ")
  739. if req.EdbCode == "" {
  740. br.Msg = "指标ID不能为空"
  741. return
  742. }
  743. if req.EdbName == "" {
  744. br.Msg = "指标名称不能为空"
  745. return
  746. }
  747. if req.Frequency == "" {
  748. br.Msg = "频率不能为空"
  749. return
  750. }
  751. if req.Unit == "" {
  752. br.Msg = "单位不能为空"
  753. return
  754. }
  755. if req.ClassifyId <= 0 {
  756. br.Msg = "请选择分类"
  757. return
  758. }
  759. tmpInfo, err := data_manage.GetBaseFromEiaSteoIndexByCode(req.EdbCode)
  760. if err != nil {
  761. if err.Error() != utils.ErrNoRow() {
  762. br.Msg = "获取失败"
  763. br.ErrMsg = "获取失败,Err:" + err.Error()
  764. return
  765. }
  766. }
  767. var source int
  768. if tmpInfo != nil {
  769. source = utils.DATA_SOURCE_EIA_STEO
  770. } else {
  771. br.Msg = "指标不存在"
  772. return
  773. }
  774. // 指标入库
  775. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(source, 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)
  776. if err != nil {
  777. br.Msg = "保存失败"
  778. if errMsg != `` {
  779. br.Msg = errMsg
  780. }
  781. br.ErrMsg = err.Error()
  782. br.IsSendEmail = isSendEmail
  783. return
  784. }
  785. // 试用平台更新用户累计新增指标数
  786. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  787. if e != nil {
  788. br.Msg = "操作失败"
  789. br.ErrMsg = "获取系统用户数据失败,Err:" + e.Error()
  790. return
  791. }
  792. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  793. go func() {
  794. var r etaTrialService.EtaTrialUserReq
  795. r.Mobile = adminItem.Mobile
  796. _, _ = etaTrialService.UpdateUserIndexNum(r)
  797. }()
  798. }
  799. //新增操作日志
  800. {
  801. // 添加钢联指标更新日志
  802. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  803. go data_stat.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", sysUser, 2)
  804. }
  805. edbLog := new(data_manage.EdbInfoLog)
  806. edbLog.EdbInfoId = edbInfo.EdbInfoId
  807. edbLog.SourceName = edbInfo.SourceName
  808. edbLog.Source = edbInfo.Source
  809. edbLog.EdbCode = edbInfo.EdbCode
  810. edbLog.EdbName = edbInfo.EdbName
  811. edbLog.ClassifyId = edbInfo.ClassifyId
  812. edbLog.SysUserId = sysUser.AdminId
  813. edbLog.SysUserRealName = sysUser.RealName
  814. edbLog.CreateTime = time.Now()
  815. edbLog.Content = string(this.Ctx.Input.RequestBody)
  816. edbLog.Status = "新增指标"
  817. edbLog.Method = this.Ctx.Input.URI()
  818. go data_manage.AddEdbInfoLog(edbLog)
  819. }
  820. // 更新es
  821. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  822. resp := new(data_manage.AddEdbInfoResp)
  823. resp.EdbInfoId = edbInfo.EdbInfoId
  824. resp.UniqueCode = edbInfo.UniqueCode
  825. resp.ClassifyId = edbInfo.ClassifyId
  826. br.Ret = 200
  827. br.Success = true
  828. br.Msg = "保存成功"
  829. br.Data = resp
  830. br.IsAddLog = true
  831. }