package allocagent

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

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

func (l *UpdateAllocAgentLogic) UpdateAllocAgent(req *types.AllocAgentInfo) (*types.BaseMsgResp, error) {
	err := l.svcCtx.DB.AllocAgent.UpdateOneID(*req.Id).
		SetNotNilUserID(req.UserId).
		SetNotNilOrganizationID(req.OrganizationId).
		SetNotNilAgents(req.Agents).
		SetNotNilStatus(req.Status).
		Exec(l.ctx)

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

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