package userreadhistory

import (
	"context"
	"server/internal/dao"
	"server/internal/model"
	"server/internal/service"
	"server/utility/ecode"
)

type sUserReadHistory struct{}

func New() service.IUserReadHistory {
	return &sUserReadHistory{}
}

func init() {
	service.RegisterUserReadHistory(New())
}

func (s *sUserReadHistory) Remove(ctx context.Context, in *model.UserReadHistoryDelIn) (out *model.UserReadHistoryCRUDOut, err error) {
	// 参数校验
	if in.UserId == 0 || len(in.BookIds) == 0 {
		return nil, ecode.Params.Sub("user_read_history_remove_param_invalid")
	}

	_, err = dao.UserReadHistory.Ctx(ctx).
		Where(dao.UserReadHistory.Columns().UserId, in.UserId).
		WhereIn(dao.UserReadHistory.Columns().BookId, in.BookIds).
		Delete()
	if err != nil {
		return nil, ecode.Fail.Sub("user_read_history_remove_failed")
	}

	return &model.UserReadHistoryCRUDOut{Success: true}, nil
}
