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

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

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).
		Exec(l.ctx)

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

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