package logic

import (
	"context"

	"github.com/zeromicro/go-zero/core/logx"
	"sundynix-micro-go/app/file/model"
	"sundynix-micro-go/app/file/rpc/file"
	"sundynix-micro-go/app/file/rpc/internal/svc"
)

type CreateFileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateFileLogic {
	return &CreateFileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 创建文件记录
func (l *CreateFileLogic) CreateFile(in *file.CreateFileReq) (*file.CreateFileResp, error) {
	ossRecord := &model.SundynixOss{
		Name:   in.Name,
		Url:    in.Url,
		Tag:    in.Tag,
		Key:    in.Key,
		Suffix: in.Suffix,
		MD5:    in.Md5,
	}

	err := l.svcCtx.DB.Create(ossRecord).Error
	if err != nil {
		return nil, err
	}

	return &file.CreateFileResp{
		File: &file.FileInfo{
			Id:        ossRecord.ID,
			Name:      ossRecord.Name,
			Url:       ossRecord.Url,
			Tag:       ossRecord.Tag,
			Key:       ossRecord.Key,
			Suffix:    ossRecord.Suffix,
			Md5:       ossRecord.MD5,
			CreatedAt: ossRecord.CreatedAt.Unix(),
		},
	}, nil
}
