package card_info_walmart

import (
	"context"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"

	"kami/api/card_info_walmart/v1"
)

func (c *ControllerV1) GroupAdd(ctx context.Context, req *v1.GroupAddReq) (res *v1.GroupAddRes, err error) {
	userInfo, err := service.SysAuth().LoginOnlyLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	isExist, err := service.CardRedeemAccount().GroupCheck(ctx, req.Name, userInfo.Id, nil)
	if err != nil {
		err = gerror.WrapCode(gcode.CodeInvalidParameter, err, "添加失败")
		return
	}
	if isExist {
		err = gerror.NewCode(gcode.CodeInvalidParameter, "分组已存在")
		return
	}
	_, err = service.CardRedeemAccount().GroupAdd(ctx, &model.RedeemAccountGroup{
		CreatedUserId: userInfo.Id,
		Category:      consts.CardRedeemAccountCategoryWalMart,
		GroupRecord:   &req.GroupRecord,
	}, nil)
	if err != nil {
		err = gerror.WrapCode(gcode.CodeInvalidParameter, err, "添加失败")
		return
	}
	return
}
