package label

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

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

func (l *CreateLabelLogic) CreateLabel(req *types.LabelInfo) (*types.BaseMsgResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	var conditions = "{}"
	//if *req.Conditions == "" {
	//	conditions = "{}"
	//} else {
	//	conditions = *req.Conditions
	//}
	_, err := l.svcCtx.DB.Label.Create().
		SetNotNilStatus(req.Status).
		SetNotNilType(req.Type).
		SetNotNilName(req.Name).
		SetNotNilFrom(req.From).
		SetNotNilMode(req.Mode).
		SetNotNilConditions(&conditions).
		SetOrganizationID(organizationId).
		Save(l.ctx)

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

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