custom_analysis_edb.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. excelModel "eta/eta_api/models/data_manage/excel"
  6. "eta/eta_api/models/data_manage/excel/request"
  7. "eta/eta_api/models/data_manage/excel/response"
  8. "eta/eta_api/services/data/excel"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // AddEdb
  16. // @Title 新增指标接口
  17. // @Description 新增指标接口
  18. // @Param request body request.AddExcelInfoReq true "type json string"
  19. // @Success 200 {object} response.AddExcelInfoResp
  20. // @router /edb/add [post]
  21. func (c *CustomAnalysisController) AddEdb() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. c.Data["json"] = br
  25. c.ServeJSON()
  26. }()
  27. sysUser := c.SysUser
  28. if sysUser == nil {
  29. br.Msg = "请登录"
  30. br.ErrMsg = "请登录,SysUser Is Empty"
  31. br.Ret = 408
  32. return
  33. }
  34. deleteCache := true
  35. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  36. defer func() {
  37. if deleteCache {
  38. _ = utils.Rc.Delete(cacheKey)
  39. }
  40. }()
  41. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  42. deleteCache = false
  43. br.Msg = "系统处理中,请稍后重试!"
  44. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  45. return
  46. }
  47. var req request.SaveExcelInfoReq
  48. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  49. if err != nil {
  50. br.Msg = "参数解析异常!"
  51. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  52. return
  53. }
  54. req.ExcelName = strings.Trim(req.ExcelName, " ")
  55. if req.ExcelName == "" {
  56. br.Msg = "请填写表格名称!"
  57. br.IsSendEmail = false
  58. return
  59. }
  60. if req.ExcelInfoId <= 0 {
  61. br.Msg = "请选择excel!"
  62. br.IsSendEmail = false
  63. return
  64. }
  65. if req.ExcelClassifyId <= 0 {
  66. br.Msg = "分类参数错误!"
  67. br.IsSendEmail = false
  68. return
  69. }
  70. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  71. if err != nil {
  72. br.Msg = "找不到该EXCEL!"
  73. br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
  74. return
  75. }
  76. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  77. br.Msg = "EXCEL异常!"
  78. br.IsSendEmail = false
  79. return
  80. }
  81. err, errMsg, isSendEmail := excel.SaveCustomAnalysisTable(excelInfo, utils.TrimLRStr(req.ExcelName), req.Content, req.ExcelImage, req.ExcelClassifyId, req.OpSheetList)
  82. if err != nil {
  83. br.Msg = "保存失败"
  84. if errMsg != `` {
  85. br.Msg = errMsg
  86. }
  87. br.ErrMsg = "保存失败,Err:" + err.Error()
  88. br.IsSendEmail = isSendEmail
  89. return
  90. }
  91. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  92. //if req.Source == 1 {
  93. // go UpdateExcelInfoFileUrl(excelInfo)
  94. //}
  95. //
  96. resp := new(response.AddExcelInfoResp)
  97. resp.ExcelInfoId = excelInfo.ExcelInfoId
  98. resp.UniqueCode = excelInfo.UniqueCode
  99. //新增操作日志
  100. //{
  101. // excelLog := &data_manage.ExcelInfoLog{
  102. // //ExcelInfoLogId: 0,
  103. // ExcelInfoId: excelInfo.ExcelInfoId,
  104. // ExcelName: req.ExcelName,
  105. // ExcelClassifyId: req.ExcelClassifyId,
  106. // SysUserId: sysUser.AdminId,
  107. // SysUserRealName: sysUser.RealName,
  108. // UniqueCode: excelInfo.UniqueCode,
  109. // CreateTime: time.Now(),
  110. // Content: string(c.Ctx.Input.RequestBody),
  111. // Status: "新增表格",
  112. // Method: c.Ctx.Input.URI(),
  113. // }
  114. // go data_manage.AddExcelInfoLog(excelLog)
  115. //}
  116. br.Ret = 200
  117. br.Success = true
  118. br.Msg = "保存成功"
  119. br.Data = resp
  120. br.IsAddLog = false //数据量太大了,不写入日志吧
  121. }
  122. func init() {
  123. excelInfo, err := excelModel.GetExcelInfoById(160)
  124. if err != nil {
  125. fmt.Println("查找excel失败:", err)
  126. return
  127. }
  128. _, err, _ = excel.GenerateExcelCustomAnalysisExcel(excelInfo)
  129. if err != nil {
  130. fmt.Println("生成excel失败:", err)
  131. return
  132. }
  133. }