eia_steo.go 24 KB

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