package admin_feature

import (
	"context"

	"tydata-server/app/main/api/internal/svc"
	"tydata-server/app/main/api/internal/types"
	"tydata-server/app/main/model"
	"tydata-server/common/xerr"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *AdminCreateFeatureLogic) AdminCreateFeature(req *types.AdminCreateFeatureReq) (resp *types.AdminCreateFeatureResp, err error) {
	// 1. 数据转换
	data := &model.Feature{
		ApiId:     req.ApiId,
		Name:      req.Name,
		CostPrice: req.CostPrice,
	}

	// 2. 数据库操作
	result, err := l.svcCtx.FeatureModel.Insert(l.ctx, nil, data)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
			"创建功能失败, err: %v, req: %+v", err, req)
	}

	// 3. 返回结果
	id, _ := result.LastInsertId()
	return &types.AdminCreateFeatureResp{Id: id}, nil
}
