base_from_ly_index_controller.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // Package data_manage
  2. // @Author gmy 2024/8/12 14:31:00
  3. package data_manage
  4. import (
  5. "encoding/json"
  6. "eta/eta_api/controllers"
  7. "eta/eta_api/models"
  8. "eta/eta_api/models/data_manage"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/services/data"
  11. etaTrialService "eta/eta_api/services/eta_trial"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/beego/beego/v2/core/logs"
  15. "github.com/tealeg/xlsx"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. type BaseFromLyIndexController struct {
  23. controllers.BaseAuthController
  24. }
  25. const (
  26. codeMax = 30 // 指标编码最大数量
  27. dataSource = "粮油商务网"
  28. )
  29. // LyIndexList
  30. // @Title 获取指标列表
  31. // @Description 获取指标列表
  32. // @Param searchParam query string false "搜索参数"
  33. // @Param PageSize query int true "每页数据条数"
  34. // @Param CurrentIndex query int true "当前页页码,从1开始"
  35. // @Success 200 {object} data_manage.BaseFromLyIndexPage
  36. // @router /ly/index/list [get]
  37. func (this *BaseFromLyIndexController) LyIndexList() {
  38. br := this.InitResponse()
  39. _, err := this.CheckSysUser(br)
  40. if err != nil {
  41. return
  42. }
  43. pageSize, _ := this.GetInt("PageSize")
  44. currentIndex, _ := this.GetInt("CurrentIndex")
  45. searchParam := this.GetString("SearchParam")
  46. if pageSize <= 0 {
  47. pageSize = utils.PageSize20
  48. }
  49. if currentIndex <= 0 {
  50. currentIndex = 1
  51. }
  52. // 获取指标列表
  53. indexList, err := data.GetIndexPage(searchParam, currentIndex, pageSize)
  54. if err != nil {
  55. br.Msg = "获取指标列表失败"
  56. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  57. return
  58. }
  59. br.Data = indexList
  60. br.Ret = 200
  61. br.Success = true
  62. br.Msg = "获取成功"
  63. }
  64. // LyIndexDataList
  65. // @Title 获取指标对应的数据列表
  66. // @Description 获取指标对应的数据列表
  67. // @Param indexId query int true "指标ID"
  68. // @Param PageSize query int true "每页数据条数"
  69. // @Param CurrentIndex query int true "当前页页码,从1开始"
  70. // @Success 200 {object} data_manage.BaseFromLyDataPage
  71. // @router /ly/index/data/list [get]
  72. func (this *BaseFromLyIndexController) LyIndexDataList() {
  73. br := this.InitResponse()
  74. _, err := this.CheckSysUser(br)
  75. if err != nil {
  76. return
  77. }
  78. // 获取指标数据列表
  79. indexId, err := this.GetInt("indexId")
  80. if err != nil {
  81. br.Msg = "获取指标ID失败"
  82. br.ErrMsg = "获取指标ID失败,Err:" + err.Error()
  83. return
  84. }
  85. pageSize, _ := this.GetInt("PageSize")
  86. currentIndex, _ := this.GetInt("CurrentIndex")
  87. if pageSize <= 0 {
  88. pageSize = utils.PageSize20
  89. }
  90. if currentIndex <= 0 {
  91. currentIndex = 1
  92. }
  93. indexDataList, err := data.GetIndexDataPage(indexId, currentIndex, pageSize)
  94. if err != nil {
  95. br.Msg = "获取指标数据列表失败"
  96. br.ErrMsg = "获取指标数据列表失败,Err:" + err.Error()
  97. return
  98. }
  99. br.Data = indexDataList
  100. br.Ret = 200
  101. br.Success = true
  102. br.Msg = "获取成功"
  103. }
  104. // LyIndexAddValidate
  105. // @Title 新增加入到指标库校验
  106. // @Description 新增加入到指标库校验
  107. // @Param req body data_manage.BaseFromLyIndexBatchAddCheckReq true "请求参数"
  108. // @Success 200 string "操作成功"
  109. // @router /ly/index/add/validate [post]
  110. func (this *BaseFromLyIndexController) LyIndexAddValidate() {
  111. br := this.InitResponse()
  112. _, err := this.CheckSysUser(br)
  113. if err != nil {
  114. return
  115. }
  116. var req *data_manage.BaseFromLyIndexBatchAddCheckReq
  117. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  118. br.Msg = "参数解析异常!"
  119. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  120. return
  121. }
  122. codeLen := len(req.IndexCodes)
  123. if codeLen > codeMax {
  124. br.Msg = "指标编码数量超过限制"
  125. br.ErrMsg = "指标编码数量超过限制,最大数量为" + string(codeMax)
  126. return
  127. }
  128. // 校验指标编码是否存在
  129. addValidate, err := data.LyIndexAddValidate(req.IndexCodes)
  130. if err != nil {
  131. return
  132. }
  133. br.Data = addValidate
  134. br.Ret = 200
  135. br.Success = true
  136. br.Msg = "操作成功"
  137. }
  138. // LyIndexAdd
  139. // @Title 指标添加到指标库
  140. // @Description 指标添加到指标库
  141. // @Param req body []data_manage.AddEdbInfoReq true "请求参数"
  142. // @Success 200 string "操作成功"
  143. // @router /ly/index/add [post]
  144. func (this *BaseFromLyIndexController) LyIndexAdd() {
  145. br := this.InitResponse()
  146. sysUser, err := this.CheckSysUser(br)
  147. if err != nil {
  148. return
  149. }
  150. deleteCache := true
  151. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_LY_" + strconv.Itoa(sysUser.AdminId)
  152. defer func() {
  153. if deleteCache {
  154. _ = utils.Rc.Delete(cacheKey)
  155. }
  156. }()
  157. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  158. deleteCache = false
  159. br.Msg = "系统处理中,请稍后重试!"
  160. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  161. return
  162. }
  163. var req []*data_manage.AddEdbInfoReq
  164. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  165. br.Msg = "参数解析异常!"
  166. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  167. return
  168. }
  169. if len(req) == 0 {
  170. br.Msg = "请选择指标"
  171. return
  172. }
  173. if len(req) > codeMax {
  174. br.Msg = "批量添加指标数量不得超过30个"
  175. return
  176. }
  177. indexNames := make([]string, 0)
  178. resp := make([]*data_manage.NameCheckResult, 0)
  179. for _, index := range req {
  180. index.EdbCode = strings.TrimSpace(index.EdbCode)
  181. if index.EdbCode == "" {
  182. br.Msg = "指标ID不可为空"
  183. return
  184. }
  185. index.EdbName = strings.TrimSpace(index.EdbName)
  186. if index.EdbName == "" {
  187. br.Msg = "请输入指标名称"
  188. return
  189. }
  190. index.Frequency = strings.TrimSpace(index.Frequency)
  191. if index.Frequency == "" {
  192. br.Msg = "请选择频度"
  193. return
  194. }
  195. index.Unit = strings.TrimSpace(index.Unit)
  196. if index.Unit == "" {
  197. br.Msg = "请输入单位"
  198. return
  199. }
  200. if index.ClassifyId <= 0 {
  201. br.Msg = "请选择分类"
  202. return
  203. }
  204. indexNames = append(indexNames, index.EdbName)
  205. resp = append(resp, &data_manage.NameCheckResult{
  206. IndexCode: index.EdbCode,
  207. IndexName: index.EdbName,
  208. Exist: false,
  209. })
  210. }
  211. // 指标名称重复校验
  212. nameCheck, err := data.LyIndexNameCheck(indexNames, resp)
  213. if err != nil {
  214. br.Msg = err.Error()
  215. br.ErrMsg = err.Error()
  216. return
  217. }
  218. for _, v := range nameCheck {
  219. if v.Exist {
  220. br.Msg = "指标名称重复"
  221. br.Data = nameCheck
  222. br.Ret = 200
  223. br.Success = true
  224. return
  225. }
  226. }
  227. for _, v := range req {
  228. var r data.LyIndexAddReq
  229. r.EdbCode = v.EdbCode
  230. r.EdbName = v.EdbName
  231. r.Frequency = v.Frequency
  232. r.Unit = v.Unit
  233. r.ClassifyId = v.ClassifyId
  234. r.AdminId = sysUser.AdminId
  235. r.AdminRealName = sysUser.RealName
  236. // 新增指标到指标库
  237. edbInfo, e, errMsg, skip := data.LyIndexAdd(r, this.Lang)
  238. if e != nil {
  239. br.Msg = "操作失败"
  240. if errMsg != "" {
  241. br.Msg = errMsg
  242. }
  243. br.ErrMsg = e.Error()
  244. return
  245. }
  246. if skip {
  247. continue
  248. }
  249. // todo 下面两段代码能否抽离出来???
  250. // 试用平台更新用户累计新增指标数
  251. if utils.BusinessCode == utils.BusinessCodeSandbox {
  252. go func() {
  253. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  254. if e != nil {
  255. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  256. utils.FileLog.Info(tips)
  257. return
  258. }
  259. if adminItem.DepartmentName != "ETA试用客户" {
  260. return
  261. }
  262. var ur etaTrialService.EtaTrialUserReq
  263. ur.Mobile = adminItem.Mobile
  264. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  265. }()
  266. }
  267. // 新增操作日志
  268. {
  269. edbLog := new(data_manage.EdbInfoLog)
  270. edbLog.EdbInfoId = edbInfo.EdbInfoId
  271. edbLog.SourceName = edbInfo.SourceName
  272. edbLog.Source = edbInfo.Source
  273. edbLog.EdbCode = edbInfo.EdbCode
  274. edbLog.EdbName = edbInfo.EdbName
  275. edbLog.ClassifyId = edbInfo.ClassifyId
  276. edbLog.SysUserId = sysUser.AdminId
  277. edbLog.SysUserRealName = sysUser.RealName
  278. edbLog.CreateTime = time.Now()
  279. edbLog.Content = string(this.Ctx.Input.RequestBody)
  280. edbLog.Status = "新增指标"
  281. edbLog.Method = this.Ctx.Input.URI()
  282. go data_manage.AddEdbInfoLog(edbLog)
  283. }
  284. }
  285. br.Msg = "操作成功"
  286. br.Ret = 200
  287. br.Success = true
  288. br.IsAddLog = true
  289. }
  290. // LyIndexDataExport
  291. // @Title 导出指标数据
  292. // @Description 导出指标数据
  293. // @Param IndexCodes query string true "指标编码"
  294. // @Success 200 string "操作成功"
  295. // @router /ly/index/data/export [get]
  296. func (this *BaseFromLyIndexController) LyIndexDataExport() {
  297. br := this.InitResponse()
  298. _, err := this.CheckSysUser(br)
  299. if err != nil {
  300. return
  301. }
  302. //获取指标
  303. var condition string
  304. var pars []interface{}
  305. classifyId, _ := this.GetInt("ClassifyId")
  306. if classifyId < 0 {
  307. br.Msg = "请选择分类"
  308. br.ErrMsg = "请选择分类"
  309. return
  310. }
  311. if classifyId >= 0 {
  312. condition += ` AND classify_id=? `
  313. pars = append(pars, classifyId)
  314. }
  315. isCheckAll, err := this.GetBool("IsCheckAll")
  316. if err != nil {
  317. return
  318. }
  319. var indexCodeString string
  320. if !isCheckAll {
  321. indexCodeString = strings.TrimSpace(this.GetString("IndexCodes"))
  322. if indexCodeString == "" {
  323. br.Msg = "请选择指标"
  324. br.ErrMsg = "请选择指标"
  325. return
  326. }
  327. }
  328. indexCodes := strings.Split(indexCodeString, ",")
  329. if len(indexCodes) > 0 && !isCheckAll {
  330. condition += ` AND index_code IN (`
  331. for _, v := range indexCodes {
  332. condition += "?,"
  333. pars = append(pars, v)
  334. }
  335. condition = condition[:len(condition)-1]
  336. condition += `) `
  337. pars = append(pars, indexCodes)
  338. }
  339. indexList, err := data_manage.GetLyIndexList(condition, pars)
  340. if err != nil {
  341. br.Msg = "获取数据失败"
  342. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  343. return
  344. }
  345. dir, _ := os.Executable()
  346. exPath := filepath.Dir(dir)
  347. downLoadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  348. xlsxFile := xlsx.NewFile()
  349. if err != nil {
  350. fmt.Println("新增Sheet失败", err.Error())
  351. return
  352. }
  353. //获取指标数据
  354. sheetNew, _ := xlsxFile.AddSheet(dataSource)
  355. indexNameRow := sheetNew.AddRow()
  356. frequencyRow := sheetNew.AddRow()
  357. unitRow := sheetNew.AddRow()
  358. lastModifyDateRow := sheetNew.AddRow()
  359. //获取分类下指标最大数据量
  360. dataMax, err := data_manage.GetLyDataMaxCount(classifyId)
  361. if err != nil {
  362. fmt.Println("获取指标最大数据量失败", err.Error())
  363. return
  364. }
  365. logs.Info("dataMax:", dataMax)
  366. setRowIndex := 6
  367. for k, sv := range indexList {
  368. //获取数据
  369. dataList, err := data_manage.GetBaseFromLyDataByIndexCode(sv.IndexCode)
  370. if err != nil {
  371. br.Msg = "获取数据失败"
  372. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  373. return
  374. }
  375. if len(dataList) > 0 {
  376. indexNameRow.AddCell().SetValue("指标名称")
  377. frequencyRow.AddCell().SetValue("频率")
  378. unitRow.AddCell().SetValue("单位")
  379. lastModifyDateRow.AddCell().SetValue("更新时间")
  380. indexNameRow.AddCell().SetValue(sv.IndexName)
  381. frequencyRow.AddCell().SetValue(sv.Frequency)
  382. unitRow.AddCell().SetValue(sv.Unit)
  383. lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
  384. indexNameRow.AddCell()
  385. frequencyRow.AddCell()
  386. unitRow.AddCell()
  387. lastModifyDateRow.AddCell()
  388. min := k * 3
  389. sheetNew.SetColWidth(min, min, 15)
  390. if len(dataList) <= 0 {
  391. for n := 0; n < dataMax; n++ {
  392. rowIndex := setRowIndex + n
  393. row := sheetNew.Row(rowIndex)
  394. row.AddCell()
  395. row.AddCell()
  396. row.AddCell()
  397. row.AddCell()
  398. }
  399. } else {
  400. endRowIndex := 0
  401. for rk, dv := range dataList {
  402. rowIndex := setRowIndex + rk
  403. row := sheetNew.Row(rowIndex)
  404. // 在第一列显示时间
  405. if k == 0 {
  406. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  407. displayDateCell := row.AddCell()
  408. style := new(xlsx.Style)
  409. style.ApplyAlignment = true
  410. style.Alignment.WrapText = true
  411. displayDateCell.SetStyle(style)
  412. displayDateCell.SetDate(displayDate)
  413. } else {
  414. row.AddCell() // 其他列不显示时间
  415. }
  416. row.AddCell().SetValue(dv.Value)
  417. row.AddCell()
  418. endRowIndex = rowIndex
  419. }
  420. if len(dataList) < dataMax {
  421. dataLen := dataMax - len(dataList)
  422. for n := 0; n < dataLen; n++ {
  423. rowIndex := (endRowIndex + 1) + n
  424. row := sheetNew.Row(rowIndex)
  425. row.AddCell()
  426. row.AddCell()
  427. row.AddCell()
  428. row.AddCell()
  429. }
  430. }
  431. }
  432. }
  433. }
  434. err = xlsxFile.Save(downLoadFilePath)
  435. if err != nil {
  436. //有指标无数据时先导出一遍空表
  437. sheet, err := xlsxFile.AddSheet("无数据")
  438. if err != nil {
  439. br.Msg = "新增Sheet失败"
  440. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  441. return
  442. }
  443. rowSecName := sheet.AddRow()
  444. celSecName := rowSecName.AddCell()
  445. celSecName.SetValue("")
  446. err = xlsxFile.Save(downLoadFilePath)
  447. if err != nil {
  448. br.Msg = "保存文件失败"
  449. br.ErrMsg = "保存文件失败"
  450. return
  451. }
  452. }
  453. fileName := dataSource
  454. fileName += time.Now().Format(utils.FormatDateUnSpace) + `.xlsx` //文件名称
  455. this.Ctx.Output.Download(downLoadFilePath, fileName)
  456. defer func() {
  457. os.Remove(downLoadFilePath)
  458. }()
  459. br.Ret = 200
  460. br.Success = true
  461. br.Msg = "success"
  462. }
  463. func (this *BaseFromLyIndexController) InitResponse() *models.BaseResponse {
  464. br := new(models.BaseResponse).Init()
  465. defer func() {
  466. if br.ErrMsg == "" {
  467. br.IsSendEmail = false
  468. }
  469. this.Data["json"] = br
  470. this.ServeJSON()
  471. }()
  472. return br
  473. }
  474. func (this *BaseFromLyIndexController) CheckSysUser(br *models.BaseResponse) (*system.Admin, error) {
  475. sysUser := this.SysUser
  476. if sysUser == nil {
  477. br.Msg = "请登录"
  478. br.ErrMsg = "请登录,SysUser Is Empty"
  479. br.Ret = 408
  480. return nil, fmt.Errorf("请登录")
  481. }
  482. return sysUser, nil
  483. }