// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package file

import (
	"context"

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

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

// 文件列表
func NewGetFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileListLogic {
	return &GetFileListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetFileListLogic) GetFileList(req *types.FileListReq) (resp *types.FileListResp, err error) {
	respRpc, err := l.svcCtx.FileRpc.GetFileList(l.ctx, &fileservice.GetFileListReq{
		Current:  int32(req.Current),
		PageSize: int32(req.PageSize),
		Name:     req.Name,
	})
	if err != nil {
		return nil, err
	}

	var list []types.FileInfo
	for _, item := range respRpc.List {
		list = append(list, types.FileInfo{
			Id:     item.Id,
			Name:   item.Name,
			Url:    item.Url,
			Tag:    item.Tag,
			Key:    item.Key,
			Suffix: item.Suffix,
			Md5:    item.Md5,
		})
	}

	return &types.FileListResp{
		List:  list,
		Total: respRpc.Total,
	}, nil
}
