package adapters

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
	"time"
)

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

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

func (l *CreateAdapterLogic) CreateAdapter(req *types.AdapterCreateReq) (resp *types.AdapterResp, err error) {
	adapter := types.AdapterInfo{}
	utils.Convert(req, &adapter)
	adapter.Id = utils.GenSnowflakeIDStr()
	adapter.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	result := l.svcCtx.DbEngin.Table("t_adapter").Create(&adapter)
	if result.Error != nil {
		return nil, result.Error
	}

	return
}
