package tutorial

import (
	"context"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"

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

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

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

func (l *CreateTutorialLogic) CreateTutorial(req *types.TutorialInfo) (*types.BaseMsgResp, error) {
	_, err := l.svcCtx.DB.Tutorial.Create().
		SetNotNilEmployeeID(req.EmployeeId).
		SetNotNilIndex(req.Index).
		SetNotNilTitle(req.Title).
		SetNotNilContent(req.Content).
		Save(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
}
