package xunji

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

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

func (l *UpdateXunjiLogic) UpdateXunji(req *types.XunjiInfo) (*types.BaseMsgResp, error) {
	err := l.svcCtx.DB.Xunji.UpdateOneID(*req.Id).
		SetNotNilStatus(req.Status).
		SetNotNilAppKey(req.AppKey).
		SetNotNilAppSecret(req.AppSecret).
		SetNotNilToken(req.Token).
		SetNotNilEncodingKey(req.EncodingKey).
		SetNotNilAgentID(req.AgentId).
		//SetNotNilOrganizationID(req.OrganizationId).
		SetNotNilAPIBase(req.ApiBase).
		SetNotNilAPIKey(req.ApiKey).
		Exec(l.ctx)

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

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