file_table_delete_file_service.go 1.6 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/dao"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
)

type DeleteFileService struct {
	transactionContext *pgTransaction.TransactionContext
}

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

// DeleteFiles 文件删除
func (ptr *DeleteFileService) DeleteFiles(ctx *domain.Context, files ...*domain.File) error {
	for _, file := range files {
		if err := ptr.delete(ctx, file); err != nil {
			return err
		}
	}
	return nil
}

func (ptr *DeleteFileService) delete(ctx *domain.Context, file *domain.File) error {
	// delete file
	if err := dao.FileDelete(ptr.transactionContext, file.FileId, domain.FileType(file.FileType)); err != nil {
		return err
	}
	// delete table
	if file.FileInfo.TableId > 0 {
		if err := dao.TableDelete(ptr.transactionContext, file.FileInfo.TableId, domain.ExcelTable); err != nil {
			return err
		}
	}
	// delete log
	if err := dao.LogDirectDelete(ptr.transactionContext, file.FileId, domain.VerifiedStepLog); err != nil {
		return err
	}
	log.Logger.Info(fmt.Sprintf("[%v] 删除文件:%v 文件类型:%v", ctx.OperatorName, file.FileInfo.Name, file.FileType))
	return nil
}