package label_relationship

import (
	"context"
	"wechat-api/ent/labelrelationship"

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

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

func (l *UpdateLabelRelationshipLogic) UpdateLabelRelationship(req *types.LabelRelationshipInfo) (*types.BaseMsgResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	err := l.svcCtx.DB.LabelRelationship.UpdateOneID(*req.Id).
		Where(labelrelationship.OrganizationID(organizationId)).
		SetNotNilStatus(req.Status).
		SetNotNilLabelID(req.LabelId).
		SetNotNilContactID(req.ContactId).
		Exec(l.ctx)

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

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