export.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package field_management
  2. import (
  3. "adm-management/errors"
  4. "adm-management/parser"
  5. v1 "adm-management/pb/v1"
  6. "git.getensh.com/common/gopkgsv2/id"
  7. "context"
  8. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  9. "encoding/json"
  10. "fmt"
  11. "os"
  12. "strings"
  13. "github.com/tealeg/xlsx"
  14. "git.getensh.com/common/gopkgsv2/database"
  15. "git.getensh.com/common/gopkgsv2/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. "gorm.io/gorm"
  19. )
  20. type FieldList struct {
  21. Field string `json:"field"`
  22. Desc string `json:"desc"`
  23. }
  24. func Excel(ctx context.Context, req *v1.ExcelRequest) (reply *v1.ExcelReply, err error) {
  25. reply = &v1.ExcelReply{}
  26. // 捕获各个task中的异常并返回给调用者
  27. defer func() {
  28. if r := recover(); r != nil {
  29. err = fmt.Errorf("%+v", r)
  30. e := &status.Status{}
  31. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  32. logger.Error("err",
  33. zap.String("system_err", err.Error()),
  34. zap.Stack("stacktrace"))
  35. }
  36. }
  37. }()
  38. // 1 生成excel
  39. // 生成一个新的文件
  40. id, _ := id.GetUniqueID()
  41. fileName := fmt.Sprintf("field-download-%d.xlsx", id)
  42. file := xlsx.NewFile()
  43. // 添加sheet页
  44. sheet, _ := file.AddSheet("字段管理列表")
  45. // 插入表头
  46. titleRow := sheet.AddRow()
  47. cell1 := titleRow.AddCell()
  48. cell1.Value = "字段名"
  49. cell2 := titleRow.AddCell()
  50. cell2.Value = "含义"
  51. cell3 := titleRow.AddCell()
  52. cell3.Value = "类型"
  53. // 从数据库中获取全部数据
  54. var fieldLists []FieldList
  55. db := database.DB()
  56. err = db.Raw("SELECT `field`, `desc` from t_adm_field_management").Find(&fieldLists).Error
  57. if err != nil {
  58. if err == gorm.ErrRecordNotFound {
  59. return reply, nil
  60. }
  61. return reply, errors.SystemError
  62. }
  63. if len(fieldLists) == 0 {
  64. return reply, err
  65. }
  66. for _, v := range fieldLists {
  67. dataRow := sheet.AddRow()
  68. fieldName := dataRow.AddCell()
  69. fieldName.Value = v.Field
  70. fieldDesc := dataRow.AddCell()
  71. fieldDesc.Value = v.Desc
  72. fieldType := dataRow.AddCell()
  73. fieldType.Value = "string"
  74. }
  75. err = file.Save(fileName)
  76. if err != nil {
  77. return reply, err
  78. }
  79. // 3 upload to oss ,得到文件地址
  80. reply.Url, err = UploadOss(fileName, fileName)
  81. // 删除本地文件
  82. go os.Remove(fileName)
  83. if err != nil {
  84. return reply, err
  85. }
  86. return reply, nil
  87. }
  88. func ossGetUrl() string {
  89. endpoint := strings.Replace(parser.Conf.Oss.EndPoint, "https://", "", -1)
  90. return "https://" + parser.Conf.Oss.DownLoadBucket + "." + endpoint
  91. }
  92. // UploadOss 上传本地文件到oss
  93. func UploadOss(source, file string) (string, error) {
  94. client, err := oss.New(parser.Conf.Oss.EndPoint, parser.Conf.Oss.AccessKey, parser.Conf.Oss.AccessSecret)
  95. if err != nil {
  96. by, _ := json.Marshal(parser.Conf.Oss)
  97. logger.Error("oss",
  98. zap.String("call", "New"),
  99. zap.String("params", string(by)),
  100. zap.String("error", err.Error()))
  101. return "", err
  102. }
  103. bucket, err := client.Bucket(parser.Conf.Oss.DownLoadBucket)
  104. if err != nil {
  105. logger.Error("oss",
  106. zap.String("call", "Bucket"),
  107. zap.String("params", parser.Conf.Oss.DownLoadBucket),
  108. zap.String("error", err.Error()))
  109. return "", err
  110. }
  111. err = bucket.PutObjectFromFile(file, source)
  112. if err != nil {
  113. logger.Error("oss",
  114. zap.String("call", "PutObjectFromFile"),
  115. zap.String("params", source),
  116. zap.String("error", err.Error()))
  117. return "", err
  118. }
  119. filePath := ossGetUrl() + "/" + file
  120. return filePath, nil
  121. }