meeting_probabilities.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage/request"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "github.com/tealeg/xlsx"
  9. "os"
  10. "path/filepath"
  11. "time"
  12. )
  13. // MeetingProbabilitiesController 加息概率
  14. type MeetingProbabilitiesController struct {
  15. BaseAuthController
  16. }
  17. // Detail
  18. // @Title 获取表格详情
  19. // @Description 获取表格详情接口
  20. // @Param ExcelInfoId query int true "表格id"
  21. // @Success 200 {object} data_manage.ExcelInfo
  22. // @router /detail [get]
  23. func (c *MeetingProbabilitiesController) Detail() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. c.Data["json"] = br
  27. c.ServeJSON()
  28. }()
  29. sysUser := c.SysUser
  30. if sysUser == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,SysUser Is Empty"
  33. br.Ret = 408
  34. return
  35. }
  36. date := c.GetString("DateTime")
  37. isExport, _ := c.GetInt("IsExport")
  38. // 获取数据详情bu
  39. excelDetail, err := models.GetMeetingInfoById(date)
  40. if err != nil {
  41. if err.Error() == utils.ErrNoRow() {
  42. br.Ret = 200
  43. br.Msg = "该日期无数据"
  44. br.Msg = "该日期无数据,Err:" + err.Error()
  45. return
  46. }
  47. br.ErrMsg = err.Error()
  48. return
  49. }
  50. if isExport == 1 {
  51. sheet := new(request.ISheet)
  52. err = json.Unmarshal([]byte(excelDetail.Content), &sheet)
  53. MeetingProbabilitiesExport(c, br, sheet)
  54. return
  55. }
  56. br.Ret = 200
  57. br.Success = true
  58. br.Msg = "获取成功"
  59. br.Data = excelDetail
  60. }
  61. // MeetingProbabilitiesExport 导出Excel
  62. func MeetingProbabilitiesExport(this *MeetingProbabilitiesController, br *models.BaseResponse, sheetReq *request.ISheet) {
  63. sysUser := this.SysUser
  64. if sysUser == nil {
  65. br.Msg = "请重新登录"
  66. return
  67. }
  68. dir, _ := os.Executable()
  69. exPath := filepath.Dir(dir)
  70. downloadPath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  71. xlsxFile := xlsx.NewFile()
  72. sheet, e := xlsxFile.AddSheet("数据报表")
  73. if e != nil {
  74. br.Msg = "新增Sheet失败"
  75. br.ErrMsg = "新增Sheet失败, Err: " + e.Error()
  76. return
  77. }
  78. for _, v := range sheetReq.Data {
  79. addRow := sheet.AddRow()
  80. for _, s := range v {
  81. if s != nil {
  82. addRow.AddCell().SetString(s.M)
  83. }
  84. }
  85. }
  86. if e = xlsxFile.Save(downloadPath); e != nil {
  87. br.Msg = "导出失败"
  88. br.ErrMsg = "保存文件失败"
  89. return
  90. }
  91. fileName := fmt.Sprint("MeetingProbabilities", time.Now().Format("2006.01.02"), ".xlsx")
  92. this.Ctx.Output.Download(downloadPath, fileName)
  93. defer func() {
  94. _ = os.Remove(downloadPath)
  95. }()
  96. br.Ret = 200
  97. br.Success = true
  98. br.Msg = "success"
  99. }