package wxcarduser

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *CreateWxCardUserLogic) CreateWxCardUser(req *types.WxCardUserInfo) (*types.BaseMsgResp, error) {
	_, err := l.svcCtx.DB.WxCardUser.Create().
		SetNotNilAccount(req.Account).
		SetNotNilAvatar(req.Avatar).
		SetNotNilNickname(req.Nickname).
		SetNotNilRemark(req.Remark).
		SetNotNilPhone(req.Phone).
		SetNotNilOpenID(req.OpenId).
		Save(l.ctx)

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

	return &types.BaseMsgResp{
		Code: 0,
		Msg:  errormsg.Success,
	}, nil
}
