package label

import (
	"context"
	"wechat-api/ent/label"
	"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 UpdateLabelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *UpdateLabelLogic) UpdateLabel(req *types.LabelInfo) (*types.BaseMsgResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	err := l.svcCtx.DB.Label.UpdateOneID(*req.Id).
		Where(label.OrganizationID(organizationId)).
		SetNotNilStatus(req.Status).
		SetNotNilType(req.Type).
		SetNotNilName(req.Name).
		SetNotNilFrom(req.From).
		SetNotNilMode(req.Mode).
		SetNotNilConditions(req.Conditions).
		Exec(l.ctx)

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

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