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 UpdateStorageConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *UpdateStorageConfigLogic) UpdateStorageConfig(in *file.UpdateStorageConfigReq) (*file.CommonResp, error) {
	conf := &model.StorageConfig{}
	if err := l.svcCtx.DB.Where("id = ?", in.Id).First(conf).Error; err != nil {
		return nil, err
	}

	conf.Type = in.Type
	conf.Name = in.Name
	conf.Endpoint = in.Endpoint
	conf.AccessKeyId = in.AccessKeyId
	conf.AccessKeySecret = in.AccessKeySecret
	conf.BucketName = in.BucketName
	conf.BucketUrl = in.BucketUrl
	conf.Region = in.Region
	conf.Status = int(in.Status)
	conf.Remark = in.Remark

	if err := l.svcCtx.DB.Save(conf).Error; err != nil {
		return nil, err
	}

	return &file.CommonResp{Code: 0, Msg: "更新成功"}, nil
}
