package logic

import (
	"context"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"sundynix-micro-go/app/file/model"
	"sundynix-micro-go/app/file/rpc/file"
	"sundynix-micro-go/app/file/rpc/internal/svc"
)

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

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

// 根据ID列表批量获取文件信息
func (l *GetFilesByIdsLogic) GetFilesByIds(in *file.GetFilesByIdsReq) (*file.GetFilesByIdsResp, error) {
	if len(in.Ids) == 0 {
		return &file.GetFilesByIdsResp{Files: []*file.FileInfo{}}, nil
	}

	var ossList []model.SundynixOss
	err := l.svcCtx.DB.Where("id IN ?", in.Ids).Find(&ossList).Error
	if err != nil {
		l.Errorf("批量查询文件失败: %v", err)
		return nil, status.Error(codes.Internal, "批量查询文件失败")
	}

	files := make([]*file.FileInfo, 0, len(ossList))
	for _, oss := range ossList {
		files = append(files, convertOssToProto(&oss))
	}

	return &file.GetFilesByIdsResp{Files: files}, nil
}
