package channel

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"

	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/entity"
	"kami/utility/lib/libErr"
)

func (s *sEntrance) AddUserEntrance(ctx context.Context, data model.EntranceUserRoadInput) error {
	err := g.Try(ctx, func(ctx context.Context) {
		_, err := dao.V2UserRoad.Ctx(ctx).Save(entity.V2UserRoad{
			UserId:    data.UserID,
			RoadId:    data.RoadID,
			NetMargin: data.NetMargin,
		})
		libErr.ErrIsNil(ctx, err, "添加用户通道失败")
	})
	return err
}

func (s *sEntrance) UpdateUserEntrance(ctx context.Context, data model.EntranceUserRoadWithIDInput) error {
	err := g.Try(ctx, func(ctx context.Context) {
		_, err := dao.V2UserRoad.Ctx(ctx).WherePri(data.ID).Update(entity.V2UserRoad{
			UserId:    data.UserID,
			RoadId:    data.RoadID,
			NetMargin: data.NetMargin,
		})
		libErr.ErrIsNil(ctx, err, "添加用户通道失败")
	})
	return err
}

func (s *sEntrance) DeleteUserEntrance(ctx context.Context, id int) error {
	if id == 0 {
		return gerror.New("ID不能为空")
	}
	err := g.Try(ctx, func(ctx context.Context) {
		_, err := dao.V2UserRoad.Ctx(ctx).WherePri(id).Delete()
		libErr.ErrIsNil(ctx, err, "更新通道失败")
	})
	return err
}
