business_data.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/data_manage/request"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/services/elastic"
  11. etaTrialService "eta/eta_api/services/eta_trial"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/rdlucklib/rdluck_tools/paging"
  18. )
  19. // EdbBusinessController 自有数据(商家)
  20. type EdbBusinessController struct {
  21. controllers.BaseAuthController
  22. }
  23. // SourceList
  24. // @Title 来源列表
  25. // @Description 来源列表接口
  26. // @Success 200 {object} []*data_manageEdbBusinessSource
  27. // @router /business_data/source/list [get]
  28. func (c *EdbBusinessController) SourceList() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. c.Data["json"] = br
  32. c.ServeJSON()
  33. }()
  34. obj := data_manage.EdbBusinessSource{}
  35. list, err := obj.GetAllList()
  36. if err != nil && err.Error() != utils.ErrNoRow() {
  37. br.Msg = "获取失败"
  38. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  39. return
  40. }
  41. br.Ret = 200
  42. br.Success = true
  43. br.Msg = "获取成功"
  44. br.Data = list
  45. }
  46. // List
  47. // @Title 获取指标列表
  48. // @Description 获取指标列表接口
  49. // @Param PageSize query int true "每页数据条数"
  50. // @Param CurrentIndex query int true "当前页页码,从1开始"
  51. // @Param Frequency query string true "频度"
  52. // @Param Source query int true "来源"
  53. // @Param Keywords query string true "编码/名称关键词"
  54. // @Success 200 {object} models.DataListResp
  55. // @router /business_data/list [get]
  56. func (c *EdbBusinessController) List() {
  57. br := new(models.BaseResponse).Init()
  58. defer func() {
  59. c.Data["json"] = br
  60. c.ServeJSON()
  61. }()
  62. req := &request.BusinessDataListReq{}
  63. if e := c.ParseForm(req); e != nil {
  64. br.Msg = "获取失败"
  65. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  66. return
  67. }
  68. pageSize := req.PageSize
  69. currentIndex := req.CurrentIndex
  70. keywords := req.Keywords
  71. frequency := req.Frequency
  72. source := req.Source
  73. var startSize int
  74. if pageSize <= 0 {
  75. pageSize = utils.PageSize20
  76. }
  77. if currentIndex <= 0 {
  78. currentIndex = 1
  79. }
  80. startSize = utils.StartIndex(currentIndex, pageSize)
  81. var condition string
  82. var pars []interface{}
  83. keywords = strings.TrimSpace(keywords)
  84. if keywords != "" {
  85. //keywordSlice := strings.Split(keywords, " ")
  86. //if len(keywordSlice) > 0 {
  87. // tmpConditionSlice := make([]string, 0)
  88. // tmpConditionSlice = append(tmpConditionSlice, ` a.index_name like ? or a.index_code like ? `)
  89. // pars = utils.GetLikeKeywordPars(pars, keywords, 2)
  90. //
  91. // for _, v := range keywordSlice {
  92. // if v == ` ` || v == `` {
  93. // continue
  94. // }
  95. // tmpConditionSlice = append(tmpConditionSlice, ` a.index_name like ? or a.index_code like ? `)
  96. // pars = utils.GetLikeKeywordPars(pars, v, 2)
  97. // }
  98. // condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
  99. //
  100. //} else {
  101. // condition += ` a.index_name like ? or a.index_code like ? `
  102. // pars = utils.GetLikeKeywordPars(pars, keywords, 2)
  103. //}
  104. // ES搜
  105. _, list, e := elastic.SearchDataSourceIndex(utils.EsDataSourceIndexName, keywords, utils.DATA_SOURCE_BUSINESS, 0, []int{}, []int{}, []string{}, startSize, req.PageSize)
  106. if e != nil {
  107. br.Msg = "获取失败"
  108. br.ErrMsg = fmt.Sprintf("ES-搜索自有数据指标失败, %v", e)
  109. return
  110. }
  111. if len(list) == 0 {
  112. br.Data = data_manage.BusinessIndexListResp{
  113. List: make([]*data_manage.BaseFromBusinessIndexItem, 0),
  114. Paging: paging.GetPaging(req.CurrentIndex, req.PageSize, 0),
  115. }
  116. br.Ret = 200
  117. br.Success = true
  118. br.Msg = "获取成功"
  119. return
  120. }
  121. var indexIds []int
  122. for _, v := range list {
  123. indexIds = append(indexIds, v.PrimaryId)
  124. }
  125. condition += fmt.Sprintf(" AND a.base_from_business_index_id IN (%s)", utils.GetOrmInReplace(len(indexIds)))
  126. pars = append(pars, indexIds)
  127. }
  128. if frequency != "" {
  129. condition += ` AND a.frequency = ? `
  130. pars = append(pars, frequency)
  131. }
  132. if source > 0 {
  133. condition += ` AND a.source = ? `
  134. pars = append(pars, source)
  135. }
  136. // 排序, 默认创建时间倒序
  137. orderFields := map[int]string{
  138. 0: data_manage.BaseFromBusinessIndexCols.CreateTime, 1: data_manage.BaseFromBusinessIndexCols.StartDate,
  139. 2: data_manage.BaseFromBusinessIndexCols.EndDate,
  140. }
  141. orderType := map[int]string{0: "DESC", 1: "ASC", 2: "DESC"}
  142. orderRule := ``
  143. _, ok1 := orderFields[req.SortField]
  144. _, ok2 := orderType[req.SortRule]
  145. if ok1 && ok2 {
  146. orderRule = fmt.Sprintf("%s %s", orderFields[req.SortField], orderType[req.SortRule])
  147. }
  148. obj := data_manage.BaseFromBusinessIndex{}
  149. total, err := obj.GetListCount(condition, pars)
  150. if err != nil {
  151. br.Msg = "获取失败"
  152. br.ErrMsg = "获取失败,Err:" + err.Error()
  153. return
  154. }
  155. list, err := obj.GetPageItemsByCondition(condition, pars, orderRule, startSize, pageSize)
  156. if err != nil {
  157. br.Msg = "获取失败"
  158. br.ErrMsg = "获取失败,Err:" + err.Error()
  159. return
  160. }
  161. if len(list) <= 0 {
  162. list = make([]*data_manage.BaseFromBusinessIndexItem, 0)
  163. }
  164. page := paging.GetPaging(currentIndex, pageSize, total)
  165. resp := data_manage.BusinessIndexListResp{
  166. List: list,
  167. Paging: page,
  168. }
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "获取成功"
  172. br.Data = resp
  173. }
  174. // AddCheck
  175. // @Title 新增校验
  176. // @Description 新增校验
  177. // @Param request body request.BusinessDataBatchAddCheckReq true "type json string"
  178. // @Success 200 string "操作成功"
  179. // @router /business_data/add_check [post]
  180. func (c *EdbBusinessController) AddCheck() {
  181. br := new(models.BaseResponse).Init()
  182. defer func() {
  183. if br.ErrMsg == "" {
  184. br.IsSendEmail = false
  185. }
  186. c.Data["json"] = br
  187. c.ServeJSON()
  188. }()
  189. sysUser := c.SysUser
  190. if sysUser == nil {
  191. br.Msg = "请登录"
  192. br.ErrMsg = "请登录,SysUser Is Empty"
  193. br.Ret = 408
  194. return
  195. }
  196. var req *request.BusinessDataBatchAddCheckReq
  197. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  198. br.Msg = "参数解析异常!"
  199. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  200. return
  201. }
  202. codeMax := 100
  203. codeLen := len(req.IndexCodes)
  204. // 获取指标库已有指标
  205. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_BUSINESS)
  206. if e != nil {
  207. br.Msg = "获取失败"
  208. br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
  209. return
  210. }
  211. existMap := make(map[string]*data_manage.EdbInfo)
  212. for _, v := range existsEdb {
  213. existMap[v.EdbCode] = v
  214. }
  215. list := make([]*data_manage.BaseFromBusinessIndexItem, 0)
  216. obj := data_manage.BaseFromBusinessIndex{}
  217. if !req.ListAll {
  218. // 非全选-不需要频率等筛选条件
  219. if codeLen == 0 {
  220. br.Msg = "请选择指标"
  221. return
  222. }
  223. if codeLen > codeMax {
  224. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  225. return
  226. }
  227. // 查询选中的指标
  228. cond := fmt.Sprintf(` AND %s IN (%s)`, data_manage.BaseFromBusinessIndexCols.IndexCode, utils.GetOrmInReplace(codeLen))
  229. pars := make([]interface{}, 0)
  230. pars = append(pars, req.IndexCodes)
  231. list, e = obj.GeItemsByCondition(cond, pars, 0)
  232. if e != nil {
  233. br.Msg = "获取失败"
  234. br.ErrMsg = "获取自有数据原始指标列表失败, Err: " + e.Error()
  235. return
  236. }
  237. } else {
  238. // 全选-需要频率等筛选条件
  239. condition := ``
  240. pars := make([]interface{}, 0)
  241. // 筛选项
  242. req.Keywords = strings.TrimSpace(req.Keywords)
  243. if req.Keywords != "" {
  244. keywordSlice := strings.Split(req.Keywords, " ")
  245. if len(keywordSlice) > 0 {
  246. tmpConditionSlice := make([]string, 0)
  247. tmpConditionSlice = append(tmpConditionSlice, ` a.index_name like ? or a.index_code like ? `)
  248. pars = utils.GetLikeKeywordPars(pars, req.Keywords, 2)
  249. for _, v := range keywordSlice {
  250. if v == ` ` || v == `` {
  251. continue
  252. }
  253. tmpConditionSlice = append(tmpConditionSlice, ` a.index_name like ? or a.index_code like ? `)
  254. pars = utils.GetLikeKeywordPars(pars, v, 2)
  255. }
  256. condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
  257. } else {
  258. condition += ` a.index_name like ? or a.index_code like ? `
  259. pars = utils.GetLikeKeywordPars(pars, req.Keywords, 2)
  260. }
  261. }
  262. if req.Frequency != "" {
  263. condition += ` AND a.frequency = ? `
  264. pars = append(pars, req.Frequency)
  265. }
  266. if req.Source > 0 {
  267. condition += ` AND a.source = ? `
  268. pars = append(pars, req.Source)
  269. }
  270. // 排除对应指标
  271. if codeLen > 0 {
  272. condition += fmt.Sprintf(` AND a.%s NOT IN (%s)`, data_manage.BaseFromBusinessIndexCols.IndexCode, utils.GetOrmInReplace(codeLen))
  273. pars = append(pars, req.IndexCodes)
  274. }
  275. // 查询max+1个指标
  276. list, e = obj.GeItemsByCondition(condition, pars, codeMax+1)
  277. if e != nil {
  278. br.Msg = "获取失败"
  279. br.ErrMsg = "获取外部原始指标列表失败, Err: " + e.Error()
  280. return
  281. }
  282. }
  283. if len(list) > codeMax {
  284. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  285. return
  286. }
  287. resp := make([]*data_manage.BaseFromBusinessIndexItem, 0)
  288. for _, v := range list {
  289. if v.EdbInfoId > 0 {
  290. v.EdbExist = 1
  291. }
  292. resp = append(resp, v)
  293. }
  294. br.Data = resp
  295. br.Msg = "校验成功"
  296. br.Ret = 200
  297. br.Success = true
  298. }
  299. // NameCheck
  300. // @Title 重名校验
  301. // @Description 批量新增
  302. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  303. // @Success 200 string "操作成功"
  304. // @router /business_data/name_check [post]
  305. func (c *EdbBusinessController) NameCheck() {
  306. br := new(models.BaseResponse).Init()
  307. defer func() {
  308. if br.ErrMsg == "" {
  309. br.IsSendEmail = false
  310. }
  311. c.Data["json"] = br
  312. c.ServeJSON()
  313. }()
  314. sysUser := c.SysUser
  315. if sysUser == nil {
  316. br.Msg = "请登录"
  317. br.ErrMsg = "请登录,SysUser Is Empty"
  318. br.Ret = 408
  319. return
  320. }
  321. var req []*data_manage.AddEdbInfoReq
  322. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  323. br.Msg = "参数解析异常!"
  324. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  325. return
  326. }
  327. if len(req) == 0 {
  328. br.Msg = "请选择指标"
  329. return
  330. }
  331. type NameCheckResult struct {
  332. EdbCode string
  333. EdbName string
  334. Exist bool
  335. }
  336. indexNames := make([]string, 0)
  337. resp := make([]*NameCheckResult, 0)
  338. for _, v := range req {
  339. v.EdbCode = strings.TrimSpace(v.EdbCode)
  340. if v.EdbCode == "" {
  341. br.Msg = "指标ID不可为空"
  342. return
  343. }
  344. v.EdbName = strings.TrimSpace(v.EdbName)
  345. if v.EdbName == "" {
  346. br.Msg = "请输入指标名称"
  347. return
  348. }
  349. indexNames = append(indexNames, v.EdbName)
  350. resp = append(resp, &NameCheckResult{
  351. EdbCode: v.EdbCode,
  352. EdbName: v.EdbName,
  353. })
  354. }
  355. // 重名校验
  356. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  357. if e != nil {
  358. br.Msg = "操作失败"
  359. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  360. return
  361. }
  362. nameExists := make(map[string]bool)
  363. for _, v := range edbList {
  364. nameExists[v.EdbName] = true
  365. }
  366. if len(nameExists) > 0 {
  367. for _, v := range resp {
  368. v.Exist = nameExists[v.EdbName]
  369. }
  370. }
  371. br.Data = resp
  372. br.Msg = "校验成功"
  373. br.Ret = 200
  374. br.Success = true
  375. }
  376. // BatchAdd
  377. // @Title 批量新增
  378. // @Description 批量新增
  379. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  380. // @Success 200 string "操作成功"
  381. // @router /business_data/batch_add [post]
  382. func (c *EdbBusinessController) BatchAdd() {
  383. br := new(models.BaseResponse).Init()
  384. defer func() {
  385. if br.ErrMsg == "" {
  386. br.IsSendEmail = false
  387. }
  388. c.Data["json"] = br
  389. c.ServeJSON()
  390. }()
  391. sysUser := c.SysUser
  392. if sysUser == nil {
  393. br.Msg = "请登录"
  394. br.ErrMsg = "请登录,SysUser Is Empty"
  395. br.Ret = 408
  396. return
  397. }
  398. deleteCache := true
  399. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_BUSINESS_" + strconv.Itoa(sysUser.AdminId)
  400. defer func() {
  401. if deleteCache {
  402. _ = utils.Rc.Delete(cacheKey)
  403. }
  404. }()
  405. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  406. deleteCache = false
  407. br.Msg = "系统处理中,请稍后重试!"
  408. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  409. return
  410. }
  411. var req []*data_manage.AddEdbInfoReq
  412. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  413. br.Msg = "参数解析异常!"
  414. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  415. return
  416. }
  417. if len(req) == 0 {
  418. br.Msg = "请选择指标"
  419. return
  420. }
  421. if len(req) > 30 {
  422. br.Msg = "批量添加指标数量不得超过30个"
  423. return
  424. }
  425. indexNames := make([]string, 0)
  426. for _, v := range req {
  427. v.EdbCode = strings.TrimSpace(v.EdbCode)
  428. if v.EdbCode == "" {
  429. br.Msg = "指标ID不可为空"
  430. return
  431. }
  432. v.EdbName = strings.TrimSpace(v.EdbName)
  433. if v.EdbName == "" {
  434. br.Msg = "请输入指标名称"
  435. return
  436. }
  437. indexNames = append(indexNames, v.EdbName)
  438. v.Frequency = strings.TrimSpace(v.Frequency)
  439. if v.Frequency == "" {
  440. br.Msg = "请选择频度"
  441. return
  442. }
  443. v.Unit = strings.TrimSpace(v.Unit)
  444. if v.Unit == "" {
  445. br.Msg = "请输入单位"
  446. return
  447. }
  448. if v.ClassifyId <= 0 {
  449. br.Msg = "请选择分类"
  450. return
  451. }
  452. }
  453. // 限定同一时间最多批量新增100个指标
  454. for _, v := range req {
  455. var r data.BusinessIndexSource2EdbReq
  456. r.EdbCode = v.EdbCode
  457. r.EdbName = v.EdbName
  458. r.Frequency = v.Frequency
  459. r.Unit = v.Unit
  460. r.ClassifyId = v.ClassifyId
  461. r.AdminId = sysUser.AdminId
  462. r.AdminRealName = sysUser.RealName
  463. edbInfo, e, errMsg, skip := data.BusinessIndexSource2Edb(r, c.Lang)
  464. if e != nil {
  465. br.Msg = "操作失败"
  466. if errMsg != "" {
  467. br.Msg = errMsg
  468. }
  469. br.ErrMsg = e.Error()
  470. return
  471. }
  472. if skip {
  473. continue
  474. }
  475. // 试用平台更新用户累计新增指标数
  476. if utils.BusinessCode == utils.BusinessCodeSandbox {
  477. go func() {
  478. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  479. if e != nil {
  480. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  481. utils.FileLog.Info(tips)
  482. return
  483. }
  484. if adminItem.DepartmentName != "ETA试用客户" {
  485. return
  486. }
  487. var ur etaTrialService.EtaTrialUserReq
  488. ur.Mobile = adminItem.Mobile
  489. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  490. }()
  491. }
  492. // 新增操作日志
  493. {
  494. edbLog := new(data_manage.EdbInfoLog)
  495. edbLog.EdbInfoId = edbInfo.EdbInfoId
  496. edbLog.SourceName = edbInfo.SourceName
  497. edbLog.Source = edbInfo.Source
  498. edbLog.EdbCode = edbInfo.EdbCode
  499. edbLog.EdbName = edbInfo.EdbName
  500. edbLog.ClassifyId = edbInfo.ClassifyId
  501. edbLog.SysUserId = sysUser.AdminId
  502. edbLog.SysUserRealName = sysUser.RealName
  503. edbLog.CreateTime = time.Now()
  504. edbLog.Content = string(c.Ctx.Input.RequestBody)
  505. edbLog.Status = "新增指标"
  506. edbLog.Method = c.Ctx.Input.URI()
  507. go data_manage.AddEdbInfoLog(edbLog)
  508. }
  509. }
  510. br.Msg = "操作成功"
  511. br.Ret = 200
  512. br.Success = true
  513. br.IsAddLog = true
  514. }
  515. // DataList
  516. // @Title 获取指标数据列表
  517. // @Description 获取指标数据列表接口
  518. // @Param PageSize query int true "每页数据条数"
  519. // @Param CurrentIndex query int true "当前页页码,从1开始"
  520. // @Param IndexCode query string true "频度"
  521. // @Success 200 {object} models.DataListResp
  522. // @router /business_data/data/list [get]
  523. func (c *EdbBusinessController) DataList() {
  524. br := new(models.BaseResponse).Init()
  525. defer func() {
  526. c.Data["json"] = br
  527. c.ServeJSON()
  528. }()
  529. req := &request.BusinessDataDataListReq{}
  530. if e := c.ParseForm(req); e != nil {
  531. br.Msg = "获取失败"
  532. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  533. return
  534. }
  535. pageSize := req.PageSize
  536. currentIndex := req.CurrentIndex
  537. var startSize int
  538. if pageSize <= 0 {
  539. pageSize = utils.PageSize20
  540. }
  541. if currentIndex <= 0 {
  542. currentIndex = 1
  543. }
  544. startSize = utils.StartIndex(currentIndex, pageSize)
  545. if req.IndexCode == "" {
  546. br.Msg = "指标编码为空"
  547. br.ErrMsg = "指标编码为空"
  548. br.IsSendEmail = false
  549. return
  550. }
  551. // 获取分页数据
  552. dataCount, dataList, err := data.GetPageBaseBusinessIndexData(req.IndexCode, startSize, pageSize)
  553. if err != nil && err.Error() != utils.ErrNoRow() {
  554. br.Msg = "获取指标信息失败"
  555. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  556. return
  557. }
  558. page := paging.GetPaging(currentIndex, pageSize, dataCount)
  559. resp := data_manage.BusinessIndexDataListResp{
  560. List: dataList,
  561. Paging: page,
  562. }
  563. br.Ret = 200
  564. br.Success = true
  565. br.Msg = "获取成功"
  566. br.Data = resp
  567. }
  568. // EdbInfoAdd
  569. // @Title 加入到指标库
  570. // @Description 加入到指标库
  571. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  572. // @Success 200 string "操作成功"
  573. // @router /business_data/edb_add [post]
  574. func (c *EdbBusinessController) EdbInfoAdd() {
  575. br := new(models.BaseResponse).Init()
  576. defer func() {
  577. c.Data["json"] = br
  578. c.ServeJSON()
  579. }()
  580. sysUser := c.SysUser
  581. if sysUser == nil {
  582. br.Msg = "请登录"
  583. br.ErrMsg = "请登录,SysUser Is Empty"
  584. br.Ret = 408
  585. return
  586. }
  587. deleteCache := true
  588. cacheKey := "CACHE_EDB_INFO_SINGLE_ADD_BUSINESS_" + strconv.Itoa(sysUser.AdminId)
  589. defer func() {
  590. if deleteCache {
  591. _ = utils.Rc.Delete(cacheKey)
  592. }
  593. }()
  594. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  595. deleteCache = false
  596. br.Msg = "系统处理中,请稍后重试!"
  597. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  598. return
  599. }
  600. req := new(data_manage.AddEdbInfoReq)
  601. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  602. br.Msg = "参数解析异常!"
  603. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  604. return
  605. }
  606. if req.EdbCode == "" {
  607. br.Msg = "指标ID不可为空"
  608. return
  609. }
  610. req.EdbName = strings.TrimSpace(req.EdbName)
  611. if req.EdbName == "" {
  612. br.Msg = "请输入指标名称"
  613. return
  614. }
  615. req.Frequency = strings.TrimSpace(req.Frequency)
  616. if req.Frequency == "" {
  617. br.Msg = "请选择频度"
  618. return
  619. }
  620. req.Unit = strings.TrimSpace(req.Unit)
  621. if req.Unit == "" {
  622. br.Msg = "请输入单位"
  623. return
  624. }
  625. if req.ClassifyId <= 0 {
  626. br.Msg = "请选择分类"
  627. return
  628. }
  629. // 新增指标
  630. var r data.BusinessIndexSource2EdbReq
  631. r.EdbCode = req.EdbCode
  632. r.EdbName = req.EdbName
  633. r.Frequency = req.Frequency
  634. r.Unit = req.Unit
  635. r.ClassifyId = req.ClassifyId
  636. r.AdminId = c.SysUser.AdminId
  637. r.AdminRealName = c.SysUser.RealName
  638. edbInfo, e, errMsg, skip := data.BusinessIndexSource2Edb(r, c.Lang)
  639. if e != nil {
  640. br.Msg = "操作失败"
  641. if errMsg != "" {
  642. br.Msg = errMsg
  643. }
  644. br.ErrMsg = e.Error()
  645. return
  646. }
  647. if skip {
  648. br.Msg = "指标已存在"
  649. return
  650. }
  651. // 试用平台更新用户累计新增指标数
  652. if utils.BusinessCode == utils.BusinessCodeSandbox {
  653. go func() {
  654. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  655. if e != nil {
  656. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  657. utils.FileLog.Info(tips)
  658. return
  659. }
  660. if adminItem.DepartmentName != "ETA试用客户" {
  661. return
  662. }
  663. var ur etaTrialService.EtaTrialUserReq
  664. ur.Mobile = adminItem.Mobile
  665. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  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(c.Ctx.Input.RequestBody)
  681. edbLog.Status = "新增指标"
  682. edbLog.Method = c.Ctx.Input.URI()
  683. go data_manage.AddEdbInfoLog(edbLog)
  684. }
  685. br.Msg = "操作成功"
  686. br.Ret = 200
  687. br.Success = true
  688. br.IsAddLog = true
  689. }