package label_tagging

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"wechat-api/ent/labeltagging"
	"wechat-api/internal/utils/dberrorhandler"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLabelTaggingLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *UpdateLabelTaggingLogic) UpdateLabelTagging(req *types.LabelTaggingInfo) (resp *types.BaseMsgResp, err error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	err = l.svcCtx.DB.LabelTagging.Update().Where(labeltagging.ID(*req.Id), labeltagging.OrganizationID(organizationId)).
		SetConditions(req.Conditions).
		SetNotNilActionLabelAdd(req.ActionLabelAdd).
		SetNotNilActionLabelDel(req.ActionLabelDel).
		Exec(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}
	l.svcCtx.Rds.Del(l.ctx, "label_tagging_info")
	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
}
