business_data.go 15 KB

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