file_table_edit_data_table_service.go 4.3 KB
package domainService

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
)

type EditDataTableService struct {
	transactionContext *pgTransaction.TransactionContext
}

// Edit 表结构编辑  【data-table】
func (ptr *EditDataTableService) Edit(ctx *domain.Context, req domain.EditTableRequest) (*domain.DataEditDataTable, error) {
	fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
	file, err := fileRepository.FindOne(map[string]interface{}{"fileId": req.FileId})
	if err != nil {
		return nil, fmt.Errorf("文件不存在")
	}
	if file.FileType != domain.TemporaryFile.ToString() {
		return nil, fmt.Errorf("文件未校验")
	}
	// 通知底层进行文件表编辑
	response, err := ByteCore.EditTable(domain.ReqEditDataTable{
		FileId:        file.FileId,
		PageNumber:    req.Where.PageNumber,
		PageSize:      req.Where.PageSize,
		Fields:        domain.ToFields(req.Fields),
		ProcessFields: domain.ToFields(req.ProcessFields),
		Action:        req.Action,
		Params:        req.Params,
		HeaderRow:     req.Where.HeaderRow,
	})
	if err != nil {
		return nil, err
		//log.Logger.Error(err.Error())
	}
	// 处理错误
	level := domain.LevelInfo
	errMsg := ""
	if len(response.InValidCells) > 0 {
		level = domain.LevelError
		errMsg = response.InValidCells[0].Err
	}

	// 日志
	var operateName string = actionName(req.Action, req.Params)
	if err = FastLog(ptr.transactionContext, domain.VerifiedStepLog, file.FileId, &ExcelTableEditLog{
		LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.VerifiedFile.ToString(), domain.FileVerify,
			ctx.WithValue(domain.ContextWithLogLevel, level).
				WithValue(domain.ContextWithLogMsg, errMsg)),
		ProcessFields: req.ProcessFields,
		OperateName:   operateName,
	}); err != nil {
		return nil, err
	}

	// 1.有修改表类型的,更新缓存数据列类型
	if response != nil && len(response.Fields) > 0 {
		// 特殊处理修改类型错误
		options := make([]redis.FileCacheOptionsFunc, 0)
		cacheService := redis.NewFileCacheService()

		if req.Action == "convert-column-type" {
			var toType = req.Params["convertType"].(string)
			var fieldName = req.ProcessFieldNames[0]
			if level == domain.LevelError {
				options = append(options, redis.WithAddConvertTypeErrors([]redis.ConvertTypeError{{FieldName: fieldName, ErrMsg: errMsg, ToType: toType}}))
			} else {
				options = append(options, redis.WithRemoveConvertTypeErrors([]redis.ConvertTypeError{{FieldName: fieldName, ErrMsg: errMsg, ToType: toType}}))
			}
			// 底层未返回更改类型以后的字段列表,手动修改缓存
			if file, err := cacheService.UpdateField(redis.KeyTemporaryFileInfo(file.FileId), file, options...); err != nil {
				return nil, err
			} else {
				response.Fields = file.Fields
			}
			return response, nil
		}

		if _, err := cacheService.Update(redis.KeyTemporaryFileInfo(file.FileId), file, response.Fields, response.Total, options...); err != nil {
			return nil, err
		}
	}
	return response, nil
}

func actionName(action string, params map[string]interface{}) string {
	if v, ok := domain.MapActionCommon[action]; ok {
		return v.Desc
	}
	if params != nil {
		switch action {
		case domain.FormatColumn:
			if v, ok := domain.MapActionFormat[params["formatMethod"].(string)]; ok {
				return v.Desc
			}
		case domain.SplitColumn:
			if v, ok := domain.MapActionSplitColumn[params["splitMethod"].(string)]; ok {
				return v.Desc
			}
		case domain.ExtractColumn:
			if v, ok := domain.MapActionExtractColumn[params["extractMethod"].(string)]; ok {
				return v.Desc
			}
		case domain.ReplaceColumn:
			if v, ok := domain.MapActionReplaceColumn[params["replaceMethod"].(string)]; ok {
				return v.Desc
			}
		}
	}
	return action
}

func NewEditDataTableService(transactionContext *pgTransaction.TransactionContext) (*EditDataTableService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &EditDataTableService{
			transactionContext: transactionContext,
		}, nil
	}
}