package label_tagging

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

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

func (l *CreateLabelTaggingLogic) CreateLabelTagging(req *types.LabelTaggingInfo) (resp *types.BaseMsgResp, err error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	_, err = l.svcCtx.DB.LabelTagging.Create().
		SetOrganizationID(organizationId).
		SetConditions(req.Conditions).
		SetNotNilActionLabelAdd(req.ActionLabelAdd).
		SetNotNilActionLabelDel(req.ActionLabelDel).
		Save(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.CreateSuccess}, nil
}
