package xunji_service

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

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

func (l *CreateXunjiServiceLogic) CreateXunjiService(req *types.XunjiServiceInfo) (*types.BaseMsgResp, error) {
	_, err := l.svcCtx.DB.XunjiService.Create().
		SetNotNilStatus(req.Status).
		SetNotNilXunjiID(req.XunjiId).
		SetNotNilAgentID(req.AgentId).
		SetNotNilOrganizationID(req.OrganizationId).
		SetNotNilWxid(req.Wxid).
		SetNotNilAPIBase(req.ApiBase).
		SetNotNilAPIKey(req.ApiKey).
		Save(l.ctx)

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

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