business_data.go 13 KB

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