package xunji

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

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

func (l *CreateXunjiLogic) CreateXunji(req *types.XunjiInfo) (*types.BaseMsgResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)

	_, err := l.svcCtx.DB.Xunji.Create().
		SetNotNilStatus(req.Status).
		SetNotNilAppKey(req.AppKey).
		SetNotNilAppSecret(req.AppSecret).
		SetNotNilToken(req.Token).
		SetNotNilEncodingKey(req.EncodingKey).
		SetOrganizationID(organizationId).
		Save(l.ctx)

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

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