business_data.go 14 KB

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