123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package field_management
- import (
- "adm-management/errors"
- "adm-management/parser"
- v1 "adm-management/pb/v1"
- "git.getensh.com/common/gopkgsv2/id"
- "context"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "encoding/json"
- "fmt"
- "os"
- "strings"
- "github.com/tealeg/xlsx"
- "git.getensh.com/common/gopkgsv2/database"
- "git.getensh.com/common/gopkgsv2/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- )
- type FieldList struct {
- Field string `json:"field"`
- Desc string `json:"desc"`
- }
- func Excel(ctx context.Context, req *v1.ExcelRequest) (reply *v1.ExcelReply, err error) {
- reply = &v1.ExcelReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- // 1 生成excel
- // 生成一个新的文件
- id, _ := id.GetUniqueID()
- fileName := fmt.Sprintf("field-download-%d.xlsx", id)
- file := xlsx.NewFile()
- // 添加sheet页
- sheet, _ := file.AddSheet("字段管理列表")
- // 插入表头
- titleRow := sheet.AddRow()
- cell1 := titleRow.AddCell()
- cell1.Value = "字段名"
- cell2 := titleRow.AddCell()
- cell2.Value = "含义"
- cell3 := titleRow.AddCell()
- cell3.Value = "类型"
- // 从数据库中获取全部数据
- var fieldLists []FieldList
- db := database.DB()
- err = db.Raw("SELECT `field`, `desc` from t_adm_field_management").Find(&fieldLists).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- return reply, errors.SystemError
- }
- if len(fieldLists) == 0 {
- return reply, err
- }
- for _, v := range fieldLists {
- dataRow := sheet.AddRow()
- fieldName := dataRow.AddCell()
- fieldName.Value = v.Field
- fieldDesc := dataRow.AddCell()
- fieldDesc.Value = v.Desc
- fieldType := dataRow.AddCell()
- fieldType.Value = "string"
- }
- err = file.Save(fileName)
- if err != nil {
- return reply, err
- }
- // 3 upload to oss ,得到文件地址
- reply.Url, err = UploadOss(fileName, fileName)
- // 删除本地文件
- go os.Remove(fileName)
- if err != nil {
- return reply, err
- }
- return reply, nil
- }
- func ossGetUrl() string {
- endpoint := strings.Replace(parser.Conf.Oss.EndPoint, "https://", "", -1)
- return "https://" + parser.Conf.Oss.DownLoadBucket + "." + endpoint
- }
- // UploadOss 上传本地文件到oss
- func UploadOss(source, file string) (string, error) {
- client, err := oss.New(parser.Conf.Oss.EndPoint, parser.Conf.Oss.AccessKey, parser.Conf.Oss.AccessSecret)
- if err != nil {
- by, _ := json.Marshal(parser.Conf.Oss)
- logger.Error("oss",
- zap.String("call", "New"),
- zap.String("params", string(by)),
- zap.String("error", err.Error()))
- return "", err
- }
- bucket, err := client.Bucket(parser.Conf.Oss.DownLoadBucket)
- if err != nil {
- logger.Error("oss",
- zap.String("call", "Bucket"),
- zap.String("params", parser.Conf.Oss.DownLoadBucket),
- zap.String("error", err.Error()))
- return "", err
- }
- err = bucket.PutObjectFromFile(file, source)
- if err != nil {
- logger.Error("oss",
- zap.String("call", "PutObjectFromFile"),
- zap.String("params", source),
- zap.String("error", err.Error()))
- return "", err
- }
- filePath := ossGetUrl() + "/" + file
- return filePath, nil
- }
|