123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package excelizeutil
- import (
- "Cold_Logistic/internal/pkg/common/codex"
- "Cold_Logistic/internal/pkg/common/global"
- "context"
- "io"
- "strings"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/goroutineutil"
- )
- type CheckFunc func(*excelize.File) error
- func CheckFileFormat(file *excelize.File, titles []string, titleRow, titleCel int, opts ...CheckFunc) error {
- sheet := file.GetSheetName(file.GetActiveSheetIndex())
- rows, err := file.GetRows(sheet)
- if err != nil {
- return errors.WithStack(err)
- }
- if len(rows) < 1 || len(rows[titleRow])-titleCel < len(titles) {
- return errors.WithCode(codex.ErrParamValidate, "文件标题头不匹配")
- }
- if len(rows) < 2 {
- return errors.WithCode(codex.ErrParamValidate, "导入文件内容为空")
- }
- // 校验表头
- for k, v := range titles {
- title := strings.TrimSpace(rows[titleRow][k+titleCel])
- if title != v {
- return errors.WithCode(codex.ErrParamValidate, "文件标题头不匹配")
- }
- }
- for _, o := range opts {
- if err = o(file); err != nil {
- return errors.WithStackOnce(err)
- }
- }
- return nil
- }
- func PutFile(ctx context.Context, keyPath string, file *excelize.File) (string, error) {
- // 更新结果上传至cos
- piper, pipeW := io.Pipe()
- defer piper.Close()
- goroutineutil.SafeGoWithCtx(ctx, func(c context.Context) {
- defer pipeW.Close()
- file.Write(pipeW)
- })
- oss := global.CommonConnectRepoInst.ObjectStore
- err := oss.PutObject(ctx, keyPath, piper)
- if err != nil {
- return "", errors.WithStackOnce(err)
- }
- return oss.GetDownLoadUrl(ctx, keyPath)
- }
|